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