Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1112)

Side by Side Diff: chrome/browser/invalidation/ticl_invalidation_service.cc

Issue 179843002: Make invalidations work for Chrome OS Kiosk Apps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
10 #include "chrome/browser/invalidation/gcm_network_channel_delegate_impl.h" 9 #include "chrome/browser/invalidation/gcm_network_channel_delegate_impl.h"
10 #include "chrome/browser/invalidation/invalidation_auth_provider.h"
11 #include "chrome/browser/invalidation/invalidation_logger.h" 11 #include "chrome/browser/invalidation/invalidation_logger.h"
12 #include "chrome/browser/invalidation/invalidation_service_util.h" 12 #include "chrome/browser/invalidation/invalidation_service_util.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/about_signin_internals.h"
15 #include "chrome/browser/signin/about_signin_internals_factory.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/signin/signin_manager.h"
19 #include "content/public/browser/notification_service.h"
20 #include "google_apis/gaia/gaia_constants.h" 14 #include "google_apis/gaia/gaia_constants.h"
21 #include "sync/notifier/gcm_network_channel_delegate.h" 15 #include "sync/notifier/gcm_network_channel_delegate.h"
22 #include "sync/notifier/invalidation_util.h" 16 #include "sync/notifier/invalidation_util.h"
23 #include "sync/notifier/invalidator.h" 17 #include "sync/notifier/invalidator.h"
24 #include "sync/notifier/invalidator_state.h" 18 #include "sync/notifier/invalidator_state.h"
25 #include "sync/notifier/non_blocking_invalidator.h" 19 #include "sync/notifier/non_blocking_invalidator.h"
26 #include "sync/notifier/object_id_invalidation_map.h" 20 #include "sync/notifier/object_id_invalidation_map.h"
27 21
28 static const char* kOAuth2Scopes[] = { 22 static const char* kOAuth2Scopes[] = {
29 GaiaConstants::kGoogleTalkOAuth2Scope 23 GaiaConstants::kGoogleTalkOAuth2Scope
(...skipping 23 matching lines...) Expand all
53 // has no significant state, -1 to never discard. 47 // has no significant state, -1 to never discard.
54 -1, 48 -1,
55 49
56 // Don't use initial delay unless the last request was an error. 50 // Don't use initial delay unless the last request was an error.
57 false, 51 false,
58 }; 52 };
59 53
60 namespace invalidation { 54 namespace invalidation {
61 55
62 TiclInvalidationService::TiclInvalidationService( 56 TiclInvalidationService::TiclInvalidationService(
63 SigninManagerBase* signin, 57 scoped_ptr<InvalidationAuthProvider> auth_provider,
64 ProfileOAuth2TokenService* oauth2_token_service,
65 Profile* profile) 58 Profile* profile)
66 : OAuth2TokenService::Consumer("ticl_invalidation"), 59 : OAuth2TokenService::Consumer("ticl_invalidation"),
67 profile_(profile), 60 profile_(profile),
68 signin_manager_(signin), 61 auth_provider_(auth_provider.Pass()),
69 oauth2_token_service_(oauth2_token_service),
70 invalidator_registrar_(new syncer::InvalidatorRegistrar()), 62 invalidator_registrar_(new syncer::InvalidatorRegistrar()),
71 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), 63 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy),
72 logger_() {} 64 logger_() {
65 }
73 66
74 TiclInvalidationService::~TiclInvalidationService() { 67 TiclInvalidationService::~TiclInvalidationService() {
75 DCHECK(CalledOnValidThread()); 68 DCHECK(CalledOnValidThread());
76 } 69 }
77 70
78 void TiclInvalidationService::Init() { 71 void TiclInvalidationService::Init() {
79 DCHECK(CalledOnValidThread()); 72 DCHECK(CalledOnValidThread());
80 73
81 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); 74 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
82 if (invalidator_storage_->GetInvalidatorClientId().empty()) { 75 if (invalidator_storage_->GetInvalidatorClientId().empty()) {
83 // This also clears any existing state. We can't reuse old invalidator 76 // This also clears any existing state. We can't reuse old invalidator
84 // state with the new ID anyway. 77 // state with the new ID anyway.
85 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); 78 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
86 } 79 }
87 80
88 if (IsReadyToStart()) { 81 if (IsReadyToStart()) {
89 StartInvalidator(PUSH_CLIENT_CHANNEL); 82 StartInvalidator(PUSH_CLIENT_CHANNEL);
90 } 83 }
91 84
92 notification_registrar_.Add(this, 85 auth_provider_->AddObserver(this);
93 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, 86 auth_provider_->GetTokenService()->AddObserver(this);
94 content::Source<Profile>(profile_));
95 oauth2_token_service_->AddObserver(this);
96 } 87 }
97 88
98 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { 89 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) {
99 // Here we perform the equivalent of Init() and StartInvalidator(), but with 90 // Here we perform the equivalent of Init() and StartInvalidator(), but with
100 // some minor changes to account for the fact that we're injecting the 91 // some minor changes to account for the fact that we're injecting the
101 // invalidator. 92 // invalidator.
102 invalidator_.reset(invalidator); 93 invalidator_.reset(invalidator);
103 94
104 invalidator_->RegisterHandler(this); 95 invalidator_->RegisterHandler(this);
105 invalidator_->UpdateRegisteredIds( 96 invalidator_->UpdateRegisteredIds(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 147
157 std::string TiclInvalidationService::GetInvalidatorClientId() const { 148 std::string TiclInvalidationService::GetInvalidatorClientId() const {
158 DCHECK(CalledOnValidThread()); 149 DCHECK(CalledOnValidThread());
159 return invalidator_storage_->GetInvalidatorClientId(); 150 return invalidator_storage_->GetInvalidatorClientId();
160 } 151 }
161 152
162 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { 153 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() {
163 return &logger_; 154 return &logger_;
164 } 155 }
165 156
166 void TiclInvalidationService::Observe( 157 InvalidationAuthProvider*
167 int type, 158 TiclInvalidationService::GetInvalidationAuthProvider() {
168 const content::NotificationSource& source, 159 return auth_provider_.get();
169 const content::NotificationDetails& details) {
170 DCHECK(CalledOnValidThread());
171 DCHECK_EQ(type, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT);
172 Logout();
173 } 160 }
174 161
175 void TiclInvalidationService::RequestAccessToken() { 162 void TiclInvalidationService::RequestAccessToken() {
176 // Only one active request at a time. 163 // Only one active request at a time.
177 if (access_token_request_ != NULL) 164 if (access_token_request_ != NULL)
178 return; 165 return;
179 request_access_token_retry_timer_.Stop(); 166 request_access_token_retry_timer_.Stop();
180 OAuth2TokenService::ScopeSet oauth2_scopes; 167 OAuth2TokenService::ScopeSet oauth2_scopes;
181 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) 168 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++)
182 oauth2_scopes.insert(kOAuth2Scopes[i]); 169 oauth2_scopes.insert(kOAuth2Scopes[i]);
183 // Invalidate previous token, otherwise token service will return the same 170 // Invalidate previous token, otherwise token service will return the same
184 // token again. 171 // token again.
185 const std::string& account_id = signin_manager_->GetAuthenticatedAccountId(); 172 const std::string& account_id = auth_provider_->GetAccountId();
186 oauth2_token_service_->InvalidateToken(account_id, 173 OAuth2TokenService* token_service = auth_provider_->GetTokenService();
187 oauth2_scopes, 174 token_service->InvalidateToken(account_id, oauth2_scopes, access_token_);
188 access_token_);
189 access_token_.clear(); 175 access_token_.clear();
190 access_token_request_ = oauth2_token_service_->StartRequest(account_id, 176 access_token_request_ =
191 oauth2_scopes, 177 token_service->StartRequest(account_id, oauth2_scopes, this);
192 this);
193 } 178 }
194 179
195 void TiclInvalidationService::OnGetTokenSuccess( 180 void TiclInvalidationService::OnGetTokenSuccess(
196 const OAuth2TokenService::Request* request, 181 const OAuth2TokenService::Request* request,
197 const std::string& access_token, 182 const std::string& access_token,
198 const base::Time& expiration_time) { 183 const base::Time& expiration_time) {
199 DCHECK_EQ(access_token_request_, request); 184 DCHECK_EQ(access_token_request_, request);
200 access_token_request_.reset(); 185 access_token_request_.reset();
201 // Reset backoff time after successful response. 186 // Reset backoff time after successful response.
202 request_access_token_backoff_.Reset(); 187 request_access_token_backoff_.Reset();
(...skipping 30 matching lines...) Expand all
233 break; 218 break;
234 } 219 }
235 default: { 220 default: {
236 // We have no way to notify the user of this. Do nothing. 221 // We have no way to notify the user of this. Do nothing.
237 } 222 }
238 } 223 }
239 } 224 }
240 225
241 void TiclInvalidationService::OnRefreshTokenAvailable( 226 void TiclInvalidationService::OnRefreshTokenAvailable(
242 const std::string& account_id) { 227 const std::string& account_id) {
243 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { 228 if (auth_provider_->GetAccountId() == account_id) {
244 if (!IsStarted() && IsReadyToStart()) { 229 if (!IsStarted() && IsReadyToStart()) {
245 StartInvalidator(PUSH_CLIENT_CHANNEL); 230 StartInvalidator(PUSH_CLIENT_CHANNEL);
246 } 231 }
247 } 232 }
248 } 233 }
249 234
250 void TiclInvalidationService::OnRefreshTokenRevoked( 235 void TiclInvalidationService::OnRefreshTokenRevoked(
251 const std::string& account_id) { 236 const std::string& account_id) {
252 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { 237 if (auth_provider_->GetAccountId() == account_id) {
253 access_token_.clear(); 238 access_token_.clear();
254 if (IsStarted()) { 239 if (IsStarted()) {
255 UpdateInvalidatorCredentials(); 240 UpdateInvalidatorCredentials();
256 } 241 }
257 } 242 }
258 } 243 }
259 244
245 void TiclInvalidationService::OnInvalidationAuthLogout() {
246 access_token_request_.reset();
247 request_access_token_retry_timer_.Stop();
248
249 if (IsStarted()) {
250 StopInvalidator();
251 }
252
253 // This service always expects to have a valid invalidator storage.
254 // So we must not only clear the old one, but also start a new one.
255 invalidator_storage_->Clear();
256 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
257 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
258 }
259
260 void TiclInvalidationService::OnInvalidatorStateChange( 260 void TiclInvalidationService::OnInvalidatorStateChange(
261 syncer::InvalidatorState state) { 261 syncer::InvalidatorState state) {
262 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { 262 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) {
263 // This may be due to normal OAuth access token expiration. If so, we must 263 // This may be due to normal OAuth access token expiration. If so, we must
264 // fetch a new one using our refresh token. Resetting the invalidator's 264 // fetch a new one using our refresh token. Resetting the invalidator's
265 // access token will not reset the invalidator's exponential backoff, so 265 // access token will not reset the invalidator's exponential backoff, so
266 // it's safe to try to update the token every time we receive this signal. 266 // it's safe to try to update the token every time we receive this signal.
267 // 267 //
268 // We won't be receiving any invalidations while the refresh is in progress, 268 // We won't be receiving any invalidations while the refresh is in progress,
269 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials 269 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials
(...skipping 13 matching lines...) Expand all
283 const syncer::ObjectIdInvalidationMap& invalidation_map) { 283 const syncer::ObjectIdInvalidationMap& invalidation_map) {
284 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); 284 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map);
285 285
286 logger_.OnInvalidation(invalidation_map); 286 logger_.OnInvalidation(invalidation_map);
287 } 287 }
288 288
289 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } 289 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; }
290 290
291 void TiclInvalidationService::Shutdown() { 291 void TiclInvalidationService::Shutdown() {
292 DCHECK(CalledOnValidThread()); 292 DCHECK(CalledOnValidThread());
293 oauth2_token_service_->RemoveObserver(this); 293 auth_provider_->GetTokenService()->RemoveObserver(this);
294 auth_provider_->RemoveObserver(this);
294 if (IsStarted()) { 295 if (IsStarted()) {
295 StopInvalidator(); 296 StopInvalidator();
296 } 297 }
297 invalidator_storage_.reset(); 298 invalidator_storage_.reset();
298 invalidator_registrar_.reset(); 299 invalidator_registrar_.reset();
299 } 300 }
300 301
301 bool TiclInvalidationService::IsReadyToStart() { 302 bool TiclInvalidationService::IsReadyToStart() {
302 if (profile_->IsManaged()) { 303 if (profile_->IsManaged()) {
303 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; 304 DVLOG(2) << "Not starting TiclInvalidationService: User is managed.";
304 return false; 305 return false;
305 } 306 }
306 307
307 if (signin_manager_->GetAuthenticatedUsername().empty()) { 308 if (auth_provider_->GetAccountId().empty()) {
308 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; 309 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in.";
309 return false; 310 return false;
310 } 311 }
311 312
312 if (!oauth2_token_service_) { 313 OAuth2TokenService* token_service = auth_provider_->GetTokenService();
314 if (!token_service) {
313 DVLOG(2) 315 DVLOG(2)
314 << "Not starting TiclInvalidationService: " 316 << "Not starting TiclInvalidationService: "
315 << "OAuth2TokenService unavailable."; 317 << "OAuth2TokenService unavailable.";
316 return false; 318 return false;
317 } 319 }
318 320
319 if (!oauth2_token_service_->RefreshTokenIsAvailable( 321 if (!token_service->RefreshTokenIsAvailable(auth_provider_->GetAccountId())) {
320 signin_manager_->GetAuthenticatedAccountId())) {
321 DVLOG(2) 322 DVLOG(2)
322 << "Not starting TiclInvalidationServce: Waiting for refresh token."; 323 << "Not starting TiclInvalidationServce: Waiting for refresh token.";
323 return false; 324 return false;
324 } 325 }
325 326
326 return true; 327 return true;
327 } 328 }
328 329
329 bool TiclInvalidationService::IsStarted() { 330 bool TiclInvalidationService::IsStarted() {
330 return invalidator_.get() != NULL; 331 return invalidator_.get() != NULL;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 385
385 UpdateInvalidatorCredentials(); 386 UpdateInvalidatorCredentials();
386 387
387 invalidator_->RegisterHandler(this); 388 invalidator_->RegisterHandler(this);
388 invalidator_->UpdateRegisteredIds( 389 invalidator_->UpdateRegisteredIds(
389 this, 390 this,
390 invalidator_registrar_->GetAllRegisteredIds()); 391 invalidator_registrar_->GetAllRegisteredIds());
391 } 392 }
392 393
393 void TiclInvalidationService::UpdateInvalidatorCredentials() { 394 void TiclInvalidationService::UpdateInvalidatorCredentials() {
394 std::string email = signin_manager_->GetAuthenticatedUsername(); 395 std::string email = auth_provider_->GetAccountId();
395 396
396 DCHECK(!email.empty()) << "Expected user to be signed in."; 397 DCHECK(!email.empty()) << "Expected user to be signed in.";
397 398
398 DVLOG(2) << "UpdateCredentials: " << email; 399 DVLOG(2) << "UpdateCredentials: " << email;
399 invalidator_->UpdateCredentials(email, access_token_); 400 invalidator_->UpdateCredentials(email, access_token_);
400 } 401 }
401 402
402 void TiclInvalidationService::StopInvalidator() { 403 void TiclInvalidationService::StopInvalidator() {
403 DCHECK(invalidator_); 404 DCHECK(invalidator_);
404 invalidator_->UnregisterHandler(this); 405 invalidator_->UnregisterHandler(this);
405 invalidator_.reset(); 406 invalidator_.reset();
406 } 407 }
407 408
408 void TiclInvalidationService::Logout() {
409 access_token_request_.reset();
410 request_access_token_retry_timer_.Stop();
411
412 if (IsStarted()) {
413 StopInvalidator();
414 }
415
416 // This service always expects to have a valid invalidator storage.
417 // So we must not only clear the old one, but also start a new one.
418 invalidator_storage_->Clear();
419 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
420 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
421 }
422
423 } // namespace invalidation 409 } // namespace invalidation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698