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 const base::TimeDelta& countdown_duration) |
| 90 : browser_(browser), |
| 91 signin_manager_(signin_manager), |
| 92 desired_close_time_(base::TimeTicks::Now() + countdown_duration) { |
| 93 constrained_window::CreateBrowserModalDialogViews( |
| 94 this, browser->window()->GetNativeWindow()) |
| 95 ->Show(); |
| 96 browser->window()->FlashFrame(true); |
| 97 browser->window()->Activate(); |
| 98 } |
| 99 |
| 100 ForcedReauthenticationDialog::~ForcedReauthenticationDialog() {} |
| 101 |
| 102 // static |
| 103 ForcedReauthenticationDialog* ForcedReauthenticationDialog::ShowDialog( |
| 104 Profile* profile, |
| 105 SigninManager* signin_manager, |
| 106 const base::TimeDelta& countdown_duration) { |
| 107 Browser* browser = FindBrowserWithProfile(profile); |
| 108 if (browser == nullptr) { // If there is no browser, we can just sign |
| 109 // out profile directly. |
| 110 Signout(signin_manager); |
| 111 return nullptr; |
| 112 } |
| 113 |
| 114 return new ForcedReauthenticationDialog(browser, signin_manager, |
| 115 countdown_duration); |
| 116 } |
| 117 |
| 118 bool ForcedReauthenticationDialog::Accept() { |
| 119 if (GetTimeRemaining() < base::TimeDelta::FromSeconds(kCloseDirectlyTimer)) { |
| 120 Signout(signin_manager_); |
| 121 } else { |
| 122 browser_->signin_view_controller()->ShowModalSignin( |
| 123 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_REAUTH, browser_, |
| 124 signin_metrics::AccessPoint::ACCESS_POINT_FORCE_SIGNIN_WARNING); |
| 125 } |
| 126 return true; |
| 127 } |
| 128 |
| 129 bool ForcedReauthenticationDialog::Cancel() { |
| 130 return true; |
| 131 } |
| 132 |
| 133 void ForcedReauthenticationDialog::WindowClosing() { |
| 134 refresh_timer_.Stop(); |
| 135 } |
| 136 |
| 137 base::string16 ForcedReauthenticationDialog::GetWindowTitle() const { |
| 138 base::TimeDelta time_left = GetTimeRemaining(); |
| 139 return base::i18n::MessageFormatter::FormatWithNumberedArgs( |
| 140 l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_TITLE), |
| 141 time_left.InMinutes(), time_left.InSeconds() % 60); |
| 142 } |
| 143 |
| 144 base::string16 ForcedReauthenticationDialog::GetDialogButtonLabel( |
| 145 ui::DialogButton button) const { |
| 146 if (button == ui::DIALOG_BUTTON_OK) { |
| 147 return l10n_util::GetStringUTF16( |
| 148 IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_CONFIRM); |
| 149 } |
| 150 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_DELAY); |
| 151 } |
| 152 |
| 153 ui::ModalType ForcedReauthenticationDialog::GetModalType() const { |
| 154 return ui::MODAL_TYPE_WINDOW; |
| 155 } |
| 156 |
| 157 void ForcedReauthenticationDialog::AddedToWidget() { |
| 158 const SkColor prompt_bar_background_color = |
| 159 GetSigninConfirmationPromptBarColor( |
| 160 GetNativeTheme(), ui::kSigninConfirmationPromptBarBackgroundAlpha); |
| 161 // Create the prompt label. |
| 162 size_t offset; |
| 163 std::string email = signin_manager_->GetAuthenticatedAccountInfo().email; |
| 164 const base::string16 domain = |
| 165 base::ASCIIToUTF16(gaia::ExtractDomainName(email)); |
| 166 const base::string16 prompt_text = |
| 167 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_SIGNIN_ALERT, domain, &offset); |
| 168 |
| 169 // Create the prompt label. |
| 170 PromptLabel* prompt_label = new PromptLabel(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 prompt_label->SetBorder(views::CreateSolidSidedBorder( |
| 179 1, 0, 1, 0, |
| 180 ui::GetSigninConfirmationPromptBarColor( |
| 181 GetNativeTheme(), ui::kSigninConfirmationPromptBarBorderAlpha))); |
| 182 prompt_label->SetBackground( |
| 183 views::CreateSolidBackground(prompt_bar_background_color)); |
| 184 |
| 185 // Create the explanation label. |
| 186 base::string16 signin_explanation_text; |
| 187 base::string16 close_warning; |
| 188 close_warning = l10n_util::GetStringUTF16( |
| 189 IDS_ENTERPRISE_FORCE_SIGNOUT_ADDITIONAL_EXPLANATION); |
| 190 if (email.empty()) { |
| 191 signin_explanation_text = l10n_util::GetStringFUTF16( |
| 192 IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION_WITHOUT_USER_NAME, |
| 193 close_warning); |
| 194 } else { |
| 195 signin_explanation_text = |
| 196 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION, |
| 197 base::ASCIIToUTF16(email), close_warning); |
| 198 } |
| 199 views::StyledLabel* explanation_label = |
| 200 new views::StyledLabel(signin_explanation_text, nullptr); |
| 201 |
| 202 // Layout the components. |
| 203 const gfx::Insets panel_insets = |
| 204 views::LayoutProvider::Get()->GetInsetsMetric( |
| 205 views::INSETS_DIALOG_CONTENTS); |
| 206 SetBorder(views::CreateEmptyBorder(panel_insets.top(), 0, |
| 207 panel_insets.bottom(), 0)); |
| 208 views::GridLayout* dialog_layout = new views::GridLayout(this); |
| 209 SetLayoutManager(dialog_layout); |
| 210 |
| 211 // Use a column set with no padding. |
| 212 dialog_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL, |
| 213 views::GridLayout::FILL, 100, |
| 214 views::GridLayout::USE_PREF, 0, 0); |
| 215 dialog_layout->StartRow(0, 0); |
| 216 dialog_layout->AddView(prompt_label, 1, 1, views::GridLayout::FILL, |
| 217 views::GridLayout::FILL, 0, 0); |
| 218 |
| 219 // Use a new column set for the explanation label so we can add padding. |
| 220 dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin); |
| 221 views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1); |
| 222 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); |
| 223 explanation_columns->AddColumn(views::GridLayout::FILL, |
| 224 views::GridLayout::FILL, 100, |
| 225 views::GridLayout::USE_PREF, 0, 0); |
| 226 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); |
| 227 dialog_layout->StartRow(0, 1); |
| 228 const int kPreferredWidth = 440; |
| 229 dialog_layout->AddView(explanation_label, 1, 1, views::GridLayout::FILL, |
| 230 views::GridLayout::FILL, kPreferredWidth, |
| 231 explanation_label->GetHeightForWidth(kPreferredWidth)); |
| 232 refresh_timer_.Start(FROM_HERE, |
| 233 base::TimeDelta::FromSeconds(kRefreshTitleTimer), this, |
| 234 &ForcedReauthenticationDialog::OnCountDown); |
| 235 } |
| 236 |
| 237 void ForcedReauthenticationDialog::OnCountDown() { |
| 238 if (desired_close_time_ <= base::TimeTicks::Now()) { |
| 239 Cancel(); |
| 240 GetWidget()->Close(); |
| 241 } |
| 242 GetWidget()->UpdateWindowTitle(); |
| 243 } |
| 244 |
| 245 base::TimeDelta ForcedReauthenticationDialog::GetTimeRemaining() const { |
| 246 base::TimeTicks now = base::TimeTicks::Now(); |
| 247 if (desired_close_time_ <= now) |
| 248 return base::TimeDelta(); |
| 249 return desired_close_time_ - now; |
| 250 } |
OLD | NEW |