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 <string> |
| 8 |
| 9 #include "base/strings/string16.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 15 #include "chrome/browser/ui/views/profiles/force_signout_dialog_view.h" |
| 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/signin/core/browser/signin_manager.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 void Signout(SigninManager* signin_manager) { |
| 22 signin_manager->SignOut( |
| 23 signin_metrics::AUTHENTICATION_FAILED_WITH_FORCE_SIGNIN, |
| 24 signin_metrics::SignoutDelete::KEEPING); |
| 25 } |
| 26 |
| 27 bool IsMatchingBrowser(Browser* browser, Profile* profile) { |
| 28 return browser->profile()->GetOriginalProfile() == |
| 29 profile->GetOriginalProfile() && |
| 30 !browser->tab_strip_model()->empty() && |
| 31 BrowserView::GetBrowserViewForBrowser(browser)->frame()->IsVisible(); |
| 32 } |
| 33 |
| 34 // Find a browser that is assoicated with |profile| to show the dialog for |
| 35 // Sign out warning. |
| 36 Browser* FindBrowserWithProfile(Profile* profile) { |
| 37 Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| 38 if (browser && IsMatchingBrowser(browser, profile)) |
| 39 return browser; |
| 40 for (auto* browser : *BrowserList::GetInstance()) { |
| 41 if (IsMatchingBrowser(browser, profile)) { |
| 42 return browser; |
| 43 } |
| 44 } |
| 45 return nullptr; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 // static |
| 51 ForceSignoutDialogView* ForceSignoutDialog::dialog_view_ = nullptr; |
| 52 |
| 53 // static |
| 54 void ForceSignoutDialog::ShowDialog(Profile* profile, |
| 55 SigninManager* signin_manager, |
| 56 const base::Closure& signout_timer) { |
| 57 if (dialog_view_) |
| 58 return; |
| 59 |
| 60 Browser* browser = FindBrowserWithProfile(profile); |
| 61 if (browser == nullptr) { // If there is no browser, we can just sign |
| 62 // out profile directly. |
| 63 Signout(signin_manager); |
| 64 return; |
| 65 } |
| 66 dialog_view_ = new ForceSignoutDialogView( |
| 67 browser, |
| 68 base::MakeUnique<ForceSignoutDialog>(signin_manager, signout_timer)); |
| 69 } |
| 70 |
| 71 // static |
| 72 void ForceSignoutDialog::ActivateModalDialog() { |
| 73 if (dialog_view_) |
| 74 dialog_view_->ActivateModalDialog(); |
| 75 } |
| 76 |
| 77 // static |
| 78 bool ForceSignoutDialog::IsShowing() { |
| 79 return dialog_view_ && dialog_view_->IsShowing(); |
| 80 } |
| 81 |
| 82 void ForceSignoutDialog::OnAccept() { |
| 83 Signout(signin_manager_); |
| 84 } |
| 85 |
| 86 void ForceSignoutDialog::OnCancel() { |
| 87 if (!signout_timer_.is_null()) |
| 88 signout_timer_.Run(); |
| 89 } |
| 90 |
| 91 std::string ForceSignoutDialog::GetEmail() { |
| 92 return signin_manager_->GetAuthenticatedAccountInfo().email; |
| 93 } |
| 94 |
| 95 bool ForceSignoutDialog::IsDelayAllowed() { |
| 96 return !signout_timer_.is_null(); |
| 97 } |
| 98 |
| 99 void ForceSignoutDialog::OnViewClosing() { |
| 100 dialog_view_ = nullptr; |
| 101 } |
| 102 |
| 103 ForceSignoutDialog::~ForceSignoutDialog() {} |
| 104 |
| 105 ForceSignoutDialog::ForceSignoutDialog(SigninManager* signin_manager, |
| 106 const base::Closure& signout_timer) |
| 107 : signin_manager_(signin_manager), signout_timer_(signout_timer) {} |
| 108 |
| 109 // static |
| 110 ForceSignoutDialogView* ForceSignoutDialog::GetDialogViewForTesting() { |
| 111 return dialog_view_; |
| 112 } |
OLD | NEW |