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