Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / sync / profile_signin_confirmation_dialog_views.cc
1 // Copyright 2013 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 #include "chrome/browser/ui/views/sync/profile_signin_confirmation_dialog_views.h"
6
7 #include <algorithm>
8
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_dialogs.h"
12 #include "chrome/browser/ui/browser_navigator.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/browser/ui/views/constrained_window_views.h"
16 #include "components/web_modal/web_contents_modal_dialog_manager.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
18 #include "content/public/browser/web_contents.h"
19 #include "google_apis/gaia/gaia_auth_util.h"
20 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/font.h"
25 #include "ui/gfx/native_widget_types.h"
26 #include "ui/gfx/range/range.h"
27 #include "ui/views/background.h"
28 #include "ui/views/border.h"
29 #include "ui/views/controls/label.h"
30 #include "ui/views/controls/styled_label.h"
31 #include "ui/views/layout/box_layout.h"
32 #include "ui/views/layout/grid_layout.h"
33 #include "ui/views/layout/layout_constants.h"
34 #include "ui/views/widget/widget.h"
35 #include "ui/views/window/dialog_client_view.h"
36
37 namespace chrome {
38 // Declared in browser_dialogs.h
39 void ShowProfileSigninConfirmationDialog(
40     Browser* browser,
41     content::WebContents* web_contents,
42     Profile* profile,
43     const std::string& username,
44     ui::ProfileSigninConfirmationDelegate* delegate) {
45   ProfileSigninConfirmationDialogViews::ShowDialog(browser,
46                                                    profile,
47                                                    username,
48                                                    delegate);
49 }
50 }  // namespace chrome
51
52 ProfileSigninConfirmationDialogViews::ProfileSigninConfirmationDialogViews(
53     Browser* browser,
54     const std::string& username,
55     ui::ProfileSigninConfirmationDelegate* delegate)
56   : browser_(browser),
57     username_(username),
58     delegate_(delegate),
59     prompt_for_new_profile_(true),
60     continue_signin_button_(NULL) {
61 }
62
63 ProfileSigninConfirmationDialogViews::~ProfileSigninConfirmationDialogViews() {}
64
65 // static
66 void ProfileSigninConfirmationDialogViews::ShowDialog(
67     Browser* browser,
68     Profile* profile,
69     const std::string& username,
70     ui::ProfileSigninConfirmationDelegate* delegate) {
71   ProfileSigninConfirmationDialogViews* dialog =
72       new ProfileSigninConfirmationDialogViews(
73           browser, username, delegate);
74   ui::CheckShouldPromptForNewProfile(
75       profile,
76       // This callback is guaranteed to be invoked, and once it is, the dialog
77       // owns itself.
78       base::Bind(&ProfileSigninConfirmationDialogViews::Show,
79                  base::Unretained(dialog)));
80 }
81
82 void ProfileSigninConfirmationDialogViews::Show(bool prompt_for_new_profile) {
83   prompt_for_new_profile_ = prompt_for_new_profile;
84   CreateBrowserModalDialogViews(
85       this, browser_->window()->GetNativeWindow())->Show();
86 }
87
88 base::string16 ProfileSigninConfirmationDialogViews::GetWindowTitle() const {
89   return l10n_util::GetStringUTF16(
90       IDS_ENTERPRISE_SIGNIN_TITLE_NEW_STYLE);
91 }
92
93 base::string16 ProfileSigninConfirmationDialogViews::GetDialogButtonLabel(
94     ui::DialogButton button) const {
95   if (button == ui::DIALOG_BUTTON_OK) {
96     // If we're giving the option to create a new profile, then OK is
97     // "Create new profile".  Otherwise it is "Continue signin".
98     return l10n_util::GetStringUTF16(
99         prompt_for_new_profile_ ?
100             IDS_ENTERPRISE_SIGNIN_CREATE_NEW_PROFILE_NEW_STYLE :
101             IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
102   }
103   return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CANCEL);
104 }
105
106 int ProfileSigninConfirmationDialogViews::GetDefaultDialogButton() const {
107   return ui::DIALOG_BUTTON_NONE;
108 }
109
110 views::View* ProfileSigninConfirmationDialogViews::CreateExtraView() {
111   if (prompt_for_new_profile_) {
112     const base::string16 continue_signin_text =
113         l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
114     continue_signin_button_ =
115         new views::LabelButton(this, continue_signin_text);
116     continue_signin_button_->SetStyle(views::Button::STYLE_BUTTON);
117     continue_signin_button_->SetFocusable(true);
118   }
119   return continue_signin_button_;
120 }
121
122 bool ProfileSigninConfirmationDialogViews::Accept() {
123   if (delegate_) {
124     if (prompt_for_new_profile_)
125       delegate_->OnSigninWithNewProfile();
126     else
127       delegate_->OnContinueSignin();
128     delegate_ = NULL;
129   }
130   return true;
131 }
132
133 bool ProfileSigninConfirmationDialogViews::Cancel() {
134   if (delegate_) {
135     delegate_->OnCancelSignin();
136     delegate_ = NULL;
137   }
138   return true;
139 }
140
141 void ProfileSigninConfirmationDialogViews::OnClosed() {
142   Cancel();
143 }
144
145 ui::ModalType ProfileSigninConfirmationDialogViews::GetModalType() const {
146   return ui::MODAL_TYPE_WINDOW;
147 }
148
149 void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged(
150     const ViewHierarchyChangedDetails& details) {
151   if (!details.is_add || details.child != this)
152     return;
153
154   const SkColor kPromptBarBackgroundColor =
155       ui::GetSigninConfirmationPromptBarColor(
156           ui::kSigninConfirmationPromptBarBackgroundAlpha);
157
158   // Create the prompt label.
159   size_t offset;
160   const base::string16 domain =
161       base::ASCIIToUTF16(gaia::ExtractDomainName(username_));
162   const base::string16 username = base::ASCIIToUTF16(username_);
163   const base::string16 prompt_text =
164       l10n_util::GetStringFUTF16(
165           IDS_ENTERPRISE_SIGNIN_ALERT_NEW_STYLE,
166           domain, &offset);
167   views::StyledLabel* prompt_label = new views::StyledLabel(prompt_text, this);
168   prompt_label->SetDisplayedOnBackgroundColor(kPromptBarBackgroundColor);
169
170   views::StyledLabel::RangeStyleInfo bold_style;
171   bold_style.font_style = gfx::Font::BOLD;
172   prompt_label->AddStyleRange(
173       gfx::Range(offset, offset + domain.size()), bold_style);
174
175   // Create the prompt bar.
176   views::View* prompt_bar = new views::View;
177   prompt_bar->SetBorder(views::Border::CreateSolidSidedBorder(
178       1,
179       0,
180       1,
181       0,
182       ui::GetSigninConfirmationPromptBarColor(
183           ui::kSigninConfirmationPromptBarBorderAlpha)));
184   prompt_bar->set_background(views::Background::CreateSolidBackground(
185       kPromptBarBackgroundColor));
186
187   // Create the explanation label.
188   std::vector<size_t> offsets;
189   const base::string16 learn_more_text =
190       l10n_util::GetStringUTF16(
191           IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
192   const base::string16 signin_explanation_text =
193       l10n_util::GetStringFUTF16(prompt_for_new_profile_ ?
194           IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITH_PROFILE_CREATION_NEW_STYLE :
195           IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITHOUT_PROFILE_CREATION_NEW_STYLE,
196           username, learn_more_text, &offsets);
197   explanation_label_ = new views::StyledLabel(signin_explanation_text, this);
198   explanation_label_->AddStyleRange(
199       gfx::Range(offsets[1], offsets[1] + learn_more_text.size()),
200       views::StyledLabel::RangeStyleInfo::CreateForLink());
201
202   // Layout the components.
203   views::GridLayout* dialog_layout = new views::GridLayout(this);
204   SetLayoutManager(dialog_layout);
205
206   // Use GridLayout inside the prompt bar because StyledLabel requires it.
207   views::GridLayout* prompt_layout = views::GridLayout::CreatePanel(prompt_bar);
208   prompt_bar->SetLayoutManager(prompt_layout);
209   prompt_layout->AddColumnSet(0)->AddColumn(
210       views::GridLayout::FILL, views::GridLayout::CENTER, 100,
211       views::GridLayout::USE_PREF, 0, 0);
212   prompt_layout->StartRow(0, 0);
213   prompt_layout->AddView(prompt_label);
214   // Use a column set with no padding.
215   dialog_layout->AddColumnSet(0)->AddColumn(
216       views::GridLayout::FILL, views::GridLayout::FILL, 100,
217       views::GridLayout::USE_PREF, 0, 0);
218   dialog_layout->StartRow(0, 0);
219   dialog_layout->AddView(
220       prompt_bar, 1, 1,
221       views::GridLayout::FILL, views::GridLayout::FILL, 0, 0);
222
223   // Use a new column set for the explanation label so we can add padding.
224   dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin);
225   views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1);
226   explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
227   explanation_columns->AddColumn(
228       views::GridLayout::FILL, views::GridLayout::FILL, 100,
229       views::GridLayout::USE_PREF, 0, 0);
230   explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
231   dialog_layout->StartRow(0, 1);
232   const int kPreferredWidth = 440;
233   dialog_layout->AddView(
234       explanation_label_, 1, 1,
235       views::GridLayout::FILL, views::GridLayout::FILL,
236       kPreferredWidth, explanation_label_->GetHeightForWidth(kPreferredWidth));
237 }
238
239 void ProfileSigninConfirmationDialogViews::StyledLabelLinkClicked(
240     const gfx::Range& range,
241     int event_flags) {
242   chrome::NavigateParams params(
243       browser_,
244       GURL("http://support.google.com/chromeos/bin/answer.py?answer=1331549"),
245       content::PAGE_TRANSITION_LINK);
246   params.disposition = NEW_POPUP;
247   params.window_action = chrome::NavigateParams::SHOW_WINDOW;
248   chrome::Navigate(&params);
249 }
250
251 void ProfileSigninConfirmationDialogViews::ButtonPressed(
252     views::Button* sender,
253     const ui::Event& event) {
254   DCHECK(prompt_for_new_profile_);
255   DCHECK_EQ(continue_signin_button_, sender);
256   if (delegate_) {
257     delegate_->OnContinueSignin();
258     delegate_ = NULL;
259   }
260   GetWidget()->Close();
261 }