OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/profiles/forced_reauthentication_dialog.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/i18n/message_formatter.h" | |
12 #include "base/strings/string16.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/browser_list.h" | |
17 #include "chrome/browser/ui/browser_window.h" | |
18 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h" | |
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
20 #include "chrome/browser/ui/views/frame/browser_view.h" | |
21 #include "chrome/grit/chromium_strings.h" | |
22 #include "chrome/grit/generated_resources.h" | |
23 #include "components/constrained_window/constrained_window_views.h" | |
24 #include "components/signin/core/browser/signin_manager.h" | |
25 #include "ui/base/l10n/l10n_util.h" | |
26 #include "ui/views/background.h" | |
27 #include "ui/views/border.h" | |
28 #include "ui/views/controls/styled_label.h" | |
29 #include "ui/views/layout/grid_layout.h" | |
30 #include "ui/views/layout/layout_constants.h" | |
31 #include "ui/views/layout/layout_provider.h" | |
32 #include "ui/views/view.h" | |
33 #include "ui/views/window/dialog_client_view.h" | |
34 | |
35 namespace { | |
36 | |
37 // Refresh title of the dialog every second. | |
38 constexpr int kRefreshTitleTimer = 1; | |
39 | |
40 // If browser windows are going to be closed soon, close browser window before | |
41 // showing sign in dialog because there might not be enough time for user to | |
42 // finish sign in. | |
43 constexpr int kCloseDirectlyTimer = 60; | |
44 | |
45 void Signout(SigninManager* signin_manager) { | |
46 signin_manager->SignOut( | |
47 signin_metrics::AUTHENTICATION_FAILED_WITH_FORCE_SIGNIN, | |
48 signin_metrics::SignoutDelete::KEEPING); | |
49 } | |
50 | |
51 bool IsMatchingBrowser(Browser* browser, Profile* profile) { | |
52 return browser->profile()->GetOriginalProfile() == | |
53 profile->GetOriginalProfile() && | |
54 !browser->tab_strip_model()->empty() && | |
55 BrowserView::GetBrowserViewForBrowser(browser)->frame()->IsVisible(); | |
56 } | |
57 | |
58 // Find a browser that is assoicated with |profile| to show the dialog for | |
59 // Sign out warning. | |
60 Browser* FindBrowserWithProfile(Profile* profile) { | |
61 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
62 if (browser && IsMatchingBrowser(browser, profile)) | |
63 return browser; | |
64 for (auto* browser : *BrowserList::GetInstance()) { | |
65 if (IsMatchingBrowser(browser, profile)) { | |
66 return browser; | |
67 } | |
68 } | |
69 return nullptr; | |
70 } | |
71 | |
72 // PromptLabel overrides the default insets of StyledLabel. | |
73 class PromptLabel : public views::StyledLabel { | |
74 public: | |
75 PromptLabel(const base::string16& text, views::StyledLabelListener* listener) | |
76 : views::StyledLabel(text, listener) {} | |
77 | |
78 gfx::Insets GetInsets() const override { | |
79 return views::LayoutProvider::Get()->GetInsetsMetric( | |
80 views::INSETS_DIALOG_CONTENTS); | |
81 } | |
82 }; | |
83 | |
84 } // namespace | |
85 | |
86 ForcedReauthenticationDialog::ForcedReauthenticationDialog( | |
87 Browser* browser, | |
88 SigninManager* signin_manager, | |
89 int countdown_duration) | |
90 : browser_(browser), | |
91 signin_manager_(signin_manager), | |
92 desired_close_time_(base::TimeTicks::Now() + | |
93 base::TimeDelta::FromSeconds(countdown_duration)) { | |
94 constrained_window::CreateBrowserModalDialogViews( | |
95 this, browser->window()->GetNativeWindow()) | |
96 ->Show(); | |
97 browser->window()->FlashFrame(true); | |
98 browser->window()->Activate(); | |
99 } | |
100 | |
101 ForcedReauthenticationDialog::~ForcedReauthenticationDialog() {} | |
102 | |
103 // static | |
104 ForcedReauthenticationDialog* ForcedReauthenticationDialog::ShowDialog( | |
105 Profile* profile, | |
106 SigninManager* signin_manager, | |
107 int countdown_duration) { | |
108 Browser* browser = FindBrowserWithProfile(profile); | |
109 if (browser == nullptr) { // If there is no browser, we can just sign | |
110 // out profile directly. | |
111 Signout(signin_manager); | |
112 return nullptr; | |
113 } | |
114 | |
115 return new ForcedReauthenticationDialog(browser, signin_manager, | |
116 countdown_duration); | |
117 } | |
118 | |
119 bool ForcedReauthenticationDialog::Accept() { | |
120 if (GetTimeRemaining() < base::TimeDelta::FromSeconds(kCloseDirectlyTimer)) { | |
sky
2017/06/07 20:37:59
Is this conditional still needed? Isn't the suppli
zmin
2017/06/07 20:56:49
I need this to control the behavior of confirm dia
| |
121 Signout(signin_manager_); | |
122 } else { | |
123 browser_->signin_view_controller()->ShowModalSignin( | |
124 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_REAUTH, browser_, | |
125 signin_metrics::AccessPoint::ACCESS_POINT_FORCE_SIGNIN_WARNING); | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 bool ForcedReauthenticationDialog::Cancel() { | |
131 return true; | |
132 } | |
133 | |
134 void ForcedReauthenticationDialog::WindowClosing() { | |
135 refresh_timer_.Stop(); | |
136 } | |
137 | |
138 base::string16 ForcedReauthenticationDialog::GetWindowTitle() const { | |
139 base::TimeDelta time_left = GetTimeRemaining(); | |
140 return base::i18n::MessageFormatter::FormatWithNumberedArgs( | |
141 l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_TITLE), | |
142 time_left.InMinutes(), time_left.InSeconds() % 60); | |
143 } | |
144 | |
145 base::string16 ForcedReauthenticationDialog::GetDialogButtonLabel( | |
146 ui::DialogButton button) const { | |
147 if (button == ui::DIALOG_BUTTON_OK) | |
148 return l10n_util::GetStringUTF16( | |
149 IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_CONFIRM); | |
150 else | |
sky
2017/06/07 20:37:59
no else after return (see chrome style guide).
zmin
2017/06/07 20:56:49
Done.
| |
151 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_DELAY); | |
152 } | |
153 | |
154 ui::ModalType ForcedReauthenticationDialog::GetModalType() const { | |
155 return ui::MODAL_TYPE_WINDOW; | |
156 } | |
157 | |
158 void ForcedReauthenticationDialog::AddedToWidget() { | |
159 const SkColor prompt_bar_background_color = | |
160 GetSigninConfirmationPromptBarColor( | |
161 GetNativeTheme(), ui::kSigninConfirmationPromptBarBackgroundAlpha); | |
162 // Create the prompt label. | |
163 size_t offset; | |
164 std::string email = signin_manager_->GetAuthenticatedAccountInfo().email; | |
165 const base::string16 domain = | |
166 base::ASCIIToUTF16(gaia::ExtractDomainName(email)); | |
167 const base::string16 prompt_text = | |
168 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_SIGNIN_ALERT, domain, &offset); | |
169 | |
170 // Create the prompt label. | |
171 PromptLabel* prompt_label = new PromptLabel(prompt_text, nullptr); | |
172 prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color); | |
173 | |
174 views::StyledLabel::RangeStyleInfo bold_style; | |
175 bold_style.weight = gfx::Font::Weight::BOLD; | |
176 prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()), | |
177 bold_style); | |
178 | |
179 prompt_label->SetBorder(views::CreateSolidSidedBorder( | |
180 1, 0, 1, 0, | |
181 ui::GetSigninConfirmationPromptBarColor( | |
182 GetNativeTheme(), ui::kSigninConfirmationPromptBarBorderAlpha))); | |
183 prompt_label->SetBackground( | |
184 views::CreateSolidBackground(prompt_bar_background_color)); | |
185 | |
186 // Create the explanation label. | |
187 base::string16 signin_explanation_text; | |
188 base::string16 close_warning; | |
189 close_warning = l10n_util::GetStringUTF16( | |
190 IDS_ENTERPRISE_FORCE_SIGNOUT_ADDITIONAL_EXPLANATION); | |
191 if (email.empty()) { | |
192 signin_explanation_text = l10n_util::GetStringFUTF16( | |
193 IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION_WITHOUT_USER_NAME, | |
194 close_warning); | |
195 } else { | |
196 signin_explanation_text = | |
197 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION, | |
198 base::ASCIIToUTF16(email), close_warning); | |
199 } | |
200 views::StyledLabel* explanation_label = | |
201 new views::StyledLabel(signin_explanation_text, nullptr); | |
202 | |
203 // Layout the components. | |
204 const gfx::Insets panel_insets = | |
205 views::LayoutProvider::Get()->GetInsetsMetric( | |
206 views::INSETS_DIALOG_CONTENTS); | |
207 SetBorder(views::CreateEmptyBorder(panel_insets.top(), 0, | |
208 panel_insets.bottom(), 0)); | |
209 views::GridLayout* dialog_layout = new views::GridLayout(this); | |
210 SetLayoutManager(dialog_layout); | |
211 | |
212 // Use a column set with no padding. | |
213 dialog_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL, | |
214 views::GridLayout::FILL, 100, | |
215 views::GridLayout::USE_PREF, 0, 0); | |
216 dialog_layout->StartRow(0, 0); | |
217 dialog_layout->AddView(prompt_label, 1, 1, views::GridLayout::FILL, | |
218 views::GridLayout::FILL, 0, 0); | |
219 | |
220 // Use a new column set for the explanation label so we can add padding. | |
221 dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin); | |
222 views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1); | |
223 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); | |
224 explanation_columns->AddColumn(views::GridLayout::FILL, | |
225 views::GridLayout::FILL, 100, | |
226 views::GridLayout::USE_PREF, 0, 0); | |
227 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); | |
228 dialog_layout->StartRow(0, 1); | |
229 const int kPreferredWidth = 440; | |
230 dialog_layout->AddView(explanation_label, 1, 1, views::GridLayout::FILL, | |
231 views::GridLayout::FILL, kPreferredWidth, | |
232 explanation_label->GetHeightForWidth(kPreferredWidth)); | |
233 refresh_timer_.Start(FROM_HERE, | |
234 base::TimeDelta::FromSeconds(kRefreshTitleTimer), this, | |
235 &ForcedReauthenticationDialog::OnCountDown); | |
236 } | |
237 | |
238 void ForcedReauthenticationDialog::OnCountDown() { | |
239 if (desired_close_time_ <= base::TimeTicks::Now()) { | |
240 Cancel(); | |
241 GetWidget()->Close(); | |
242 } | |
243 GetWidget()->UpdateWindowTitle(); | |
244 } | |
245 | |
246 base::TimeDelta ForcedReauthenticationDialog::GetTimeRemaining() const { | |
247 base::TimeTicks now = base::TimeTicks::Now(); | |
248 if (desired_close_time_ <= now) | |
249 return base::TimeDelta(); | |
250 return desired_close_time_ - now; | |
251 } | |
OLD | NEW |