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