| 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 dismissalCountHistogramName = _dismissalCountHistogramName; |
| 132 @synthesize countTilSigninHistogramName = _countTilSigninHistogramName; |
| 133 @synthesize countTilXHistogramName = _countTilXHistogramName; |
| 112 | 134 |
| 113 - (instancetype)init { | 135 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 114 self = [super init]; | 136 self = [super init]; |
| 115 if (self) { | 137 if (self) { |
| 138 _browserState = browserState; |
| 116 NSArray* identities = ios::GetChromeBrowserProvider() | 139 NSArray* identities = ios::GetChromeBrowserProvider() |
| 117 ->GetChromeIdentityService() | 140 ->GetChromeIdentityService() |
| 118 ->GetAllIdentitiesSortedForDisplay(); | 141 ->GetAllIdentitiesSortedForDisplay(); |
| 119 if (identities.count != 0) { | 142 if (identities.count != 0) { |
| 120 [self selectIdentity:identities[0]]; | 143 [self selectIdentity:identities[0]]; |
| 121 } | 144 } |
| 122 _identityServiceObserver = | 145 _identityServiceObserver = |
| 123 base::MakeUnique<ChromeIdentityServiceObserverBridge>(self); | 146 base::MakeUnique<ChromeIdentityServiceObserverBridge>(self); |
| 124 } | 147 } |
| 125 return self; | 148 return self; |
| 126 } | 149 } |
| 127 | 150 |
| 151 - (void)dealloc { |
| 152 if (!_displayedCountPreferenceKey.empty() && |
| 153 !_dismissalCountHistogramName.empty() && |
| 154 (_signinPromoViewState == SigninPromoViewState::Visible || |
| 155 _signinPromoViewState == SigninPromoViewState::Hidden)) { |
| 156 PrefService* prefs = _browserState->GetPrefs(); |
| 157 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 158 UMA_HISTOGRAM_COUNTS_100(_dismissalCountHistogramName, displayedCount); |
| 159 } |
| 160 } |
| 161 |
| 128 - (SigninPromoViewConfigurator*)createConfigurator { | 162 - (SigninPromoViewConfigurator*)createConfigurator { |
| 129 if (_defaultIdentity) { | 163 if (_defaultIdentity) { |
| 130 return [[SigninPromoViewConfigurator alloc] | 164 return [[SigninPromoViewConfigurator alloc] |
| 131 initWithUserEmail:_defaultIdentity.userEmail | 165 initWithUserEmail:_defaultIdentity.userEmail |
| 132 userFullName:_defaultIdentity.userFullName | 166 userFullName:_defaultIdentity.userFullName |
| 133 userImage:_identityAvatar]; | 167 userImage:_identityAvatar]; |
| 134 } | 168 } |
| 135 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil | 169 return [[SigninPromoViewConfigurator alloc] initWithUserEmail:nil |
| 136 userFullName:nil | 170 userFullName:nil |
| 137 userImage:nil]; | 171 userImage:nil]; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 158 _identityAvatar = identityAvatar; | 192 _identityAvatar = identityAvatar; |
| 159 [self sendConsumerNotificationWithIdentityChanged:NO]; | 193 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 160 } | 194 } |
| 161 | 195 |
| 162 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { | 196 - (void)sendConsumerNotificationWithIdentityChanged:(BOOL)identityChanged { |
| 163 SigninPromoViewConfigurator* configurator = [self createConfigurator]; | 197 SigninPromoViewConfigurator* configurator = [self createConfigurator]; |
| 164 [_consumer configureSigninPromoWithConfigurator:configurator | 198 [_consumer configureSigninPromoWithConfigurator:configurator |
| 165 identityChanged:identityChanged]; | 199 identityChanged:identityChanged]; |
| 166 } | 200 } |
| 167 | 201 |
| 202 - (void)sendCountTillSigninHistogram { |
| 203 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed || |
| 204 _signinPromoViewState != SigninPromoViewState::Unused); |
| 205 _signinPromoViewState = SigninPromoViewState::SigninStarted; |
| 206 if (_displayedCountPreferenceKey.empty() || |
| 207 _countTilSigninHistogramName.empty()) |
| 208 return; |
| 209 PrefService* prefs = _browserState->GetPrefs(); |
| 210 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 211 UMA_HISTOGRAM_COUNTS_100(_countTilSigninHistogramName, displayedCount); |
| 212 } |
| 213 |
| 214 - (void)signinPromoViewVisible { |
| 215 DCHECK(_signinPromoViewState != SigninPromoViewState::Dismissed); |
| 216 if (_signinPromoViewState == SigninPromoViewState::Visible) |
| 217 return; |
| 218 _signinPromoViewState = SigninPromoViewState::Visible; |
| 219 if (_displayedCountPreferenceKey.empty()) |
| 220 return; |
| 221 PrefService* prefs = _browserState->GetPrefs(); |
| 222 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 223 ++displayedCount; |
| 224 prefs->SetInteger(_displayedCountPreferenceKey, displayedCount); |
| 225 if (displayedCount >= kAutomaticSigninPromoViewDismissCount && |
| 226 !_alreadySeenSigninViewPreferenceKey.empty()) { |
| 227 PrefService* prefs = _browserState->GetPrefs(); |
| 228 prefs->SetBoolean(prefs::kIosBookmarkPromoAlreadySeen, true); |
| 229 } |
| 230 } |
| 231 |
| 232 - (void)signinPromoViewHidden { |
| 233 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 234 _signinPromoViewState != SigninPromoViewState::Dismissed); |
| 235 if (_signinPromoViewState != SigninPromoViewState::Visible) |
| 236 return; |
| 237 _signinPromoViewState = SigninPromoViewState::Hidden; |
| 238 } |
| 239 |
| 240 - (void)signinPromoViewDismissed { |
| 241 DCHECK(_signinPromoViewState != SigninPromoViewState::Unused || |
| 242 _signinPromoViewState != SigninPromoViewState::Hidden); |
| 243 _signinPromoViewState = SigninPromoViewState::Dismissed; |
| 244 if (_displayedCountPreferenceKey.empty() || _countTilXHistogramName.empty()) |
| 245 return; |
| 246 PrefService* prefs = _browserState->GetPrefs(); |
| 247 int displayedCount = prefs->GetInteger(_displayedCountPreferenceKey); |
| 248 UMA_HISTOGRAM_COUNTS_100(_countTilXHistogramName, displayedCount); |
| 249 } |
| 250 |
| 168 #pragma mark - ChromeIdentityServiceObserver | 251 #pragma mark - ChromeIdentityServiceObserver |
| 169 | 252 |
| 170 - (void)onIdentityListChanged { | 253 - (void)onIdentityListChanged { |
| 171 ChromeIdentity* newIdentity = nil; | 254 ChromeIdentity* newIdentity = nil; |
| 172 NSArray* identities = ios::GetChromeBrowserProvider() | 255 NSArray* identities = ios::GetChromeBrowserProvider() |
| 173 ->GetChromeIdentityService() | 256 ->GetChromeIdentityService() |
| 174 ->GetAllIdentitiesSortedForDisplay(); | 257 ->GetAllIdentitiesSortedForDisplay(); |
| 175 if (identities.count != 0) { | 258 if (identities.count != 0) { |
| 176 newIdentity = identities[0]; | 259 newIdentity = identities[0]; |
| 177 } | 260 } |
| 178 if (newIdentity != _defaultIdentity) { | 261 if (newIdentity != _defaultIdentity) { |
| 179 [self selectIdentity:newIdentity]; | 262 [self selectIdentity:newIdentity]; |
| 180 [self sendConsumerNotificationWithIdentityChanged:YES]; | 263 [self sendConsumerNotificationWithIdentityChanged:YES]; |
| 181 } | 264 } |
| 182 } | 265 } |
| 183 | 266 |
| 184 - (void)onProfileUpdate:(ChromeIdentity*)identity { | 267 - (void)onProfileUpdate:(ChromeIdentity*)identity { |
| 185 if (identity == _defaultIdentity) { | 268 if (identity == _defaultIdentity) { |
| 186 [self sendConsumerNotificationWithIdentityChanged:NO]; | 269 [self sendConsumerNotificationWithIdentityChanged:NO]; |
| 187 } | 270 } |
| 188 } | 271 } |
| 189 | 272 |
| 190 #pragma mark - SigninPromoViewDelegate | 273 #pragma mark - SigninPromoViewDelegate |
| 191 | 274 |
| 192 - (void)signinPromoViewDidTapSigninWithNewAccount: | 275 - (void)signinPromoViewDidTapSigninWithNewAccount: |
| 193 (SigninPromoView*)signinPromoView { | 276 (SigninPromoView*)signinPromoView { |
| 194 DCHECK(!_defaultIdentity); | 277 DCHECK(!_defaultIdentity); |
| 278 [self sendCountTillSigninHistogram]; |
| 195 RecordSigninUserActionForAccessPoint(_accessPoint); | 279 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 196 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); | 280 RecordSigninNewAccountUserActionForAccessPoint(_accessPoint); |
| 197 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 281 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 198 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 282 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 199 accessPoint:_accessPoint | 283 accessPoint:_accessPoint |
| 200 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; | 284 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NEW_ACCOUNT]; |
| 201 [signinPromoView chromeExecuteCommand:command]; | 285 [signinPromoView chromeExecuteCommand:command]; |
| 202 } | 286 } |
| 203 | 287 |
| 204 - (void)signinPromoViewDidTapSigninWithDefaultAccount: | 288 - (void)signinPromoViewDidTapSigninWithDefaultAccount: |
| 205 (SigninPromoView*)signinPromoView { | 289 (SigninPromoView*)signinPromoView { |
| 206 DCHECK(_defaultIdentity); | 290 DCHECK(_defaultIdentity); |
| 291 [self sendCountTillSigninHistogram]; |
| 207 RecordSigninUserActionForAccessPoint(_accessPoint); | 292 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 208 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); | 293 RecordSigninDefaultUserActionForAccessPoint(_accessPoint); |
| 209 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 294 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 210 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 295 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 211 identity:_defaultIdentity | 296 identity:_defaultIdentity |
| 212 accessPoint:_accessPoint | 297 accessPoint:_accessPoint |
| 213 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT | 298 promoAction:signin_metrics::PromoAction::PROMO_ACTION_WITH_DEFAULT |
| 214 callback:nil]; | 299 callback:nil]; |
| 215 [signinPromoView chromeExecuteCommand:command]; | 300 [signinPromoView chromeExecuteCommand:command]; |
| 216 } | 301 } |
| 217 | 302 |
| 218 - (void)signinPromoViewDidTapSigninWithOtherAccount: | 303 - (void)signinPromoViewDidTapSigninWithOtherAccount: |
| 219 (SigninPromoView*)signinPromoView { | 304 (SigninPromoView*)signinPromoView { |
| 220 DCHECK(_defaultIdentity); | 305 DCHECK(_defaultIdentity); |
| 306 [self sendCountTillSigninHistogram]; |
| 221 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); | 307 RecordSigninNotDefaultUserActionForAccessPoint(_accessPoint); |
| 222 RecordSigninUserActionForAccessPoint(_accessPoint); | 308 RecordSigninUserActionForAccessPoint(_accessPoint); |
| 223 ShowSigninCommand* command = [[ShowSigninCommand alloc] | 309 ShowSigninCommand* command = [[ShowSigninCommand alloc] |
| 224 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN | 310 initWithOperation:AUTHENTICATION_OPERATION_SIGNIN |
| 225 accessPoint:_accessPoint | 311 accessPoint:_accessPoint |
| 226 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; | 312 promoAction:signin_metrics::PromoAction::PROMO_ACTION_NOT_DEFAULT]; |
| 227 [signinPromoView chromeExecuteCommand:command]; | 313 [signinPromoView chromeExecuteCommand:command]; |
| 228 } | 314 } |
| 229 | 315 |
| 230 @end | 316 @end |
| OLD | NEW |