OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 #import "ios/chrome/browser/ui/authentication/re_signin_infobar_delegate.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/user_metrics.h" |
| 15 #include "components/infobars/core/infobar_manager.h" |
| 16 #include "components/strings/grit/components_strings.h" |
| 17 #import "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 18 #include "ios/chrome/browser/infobars/infobar.h" |
| 19 #include "ios/chrome/browser/signin/authentication_service.h" |
| 20 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 21 #include "ios/chrome/browser/tabs/tab.h" |
| 22 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 23 #import "ios/chrome/browser/ui/commands/show_signin_command.h" |
| 24 #include "ios/chrome/grit/ios_strings.h" |
| 25 #include "ui/base/l10n/l10n_util.h" |
| 26 |
| 27 // static |
| 28 bool ReSignInInfoBarDelegate::Create(ios::ChromeBrowserState* browser_state, |
| 29 Tab* tab) { |
| 30 infobars::InfoBarManager* infobar_manager = [tab infoBarManager]; |
| 31 DCHECK(infobar_manager); |
| 32 |
| 33 std::unique_ptr<infobars::InfoBar> infobar = |
| 34 ReSignInInfoBarDelegate::CreateInfoBar(infobar_manager, browser_state); |
| 35 if (!infobar) |
| 36 return false; |
| 37 return !!infobar_manager->AddInfoBar(std::move(infobar)); |
| 38 } |
| 39 |
| 40 // static |
| 41 std::unique_ptr<infobars::InfoBar> ReSignInInfoBarDelegate::CreateInfoBar( |
| 42 infobars::InfoBarManager* infobar_manager, |
| 43 ios::ChromeBrowserState* browser_state) { |
| 44 DCHECK(infobar_manager); |
| 45 std::unique_ptr<ReSignInInfoBarDelegate> delegate = |
| 46 ReSignInInfoBarDelegate::CreateInfoBarDelegate(browser_state); |
| 47 if (!delegate) |
| 48 return nullptr; |
| 49 return infobar_manager->CreateConfirmInfoBar(std::move(delegate)); |
| 50 } |
| 51 |
| 52 // static |
| 53 std::unique_ptr<ReSignInInfoBarDelegate> |
| 54 ReSignInInfoBarDelegate::CreateInfoBarDelegate( |
| 55 ios::ChromeBrowserState* browser_state) { |
| 56 DCHECK(browser_state); |
| 57 // Do not ask user to sign in if current profile is incognito. |
| 58 if (browser_state->IsOffTheRecord()) |
| 59 return nullptr; |
| 60 // Returns null if user does not need to be prompted to sign in again. |
| 61 AuthenticationService* authService = |
| 62 AuthenticationServiceFactory::GetForBrowserState(browser_state); |
| 63 if (!authService->ShouldPromptForSignIn()) |
| 64 return nullptr; |
| 65 // Returns null if user has already signed in via some other path. |
| 66 if ([authService->GetAuthenticatedUserEmail() length]) { |
| 67 authService->SetPromptForSignIn(false); |
| 68 return nullptr; |
| 69 } |
| 70 base::RecordAction( |
| 71 base::UserMetricsAction("Signin_Impression_FromReSigninInfobar")); |
| 72 // User needs to be reminded to sign in again. Creates a new infobar delegate |
| 73 // and returns it. |
| 74 return base::MakeUnique<ReSignInInfoBarDelegate>(browser_state); |
| 75 } |
| 76 |
| 77 ReSignInInfoBarDelegate::ReSignInInfoBarDelegate( |
| 78 ios::ChromeBrowserState* browser_state) |
| 79 : browser_state_(browser_state), |
| 80 icon_([UIImage imageNamed:@"infobar_warning"]) { |
| 81 DCHECK(browser_state_); |
| 82 DCHECK(!browser_state_->IsOffTheRecord()); |
| 83 } |
| 84 |
| 85 ReSignInInfoBarDelegate::~ReSignInInfoBarDelegate() {} |
| 86 |
| 87 infobars::InfoBarDelegate::InfoBarIdentifier |
| 88 ReSignInInfoBarDelegate::GetIdentifier() const { |
| 89 return RE_SIGN_IN_INFOBAR_DELEGATE; |
| 90 } |
| 91 |
| 92 base::string16 ReSignInInfoBarDelegate::GetMessageText() const { |
| 93 return l10n_util::GetStringUTF16(IDS_IOS_SYNC_LOGIN_INFO_OUT_OF_DATE); |
| 94 } |
| 95 |
| 96 int ReSignInInfoBarDelegate::GetButtons() const { |
| 97 return BUTTON_OK; |
| 98 } |
| 99 |
| 100 base::string16 ReSignInInfoBarDelegate::GetButtonLabel( |
| 101 InfoBarButton button) const { |
| 102 return l10n_util::GetStringUTF16( |
| 103 IDS_IOS_SYNC_INFOBAR_SIGN_IN_SETTINGS_BUTTON_MOBILE); |
| 104 } |
| 105 |
| 106 gfx::Image ReSignInInfoBarDelegate::GetIcon() const { |
| 107 return icon_; |
| 108 } |
| 109 |
| 110 bool ReSignInInfoBarDelegate::Accept() { |
| 111 base::RecordAction( |
| 112 base::UserMetricsAction("Signin_Signin_FromReSigninInfobar")); |
| 113 UIView* infobarView = static_cast<InfoBarIOS*>(infobar())->view(); |
| 114 DCHECK(infobarView); |
| 115 base::scoped_nsobject<ShowSigninCommand> command([[ShowSigninCommand alloc] |
| 116 initWithOperation:AUTHENTICATION_OPERATION_REAUTHENTICATE |
| 117 signInAccessPoint:signin_metrics::AccessPoint:: |
| 118 ACCESS_POINT_RESIGNIN_INFOBAR]); |
| 119 [infobarView chromeExecuteCommand:command]; |
| 120 |
| 121 // Stop displaying the infobar once user interacted with it. |
| 122 AuthenticationServiceFactory::GetForBrowserState(browser_state_) |
| 123 ->SetPromptForSignIn(false); |
| 124 return true; |
| 125 } |
| 126 |
| 127 void ReSignInInfoBarDelegate::InfoBarDismissed() { |
| 128 // Stop displaying the infobar once user interacted with it. |
| 129 AuthenticationServiceFactory::GetForBrowserState(browser_state_) |
| 130 ->SetPromptForSignIn(false); |
| 131 } |
OLD | NEW |