| 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.ImpressionsTilDismiss", |
| 159 displayedCount); |
| 160 break; |
| 161 case ios::SigninPromoViewHistograms::None: |
| 162 break; |
| 163 } |
| 164 } |
| 165 } |
| 166 |
| 128 - (SigninPromoViewConfigurator*)createConfigurator { | 167 - (SigninPromoViewConfigurator*)createConfigurator { |
| 129 if (_defaultIdentity) { | 168 if (_defaultIdentity) { |
| 130 return [[SigninPromoViewConfigurator alloc] | 169 return [[SigninPromoViewConfigurator alloc] |
| 131 initWithUserEmail:_defaultIdentity.userEmail | 170 initWithUserEmail:_defaultIdentity.userEmail |
| 132 userFullName:_defaultIdentity.userFullName | 171 userFullName:_defaultIdentity.userFullName |
| 133 userImage:_identityAvatar]; | 172 userImage:_identityAvatar]; |
| 134 } | 173 } |
| 135 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil | 174 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil |
| 136 userFullName:nil | 175 userFullName:nil |
| 137 userImage:nil]; | 176 userImage:nil]; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 158 _identityAvatar = identityAvatar; | 197 _identityAvatar = identityAvatar; |
| 159 [self sendConsumerNotificationWithIdentityChanged:NO]; | 198 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 160 } | 199 } |
| 161 | 200 |
| 162 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { | 201 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { |
| 163 SigninPromoViewConfigurator* configurator = [self createConfigurator]; | 202 SigninPromoViewConfigurator* configurator = [self createConfigurator]; |
| 164 [_consumer configureSigninPromoWithConfigurator:configurator | 203 [_consumer configureSigninPromoWithConfigurator:configurator |
| 165 identityChanged:identityChanged]; | 204 identityChanged:identityChanged]; |
| 166 } | 205 } |
| 167 | 206 |
| 207 - (void)sendImpressionsTillSigninButtonsHistogram { |
| 208 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed || |
| 209 _signinPromoViewState != SigninPromoViewState::Unused); |
| 210 _signinPromoViewState = SigninPromoViewState::SigninStarted; |
| 211 if (!_displayedCountPreferenceKey) |
| 212 return; |
| 213 PrefService* prefs = _browserState->GetPrefs(); |
| 214 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 215 switch (_histograms) { |
| 216 case ios::SigninPromoViewHistograms::Bookmarks: |
| 217 UMA_HISTOGRAM_COUNTS_100( |
| 218 "MobileSignInPromo.BookmarkManager.ImpressionsTilSigninButtons", |
| 219 displayedCount); |
| 220 break; |
| 221 case ios::SigninPromoViewHistograms::None: |
| 222 break; |
| 223 } |
| 224 } |
| 225 |
| 226 - (void)signinPromoViewVisible { |
| 227 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed); |
| 228 if (_signinPromoViewState == SigninPromoViewState::Visible) |
| 229 return; |
| 230 _signinPromoViewState = SigninPromoViewState::Visible; |
| 231 if (!_displayedCountPreferenceKey) |
| 232 return; |
| 233 PrefService* prefs = _browserState->GetPrefs(); |
| 234 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 235 ++displayedCount; |
| 236 prefs->SetInteger(_displayedCountPreferenceKey, displayedCount); |
| 237 if (displayedCount >= kAutomaticSigninPromoViewDismissCount && |
| 238 _alreadySeenSigninViewPreferenceKey) { |
| 239 prefs->SetBoolean(prefs::kIosBookmarkPromoAlreadySeen, true); |
| 240 } |
| 241 } |
| 242 |
| 243 - (void)signinPromoViewHidden { |
| 244 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 245 _signinPromoViewState != SigninPromoViewState::Dismissed); |
| 246 if (_signinPromoViewState != SigninPromoViewState::Visible) |
| 247 return; |
| 248 _signinPromoViewState = SigninPromoViewState::Hidden; |
| 249 } |
| 250 |
| 251 - (void)signinPromoViewDismissed { |
| 252 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 253 _signinPromoViewState != SigninPromoViewState::Hidden); |
| 254 _signinPromoViewState = SigninPromoViewState::Dismissed; |
| 255 if (!_displayedCountPreferenceKey) |
| 256 return; |
| 257 PrefService* prefs = _browserState->GetPrefs(); |
| 258 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 259 switch (_histograms) { |
| 260 case ios::SigninPromoViewHistograms::Bookmarks: |
| 261 UMA_HISTOGRAM_COUNTS_100( |
| 262 "MobileSignInPromo.BookmarkManager.ImpressionsTilXButton", |
| 263 displayedCount); |
| 264 break; |
| 265 case ios::SigninPromoViewHistograms::None: |
| 266 break; |
| 267 } |
| 268 } |
| 269 |
| 168 #pragma mark - ChromeIdentityServiceObserver | 270 #pragma mark - ChromeIdentityServiceObserver |
| 169 | 271 |
| 170 - (void)onIdentityListChanged { | 272 - (void)onIdentityListChanged { |
| 171 ChromeIdentity* newIdentity = nil; | 273 ChromeIdentity* newIdentity = nil; |
| 172 NSArray* identities = ios::GetChromeBrowserProvider() | 274 NSArray* identities = ios::GetChromeBrowserProvider() |
| 173 ->GetChromeIdentityService() | 275 ->GetChromeIdentityService() |
| 174 ->GetAllIdentitiesSortedForDisplay(); | 276 ->GetAllIdentitiesSortedForDisplay(); |
| 175 if (identities.count != 0) { | 277 if (identities.count != 0) { |
| 176 newIdentity = identities[0]; | 278 newIdentity = identities[0]; |
| 177 } | 279 } |
| 178 if (newIdentity != _defaultIdentity) { | 280 if (newIdentity != _defaultIdentity) { |
| 179 [self selectIdentity:newIdentity]; | 281 [self selectIdentity:newIdentity]; |
| 180 [self sendConsumerNotificationWithIdentityChanged:YES]; | 282 [self sendConsumerNotificationWithIdentityChanged:YES]; |
| 181 } | 283 } |
| 182 } | 284 } |
| 183 | 285 |
| 184 - (void)onProfileUpdate:(ChromeIdentity*)identity { | 286 - (void)onProfileUpdate:(ChromeIdentity*)identity { |
| 185 if (identity == _defaultIdentity) { | 287 if (identity == _defaultIdentity) { |
| 186 [self sendConsumerNotificationWithIdentityChanged:NO]; | 288 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 187 } | 289 } |
| 188 } | 290 } |
| 189 | 291 |
| 190 #pragma mark - SigninPromoViewDelegate | 292 #pragma mark - SigninPromoViewDelegate |
| 191 | 293 |
| 192 - (void)signinPromoViewDidTapSigninWithNewAccount: | 294 - (void)signinPromoViewDidTapSigninWithNewAccount: |
| 193 (SigninPromoView*)signinPromoView { | 295 (SigninPromoView*)signinPromoView { |
| 194 DCHECK(!_defaultIdentity); | 296 DCHECK(!_defaultIdentity); |
| 297 [self sendImpressionsTillSigninButtonsHistogram]; |
| 195 RecordSigninUserActionForAccessPoint(_accessPoint); | 298 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 196 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); | 299 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); |
| 197 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 300 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 198 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 301 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 199 accessPoint:_accessPoint | 302 accessPoint:_accessPoint |
| 200 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; | 303 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; |
| 201 [signinPromoView chromeExecuteCommand:command]; | 304 [signinPromoView chromeExecuteCommand:command]; |
| 202 } | 305 } |
| 203 | 306 |
| 204 - (void)signinPromoViewDidTapSigninWithDefaultAccount: | 307 - (void)signinPromoViewDidTapSigninWithDefaultAccount: |
| 205 (SigninPromoView*)signinPromoView { | 308 (SigninPromoView*)signinPromoView { |
| 206 DCHECK(_defaultIdentity); | 309 DCHECK(_defaultIdentity); |
| 310 [self sendImpressionsTillSigninButtonsHistogram]; |
| 207 RecordSigninUserActionForAccessPoint(_accessPoint); | 311 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 208 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); | 312 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); |
| 209 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 313 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 210 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 314 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 211 identity:_defaultIdentity | 315 identity:_defaultIdentity |
| 212 accessPoint:_accessPoint | 316 accessPoint:_accessPoint |
| 213 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT | 317 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT |
| 214 callback:nil]; | 318 callback:nil]; |
| 215 [signinPromoView chromeExecuteCommand:command]; | 319 [signinPromoView chromeExecuteCommand:command]; |
| 216 } | 320 } |
| 217 | 321 |
| 218 - (void)signinPromoViewDidTapSigninWithOtherAccount: | 322 - (void)signinPromoViewDidTapSigninWithOtherAccount: |
| 219 (SigninPromoView*)signinPromoView { | 323 (SigninPromoView*)signinPromoView { |
| 220 DCHECK(_defaultIdentity); | 324 DCHECK(_defaultIdentity); |
| 325 [self sendImpressionsTillSigninButtonsHistogram]; |
| 221 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); | 326 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); |
| 222 RecordSigninUserActionForAccessPoint(_accessPoint); | 327 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 223 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 328 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 224 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 329 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 225 accessPoint:_accessPoint | 330 accessPoint:_accessPoint |
| 226 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; | 331 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; |
| 227 [signinPromoView chromeExecuteCommand:command]; | 332 [signinPromoView chromeExecuteCommand:command]; |
| 228 } | 333 } |
| 229 | 334 |
| 230 @end | 335 @end |
| OLD | NEW |