| 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/browser/ui/views/harmony/chrome_layout_provider.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/view.h" | |
| 32 #include "ui/views/window/dialog_client_view.h" | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 // Refresh title of the dialog every second. | |
| 37 constexpr int kRefreshTitleTimer = 1; | |
| 38 | |
| 39 // If browser windows are going to be closed soon, close browser window before | |
| 40 // showing sign in dialog because there might not be enough time for user to | |
| 41 // finish sign in. | |
| 42 constexpr int kCloseDirectlyTimer = 60; | |
| 43 | |
| 44 void Signout(SigninManager* signin_manager) { | |
| 45 signin_manager->SignOut( | |
| 46 signin_metrics::AUTHENTICATION_FAILED_WITH_FORCE_SIGNIN, | |
| 47 signin_metrics::SignoutDelete::KEEPING); | |
| 48 } | |
| 49 | |
| 50 bool IsMatchingBrowser(Browser* browser, Profile* profile) { | |
| 51 return browser->profile()->GetOriginalProfile() == | |
| 52 profile->GetOriginalProfile() && | |
| 53 !browser->tab_strip_model()->empty() && | |
| 54 BrowserView::GetBrowserViewForBrowser(browser)->frame()->IsVisible(); | |
| 55 } | |
| 56 | |
| 57 // Find a browser that is assoicated with |profile| to show the dialog for | |
| 58 // Sign out warning. | |
| 59 Browser* FindBrowserWithProfile(Profile* profile) { | |
| 60 Browser* browser = BrowserList::GetInstance()->GetLastActive(); | |
| 61 if (browser && IsMatchingBrowser(browser, profile)) | |
| 62 return browser; | |
| 63 for (auto* browser : *BrowserList::GetInstance()) { | |
| 64 if (IsMatchingBrowser(browser, profile)) { | |
| 65 return browser; | |
| 66 } | |
| 67 } | |
| 68 return nullptr; | |
| 69 } | |
| 70 | |
| 71 // PromptLabel overrides the default insets of StyledLabel. | |
| 72 class PromptLabel : public views::StyledLabel { | |
| 73 public: | |
| 74 PromptLabel(const base::string16& text, views::StyledLabelListener* listener) | |
| 75 : views::StyledLabel(text, listener) {} | |
| 76 | |
| 77 gfx::Insets GetInsets() const override { | |
| 78 return ChromeLayoutProvider::Get()->GetInsetsMetric( | |
| 79 views::INSETS_DIALOG_CONTENTS); | |
| 80 } | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 ForcedReauthenticationDialog::ForcedReauthenticationDialog( | |
| 86 Browser* browser, | |
| 87 SigninManager* signin_manager, | |
| 88 const base::TimeDelta& countdown_duration) | |
| 89 : browser_(browser), | |
| 90 signin_manager_(signin_manager), | |
| 91 desired_close_time_(base::TimeTicks::Now() + countdown_duration) { | |
| 92 constrained_window::CreateBrowserModalDialogViews( | |
| 93 this, browser->window()->GetNativeWindow()) | |
| 94 ->Show(); | |
| 95 browser->window()->FlashFrame(true); | |
| 96 browser->window()->Activate(); | |
| 97 } | |
| 98 | |
| 99 ForcedReauthenticationDialog::~ForcedReauthenticationDialog() {} | |
| 100 | |
| 101 // static | |
| 102 ForcedReauthenticationDialog* ForcedReauthenticationDialog::ShowDialog( | |
| 103 Profile* profile, | |
| 104 SigninManager* signin_manager, | |
| 105 const base::TimeDelta& countdown_duration) { | |
| 106 Browser* browser = FindBrowserWithProfile(profile); | |
| 107 if (browser == nullptr) { // If there is no browser, we can just sign | |
| 108 // out profile directly. | |
| 109 Signout(signin_manager); | |
| 110 return nullptr; | |
| 111 } | |
| 112 | |
| 113 return new ForcedReauthenticationDialog(browser, signin_manager, | |
| 114 countdown_duration); | |
| 115 } | |
| 116 | |
| 117 bool ForcedReauthenticationDialog::Accept() { | |
| 118 if (GetTimeRemaining() < base::TimeDelta::FromSeconds(kCloseDirectlyTimer)) { | |
| 119 Signout(signin_manager_); | |
| 120 } else { | |
| 121 browser_->signin_view_controller()->ShowModalSignin( | |
| 122 profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_REAUTH, browser_, | |
| 123 signin_metrics::AccessPoint::ACCESS_POINT_FORCE_SIGNIN_WARNING); | |
| 124 } | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 bool ForcedReauthenticationDialog::Cancel() { | |
| 129 return true; | |
| 130 } | |
| 131 | |
| 132 void ForcedReauthenticationDialog::WindowClosing() { | |
| 133 refresh_timer_.Stop(); | |
| 134 } | |
| 135 | |
| 136 base::string16 ForcedReauthenticationDialog::GetWindowTitle() const { | |
| 137 base::TimeDelta time_left = GetTimeRemaining(); | |
| 138 return base::i18n::MessageFormatter::FormatWithNumberedArgs( | |
| 139 l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_TITLE), | |
| 140 time_left.InMinutes(), time_left.InSeconds() % 60); | |
| 141 } | |
| 142 | |
| 143 base::string16 ForcedReauthenticationDialog::GetDialogButtonLabel( | |
| 144 ui::DialogButton button) const { | |
| 145 if (button == ui::DIALOG_BUTTON_OK) { | |
| 146 return l10n_util::GetStringUTF16( | |
| 147 IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_CONFIRM); | |
| 148 } | |
| 149 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_DELAY); | |
| 150 } | |
| 151 | |
| 152 ui::ModalType ForcedReauthenticationDialog::GetModalType() const { | |
| 153 return ui::MODAL_TYPE_WINDOW; | |
| 154 } | |
| 155 | |
| 156 void ForcedReauthenticationDialog::AddedToWidget() { | |
| 157 const SkColor prompt_bar_background_color = | |
| 158 GetSigninConfirmationPromptBarColor( | |
| 159 GetNativeTheme(), ui::kSigninConfirmationPromptBarBackgroundAlpha); | |
| 160 // Create the prompt label. | |
| 161 size_t offset; | |
| 162 std::string email = signin_manager_->GetAuthenticatedAccountInfo().email; | |
| 163 const base::string16 domain = | |
| 164 base::ASCIIToUTF16(gaia::ExtractDomainName(email)); | |
| 165 const base::string16 prompt_text = | |
| 166 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_SIGNIN_ALERT, domain, &offset); | |
| 167 | |
| 168 // Create the prompt label. | |
| 169 PromptLabel* prompt_label = new PromptLabel(prompt_text, nullptr); | |
| 170 prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color); | |
| 171 | |
| 172 views::StyledLabel::RangeStyleInfo bold_style; | |
| 173 bold_style.weight = gfx::Font::Weight::BOLD; | |
| 174 prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()), | |
| 175 bold_style); | |
| 176 | |
| 177 prompt_label->SetBorder(views::CreateSolidSidedBorder( | |
| 178 1, 0, 1, 0, | |
| 179 ui::GetSigninConfirmationPromptBarColor( | |
| 180 GetNativeTheme(), ui::kSigninConfirmationPromptBarBorderAlpha))); | |
| 181 prompt_label->SetBackground( | |
| 182 views::CreateSolidBackground(prompt_bar_background_color)); | |
| 183 | |
| 184 // Create the explanation label. | |
| 185 base::string16 signin_explanation_text; | |
| 186 base::string16 close_warning; | |
| 187 close_warning = l10n_util::GetStringUTF16( | |
| 188 IDS_ENTERPRISE_FORCE_SIGNOUT_ADDITIONAL_EXPLANATION); | |
| 189 if (email.empty()) { | |
| 190 signin_explanation_text = l10n_util::GetStringFUTF16( | |
| 191 IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION_WITHOUT_USER_NAME, | |
| 192 close_warning); | |
| 193 } else { | |
| 194 signin_explanation_text = | |
| 195 l10n_util::GetStringFUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION, | |
| 196 base::ASCIIToUTF16(email), close_warning); | |
| 197 } | |
| 198 views::StyledLabel* explanation_label = | |
| 199 new views::StyledLabel(signin_explanation_text, nullptr); | |
| 200 | |
| 201 ChromeLayoutProvider* provider = ChromeLayoutProvider::Get(); | |
| 202 // Layout the components. | |
| 203 const gfx::Insets dialog_insets = | |
| 204 provider->GetInsetsMetric(views::INSETS_DIALOG_CONTENTS); | |
| 205 SetBorder(views::CreateEmptyBorder(dialog_insets.top(), 0, | |
| 206 dialog_insets.bottom(), 0)); | |
| 207 views::GridLayout* dialog_layout = new views::GridLayout(this); | |
| 208 SetLayoutManager(dialog_layout); | |
| 209 | |
| 210 // Use a column set with no padding. | |
| 211 dialog_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL, | |
| 212 views::GridLayout::FILL, 100, | |
| 213 views::GridLayout::USE_PREF, 0, 0); | |
| 214 dialog_layout->StartRow(0, 0); | |
| 215 dialog_layout->AddView(prompt_label, 1, 1, views::GridLayout::FILL, | |
| 216 views::GridLayout::FILL, 0, 0); | |
| 217 | |
| 218 // Use a new column set for the explanation label so we can add padding. | |
| 219 dialog_layout->AddPaddingRow(0.0, dialog_insets.top()); | |
| 220 views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1); | |
| 221 | |
| 222 explanation_columns->AddPaddingColumn(0.0, dialog_insets.left()); | |
| 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, dialog_insets.right()); | |
| 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 |