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