Chromium Code Reviews| Index: chrome/browser/ui/views/profiles/force_signout_dialog.cc |
| diff --git a/chrome/browser/ui/views/profiles/force_signout_dialog.cc b/chrome/browser/ui/views/profiles/force_signout_dialog.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..405d8eefe590264c9704c701115946dada239a00 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/profiles/force_signout_dialog.cc |
| @@ -0,0 +1,245 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/views/profiles/force_signout_dialog.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/i18n/message_formatter.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/timer/timer.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/views/frame/browser_view.h" |
| +#include "chrome/grit/chromium_strings.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "components/constrained_window/constrained_window_views.h" |
| +#include "components/signin/core/browser/signin_manager.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/views/background.h" |
| +#include "ui/views/border.h" |
| +#include "ui/views/controls/styled_label.h" |
| +#include "ui/views/layout/grid_layout.h" |
| +#include "ui/views/layout/layout_constants.h" |
| +#include "ui/views/layout/layout_provider.h" |
| +#include "ui/views/view.h" |
| +#include "ui/views/window/dialog_client_view.h" |
| + |
| +namespace { |
| + |
| +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.
|
| +const int kCloseDirectlyTimer = |
| + 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.
|
| + // before showing sign in dialog because there might not be enough time |
| + // for user to finish sign in. |
| +void Signout(SigninManager* signin_manager) { |
| + signin_manager->SignOut( |
| + signin_metrics::AUTHENTICATION_FAILED_WITH_FORCE_SIGNIN, |
| + signin_metrics::SignoutDelete::KEEPING); |
| +} |
| + |
| +bool IsMatchingBrowser(Browser* browser, Profile* profile) { |
| + return browser->profile()->GetOriginalProfile() == |
| + profile->GetOriginalProfile() && |
| + !browser->tab_strip_model()->empty() && |
| + BrowserView::GetBrowserViewForBrowser(browser)->frame()->IsVisible(); |
| +} |
| + |
| +// Find a browser that is assoicated with |profile| to show the dialog for |
| +// Sign out warning. |
| +Browser* FindBrowserWithProfile(Profile* profile) { |
| + Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| + if (browser && IsMatchingBrowser(browser, profile)) |
| + return browser; |
| + for (auto* browser : *BrowserList::GetInstance()) { |
| + if (IsMatchingBrowser(browser, profile)) { |
| + return browser; |
| + } |
| + } |
| + return nullptr; |
| +} |
| + |
| +} // namespace |
| + |
| +ForceSignoutDialog::ForceSignoutDialog(Browser* browser, |
| + SigninManager* signin_manager, |
| + base::Timer* close_timer) |
| + : browser_(browser), |
| + signin_manager_(signin_manager), |
| + close_timer_(close_timer) { |
| + constrained_window::CreateBrowserModalDialogViews( |
| + this, browser->window()->GetNativeWindow()) |
| + ->Show(); |
| + browser->window()->FlashFrame(true); |
| + browser->window()->Activate(); |
| +} |
| + |
| +ForceSignoutDialog::~ForceSignoutDialog() {} |
| + |
| +// static |
| +ForceSignoutDialog* ForceSignoutDialog::ShowDialog( |
| + Profile* profile, |
| + SigninManager* signin_manager, |
| + base::Timer* close_timer) { |
| + DCHECK(close_timer); |
| + Browser* browser = FindBrowserWithProfile(profile); |
| + if (browser == nullptr) { // If there is no browser, we can just sign |
| + // out profile directly. |
| + Signout(signin_manager); |
| + close_timer->Stop(); |
| + return nullptr; |
| + } |
| + |
| + return new ForceSignoutDialog(browser, signin_manager, close_timer); |
| +} |
| + |
| +bool ForceSignoutDialog::Accept() { |
| + if (GetCountDownRemainTime() < |
| + base::TimeDelta::FromSeconds(kCloseDirectlyTimer)) { |
| + Signout(signin_manager_); |
| + } else { |
| + browser_->signin_view_controller()->ShowModalSignin( |
| + profiles::BubbleViewMode::BUBBLE_VIEW_MODE_GAIA_REAUTH, browser_, |
| + signin_metrics::AccessPoint::ACCESS_POINT_FORCE_SIGNIN_WARNING); |
| + } |
| + return true; |
| +} |
| + |
| +bool ForceSignoutDialog::Cancel() { |
| + return true; |
| +} |
| + |
| +void ForceSignoutDialog::WindowClosing() { |
| + refresh_timer_.Stop(); |
| +} |
| + |
| +base::string16 ForceSignoutDialog::GetWindowTitle() const { |
| + base::TimeDelta time_left = GetCountDownRemainTime(); |
| + if (time_left.InSeconds() < 0) |
| + time_left = base::TimeDelta::FromSeconds(0); |
| + return base::i18n::MessageFormatter::FormatWithNumberedArgs( |
| + l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_TITLE), |
| + time_left.InMinutes(), time_left.InSeconds() % 60); |
| +} |
| + |
| +base::string16 ForceSignoutDialog::GetDialogButtonLabel( |
| + ui::DialogButton button) const { |
| + if (button == ui::DIALOG_BUTTON_OK) |
| + return l10n_util::GetStringUTF16( |
| + IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_CONFIRM); |
| + else |
| + return l10n_util::GetStringUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_CLOSE_DELAY); |
| +} |
| + |
| +ui::ModalType ForceSignoutDialog::GetModalType() const { |
| + return ui::MODAL_TYPE_WINDOW; |
| +} |
| + |
| +void ForceSignoutDialog::AddedToWidget() { |
| + const SkColor prompt_bar_background_color = |
| + GetSigninConfirmationPromptBarColor( |
| + GetNativeTheme(), ui::kSigninConfirmationPromptBarBackgroundAlpha); |
| + // Create the prompt label. |
| + size_t offset; |
| + std::string email = signin_manager_->GetAuthenticatedAccountInfo().email; |
| + const base::string16 domain = |
| + base::ASCIIToUTF16(gaia::ExtractDomainName(email)); |
| + const base::string16 prompt_text = |
| + l10n_util::GetStringFUTF16(IDS_ENTERPRISE_SIGNIN_ALERT, domain, &offset); |
| + views::StyledLabel* prompt_label = |
| + new views::StyledLabel(prompt_text, nullptr); |
| + prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color); |
| + |
| + views::StyledLabel::RangeStyleInfo bold_style; |
| + bold_style.weight = gfx::Font::Weight::BOLD; |
| + prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()), |
| + bold_style); |
| + |
| + // Create the prompt bar. |
| + views::View* prompt_bar = new views::View; |
| + prompt_bar->SetBorder(views::CreateSolidSidedBorder( |
| + 1, 0, 1, 0, |
| + ui::GetSigninConfirmationPromptBarColor( |
| + GetNativeTheme(), ui::kSigninConfirmationPromptBarBorderAlpha))); |
| + prompt_bar->SetBackground( |
| + views::CreateSolidBackground(prompt_bar_background_color)); |
| + |
| + // Create the explanation label. |
| + base::string16 signin_explanation_text; |
| + base::string16 close_warning; |
| + close_warning = l10n_util::GetStringUTF16( |
| + IDS_ENTERPRISE_FORCE_SIGNOUT_ADDITIONAL_EXPLANATION); |
| + if (email.empty()) { |
| + signin_explanation_text = l10n_util::GetStringFUTF16( |
| + IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION_WITHOUT_USER_NAME, |
| + close_warning); |
| + } else { |
| + signin_explanation_text = |
| + l10n_util::GetStringFUTF16(IDS_ENTERPRISE_FORCE_SIGNOUT_EXPLANATION, |
| + base::ASCIIToUTF16(email), close_warning); |
| + } |
| + views::StyledLabel* explanation_label = |
| + new views::StyledLabel(signin_explanation_text, nullptr); |
| + |
| + // Layout the components. |
| + const gfx::Insets panel_insets = |
| + views::LayoutProvider::Get()->GetInsetsMetric( |
| + views::INSETS_DIALOG_CONTENTS); |
| + SetBorder(views::CreateEmptyBorder(panel_insets.top(), 0, |
| + panel_insets.bottom(), 0)); |
| + 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
|
| + SetLayoutManager(dialog_layout); |
| + |
| + // Use GridLayout inside the prompt bar because StyledLabel requires it. |
| + views::GridLayout* prompt_layout = views::GridLayout::CreatePanel(prompt_bar); |
| + prompt_bar->SetLayoutManager(prompt_layout); |
| + prompt_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL, |
| + views::GridLayout::CENTER, 100, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + prompt_layout->StartRow(0, 0); |
| + prompt_layout->AddView(prompt_label); |
| + // Use a column set with no padding. |
| + dialog_layout->AddColumnSet(0)->AddColumn(views::GridLayout::FILL, |
| + views::GridLayout::FILL, 100, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + dialog_layout->StartRow(0, 0); |
| + dialog_layout->AddView(prompt_bar, 1, 1, views::GridLayout::FILL, |
| + views::GridLayout::FILL, 0, 0); |
| + |
| + // Use a new column set for the explanation label so we can add padding. |
| + dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin); |
| + views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1); |
| + explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); |
| + explanation_columns->AddColumn(views::GridLayout::FILL, |
| + views::GridLayout::FILL, 100, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew); |
| + dialog_layout->StartRow(0, 1); |
| + const int kPreferredWidth = 440; |
| + dialog_layout->AddView(explanation_label, 1, 1, views::GridLayout::FILL, |
| + views::GridLayout::FILL, kPreferredWidth, |
| + explanation_label->GetHeightForWidth(kPreferredWidth)); |
| + refresh_timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromSeconds(kRefreshTitleTimer), this, |
| + &ForceSignoutDialog::OnCountDown); |
| +} |
| + |
| +void ForceSignoutDialog::OnCountDown() { |
| + if (!close_timer_->IsRunning()) { |
| + Cancel(); |
| + GetWidget()->Close(); |
| + } |
| + GetWidget()->UpdateWindowTitle(); |
| +} |
| + |
| +base::TimeDelta ForceSignoutDialog::GetCountDownRemainTime() const { |
| + 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
|
| +} |