profile_signin_confirmation_dialog_views.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 "base/strings/utf_string_conversions.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_dialogs.h"
10#include "chrome/browser/ui/browser_navigator.h"
11#include "chrome/browser/ui/browser_window.h"
12#include "chrome/browser/ui/host_desktop.h"
13#include "chrome/browser/ui/views/constrained_window_views.h"
14#include "components/web_modal/web_contents_modal_dialog_manager.h"
15#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
16#include "content/public/browser/web_contents.h"
17#include "content/public/browser/web_contents_view.h"
18#include "google_apis/gaia/gaia_auth_util.h"
19#include "grit/chromium_strings.h"
20#include "grit/generated_resources.h"
21#include "third_party/skia/include/core/SkColor.h"
22#include "ui/base/l10n/l10n_util.h"
23#include "ui/base/range/range.h"
24#include "ui/gfx/font.h"
25#include "ui/gfx/native_widget_types.h"
26#include "ui/views/background.h"
27#include "ui/views/controls/label.h"
28#include "ui/views/controls/link.h"
29#include "ui/views/controls/styled_label.h"
30#include "ui/views/layout/box_layout.h"
31#include "ui/views/layout/grid_layout.h"
32#include "ui/views/layout/layout_constants.h"
33#include "ui/views/widget/widget.h"
34
35namespace {
36
37// Wrap a view in a fixed-width container.
38views::View* MakeFixedWidth(views::View* view, int width) {
39  views::View* container = new views::View;
40  views::GridLayout* layout = views::GridLayout::CreatePanel(container);
41  container->SetLayoutManager(layout);
42  layout->AddColumnSet(0)->AddColumn(
43      views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
44      views::GridLayout::FIXED, width, false);
45  layout->StartRow(0, 0);
46  layout->AddView(view, 1, 1, views::GridLayout::FILL, views::GridLayout::FILL);
47  return container;
48}
49
50}  // namespace
51
52namespace chrome {
53// Declared in browser_dialogs.h
54void ShowProfileSigninConfirmationDialog(
55    Browser* browser,
56    content::WebContents* web_contents,
57    Profile* profile,
58    const std::string& username,
59    ui::ProfileSigninConfirmationDelegate* delegate) {
60  ProfileSigninConfirmationDialogViews::ShowDialog(browser,
61                                                   profile,
62                                                   username,
63                                                   delegate);
64}
65}  // namespace chrome
66
67ProfileSigninConfirmationDialogViews::ProfileSigninConfirmationDialogViews(
68    Browser* browser,
69    Profile* profile,
70    const std::string& username,
71    ui::ProfileSigninConfirmationDelegate* delegate)
72  : browser_(browser),
73    profile_(profile),
74    username_(username),
75    delegate_(delegate),
76    prompt_for_new_profile_(true),
77    link_(NULL) {
78}
79
80ProfileSigninConfirmationDialogViews::~ProfileSigninConfirmationDialogViews() {}
81
82// static
83void ProfileSigninConfirmationDialogViews::ShowDialog(
84    Browser* browser,
85    Profile* profile,
86    const std::string& username,
87    ui::ProfileSigninConfirmationDelegate* delegate) {
88  ProfileSigninConfirmationDialogViews* dialog =
89      new ProfileSigninConfirmationDialogViews(
90          browser, profile, username, delegate);
91  ui::CheckShouldPromptForNewProfile(
92      profile,
93      // This callback is guaranteed to be invoked, and once it is, the dialog
94      // owns itself.
95      base::Bind(&ProfileSigninConfirmationDialogViews::Show,
96                 base::Unretained(dialog)));
97}
98
99void ProfileSigninConfirmationDialogViews::Show(bool prompt_for_new_profile) {
100  prompt_for_new_profile_ = prompt_for_new_profile;
101  CreateBrowserModalDialogViews(
102      this, browser_->window()->GetNativeWindow())->Show();
103}
104
105string16 ProfileSigninConfirmationDialogViews::GetWindowTitle() const {
106  return l10n_util::GetStringUTF16(
107      IDS_ENTERPRISE_SIGNIN_TITLE_NEW_STYLE);
108}
109
110string16 ProfileSigninConfirmationDialogViews::GetDialogButtonLabel(
111    ui::DialogButton button) const {
112  return l10n_util::GetStringUTF16((button == ui::DIALOG_BUTTON_OK) ?
113      IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE :
114      IDS_ENTERPRISE_SIGNIN_CANCEL);
115}
116
117int ProfileSigninConfirmationDialogViews::GetDefaultDialogButton() const {
118  return ui::DIALOG_BUTTON_NONE;
119}
120
121views::View* ProfileSigninConfirmationDialogViews::CreateExtraView() {
122  if (prompt_for_new_profile_) {
123    const string16 create_profile_text =
124        l10n_util::GetStringUTF16(
125            IDS_ENTERPRISE_SIGNIN_CREATE_NEW_PROFILE_NEW_STYLE);
126    link_ = new views::Link(create_profile_text);
127    link_->SetUnderline(false);
128    link_->set_listener(this);
129    link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
130  }
131  return link_;
132}
133
134bool ProfileSigninConfirmationDialogViews::Accept() {
135  if (delegate_) {
136    delegate_->OnContinueSignin();
137    delegate_ = NULL;
138  }
139  return true;
140}
141
142bool ProfileSigninConfirmationDialogViews::Cancel() {
143  if (delegate_) {
144    delegate_->OnCancelSignin();
145    delegate_ = NULL;
146  }
147  return true;
148}
149
150void ProfileSigninConfirmationDialogViews::OnClose() {
151  Cancel();
152}
153
154ui::ModalType ProfileSigninConfirmationDialogViews::GetModalType() const {
155  return ui::MODAL_TYPE_WINDOW;
156}
157
158void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged(
159    const ViewHierarchyChangedDetails& details) {
160  if (!details.is_add || details.child != this)
161    return;
162
163  // Layout the labels in a single fixed-width column.
164  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
165
166  // Create the prompt label.
167  std::vector<size_t> offsets;
168  const string16 domain = ASCIIToUTF16(gaia::ExtractDomainName(username_));
169  const string16 username = ASCIIToUTF16(username_);
170  const string16 prompt_text =
171      l10n_util::GetStringFUTF16(
172          IDS_ENTERPRISE_SIGNIN_ALERT_NEW_STYLE,
173          username, domain, &offsets);
174  views::StyledLabel* prompt_label = new views::StyledLabel(prompt_text, this);
175  views::StyledLabel::RangeStyleInfo bold_style;
176  bold_style.font_style = gfx::Font::BOLD;
177  prompt_label->AddStyleRange(
178      ui::Range(offsets[1], offsets[1] + domain.size()), bold_style);
179
180  // Add the prompt label with a darker background and border.
181  const int kDialogWidth = 440;
182  views::View* prompt_container = MakeFixedWidth(prompt_label, kDialogWidth);
183  prompt_container->set_border(
184      views::Border::CreateSolidSidedBorder(
185          1, 0, 1, 0,
186          ui::GetSigninConfirmationPromptBarColor(
187              ui::kSigninConfirmationPromptBarBorderAlpha)));
188  prompt_container->set_background(
189      views::Background::CreateSolidBackground(
190          ui::GetSigninConfirmationPromptBarColor(
191              ui::kSigninConfirmationPromptBarBackgroundAlpha)));
192  AddChildView(prompt_container);
193
194  // Create and add the explanation label.
195  offsets.clear();
196  const string16 learn_more_text =
197      l10n_util::GetStringUTF16(
198          IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
199  const string16 signin_explanation_text =
200      l10n_util::GetStringFUTF16(prompt_for_new_profile_ ?
201          IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITH_PROFILE_CREATION_NEW_STYLE :
202          IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITHOUT_PROFILE_CREATION_NEW_STYLE,
203          username, learn_more_text, &offsets);
204  explanation_label_ = new views::StyledLabel(signin_explanation_text, this);
205  views::StyledLabel::RangeStyleInfo link_style =
206      views::StyledLabel::RangeStyleInfo::CreateForLink();
207  link_style.font_style = gfx::Font::NORMAL;
208  explanation_label_->AddStyleRange(
209      ui::Range(offsets[1], offsets[1] + learn_more_text.size()),
210      link_style);
211  // TODO(dconnelly): set the background color on the label (crbug.com/244630)
212  AddChildView(MakeFixedWidth(explanation_label_, kDialogWidth));
213}
214
215void ProfileSigninConfirmationDialogViews::LinkClicked(views::Link* source,
216                                                       int event_flags) {
217  if (delegate_) {
218    delegate_->OnSigninWithNewProfile();
219    delegate_ = NULL;
220  }
221  GetWidget()->Close();
222}
223
224void ProfileSigninConfirmationDialogViews::StyledLabelLinkClicked(
225    const ui::Range& range,
226    int event_flags) {
227  chrome::NavigateParams params(
228      browser_,
229      GURL("http://support.google.com/chromeos/bin/answer.py?answer=1331549"),
230      content::PAGE_TRANSITION_LINK);
231  params.disposition = NEW_POPUP;
232  params.window_action = chrome::NavigateParams::SHOW_WINDOW;
233  chrome::Navigate(&params);
234}
235