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

Side by Side Diff: chrome/browser/signin/signin_tracker.cc

Issue 19567004: Convert SigninTracker to use OAuth2TokenService notifications (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Filip's review comments Created 7 years, 3 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/signin/signin_tracker.h" 5 #include "chrome/browser/signin/signin_tracker.h"
6 6
7 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/browser/signin/signin_manager.h" 11 #include "chrome/browser/signin/signin_manager.h"
10 #include "chrome/browser/signin/signin_manager_factory.h" 12 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "chrome/browser/signin/token_service.h"
12 #include "chrome/browser/signin/token_service_factory.h"
13 #include "content/public/browser/notification_details.h" 13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h" 14 #include "content/public/browser/notification_source.h"
15 #include "google_apis/gaia/gaia_constants.h" 15 #include "google_apis/gaia/gaia_constants.h"
16 16
17 static const char* kSignedInServices[] = {
18 GaiaConstants::kSyncService,
19 GaiaConstants::kGaiaOAuth2LoginRefreshToken
20 };
21 static const int kNumSignedInServices =
22 arraysize(kSignedInServices);
23
24 // Helper to check if the given token service is relevant for sync.
25 SigninTracker::SigninTracker(Profile* profile, Observer* observer) 17 SigninTracker::SigninTracker(Profile* profile, Observer* observer)
26 : state_(WAITING_FOR_GAIA_VALIDATION), 18 : state_(WAITING_FOR_GAIA_VALIDATION),
27 profile_(profile), 19 profile_(profile),
28 observer_(observer), 20 observer_(observer),
29 credentials_valid_(false) { 21 credentials_valid_(false) {
22 DCHECK(profile_);
30 Initialize(); 23 Initialize();
31 } 24 }
32 25
33 SigninTracker::~SigninTracker() { 26 SigninTracker::~SigninTracker() {
27 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
28 RemoveObserver(this);
34 } 29 }
35 30
36 void SigninTracker::Initialize() { 31 void SigninTracker::Initialize() {
37 DCHECK(observer_); 32 DCHECK(observer_);
38 // Register for notifications from the SigninManager. 33 // Register for notifications from the SigninManager.
39 registrar_.Add(this, 34 registrar_.Add(this,
40 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 35 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
41 content::Source<Profile>(profile_)); 36 content::Source<Profile>(profile_));
42 registrar_.Add(this, 37 registrar_.Add(this,
43 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, 38 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED,
44 content::Source<Profile>(profile_)); 39 content::Source<Profile>(profile_));
45 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
46 registrar_.Add(this,
47 chrome::NOTIFICATION_TOKEN_AVAILABLE,
48 content::Source<TokenService>(token_service));
49 registrar_.Add(this,
50 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED,
51 content::Source<TokenService>(token_service));
52 40
53 if (state_ == SERVICES_INITIALIZING) 41 OAuth2TokenService* token_service =
54 HandleServiceStateChange(); 42 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
43 token_service->AddObserver(this);
55 } 44 }
56 45
57 void SigninTracker::Observe(int type, 46 void SigninTracker::Observe(int type,
58 const content::NotificationSource& source, 47 const content::NotificationSource& source,
59 const content::NotificationDetails& details) { 48 const content::NotificationDetails& details) {
60 // We should not get more than one of these notifications. 49 // We should not get more than one of these notifications.
61 switch (type) { 50 switch (type) {
62 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: 51 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL:
Andrew T Wilson (Slow) 2013/08/30 09:03:33 I wonder if we should not listen for this, but ins
Roger Tawa OOO till Jul 10th 2013/08/30 15:55:40 I think it makes sense to have the multiple states
63 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); 52 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION);
64 state_ = SERVICES_INITIALIZING; 53 state_ = SERVICES_INITIALIZING;
65 // If our services are already signed in, see if it's possible to 54 // If our services are already signed in, see if it's possible to
66 // transition to the SIGNIN_COMPLETE state. 55 // transition to the SIGNIN_COMPLETE state.
67 HandleServiceStateChange(); 56 HandleServiceStateChange();
68 break; 57 break;
69 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { 58 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: {
70 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); 59 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION);
71 const GoogleServiceAuthError& error = 60 const GoogleServiceAuthError& error =
72 *(content::Details<const GoogleServiceAuthError>(details).ptr()); 61 *(content::Details<const GoogleServiceAuthError>(details).ptr());
73 observer_->SigninFailed(error); 62 observer_->SigninFailed(error);
74 break; 63 break;
75 } 64 }
76 case chrome::NOTIFICATION_TOKEN_AVAILABLE:
77 // A new token is available - check to see if we're all signed in now.
78 HandleServiceStateChange();
79 break;
80 case chrome::NOTIFICATION_TOKEN_REQUEST_FAILED:
81 if (state_ == SERVICES_INITIALIZING) {
82 const TokenService::TokenRequestFailedDetails& token_details =
83 *(content::Details<const TokenService::TokenRequestFailedDetails>(
84 details).ptr());
85 for (int i = 0; i < kNumSignedInServices; ++i) {
86 if (token_details.service() == kSignedInServices[i]) {
87 // We got an error loading one of our tokens, so notify our
88 // observer.
89 state_ = WAITING_FOR_GAIA_VALIDATION;
90 observer_->SigninFailed(token_details.error());
91 }
92 }
93 }
94 break;
95 default: 65 default:
96 NOTREACHED(); 66 NOTREACHED() << "Invalid type=" << type;
97 } 67 }
98 } 68 }
99 69
70 void SigninTracker::OnRefreshTokenAvailable(const std::string& account_id) {
71 HandleServiceStateChange();
72 }
73
74 void SigninTracker::OnRefreshTokenRevoked(const std::string& account_id) {
75 if (state_ == SERVICES_INITIALIZING) {
76 // We got an error loading one of our tokens, so notify our observer.
77 state_ = WAITING_FOR_GAIA_VALIDATION;
78 observer_->SigninFailed(
79 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED));
80 }
81 }
82
100 void SigninTracker::HandleServiceStateChange() { 83 void SigninTracker::HandleServiceStateChange() {
101 if (state_ != SERVICES_INITIALIZING) { 84 if (state_ != SERVICES_INITIALIZING) {
102 // Ignore service updates until after our GAIA credentials are validated. 85 // Ignore service updates until after our GAIA credentials are validated.
103 return; 86 return;
104 } 87 }
105 88
106 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); 89 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
107 state_ = GetSigninState(profile_, &error); 90 state_ = GetSigninState(profile_, &error);
108 switch (state_) { 91 switch (state_) {
109 case WAITING_FOR_GAIA_VALIDATION: 92 case WAITING_FOR_GAIA_VALIDATION:
110 observer_->SigninFailed(error); 93 observer_->SigninFailed(error);
111 break; 94 break;
112 case SIGNIN_COMPLETE: 95 case SIGNIN_COMPLETE:
113 observer_->SigninSuccess(); 96 observer_->SigninSuccess();
114 break; 97 break;
115 default: 98 default:
116 // State has not changed, nothing to do. 99 // State has not changed, nothing to do.
117 DCHECK_EQ(state_, SERVICES_INITIALIZING); 100 DCHECK_EQ(state_, SERVICES_INITIALIZING);
118 break; 101 break;
119 } 102 }
120 } 103 }
121 104
122 // static 105 // static
123 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { 106 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) {
124 // See if we have all of the tokens required. 107 // SigninTracker only tracks the primary account, so only need to check
125 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); 108 // that one.
126 for (int i = 0; i < kNumSignedInServices; ++i) { 109 return ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
127 if (!token_service->HasTokenForService(kSignedInServices[i])) { 110 RefreshTokenIsAvailable();
128 // Don't have a token for one of our signed-in services.
129 return false;
130 }
131 }
132 return true;
133 } 111 }
134 112
135 // static 113 // static
136 SigninTracker::LoginState SigninTracker::GetSigninState( 114 SigninTracker::LoginState SigninTracker::GetSigninState(
137 Profile* profile, 115 Profile* profile,
138 GoogleServiceAuthError* error) { 116 GoogleServiceAuthError* error) {
139 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); 117 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
140 if (signin->GetAuthenticatedUsername().empty()) { 118 if (signin->GetAuthenticatedUsername().empty()) {
141 // User is signed out, trigger a signin failure. 119 // User is signed out, trigger a signin failure.
142 if (error) 120 if (error)
143 *error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); 121 *error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
144 return WAITING_FOR_GAIA_VALIDATION; 122 return WAITING_FOR_GAIA_VALIDATION;
145 } 123 }
146 124
147 // If we haven't loaded all our service tokens yet, just exit (we'll be called 125 // If we haven't loaded all our service tokens yet, just exit (we'll be called
148 // again when another token is loaded, or will transition to SigninFailed if 126 // again when another token is loaded, or will transition to SigninFailed if
149 // the loading fails). 127 // the loading fails).
150 if (!AreServiceTokensLoaded(profile)) 128 if (!AreServiceTokensLoaded(profile))
151 return SERVICES_INITIALIZING; 129 return SERVICES_INITIALIZING;
152 130
153 return SIGNIN_COMPLETE; 131 return SIGNIN_COMPLETE;
154 } 132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698