profile_signin_confirmation_dialog_views.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 "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"
37
38namespace chrome {
39// Declared in browser_dialogs.h
40void 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);
50}
51}  // namespace chrome
52
53ProfileSigninConfirmationDialogViews::ProfileSigninConfirmationDialogViews(
54    Browser* browser,
55    const std::string& username,
56    ui::ProfileSigninConfirmationDelegate* delegate)
57  : browser_(browser),
58    username_(username),
59    delegate_(delegate),
60    prompt_for_new_profile_(true),
61    continue_signin_button_(NULL) {
62}
63
64ProfileSigninConfirmationDialogViews::~ProfileSigninConfirmationDialogViews() {}
65
66// static
67void ProfileSigninConfirmationDialogViews::ShowDialog(
68    Browser* browser,
69    Profile* profile,
70    const std::string& username,
71    ui::ProfileSigninConfirmationDelegate* delegate) {
72  ProfileSigninConfirmationDialogViews* dialog =
73      new ProfileSigninConfirmationDialogViews(
74          browser, username, delegate);
75  ui::CheckShouldPromptForNewProfile(
76      profile,
77      // This callback is guaranteed to be invoked, and once it is, the dialog
78      // owns itself.
79      base::Bind(&ProfileSigninConfirmationDialogViews::Show,
80                 base::Unretained(dialog)));
81}
82
83void ProfileSigninConfirmationDialogViews::Show(bool prompt_for_new_profile) {
84  prompt_for_new_profile_ = prompt_for_new_profile;
85  CreateBrowserModalDialogViews(
86      this, browser_->window()->GetNativeWindow())->Show();
87}
88
89base::string16 ProfileSigninConfirmationDialogViews::GetWindowTitle() const {
90  return l10n_util::GetStringUTF16(
91      IDS_ENTERPRISE_SIGNIN_TITLE_NEW_STYLE);
92}
93
94base::string16 ProfileSigninConfirmationDialogViews::GetDialogButtonLabel(
95    ui::DialogButton button) const {
96  if (button == ui::DIALOG_BUTTON_OK) {
97    // If we're giving the option to create a new profile, then OK is
98    // "Create new profile".  Otherwise it is "Continue signin".
99    return l10n_util::GetStringUTF16(
100        prompt_for_new_profile_ ?
101            IDS_ENTERPRISE_SIGNIN_CREATE_NEW_PROFILE_NEW_STYLE :
102            IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
103  }
104  return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CANCEL);
105}
106
107int ProfileSigninConfirmationDialogViews::GetDefaultDialogButton() const {
108  return ui::DIALOG_BUTTON_NONE;
109}
110
111views::View* ProfileSigninConfirmationDialogViews::CreateExtraView() {
112  if (prompt_for_new_profile_) {
113    const base::string16 continue_signin_text =
114        l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
115    continue_signin_button_ =
116        new views::LabelButton(this, continue_signin_text);
117    continue_signin_button_->SetStyle(views::Button::STYLE_BUTTON);
118    continue_signin_button_->SetFocusable(true);
119  }
120  return continue_signin_button_;
121}
122
123bool ProfileSigninConfirmationDialogViews::Accept() {
124  if (delegate_) {
125    if (prompt_for_new_profile_)
126      delegate_->OnSigninWithNewProfile();
127    else
128      delegate_->OnContinueSignin();
129    delegate_ = NULL;
130  }
131  return true;
132}
133
134bool ProfileSigninConfirmationDialogViews::Cancel() {
135  if (delegate_) {
136    delegate_->OnCancelSignin();
137    delegate_ = NULL;
138  }
139  return true;
140}
141
142void ProfileSigninConfirmationDialogViews::OnClosed() {
143  Cancel();
144}
145
146ui::ModalType ProfileSigninConfirmationDialogViews::GetModalType() const {
147  return ui::MODAL_TYPE_WINDOW;
148}
149
150void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged(
151    const ViewHierarchyChangedDetails& details) {
152  if (!details.is_add || details.child != this)
153    return;
154
155  const SkColor kPromptBarBackgroundColor =
156      ui::GetSigninConfirmationPromptBarColor(
157          ui::kSigninConfirmationPromptBarBackgroundAlpha);
158
159  // Create the prompt label.
160  size_t offset;
161  const base::string16 domain =
162      base::ASCIIToUTF16(gaia::ExtractDomainName(username_));
163  const base::string16 username = base::ASCIIToUTF16(username_);
164  const base::string16 prompt_text =
165      l10n_util::GetStringFUTF16(
166          IDS_ENTERPRISE_SIGNIN_ALERT_NEW_STYLE,
167          domain, &offset);
168  views::StyledLabel* prompt_label = new views::StyledLabel(prompt_text, this);
169  prompt_label->SetDisplayedOnBackgroundColor(kPromptBarBackgroundColor);
170
171  views::StyledLabel::RangeStyleInfo bold_style;
172  bold_style.font_style = gfx::Font::BOLD;
173  prompt_label->AddStyleRange(
174      gfx::Range(offset, offset + domain.size()), bold_style);
175
176  // Create the prompt bar.
177  views::View* prompt_bar = new views::View;
178  prompt_bar->SetBorder(views::Border::CreateSolidSidedBorder(
179      1,
180      0,
181      1,
182      0,
183      ui::GetSigninConfirmationPromptBarColor(
184          ui::kSigninConfirmationPromptBarBorderAlpha)));
185  prompt_bar->set_background(views::Background::CreateSolidBackground(
186      kPromptBarBackgroundColor));
187
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());
202
203  // Layout the components.
204  views::GridLayout* dialog_layout = new views::GridLayout(this);
205  SetLayoutManager(dialog_layout);
206
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);
223
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));
238}
239
240void 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);
250}
251
252void 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;
260  }
261  GetWidget()->Close();
262}
263