| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/authentication/signin_promo_view_mediator.h" | 5 #import "ios/chrome/browser/ui/authentication/signin_promo_view_mediator.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
| 9 #include "base/metrics/user_metrics_action.h" | 10 #include "base/metrics/user_metrics_action.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "components/prefs/pref_service.h" |
| 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 14 #include "ios/chrome/browser/pref_names.h" |
| 11 #include "ios/chrome/browser/signin/chrome_identity_service_observer_bridge.h" | 15 #include "ios/chrome/browser/signin/chrome_identity_service_observer_bridge.h" |
| 12 #import "ios/chrome/browser/ui/authentication/signin_promo_view_configurator.h" | 16 #import "ios/chrome/browser/ui/authentication/signin_promo_view_configurator.h" |
| 13 #import "ios/chrome/browser/ui/authentication/signin_promo_view_consumer.h" | 17 #import "ios/chrome/browser/ui/authentication/signin_promo_view_consumer.h" |
| 14 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" | 18 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 15 #import "ios/chrome/browser/ui/commands/show_signin_command.h" | 19 #import "ios/chrome/browser/ui/commands/show_signin_command.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" | 20 #include "ios/chrome/grit/ios_strings.h" |
| 17 #import "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 21 #import "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 18 #import "ios/public/provider/chrome/browser/signin/chrome_identity.h" | 22 #import "ios/public/provider/chrome/browser/signin/chrome_identity.h" |
| 19 #import "ios/public/provider/chrome/browser/signin/signin_resources_provider.h" | 23 #import "ios/public/provider/chrome/browser/signin/signin_resources_provider.h" |
| 20 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" | 24 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 22 | 26 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) | 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." | 28 #error "This file requires ARC support." |
| 25 #endif | 29 #endif |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 32 const int kAutomaticSigninPromoViewDismissCount = 20; |
| 33 |
| 28 void RecordSigninUserActionForAccessPoint( | 34 void RecordSigninUserActionForAccessPoint( |
| 29 signin_metrics::AccessPoint access_point) { | 35 signin_metrics::AccessPoint access_point) { |
| 30 switch (access_point) { | 36 switch (access_point) { |
| 31 case signin_metrics::AccessPoint::ACCESS_POINT_BOOKMARK_MANAGER: | 37 case signin_metrics::AccessPoint::ACCESS_POINT_BOOKMARK_MANAGER: |
| 32 base::RecordAction( | 38 base::RecordAction( |
| 33 base::UserMetricsAction("Signin_Signin_FromBookmarkManager")); | 39 base::UserMetricsAction("Signin_Signin_FromBookmarkManager")); |
| 34 break; | 40 break; |
| 35 case signin_metrics::AccessPoint::ACCESS_POINT_RECENT_TABS: | 41 case signin_metrics::AccessPoint::ACCESS_POINT_RECENT_TABS: |
| 36 base::RecordAction( | 42 base::RecordAction( |
| 37 base::UserMetricsAction("Signin_Signin_FromRecentTabs")); | 43 base::UserMetricsAction("Signin_Signin_FromRecentTabs")); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 case signin_metrics::AccessPoint::ACCESS_POINT_RECENT_TABS: | 95 case signin_metrics::AccessPoint::ACCESS_POINT_RECENT_TABS: |
| 90 base::RecordAction( | 96 base::RecordAction( |
| 91 base::UserMetricsAction("Signin_SigninNewAccount_FromRecentTabs")); | 97 base::UserMetricsAction("Signin_SigninNewAccount_FromRecentTabs")); |
| 92 break; | 98 break; |
| 93 default: | 99 default: |
| 94 NOTREACHED() << "Unexpected value for access point " | 100 NOTREACHED() << "Unexpected value for access point " |
| 95 << static_cast<int>(access_point); | 101 << static_cast<int>(access_point); |
| 96 break; | 102 break; |
| 97 } | 103 } |
| 98 } | 104 } |
| 105 |
| 106 enum class SigninPromoViewState { |
| 107 Unused = 0, |
| 108 Visible, |
| 109 Hidden, |
| 110 SigninStarted, |
| 111 Dismissed, |
| 112 }; |
| 99 } // namespace | 113 } // namespace |
| 100 | 114 |
| 101 @interface SigninPromoViewMediator ()<ChromeIdentityServiceObserver> | 115 @interface SigninPromoViewMediator ()<ChromeIdentityServiceObserver> |
| 102 @end | 116 @end |
| 103 | 117 |
| 104 @implementation SigninPromoViewMediator { | 118 @implementation SigninPromoViewMediator { |
| 119 ios::ChromeBrowserState* _browserState; |
| 105 std::unique_ptr<ChromeIdentityServiceObserverBridge> _identityServiceObserver; | 120 std::unique_ptr<ChromeIdentityServiceObserverBridge> _identityServiceObserver; |
| 106 UIImage* _identityAvatar; | 121 UIImage* _identityAvatar; |
| 122 SigninPromoViewState _signinPromoViewState; |
| 107 } | 123 } |
| 108 | 124 |
| 109 @synthesize consumer = _consumer; | 125 @synthesize consumer = _consumer; |
| 110 @synthesize defaultIdentity = _defaultIdentity; | 126 @synthesize defaultIdentity = _defaultIdentity; |
| 111 @synthesize accessPoint = _accessPoint; | 127 @synthesize accessPoint = _accessPoint; |
| 128 @synthesize displayedCountPreferenceKey = _displayedCountPreferenceKey; |
| 129 @synthesize alreadySeenSigninViewPreferenceKey = |
| 130 _alreadySeenSigninViewPreferenceKey; |
| 131 @synthesize histograms = _histograms; |
| 112 | 132 |
| 113 - (instancetype)init { | 133 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 114 self = [super init]; | 134 self = [super init]; |
| 115 if (self) { | 135 if (self) { |
| 136 _browserState = browserState; |
| 116 NSArray* identities = ios::GetChromeBrowserProvider() | 137 NSArray* identities = ios::GetChromeBrowserProvider() |
| 117 ->GetChromeIdentityService() | 138 ->GetChromeIdentityService() |
| 118 ->GetAllIdentitiesSortedForDisplay(); | 139 ->GetAllIdentitiesSortedForDisplay(); |
| 119 if (identities.count != 0) { | 140 if (identities.count != 0) { |
| 120 [self selectIdentity:identities[0]]; | 141 [self selectIdentity:identities[0]]; |
| 121 } | 142 } |
| 122 _identityServiceObserver = | 143 _identityServiceObserver = |
| 123 base::MakeUnique<ChromeIdentityServiceObserverBridge>(self); | 144 base::MakeUnique<ChromeIdentityServiceObserverBridge>(self); |
| 124 } | 145 } |
| 125 return self; | 146 return self; |
| 126 } | 147 } |
| 127 | 148 |
| 149 - (void)dealloc { |
| 150 if (_displayedCountPreferenceKey && |
| 151 (_signinPromoViewState == SigninPromoViewState::Visible || |
| 152 _signinPromoViewState == SigninPromoViewState::Hidden)) { |
| 153 PrefService* prefs = _browserState->GetPrefs(); |
| 154 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 155 switch (_histograms) { |
| 156 case ios::SigninPromoViewHistograms::Bookmarks: |
| 157 UMA_HISTOGRAM_COUNTS_100( |
| 158 "MobileSignInPromo.BookmarkManager.DismissalCount", displayedCount); |
| 159 break; |
| 160 case ios::SigninPromoViewHistograms::None: |
| 161 break; |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 128 - (SigninPromoViewConfigurator*)createConfigurator { | 166 - (SigninPromoViewConfigurator*)createConfigurator { |
| 129 if (_defaultIdentity) { | 167 if (_defaultIdentity) { |
| 130 return [[SigninPromoViewConfigurator alloc] | 168 return [[SigninPromoViewConfigurator alloc] |
| 131 initWithUserEmail:_defaultIdentity.userEmail | 169 initWithUserEmail:_defaultIdentity.userEmail |
| 132 userFullName:_defaultIdentity.userFullName | 170 userFullName:_defaultIdentity.userFullName |
| 133 userImage:_identityAvatar]; | 171 userImage:_identityAvatar]; |
| 134 } | 172 } |
| 135 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil | 173 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil |
| 136 userFullName:nil | 174 userFullName:nil |
| 137 userImage:nil]; | 175 userImage:nil]; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 158 _identityAvatar = identityAvatar; | 196 _identityAvatar = identityAvatar; |
| 159 [self sendConsumerNotificationWithIdentityChanged:NO]; | 197 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 160 } | 198 } |
| 161 | 199 |
| 162 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { | 200 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { |
| 163 SigninPromoViewConfigurator* configurator = [self createConfigurator]; | 201 SigninPromoViewConfigurator* configurator = [self createConfigurator]; |
| 164 [_consumer configureSigninPromoWithConfigurator:configurator | 202 [_consumer configureSigninPromoWithConfigurator:configurator |
| 165 identityChanged:identityChanged]; | 203 identityChanged:identityChanged]; |
| 166 } | 204 } |
| 167 | 205 |
| 206 - (void)sendCountTillSigninHistogram { |
| 207 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed || |
| 208 _signinPromoViewState != SigninPromoViewState::Unused); |
| 209 _signinPromoViewState = SigninPromoViewState::SigninStarted; |
| 210 if (!_displayedCountPreferenceKey) |
| 211 return; |
| 212 PrefService* prefs = _browserState->GetPrefs(); |
| 213 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 214 switch (_histograms) { |
| 215 case ios::SigninPromoViewHistograms::Bookmarks: |
| 216 UMA_HISTOGRAM_COUNTS_100( |
| 217 "MobileSignInPromo.BookmarkManager.CountTilSignin", displayedCount); |
| 218 break; |
| 219 case ios::SigninPromoViewHistograms::None: |
| 220 break; |
| 221 } |
| 222 } |
| 223 |
| 224 - (void)signinPromoViewVisible { |
| 225 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed); |
| 226 if (_signinPromoViewState == SigninPromoViewState::Visible) |
| 227 return; |
| 228 _signinPromoViewState = SigninPromoViewState::Visible; |
| 229 if (!_displayedCountPreferenceKey) |
| 230 return; |
| 231 PrefService* prefs = _browserState->GetPrefs(); |
| 232 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 233 ++displayedCount; |
| 234 prefs->SetInteger(_displayedCountPreferenceKey, displayedCount); |
| 235 if (displayedCount >= kAutomaticSigninPromoViewDismissCount && |
| 236 _alreadySeenSigninViewPreferenceKey) { |
| 237 prefs->SetBoolean(prefs::kIosBookmarkPromoAlreadySeen, true); |
| 238 } |
| 239 } |
| 240 |
| 241 - (void)signinPromoViewHidden { |
| 242 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 243 _signinPromoViewState != SigninPromoViewState::Dismissed); |
| 244 if (_signinPromoViewState != SigninPromoViewState::Visible) |
| 245 return; |
| 246 _signinPromoViewState = SigninPromoViewState::Hidden; |
| 247 } |
| 248 |
| 249 - (void)signinPromoViewDismissed { |
| 250 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 251 _signinPromoViewState != SigninPromoViewState::Hidden); |
| 252 _signinPromoViewState = SigninPromoViewState::Dismissed; |
| 253 if (!_displayedCountPreferenceKey) |
| 254 return; |
| 255 PrefService* prefs = _browserState->GetPrefs(); |
| 256 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 257 switch (_histograms) { |
| 258 case ios::SigninPromoViewHistograms::Bookmarks: |
| 259 UMA_HISTOGRAM_COUNTS_100("MobileSignInPromo.BookmarkManager.CountTilX", |
| 260 displayedCount); |
| 261 break; |
| 262 case ios::SigninPromoViewHistograms::None: |
| 263 break; |
| 264 } |
| 265 } |
| 266 |
| 168 #pragma mark - ChromeIdentityServiceObserver | 267 #pragma mark - ChromeIdentityServiceObserver |
| 169 | 268 |
| 170 - (void)onIdentityListChanged { | 269 - (void)onIdentityListChanged { |
| 171 ChromeIdentity* newIdentity = nil; | 270 ChromeIdentity* newIdentity = nil; |
| 172 NSArray* identities = ios::GetChromeBrowserProvider() | 271 NSArray* identities = ios::GetChromeBrowserProvider() |
| 173 ->GetChromeIdentityService() | 272 ->GetChromeIdentityService() |
| 174 ->GetAllIdentitiesSortedForDisplay(); | 273 ->GetAllIdentitiesSortedForDisplay(); |
| 175 if (identities.count != 0) { | 274 if (identities.count != 0) { |
| 176 newIdentity = identities[0]; | 275 newIdentity = identities[0]; |
| 177 } | 276 } |
| 178 if (newIdentity != _defaultIdentity) { | 277 if (newIdentity != _defaultIdentity) { |
| 179 [self selectIdentity:newIdentity]; | 278 [self selectIdentity:newIdentity]; |
| 180 [self sendConsumerNotificationWithIdentityChanged:YES]; | 279 [self sendConsumerNotificationWithIdentityChanged:YES]; |
| 181 } | 280 } |
| 182 } | 281 } |
| 183 | 282 |
| 184 - (void)onProfileUpdate:(ChromeIdentity*)identity { | 283 - (void)onProfileUpdate:(ChromeIdentity*)identity { |
| 185 if (identity == _defaultIdentity) { | 284 if (identity == _defaultIdentity) { |
| 186 [self sendConsumerNotificationWithIdentityChanged:NO]; | 285 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 187 } | 286 } |
| 188 } | 287 } |
| 189 | 288 |
| 190 #pragma mark - SigninPromoViewDelegate | 289 #pragma mark - SigninPromoViewDelegate |
| 191 | 290 |
| 192 - (void)signinPromoViewDidTapSigninWithNewAccount: | 291 - (void)signinPromoViewDidTapSigninWithNewAccount: |
| 193 (SigninPromoView*)signinPromoView { | 292 (SigninPromoView*)signinPromoView { |
| 194 DCHECK(!_defaultIdentity); | 293 DCHECK(!_defaultIdentity); |
| 294 [self sendCountTillSigninHistogram]; |
| 195 RecordSigninUserActionForAccessPoint(_accessPoint); | 295 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 196 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); | 296 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); |
| 197 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 297 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 198 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 298 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 199 accessPoint:_accessPoint | 299 accessPoint:_accessPoint |
| 200 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; | 300 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; |
| 201 [signinPromoView chromeExecuteCommand:command]; | 301 [signinPromoView chromeExecuteCommand:command]; |
| 202 } | 302 } |
| 203 | 303 |
| 204 - (void)signinPromoViewDidTapSigninWithDefaultAccount: | 304 - (void)signinPromoViewDidTapSigninWithDefaultAccount: |
| 205 (SigninPromoView*)signinPromoView { | 305 (SigninPromoView*)signinPromoView { |
| 206 DCHECK(_defaultIdentity); | 306 DCHECK(_defaultIdentity); |
| 307 [self sendCountTillSigninHistogram]; |
| 207 RecordSigninUserActionForAccessPoint(_accessPoint); | 308 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 208 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); | 309 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); |
| 209 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 310 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 210 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 311 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 211 identity:_defaultIdentity | 312 identity:_defaultIdentity |
| 212 accessPoint:_accessPoint | 313 accessPoint:_accessPoint |
| 213 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT | 314 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT |
| 214 callback:nil]; | 315 callback:nil]; |
| 215 [signinPromoView chromeExecuteCommand:command]; | 316 [signinPromoView chromeExecuteCommand:command]; |
| 216 } | 317 } |
| 217 | 318 |
| 218 - (void)signinPromoViewDidTapSigninWithOtherAccount: | 319 - (void)signinPromoViewDidTapSigninWithOtherAccount: |
| 219 (SigninPromoView*)signinPromoView { | 320 (SigninPromoView*)signinPromoView { |
| 220 DCHECK(_defaultIdentity); | 321 DCHECK(_defaultIdentity); |
| 322 [self sendCountTillSigninHistogram]; |
| 221 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); | 323 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); |
| 222 RecordSigninUserActionForAccessPoint(_accessPoint); | 324 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 223 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 325 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 224 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 326 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 225 accessPoint:_accessPoint | 327 accessPoint:_accessPoint |
| 226 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; | 328 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; |
| 227 [signinPromoView chromeExecuteCommand:command]; | 329 [signinPromoView chromeExecuteCommand:command]; |
| 228 } | 330 } |
| 229 | 331 |
| 230 @end | 332 @end |
| OLD | NEW |