OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/invalidation/ticl_invalidation_service.h" | 5 #include "chrome/browser/invalidation/ticl_invalidation_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/invalidation/invalidation_service_util.h" | 10 #include "chrome/browser/invalidation/invalidation_service_util.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 -1, | 49 -1, |
50 | 50 |
51 // Don't use initial delay unless the last request was an error. | 51 // Don't use initial delay unless the last request was an error. |
52 false, | 52 false, |
53 }; | 53 }; |
54 | 54 |
55 namespace invalidation { | 55 namespace invalidation { |
56 | 56 |
57 TiclInvalidationService::TiclInvalidationService( | 57 TiclInvalidationService::TiclInvalidationService( |
58 SigninManagerBase* signin, | 58 SigninManagerBase* signin, |
59 TokenService* token_service, | 59 ProfileOAuth2TokenService* oauth2_token_service, |
60 OAuth2TokenService* oauth2_token_service, | |
61 Profile* profile) | 60 Profile* profile) |
62 : profile_(profile), | 61 : profile_(profile), |
63 signin_manager_(signin), | 62 signin_manager_(signin), |
64 token_service_(token_service), | |
65 oauth2_token_service_(oauth2_token_service), | 63 oauth2_token_service_(oauth2_token_service), |
66 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 64 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
67 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { | 65 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { |
68 } | 66 } |
69 | 67 |
70 TiclInvalidationService::~TiclInvalidationService() { | 68 TiclInvalidationService::~TiclInvalidationService() { |
71 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
70 oauth2_token_service_->RemoveObserver(this); | |
72 } | 71 } |
73 | 72 |
74 void TiclInvalidationService::Init() { | 73 void TiclInvalidationService::Init() { |
75 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
76 | 75 |
77 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 76 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
78 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 77 if (invalidator_storage_->GetInvalidatorClientId().empty()) { |
79 // This also clears any existing state. We can't reuse old invalidator | 78 // This also clears any existing state. We can't reuse old invalidator |
80 // state with the new ID anyway. | 79 // state with the new ID anyway. |
81 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 80 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
82 } | 81 } |
83 | 82 |
84 if (IsReadyToStart()) { | 83 if (IsReadyToStart()) { |
85 StartInvalidator(); | 84 StartInvalidator(); |
86 } | 85 } |
87 | 86 |
88 notification_registrar_.Add(this, | 87 notification_registrar_.Add(this, |
89 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 88 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
90 content::Source<Profile>(profile_)); | 89 content::Source<Profile>(profile_)); |
91 notification_registrar_.Add(this, | 90 oauth2_token_service_->AddObserver(this); |
92 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
93 content::Source<TokenService>(token_service_)); | |
94 notification_registrar_.Add(this, | |
95 chrome::NOTIFICATION_TOKENS_CLEARED, | |
96 content::Source<TokenService>(token_service_)); | |
97 } | 91 } |
98 | 92 |
99 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | 93 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { |
100 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 94 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
101 // some minor changes to account for the fact that we're injecting the | 95 // some minor changes to account for the fact that we're injecting the |
102 // invalidator. | 96 // invalidator. |
103 invalidator_.reset(invalidator); | 97 invalidator_.reset(invalidator); |
104 | 98 |
105 invalidator_->RegisterHandler(this); | 99 invalidator_->RegisterHandler(this); |
106 invalidator_->UpdateRegisteredIds( | 100 invalidator_->UpdateRegisteredIds( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 std::string TiclInvalidationService::GetInvalidatorClientId() const { | 158 std::string TiclInvalidationService::GetInvalidatorClientId() const { |
165 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
166 return invalidator_storage_->GetInvalidatorClientId(); | 160 return invalidator_storage_->GetInvalidatorClientId(); |
167 } | 161 } |
168 | 162 |
169 void TiclInvalidationService::Observe( | 163 void TiclInvalidationService::Observe( |
170 int type, | 164 int type, |
171 const content::NotificationSource& source, | 165 const content::NotificationSource& source, |
172 const content::NotificationDetails& details) { | 166 const content::NotificationDetails& details) { |
173 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
174 | 168 DCHECK_EQ(type, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT); |
175 switch (type) { | 169 Logout(); |
Andrew T Wilson (Slow)
2013/09/03 07:27:59
I'm fine with this, but I wonder if maybe we shoul
fgorski
2013/09/03 23:29:25
Drew, I don't know this class that well yet. I am
| |
176 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | |
177 if (!IsStarted() && IsReadyToStart()) { | |
178 StartInvalidator(); | |
179 } | |
180 break; | |
181 } | |
182 case chrome::NOTIFICATION_TOKENS_CLEARED: { | |
183 access_token_.clear(); | |
184 if (IsStarted()) { | |
185 UpdateInvalidatorCredentials(); | |
186 } | |
187 break; | |
188 } | |
189 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: { | |
190 Logout(); | |
191 break; | |
192 } | |
193 default: { | |
194 NOTREACHED(); | |
195 } | |
196 } | |
197 } | 170 } |
198 | 171 |
199 void TiclInvalidationService::RequestAccessToken() { | 172 void TiclInvalidationService::RequestAccessToken() { |
200 // Only one active request at a time. | 173 // Only one active request at a time. |
201 if (access_token_request_ != NULL) | 174 if (access_token_request_ != NULL) |
202 return; | 175 return; |
203 request_access_token_retry_timer_.Stop(); | 176 request_access_token_retry_timer_.Stop(); |
204 OAuth2TokenService::ScopeSet oauth2_scopes; | 177 OAuth2TokenService::ScopeSet oauth2_scopes; |
205 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) | 178 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) |
206 oauth2_scopes.insert(kOAuth2Scopes[i]); | 179 oauth2_scopes.insert(kOAuth2Scopes[i]); |
207 // Invalidate previous token, otherwise token service will return the same | 180 // Invalidate previous token, otherwise token service will return the same |
208 // token again. | 181 // token again. |
209 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); | 182 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); |
210 access_token_.clear(); | 183 access_token_.clear(); |
211 access_token_request_ = | 184 access_token_request_ = oauth2_token_service_->StartRequest( |
212 oauth2_token_service_->StartRequest(oauth2_scopes, this); | 185 oauth2_token_service_->GetPrimaryAccountId(), oauth2_scopes, this); |
213 } | 186 } |
214 | 187 |
215 void TiclInvalidationService::OnGetTokenSuccess( | 188 void TiclInvalidationService::OnGetTokenSuccess( |
216 const OAuth2TokenService::Request* request, | 189 const OAuth2TokenService::Request* request, |
217 const std::string& access_token, | 190 const std::string& access_token, |
218 const base::Time& expiration_time) { | 191 const base::Time& expiration_time) { |
219 DCHECK_EQ(access_token_request_, request); | 192 DCHECK_EQ(access_token_request_, request); |
220 access_token_request_.reset(); | 193 access_token_request_.reset(); |
221 // Reset backoff time after successful response. | 194 // Reset backoff time after successful response. |
222 request_access_token_backoff_.Reset(); | 195 request_access_token_backoff_.Reset(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 invalidator_registrar_->UpdateInvalidatorState( | 241 invalidator_registrar_->UpdateInvalidatorState( |
269 syncer::INVALIDATION_CREDENTIALS_REJECTED); | 242 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
270 break; | 243 break; |
271 } | 244 } |
272 default: { | 245 default: { |
273 // We have no way to notify the user of this. Do nothing. | 246 // We have no way to notify the user of this. Do nothing. |
274 } | 247 } |
275 } | 248 } |
276 } | 249 } |
277 | 250 |
251 void TiclInvalidationService::OnRefreshTokenAvailable( | |
252 const std::string& account_id) { | |
253 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | |
254 if (!IsStarted() && IsReadyToStart()) { | |
255 StartInvalidator(); | |
256 } | |
257 } | |
258 } | |
259 | |
260 void TiclInvalidationService::OnRefreshTokenRevoked( | |
261 const std::string& account_id) { | |
262 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | |
263 access_token_.clear(); | |
264 if (IsStarted()) { | |
265 UpdateInvalidatorCredentials(); | |
Andrew T Wilson (Slow)
2013/09/03 07:27:59
Why do we not Logout() here?
fgorski
2013/09/03 23:29:25
As per comment above. I am trying to recreate a lo
| |
266 } | |
267 } | |
268 } | |
269 | |
278 void TiclInvalidationService::OnInvalidatorStateChange( | 270 void TiclInvalidationService::OnInvalidatorStateChange( |
279 syncer::InvalidatorState state) { | 271 syncer::InvalidatorState state) { |
280 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 272 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
281 // This may be due to normal OAuth access token expiration. If so, we must | 273 // This may be due to normal OAuth access token expiration. If so, we must |
282 // fetch a new one using our refresh token. Resetting the invalidator's | 274 // fetch a new one using our refresh token. Resetting the invalidator's |
283 // access token will not reset the invalidator's exponential backoff, so | 275 // access token will not reset the invalidator's exponential backoff, so |
284 // it's safe to try to update the token every time we receive this signal. | 276 // it's safe to try to update the token every time we receive this signal. |
285 // | 277 // |
286 // We won't be receiving any invalidations while the refresh is in progress, | 278 // We won't be receiving any invalidations while the refresh is in progress, |
287 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials | 279 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; | 312 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; |
321 return false; | 313 return false; |
322 } | 314 } |
323 | 315 |
324 if (!oauth2_token_service_) { | 316 if (!oauth2_token_service_) { |
325 DVLOG(2) | 317 DVLOG(2) |
326 << "Not starting TiclInvalidationService: TokenService unavailable."; | 318 << "Not starting TiclInvalidationService: TokenService unavailable."; |
327 return false; | 319 return false; |
328 } | 320 } |
329 | 321 |
330 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { | 322 if (!oauth2_token_service_->RefreshTokenIsAvailable( |
323 oauth2_token_service_->GetPrimaryAccountId())) { | |
331 DVLOG(2) | 324 DVLOG(2) |
332 << "Not starting TiclInvalidationServce: Waiting for refresh token."; | 325 << "Not starting TiclInvalidationServce: Waiting for refresh token."; |
333 return false; | 326 return false; |
334 } | 327 } |
335 | 328 |
336 return true; | 329 return true; |
337 } | 330 } |
338 | 331 |
339 bool TiclInvalidationService::IsStarted() { | 332 bool TiclInvalidationService::IsStarted() { |
340 return invalidator_.get() != NULL; | 333 return invalidator_.get() != NULL; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 } | 392 } |
400 | 393 |
401 // This service always expects to have a valid invalidator storage. | 394 // This service always expects to have a valid invalidator storage. |
402 // So we must not only clear the old one, but also start a new one. | 395 // So we must not only clear the old one, but also start a new one. |
403 invalidator_storage_->Clear(); | 396 invalidator_storage_->Clear(); |
404 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 397 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
405 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 398 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
406 } | 399 } |
407 | 400 |
408 } // namespace invalidation | 401 } // namespace invalidation |
OLD | NEW |