Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/ui/views/profiles/force_signout_dialog.cc

Issue 2862653002: If force-sign-in policy is enabled, popup warning dialog before window closing if auth token becom… (Closed)
Patch Set: cr and rebase from master Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/force_signout_dialog.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/browser/ui/views/frame/browser_view.h"
20 #include "chrome/grit/chromium_strings.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "components/constrained_window/constrained_window_views.h"
23 #include "components/signin/core/browser/signin_manager.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/views/background.h"
26 #include "ui/views/border.h"
27 #include "ui/views/controls/styled_label.h"
28 #include "ui/views/layout/grid_layout.h"
29 #include "ui/views/layout/layout_constants.h"
30 #include "ui/views/layout/layout_provider.h"
31 #include "ui/views/view.h"
32 #include "ui/views/window/dialog_client_view.h"
33
34 namespace {
35
36 void Signout(SigninManager* signin_manager) {
37 signin_manager->SignOut(
38 signin_metrics::AUTHENTICATION_FAILED_WITH_FORCE_SIGNIN,
39 signin_metrics::SignoutDelete::KEEPING);
40 }
41
42 bool IsMatchingBrowser(Browser* browser, Profile* profile) {
43 return browser->profile()->GetOriginalProfile() ==
44 profile->GetOriginalProfile() &&
45 !browser->tab_strip_model()->empty() &&
46 BrowserView::GetBrowserViewForBrowser(browser)->frame()->IsVisible();
47 }
48
49 // Find a browser that is assoicated with |profile| to show the dialog for
50 // Sign out warning.
51 Browser* FindBrowserWithProfile(Profile* profile) {
52 Browser* browser = BrowserList::GetInstance()->GetLastActive();
53 if (browser && IsMatchingBrowser(browser, profile))
54 return browser;
55 for (auto* browser : *BrowserList::GetInstance()) {
56 if (IsMatchingBrowser(browser, profile)) {
57 return browser;
58 }
59 }
60 return nullptr;
61 }
62
63 } // namespace
64
65 ForceSignoutDialog::ForceSignoutDialog(Browser* browser,
66 SigninManager* signin_manager,
67 const SignoutCallback& signout_callback,
68 bool delay_allowed)
69 : browser_(browser),
70 signin_manager_(signin_manager),
71 signout_callback_(signout_callback),
72 delay_allowed_(delay_allowed) {
73 BrowserModalDialogList::GetInstance()->AddDialog(this);
74 BrowserList::AddObserver(this);
75 constrained_window::CreateBrowserModalDialogViews(
76 this, browser->window()->GetNativeWindow())
77 ->Show();
78 }
79
80 ForceSignoutDialog::~ForceSignoutDialog() {
81 BrowserList::RemoveObserver(this);
82 BrowserModalDialogList::GetInstance()->RemoveDialog(this);
83 }
sky 2017/05/18 16:50:51 It's possible to get here without accept/cancel be
84
85 // static
86 ForceSignoutDialog* ForceSignoutDialog::ShowDialog(
87 Profile* profile,
88 SigninManager* signin_manager,
89 const SignoutCallback& signout_callback,
90 bool delay_allowed) {
91 Browser* browser = FindBrowserWithProfile(profile);
92 if (browser == nullptr) { // If there is no browser, we can just sign
93 // out profile directly.
94 Signout(signin_manager);
95 if (!signout_callback.is_null())
96 signout_callback.Run(true);
97 return nullptr;
98 }
99
100 return new ForceSignoutDialog(browser, signin_manager, signout_callback,
101 delay_allowed);
102 }
103
104 void ForceSignoutDialog::ActivateModalDialog(Browser* browser) {
105 if (browser_ && browser_ != browser) {
106 browser_->window()->FlashFrame(true);
107 browser_->window()->Activate();
108 }
109 GetWidget()->Show();
110 GetWidget()->Activate();
111 }
112
113 bool ForceSignoutDialog::IsShowing() {
114 return GetWidget()->IsVisible();
115 }
116
117 void ForceSignoutDialog::OnBrowserRemoved(Browser* browser) {
118 if (browser_ == browser)
sky 2017/05/18 16:50:51 You shouldn't need this. The dialog is parented to
119 browser_ = nullptr;
120 }
121
122 bool ForceSignoutDialog::Accept() {
123 Signout(signin_manager_);
124 if (!signout_callback_.is_null())
125 signout_callback_.Run(true);
126 return true;
127 }
128
129 bool ForceSignoutDialog::Cancel() {
130 if (!signout_callback_.is_null())
131 signout_callback_.Run(false);
132 return true;
133 }
134
135 base::string16 ForceSignoutDialog::GetWindowTitle() const {
136 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_TITLE);
137 }
138
139 base::string16 ForceSignoutDialog::GetDialogButtonLabel(
140 ui::DialogButton button) const {
141 if (button == ui::DIALOG_BUTTON_OK)
142 return l10n_util::GetStringUTF16(
143 IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_CONFIRM);
144 else
145 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_DELAY);
146 }
147
148 int ForceSignoutDialog::GetDialogButtons() const {
149 if (delay_allowed_)
150 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
151 return ui::DIALOG_BUTTON_OK;
152 }
153
154 ui::ModalType ForceSignoutDialog::GetModalType() const {
155 return ui::MODAL_TYPE_WINDOW;
156 }
157
158 void ForceSignoutDialog::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 views::StyledLabel* prompt_label =
170 new views::StyledLabel(prompt_text, nullptr);
171 prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color);
172
173 views::StyledLabel::RangeStyleInfo bold_style;
174 bold_style.weight = gfx::Font::Weight::BOLD;
175 prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()),
176 bold_style);
177
178 // Create the prompt bar.
179 views::View* prompt_bar = new views::View;
180 prompt_bar->SetBorder(views::CreateSolidSidedBorder(
181 1, 0, 1, 0,
182 ui::GetSigninConfirmationPromptBarColor(
183 GetNativeTheme(), ui::kSigninConfirmationPromptBarBorderAlpha)));
184 prompt_bar->set_background(
185 views::Background::CreateSolidBackground(prompt_bar_background_color));
186
187 // Create the explanation label.
188 base::string16 signin_explanation_text;
189 base::string16 close_warning;
190 if (delay_allowed_) {
191 close_warning = l10n_util::GetStringUTF16(
192 IDS_ENTERPRISE_FORCE_SIGNOUT_ADDITIONAL_EXPLANATION);
193 }
194 if (email.empty()) {
195 signin_explanation_text = l10n_util::GetStringFUTF16(
196 IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION_WITHOUT_USER_NAME,
197 close_warning);
198 } else {
199 signin_explanation_text =
200 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION,
201 base::ASCIIToUTF16(email), close_warning);
202 }
203 views::StyledLabel* explanation_label =
204 new views::StyledLabel(signin_explanation_text, nullptr);
205
206 // Layout the components.
207 const gfx::Insets panel_insets =
208 views::LayoutProvider::Get()->GetInsetsMetric(views::INSETS_PANEL);
209 SetBorder(views::CreateEmptyBorder(panel_insets.top(), 0,
210 panel_insets.bottom(), 0));
211 views::GridLayout* dialog_layout = new views::GridLayout(this);
212 SetLayoutManager(dialog_layout);
213
214 // Use GridLayout inside the prompt bar because StyledLabel requires it.
215 views::GridLayout* prompt_layout = views::GridLayout::CreatePanel(prompt_bar);
216 prompt_bar->SetLayoutManager(prompt_layout);
217 prompt_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL,
218 views::GridLayout::CENTER, 100,
219 views::GridLayout::USE_PREF, 0, 0);
220 prompt_layout->StartRow(0, 0);
221 prompt_layout->AddView(prompt_label);
222 // Use a column set with no padding.
223 dialog_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL,
224 views::GridLayout::FILL, 100,
225 views::GridLayout::USE_PREF, 0, 0);
226 dialog_layout->StartRow(0, 0);
227 dialog_layout->AddView(prompt_bar, 1, 1, views::GridLayout::FILL,
228 views::GridLayout::FILL, 0, 0);
229
230 // Use a new column set for the explanation label so we can add padding.
231 dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin);
232 views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1);
233 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
234 explanation_columns->AddColumn(views::GridLayout::FILL,
235 views::GridLayout::FILL, 100,
236 views::GridLayout::USE_PREF, 0, 0);
237 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
238 dialog_layout->StartRow(0, 1);
239 const int kPreferredWidth = 440;
240 dialog_layout->AddView(explanation_label, 1, 1, views::GridLayout::FILL,
241 views::GridLayout::FILL, kPreferredWidth,
242 explanation_label->GetHeightForWidth(kPreferredWidth));
243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698