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

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: Rebase. 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/invalidation/gcm_invalidation_bridge.h" 9 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h"
10 #include "chrome/browser/invalidation/invalidation_auth_provider.h"
10 #include "chrome/browser/invalidation/invalidation_logger.h" 11 #include "chrome/browser/invalidation/invalidation_logger.h"
11 #include "chrome/browser/invalidation/invalidation_service_util.h" 12 #include "chrome/browser/invalidation/invalidation_service_util.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/about_signin_internals.h" 14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
14 #include "chrome/browser/signin/about_signin_internals_factory.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
17 #include "chrome/common/chrome_content_client.h" 15 #include "chrome/common/chrome_content_client.h"
18 #include "google_apis/gaia/gaia_constants.h" 16 #include "google_apis/gaia/gaia_constants.h"
19 #include "sync/notifier/gcm_network_channel_delegate.h" 17 #include "sync/notifier/gcm_network_channel_delegate.h"
20 #include "sync/notifier/invalidation_util.h" 18 #include "sync/notifier/invalidation_util.h"
21 #include "sync/notifier/invalidator.h" 19 #include "sync/notifier/invalidator.h"
22 #include "sync/notifier/invalidator_state.h" 20 #include "sync/notifier/invalidator_state.h"
23 #include "sync/notifier/non_blocking_invalidator.h" 21 #include "sync/notifier/non_blocking_invalidator.h"
24 #include "sync/notifier/object_id_invalidation_map.h" 22 #include "sync/notifier/object_id_invalidation_map.h"
25 23
26 static const char* kOAuth2Scopes[] = { 24 static const char* kOAuth2Scopes[] = {
(...skipping 24 matching lines...) Expand all
51 // has no significant state, -1 to never discard. 49 // has no significant state, -1 to never discard.
52 -1, 50 -1,
53 51
54 // Don't use initial delay unless the last request was an error. 52 // Don't use initial delay unless the last request was an error.
55 false, 53 false,
56 }; 54 };
57 55
58 namespace invalidation { 56 namespace invalidation {
59 57
60 TiclInvalidationService::TiclInvalidationService( 58 TiclInvalidationService::TiclInvalidationService(
61 SigninManagerBase* signin, 59 scoped_ptr<InvalidationAuthProvider> auth_provider,
62 ProfileOAuth2TokenService* oauth2_token_service,
63 Profile* profile) 60 Profile* profile)
64 : OAuth2TokenService::Consumer("ticl_invalidation"), 61 : OAuth2TokenService::Consumer("ticl_invalidation"),
65 profile_(profile), 62 profile_(profile),
66 signin_manager_(signin), 63 auth_provider_(auth_provider.Pass()),
67 oauth2_token_service_(oauth2_token_service),
68 invalidator_registrar_(new syncer::InvalidatorRegistrar()), 64 invalidator_registrar_(new syncer::InvalidatorRegistrar()),
69 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), 65 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy),
70 logger_() {} 66 logger_() {}
71 67
72 TiclInvalidationService::~TiclInvalidationService() { 68 TiclInvalidationService::~TiclInvalidationService() {
73 DCHECK(CalledOnValidThread()); 69 DCHECK(CalledOnValidThread());
74 } 70 }
75 71
76 void TiclInvalidationService::Init() { 72 void TiclInvalidationService::Init() {
77 DCHECK(CalledOnValidThread()); 73 DCHECK(CalledOnValidThread());
78 74
79 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); 75 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
80 if (invalidator_storage_->GetInvalidatorClientId().empty()) { 76 if (invalidator_storage_->GetInvalidatorClientId().empty()) {
81 // This also clears any existing state. We can't reuse old invalidator 77 // This also clears any existing state. We can't reuse old invalidator
82 // state with the new ID anyway. 78 // state with the new ID anyway.
83 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); 79 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
84 } 80 }
85 81
86 if (IsReadyToStart()) { 82 if (IsReadyToStart()) {
87 StartInvalidator(PUSH_CLIENT_CHANNEL); 83 StartInvalidator(PUSH_CLIENT_CHANNEL);
88 } 84 }
89 85
90 SigninManagerBase* signin_manager = 86 auth_provider_->AddObserver(this);
91 SigninManagerFactory::GetForProfile(profile_); 87 auth_provider_->GetTokenService()->AddObserver(this);
92 signin_manager->AddObserver(this);
93
94 oauth2_token_service_->AddObserver(this);
95 } 88 }
96 89
97 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { 90 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) {
98 // Here we perform the equivalent of Init() and StartInvalidator(), but with 91 // Here we perform the equivalent of Init() and StartInvalidator(), but with
99 // some minor changes to account for the fact that we're injecting the 92 // some minor changes to account for the fact that we're injecting the
100 // invalidator. 93 // invalidator.
101 invalidator_.reset(invalidator); 94 invalidator_.reset(invalidator);
102 95
103 invalidator_->RegisterHandler(this); 96 invalidator_->RegisterHandler(this);
104 invalidator_->UpdateRegisteredIds( 97 invalidator_->UpdateRegisteredIds(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 148
156 std::string TiclInvalidationService::GetInvalidatorClientId() const { 149 std::string TiclInvalidationService::GetInvalidatorClientId() const {
157 DCHECK(CalledOnValidThread()); 150 DCHECK(CalledOnValidThread());
158 return invalidator_storage_->GetInvalidatorClientId(); 151 return invalidator_storage_->GetInvalidatorClientId();
159 } 152 }
160 153
161 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { 154 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() {
162 return &logger_; 155 return &logger_;
163 } 156 }
164 157
165 void TiclInvalidationService::GoogleSignedOut(const std::string& username) { 158 InvalidationAuthProvider*
166 DCHECK(CalledOnValidThread()); 159 TiclInvalidationService::GetInvalidationAuthProvider() {
167 Logout(); 160 return auth_provider_.get();
168 } 161 }
169 162
170 void TiclInvalidationService::RequestDetailedStatus( 163 void TiclInvalidationService::RequestDetailedStatus(
171 base::Callback<void(const base::DictionaryValue&)> return_callback) { 164 base::Callback<void(const base::DictionaryValue&)> return_callback) {
172 invalidator_->RequestDetailedStatus(return_callback); 165 invalidator_->RequestDetailedStatus(return_callback);
173 } 166 }
174 167
175 void TiclInvalidationService::RequestAccessToken() { 168 void TiclInvalidationService::RequestAccessToken() {
176 // Only one active request at a time. 169 // Only one active request at a time.
177 if (access_token_request_ != NULL) 170 if (access_token_request_ != NULL)
178 return; 171 return;
179 request_access_token_retry_timer_.Stop(); 172 request_access_token_retry_timer_.Stop();
180 OAuth2TokenService::ScopeSet oauth2_scopes; 173 OAuth2TokenService::ScopeSet oauth2_scopes;
181 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) 174 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++)
182 oauth2_scopes.insert(kOAuth2Scopes[i]); 175 oauth2_scopes.insert(kOAuth2Scopes[i]);
183 // Invalidate previous token, otherwise token service will return the same 176 // Invalidate previous token, otherwise token service will return the same
184 // token again. 177 // token again.
185 const std::string& account_id = signin_manager_->GetAuthenticatedAccountId(); 178 const std::string& account_id = auth_provider_->GetAccountId();
186 oauth2_token_service_->InvalidateToken(account_id, 179 OAuth2TokenService* token_service = auth_provider_->GetTokenService();
187 oauth2_scopes, 180 token_service->InvalidateToken(account_id, oauth2_scopes, access_token_);
188 access_token_);
189 access_token_.clear(); 181 access_token_.clear();
190 access_token_request_ = oauth2_token_service_->StartRequest(account_id, 182 access_token_request_ =
191 oauth2_scopes, 183 token_service->StartRequest(account_id, oauth2_scopes, this);
192 this);
193 } 184 }
194 185
195 void TiclInvalidationService::OnGetTokenSuccess( 186 void TiclInvalidationService::OnGetTokenSuccess(
196 const OAuth2TokenService::Request* request, 187 const OAuth2TokenService::Request* request,
197 const std::string& access_token, 188 const std::string& access_token,
198 const base::Time& expiration_time) { 189 const base::Time& expiration_time) {
199 DCHECK_EQ(access_token_request_, request); 190 DCHECK_EQ(access_token_request_, request);
200 access_token_request_.reset(); 191 access_token_request_.reset();
201 // Reset backoff time after successful response. 192 // Reset backoff time after successful response.
202 request_access_token_backoff_.Reset(); 193 request_access_token_backoff_.Reset();
(...skipping 30 matching lines...) Expand all
233 break; 224 break;
234 } 225 }
235 default: { 226 default: {
236 // We have no way to notify the user of this. Do nothing. 227 // We have no way to notify the user of this. Do nothing.
237 } 228 }
238 } 229 }
239 } 230 }
240 231
241 void TiclInvalidationService::OnRefreshTokenAvailable( 232 void TiclInvalidationService::OnRefreshTokenAvailable(
242 const std::string& account_id) { 233 const std::string& account_id) {
243 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { 234 if (auth_provider_->GetAccountId() == account_id) {
244 if (!IsStarted() && IsReadyToStart()) { 235 if (!IsStarted() && IsReadyToStart()) {
245 StartInvalidator(PUSH_CLIENT_CHANNEL); 236 StartInvalidator(PUSH_CLIENT_CHANNEL);
246 } 237 }
247 } 238 }
248 } 239 }
249 240
250 void TiclInvalidationService::OnRefreshTokenRevoked( 241 void TiclInvalidationService::OnRefreshTokenRevoked(
251 const std::string& account_id) { 242 const std::string& account_id) {
252 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { 243 if (auth_provider_->GetAccountId() == account_id) {
253 access_token_.clear(); 244 access_token_.clear();
254 if (IsStarted()) { 245 if (IsStarted()) {
255 UpdateInvalidatorCredentials(); 246 UpdateInvalidatorCredentials();
256 } 247 }
257 } 248 }
258 } 249 }
259 250
251 void TiclInvalidationService::OnInvalidationAuthLogout() {
252 access_token_request_.reset();
253 request_access_token_retry_timer_.Stop();
254
255 if (IsStarted()) {
256 StopInvalidator();
257 }
258
259 // This service always expects to have a valid invalidator storage.
260 // So we must not only clear the old one, but also start a new one.
261 invalidator_storage_->Clear();
262 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
263 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
264 }
265
260 void TiclInvalidationService::OnInvalidatorStateChange( 266 void TiclInvalidationService::OnInvalidatorStateChange(
261 syncer::InvalidatorState state) { 267 syncer::InvalidatorState state) {
262 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { 268 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) {
263 // This may be due to normal OAuth access token expiration. If so, we must 269 // 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 270 // fetch a new one using our refresh token. Resetting the invalidator's
265 // access token will not reset the invalidator's exponential backoff, so 271 // 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. 272 // it's safe to try to update the token every time we receive this signal.
267 // 273 //
268 // We won't be receiving any invalidations while the refresh is in progress, 274 // 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 275 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials
(...skipping 13 matching lines...) Expand all
283 const syncer::ObjectIdInvalidationMap& invalidation_map) { 289 const syncer::ObjectIdInvalidationMap& invalidation_map) {
284 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); 290 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map);
285 291
286 logger_.OnInvalidation(invalidation_map); 292 logger_.OnInvalidation(invalidation_map);
287 } 293 }
288 294
289 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } 295 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; }
290 296
291 void TiclInvalidationService::Shutdown() { 297 void TiclInvalidationService::Shutdown() {
292 DCHECK(CalledOnValidThread()); 298 DCHECK(CalledOnValidThread());
293 SigninManagerBase* signin_manager = 299 auth_provider_->GetTokenService()->RemoveObserver(this);
294 SigninManagerFactory::GetForProfile(profile_); 300 auth_provider_->RemoveObserver(this);
295 signin_manager->RemoveObserver(this);
296 oauth2_token_service_->RemoveObserver(this);
297 if (IsStarted()) { 301 if (IsStarted()) {
298 StopInvalidator(); 302 StopInvalidator();
299 } 303 }
300 invalidator_storage_.reset(); 304 invalidator_storage_.reset();
301 invalidator_registrar_.reset(); 305 invalidator_registrar_.reset();
302 } 306 }
303 307
304 bool TiclInvalidationService::IsReadyToStart() { 308 bool TiclInvalidationService::IsReadyToStart() {
305 if (profile_->IsManaged()) { 309 if (profile_->IsManaged()) {
306 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; 310 DVLOG(2) << "Not starting TiclInvalidationService: User is managed.";
307 return false; 311 return false;
308 } 312 }
309 313
310 if (signin_manager_->GetAuthenticatedUsername().empty()) { 314 if (auth_provider_->GetAccountId().empty()) {
311 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; 315 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in.";
312 return false; 316 return false;
313 } 317 }
314 318
315 if (!oauth2_token_service_) { 319 OAuth2TokenService* token_service = auth_provider_->GetTokenService();
320 if (!token_service) {
316 DVLOG(2) 321 DVLOG(2)
317 << "Not starting TiclInvalidationService: " 322 << "Not starting TiclInvalidationService: "
318 << "OAuth2TokenService unavailable."; 323 << "OAuth2TokenService unavailable.";
319 return false; 324 return false;
320 } 325 }
321 326
322 if (!oauth2_token_service_->RefreshTokenIsAvailable( 327 if (!token_service->RefreshTokenIsAvailable(auth_provider_->GetAccountId())) {
323 signin_manager_->GetAuthenticatedAccountId())) {
324 DVLOG(2) 328 DVLOG(2)
325 << "Not starting TiclInvalidationServce: Waiting for refresh token."; 329 << "Not starting TiclInvalidationServce: Waiting for refresh token.";
326 return false; 330 return false;
327 } 331 }
328 332
329 return true; 333 return true;
330 } 334 }
331 335
332 bool TiclInvalidationService::IsStarted() { 336 bool TiclInvalidationService::IsStarted() {
333 return invalidator_.get() != NULL; 337 return invalidator_.get() != NULL;
(...skipping 21 matching lines...) Expand all
355 notifier::NotifierOptions options = 359 notifier::NotifierOptions options =
356 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); 360 ParseNotifierOptions(*CommandLine::ForCurrentProcess());
357 options.request_context_getter = profile_->GetRequestContext(); 361 options.request_context_getter = profile_->GetRequestContext();
358 options.auth_mechanism = "X-OAUTH2"; 362 options.auth_mechanism = "X-OAUTH2";
359 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); 363 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method);
360 network_channel_creator = 364 network_channel_creator =
361 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); 365 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options);
362 break; 366 break;
363 } 367 }
364 case GCM_NETWORK_CHANNEL: { 368 case GCM_NETWORK_CHANNEL: {
365 gcm_invalidation_bridge_.reset(new GCMInvalidationBridge(profile_)); 369 gcm::GCMProfileService* gcm_profile_service =
370 gcm::GCMProfileServiceFactory::GetForProfile(profile_);
371 gcm_invalidation_bridge_.reset(
372 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get()));
366 network_channel_creator = 373 network_channel_creator =
367 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( 374 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator(
368 profile_->GetRequestContext(), 375 profile_->GetRequestContext(),
369 gcm_invalidation_bridge_->CreateDelegate().Pass()); 376 gcm_invalidation_bridge_->CreateDelegate().Pass());
370 break; 377 break;
371 } 378 }
372 default: { 379 default: {
373 NOTREACHED(); 380 NOTREACHED();
374 return; 381 return;
375 } 382 }
(...skipping 10 matching lines...) Expand all
386 393
387 UpdateInvalidatorCredentials(); 394 UpdateInvalidatorCredentials();
388 395
389 invalidator_->RegisterHandler(this); 396 invalidator_->RegisterHandler(this);
390 invalidator_->UpdateRegisteredIds( 397 invalidator_->UpdateRegisteredIds(
391 this, 398 this,
392 invalidator_registrar_->GetAllRegisteredIds()); 399 invalidator_registrar_->GetAllRegisteredIds());
393 } 400 }
394 401
395 void TiclInvalidationService::UpdateInvalidatorCredentials() { 402 void TiclInvalidationService::UpdateInvalidatorCredentials() {
396 std::string email = signin_manager_->GetAuthenticatedUsername(); 403 std::string email = auth_provider_->GetAccountId();
397 404
398 DCHECK(!email.empty()) << "Expected user to be signed in."; 405 DCHECK(!email.empty()) << "Expected user to be signed in.";
399 406
400 DVLOG(2) << "UpdateCredentials: " << email; 407 DVLOG(2) << "UpdateCredentials: " << email;
401 invalidator_->UpdateCredentials(email, access_token_); 408 invalidator_->UpdateCredentials(email, access_token_);
402 } 409 }
403 410
404 void TiclInvalidationService::StopInvalidator() { 411 void TiclInvalidationService::StopInvalidator() {
405 DCHECK(invalidator_); 412 DCHECK(invalidator_);
406 gcm_invalidation_bridge_.reset(); 413 gcm_invalidation_bridge_.reset();
407 invalidator_->UnregisterHandler(this); 414 invalidator_->UnregisterHandler(this);
408 invalidator_.reset(); 415 invalidator_.reset();
409 } 416 }
410 417
411 void TiclInvalidationService::Logout() {
412 access_token_request_.reset();
413 request_access_token_retry_timer_.Stop();
414
415 if (IsStarted()) {
416 StopInvalidator();
417 }
418
419 // This service always expects to have a valid invalidator storage.
420 // So we must not only clear the old one, but also start a new one.
421 invalidator_storage_->Clear();
422 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs()));
423 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId());
424 }
425
426 } // namespace invalidation 418 } // namespace invalidation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698