- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / one_click_signin_view_controller.mm
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/one_click_signin_view_controller.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "base/mac/bundle_locations.h"
10 #import "chrome/browser/ui/chrome_style.h"
11 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
12 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
13 #include "chrome/browser/ui/sync/one_click_signin_histogram.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/web_contents.h"
16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h"
18 #include "skia/ext/skia_utils_mac.h"
19 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
20 #include "ui/base/l10n/l10n_util_mac.h"
21
22 namespace {
23
24 // Shift the origin of |view|'s frame by the given amount in the
25 // positive y direction (up).
26 void ShiftOriginY(NSView* view, CGFloat amount) {
27   NSPoint origin = [view frame].origin;
28   origin.y += amount;
29   [view setFrameOrigin:origin];
30 }
31
32 }  // namespace
33
34 @interface OneClickSigninViewController ()
35 - (CGFloat)initializeInformativeTextView;
36 - (void)close;
37 @end
38
39 @implementation OneClickSigninViewController
40
41
42 - (id)initWithNibName:(NSString*)nibName
43           webContents:(content::WebContents*)webContents
44          syncCallback:(const BrowserWindow::StartSyncCallback&)syncCallback
45         closeCallback:(const base::Closure&)closeCallback
46          isSyncDialog:(BOOL)isSyncDialog
47                 email:(const string16&)email
48          errorMessage:(NSString*)errorMessage {
49   if ((self = [super initWithNibName:nibName
50                               bundle:base::mac::FrameworkBundle()])) {
51     webContents_ = webContents;
52     startSyncCallback_ = syncCallback;
53     closeCallback_ = closeCallback;
54     isSyncDialog_ = isSyncDialog;
55     clickedLearnMore_ = NO;
56     email_ = email;
57     errorMessage_.reset([errorMessage retain]);
58     if (isSyncDialog_)
59       DCHECK(!startSyncCallback_.is_null());
60   }
61   return self;
62 }
63
64 - (void)viewWillClose {
65   // This is usually called after a click handler has initiated sync
66   // and has reset the callback. However, in the case that we are closing
67   // the window and nothing else has initiated the sync, we must do so here
68   if (isSyncDialog_ && !startSyncCallback_.is_null()) {
69     base::ResetAndReturn(&startSyncCallback_).Run(
70         OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
71   }
72 }
73
74 - (IBAction)ok:(id)sender {
75   if (isSyncDialog_) {
76     OneClickSigninHelper::LogConfirmHistogramValue(
77         clickedLearnMore_ ?
78             one_click_signin::HISTOGRAM_CONFIRM_LEARN_MORE_OK :
79             one_click_signin::HISTOGRAM_CONFIRM_OK);
80
81     base::ResetAndReturn(&startSyncCallback_).Run(
82       OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
83   }
84   [self close];
85 }
86
87 - (IBAction)onClickUndo:(id)sender {
88   if (isSyncDialog_) {
89     OneClickSigninHelper::LogConfirmHistogramValue(
90         clickedLearnMore_ ?
91             one_click_signin::HISTOGRAM_CONFIRM_LEARN_MORE_UNDO :
92             one_click_signin::HISTOGRAM_CONFIRM_UNDO);
93
94     base::ResetAndReturn(&startSyncCallback_).Run(
95       OneClickSigninSyncStarter::UNDO_SYNC);
96   }
97   [self close];
98 }
99
100 - (IBAction)onClickAdvancedLink:(id)sender {
101   if (isSyncDialog_) {
102     OneClickSigninHelper::LogConfirmHistogramValue(
103         clickedLearnMore_ ?
104             one_click_signin::HISTOGRAM_CONFIRM_LEARN_MORE_ADVANCED :
105             one_click_signin::HISTOGRAM_CONFIRM_ADVANCED);
106
107     base::ResetAndReturn(&startSyncCallback_).Run(
108         OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST);
109   }
110   else {
111     content::OpenURLParams params(GURL(chrome::kChromeUISettingsURL),
112                                   content::Referrer(), CURRENT_TAB,
113                                   content::PAGE_TRANSITION_LINK, false);
114     webContents_->OpenURL(params);
115   }
116   [self close];
117 }
118
119 - (IBAction)onClickClose:(id)sender {
120   if (isSyncDialog_) {
121     OneClickSigninHelper::LogConfirmHistogramValue(
122         clickedLearnMore_ ?
123             one_click_signin::HISTOGRAM_CONFIRM_LEARN_MORE_CLOSE :
124             one_click_signin::HISTOGRAM_CONFIRM_CLOSE);
125
126     base::ResetAndReturn(&startSyncCallback_).Run(
127         OneClickSigninSyncStarter::UNDO_SYNC);
128   }
129   [self close];
130 }
131
132 - (void)awakeFromNib {
133   // Lay out the text controls from the bottom up.
134   CGFloat totalYOffset = 0.0;
135
136   if ([errorMessage_ length] == 0) {
137     totalYOffset +=
138         [GTMUILocalizerAndLayoutTweaker sizeToFitView:advancedLink_].height;
139     [[advancedLink_ cell] setTextColor:
140         gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor())];
141   } else {
142     // Don't display the advanced link for the error bubble.
143     // To align the Learn More link with the OK button, we need to offset by
144     // the height of the Advanced link, plus the padding between it and the
145     // Learn More link above.
146     float advancedLinkHeightPlusPadding =
147         [informativePlaceholderTextField_ frame].origin.y -
148         [advancedLink_ frame].origin.y;
149
150     totalYOffset -= advancedLinkHeightPlusPadding;
151     [advancedLink_ removeFromSuperview];
152   }
153
154   if (informativePlaceholderTextField_) {
155     ShiftOriginY(informativePlaceholderTextField_, totalYOffset);
156     totalYOffset += [self initializeInformativeTextView];
157   }
158
159   ShiftOriginY(messageTextField_, totalYOffset);
160   totalYOffset +=
161       [GTMUILocalizerAndLayoutTweaker
162           sizeToFitFixedWidthTextField:messageTextField_];
163
164   if (closeButton_)
165     ShiftOriginY(closeButton_, totalYOffset);
166
167   NSSize delta = NSMakeSize(0.0, totalYOffset);
168
169   if (isSyncDialog_) {
170     [messageTextField_ setStringValue:l10n_util::GetNSStringWithFixup(
171         IDS_ONE_CLICK_SIGNIN_DIALOG_TITLE_NEW)];
172   } else if ([errorMessage_ length] != 0) {
173     [messageTextField_ setStringValue:errorMessage_];
174   }
175
176   // Resize bubble and window to hold the controls.
177   [GTMUILocalizerAndLayoutTweaker
178       resizeViewWithoutAutoResizingSubViews:[self view]
179                                       delta:delta];
180
181   if (isSyncDialog_) {
182     OneClickSigninHelper::LogConfirmHistogramValue(
183         one_click_signin::HISTOGRAM_CONFIRM_SHOWN);
184   }
185 }
186
187 - (CGFloat)initializeInformativeTextView {
188   NSRect oldFrame = [informativePlaceholderTextField_ frame];
189
190   // Replace the placeholder NSTextField with the real label NSTextView. The
191   // former doesn't show links in a nice way, but the latter can't be added in
192   // a xib without a containing scroll view, so create the NSTextView
193   // programmatically.
194   informativeTextView_.reset(
195       [[HyperlinkTextView alloc] initWithFrame:oldFrame]);
196   [informativeTextView_.get() setAutoresizingMask:
197       [informativePlaceholderTextField_ autoresizingMask]];
198   [informativeTextView_.get() setDelegate:self];
199
200   // Set the text.
201   NSString* learnMoreText = l10n_util::GetNSStringWithFixup(IDS_LEARN_MORE);
202   NSString* messageText;
203
204   ui::ResourceBundle::FontStyle fontStyle = isSyncDialog_ ?
205       chrome_style::kTextFontStyle : ui::ResourceBundle::SmallFont;
206   NSFont* font = ui::ResourceBundle::GetSharedInstance().GetFont(
207       fontStyle).GetNativeFont();
208
209   // The non-modal bubble already has a text content and only needs the
210   // Learn More link (in a smaller font).
211   if (isSyncDialog_) {
212     messageText = l10n_util::GetNSStringFWithFixup(
213         IDS_ONE_CLICK_SIGNIN_DIALOG_MESSAGE_NEW, email_);
214     messageText = [messageText stringByAppendingString:@" "];
215   } else {
216     messageText = @"";
217   }
218
219   NSColor* linkColor =
220       gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor());
221   [informativeTextView_ setMessageAndLink:messageText
222                                  withLink:learnMoreText
223                                  atOffset:[messageText length]
224                                      font:font
225                              messageColor:[NSColor blackColor]
226                                 linkColor:linkColor];
227
228   // Size to fit.
229   [[informativePlaceholderTextField_ cell] setAttributedStringValue:
230       [informativeTextView_ attributedString]];
231   [GTMUILocalizerAndLayoutTweaker
232         sizeToFitFixedWidthTextField:informativePlaceholderTextField_];
233   NSRect newFrame = [informativePlaceholderTextField_ frame];
234   [informativeTextView_ setFrame:newFrame];
235
236   // Swap placeholder.
237   [[informativePlaceholderTextField_ superview]
238      replaceSubview:informativePlaceholderTextField_
239                with:informativeTextView_.get()];
240   informativePlaceholderTextField_ = nil;  // Now released.
241
242   return NSHeight(newFrame) - NSHeight(oldFrame);
243 }
244
245 - (BOOL)textView:(NSTextView*)textView
246    clickedOnLink:(id)link
247          atIndex:(NSUInteger)charIndex {
248   if (isSyncDialog_ && !clickedLearnMore_) {
249     clickedLearnMore_ = YES;
250
251     OneClickSigninHelper::LogConfirmHistogramValue(
252         one_click_signin::HISTOGRAM_CONFIRM_LEARN_MORE);
253   }
254   WindowOpenDisposition location = isSyncDialog_ ?
255                                    NEW_WINDOW : NEW_FOREGROUND_TAB;
256   content::OpenURLParams params(GURL(chrome::kChromeSyncLearnMoreURL),
257                                 content::Referrer(), location,
258                                 content::PAGE_TRANSITION_LINK, false);
259   webContents_->OpenURL(params);
260   return YES;
261 }
262
263 - (void)close {
264   base::ResetAndReturn(&closeCallback_).Run();
265 }
266
267 @end