OLD | NEW |
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/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/signin/token_service.h" | 8 #include "chrome/browser/signin/token_service.h" |
9 #include "chrome/browser/signin/token_service_factory.h" | 9 #include "chrome/browser/signin/token_service_factory.h" |
10 #include "chrome/browser/sync/profile_sync_service.h" | 10 #include "chrome/browser/sync/profile_sync_service.h" |
11 #include "chrome/browser/sync/profile_sync_service_factory.h" | 11 #include "chrome/browser/sync/profile_sync_service_factory.h" |
12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
13 #include "chrome/common/net/gaia/gaia_constants.h" | 13 #include "chrome/common/net/gaia/gaia_constants.h" |
14 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
16 | 16 |
17 static const char* kSignedInServices[] = { | 17 static const char* kSignedInServices[] = { |
18 GaiaConstants::kSyncService, | 18 GaiaConstants::kSyncService, |
19 GaiaConstants::kGaiaOAuth2LoginRefreshToken | 19 GaiaConstants::kGaiaOAuth2LoginRefreshToken |
20 }; | 20 }; |
21 static const int kNumSignedInServices = | 21 static const int kNumSignedInServices = |
22 arraysize(kSignedInServices); | 22 arraysize(kSignedInServices); |
23 | 23 |
24 // Helper to check if the given token service is relevant for sync. | 24 // Helper to check if the given token service is relevant for sync. |
25 SigninTracker::SigninTracker(Profile* profile, Observer* observer) | 25 SigninTracker::SigninTracker(Profile* profile, Observer* observer) |
26 : state_(WAITING_FOR_GAIA_VALIDATION), | 26 : state_(WAITING_FOR_GAIA_VALIDATION), |
27 profile_(profile), | 27 profile_(profile), |
28 observer_(observer), | 28 observer_(observer), |
29 credentials_valid_(false) { | 29 credentials_valid_(false) { |
| 30 Initialize(); |
| 31 } |
| 32 |
| 33 SigninTracker::SigninTracker(Profile* profile, |
| 34 Observer* observer, |
| 35 LoginState state) |
| 36 : state_(state), |
| 37 profile_(profile), |
| 38 observer_(observer), |
| 39 credentials_valid_(false) { |
| 40 Initialize(); |
| 41 } |
| 42 |
| 43 SigninTracker::~SigninTracker() { |
| 44 ProfileSyncService* service = |
| 45 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 46 service->RemoveObserver(this); |
| 47 } |
| 48 |
| 49 void SigninTracker::Initialize() { |
30 DCHECK(observer_); | 50 DCHECK(observer_); |
31 // Register for notifications from the SigninManager. | 51 // Register for notifications from the SigninManager. |
32 registrar_.Add(this, | 52 registrar_.Add(this, |
33 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 53 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
34 content::Source<Profile>(profile_)); | 54 content::Source<Profile>(profile_)); |
35 registrar_.Add(this, | 55 registrar_.Add(this, |
36 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 56 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
37 content::Source<Profile>(profile_)); | 57 content::Source<Profile>(profile_)); |
38 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 58 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
39 registrar_.Add(this, | 59 registrar_.Add(this, |
40 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 60 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
41 content::Source<TokenService>(token_service)); | 61 content::Source<TokenService>(token_service)); |
42 registrar_.Add(this, | 62 registrar_.Add(this, |
43 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 63 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
44 content::Source<TokenService>(token_service)); | 64 content::Source<TokenService>(token_service)); |
45 | 65 |
46 // Also listen for notifications from the various signed in services (only | 66 // Also listen for notifications from the various signed in services (only |
47 // sync for now). | 67 // sync for now). |
48 ProfileSyncService* service = | 68 ProfileSyncService* service = |
49 ProfileSyncServiceFactory::GetForProfile(profile_); | 69 ProfileSyncServiceFactory::GetForProfile(profile_); |
50 service->AddObserver(this); | 70 service->AddObserver(this); |
51 } | |
52 | 71 |
53 SigninTracker::~SigninTracker() { | 72 if (state_ == SERVICES_INITIALIZING) |
54 ProfileSyncService* service = | 73 HandleServiceStateChange(); |
55 ProfileSyncServiceFactory::GetForProfile(profile_); | |
56 service->RemoveObserver(this); | |
57 } | 74 } |
58 | 75 |
59 void SigninTracker::Observe(int type, | 76 void SigninTracker::Observe(int type, |
60 const content::NotificationSource& source, | 77 const content::NotificationSource& source, |
61 const content::NotificationDetails& details) { | 78 const content::NotificationDetails& details) { |
62 // We should not get more than one of these notifications. | 79 // We should not get more than one of these notifications. |
63 switch (type) { | 80 switch (type) { |
64 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: | 81 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: |
| 82 LOG(WARNING) << "Got NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL"; |
65 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 83 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
66 state_ = SERVICES_INITIALIZING; | 84 state_ = SERVICES_INITIALIZING; |
67 observer_->GaiaCredentialsValid(); | 85 observer_->GaiaCredentialsValid(); |
68 // If our services are already signed in, see if it's possible to | 86 // If our services are already signed in, see if it's possible to |
69 // transition to the SIGNIN_COMPLETE state. | 87 // transition to the SIGNIN_COMPLETE state. |
70 if (AreServicesSignedIn(profile_)) | 88 if (AreServicesSignedIn(profile_)) |
71 HandleServiceStateChange(); | 89 HandleServiceStateChange(); |
72 break; | 90 break; |
73 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { | 91 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { |
| 92 LOG(WARNING) << "Got NOTIFICATION_GOOGLE_SIGNIN_FAILED"; |
74 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 93 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
75 const GoogleServiceAuthError& error = | 94 const GoogleServiceAuthError& error = |
76 *(content::Details<const GoogleServiceAuthError>(details).ptr()); | 95 *(content::Details<const GoogleServiceAuthError>(details).ptr()); |
77 observer_->SigninFailed(error); | 96 observer_->SigninFailed(error); |
78 break; | 97 break; |
79 } | 98 } |
80 case chrome::NOTIFICATION_TOKEN_AVAILABLE: | 99 case chrome::NOTIFICATION_TOKEN_AVAILABLE: |
| 100 LOG(WARNING) << "Got NOTIFICATION_TOKEN_AVAILABLE"; |
81 // A new token is available - check to see if we're all signed in now. | 101 // A new token is available - check to see if we're all signed in now. |
82 HandleServiceStateChange(); | 102 HandleServiceStateChange(); |
83 break; | 103 break; |
84 case chrome::NOTIFICATION_TOKEN_REQUEST_FAILED: | 104 case chrome::NOTIFICATION_TOKEN_REQUEST_FAILED: |
| 105 LOG(WARNING) << "Got NOTIFICATION_TOKEN_REQUEST_FAILED"; |
85 if (state_ == SERVICES_INITIALIZING) { | 106 if (state_ == SERVICES_INITIALIZING) { |
86 const TokenService::TokenRequestFailedDetails& token_details = | 107 const TokenService::TokenRequestFailedDetails& token_details = |
87 *(content::Details<const TokenService::TokenRequestFailedDetails>( | 108 *(content::Details<const TokenService::TokenRequestFailedDetails>( |
88 details).ptr()); | 109 details).ptr()); |
89 for (int i = 0; i < kNumSignedInServices; ++i) { | 110 for (int i = 0; i < kNumSignedInServices; ++i) { |
90 if (token_details.service() == kSignedInServices[i]) { | 111 if (token_details.service() == kSignedInServices[i]) { |
91 // We got an error loading one of our tokens, so notify our | 112 // We got an error loading one of our tokens, so notify our |
92 // observer. | 113 // observer. |
| 114 LOG(WARNING) << "resetting state_ to WAITING_FOR_GAIA_VALIDATION"; |
93 state_ = WAITING_FOR_GAIA_VALIDATION; | 115 state_ = WAITING_FOR_GAIA_VALIDATION; |
94 observer_->SigninFailed(token_details.error()); | 116 observer_->SigninFailed(token_details.error()); |
95 } | 117 } |
96 } | 118 } |
97 } | 119 } |
98 break; | 120 break; |
99 default: | 121 default: |
100 NOTREACHED(); | 122 NOTREACHED(); |
101 } | 123 } |
102 } | 124 } |
103 | 125 |
104 // Called when the ProfileSyncService state changes. | 126 // Called when the ProfileSyncService state changes. |
105 void SigninTracker::OnStateChanged() { | 127 void SigninTracker::OnStateChanged() { |
106 HandleServiceStateChange(); | 128 HandleServiceStateChange(); |
107 } | 129 } |
108 | 130 |
109 void SigninTracker::HandleServiceStateChange() { | 131 void SigninTracker::HandleServiceStateChange() { |
110 if (state_ != SERVICES_INITIALIZING) { | 132 if (state_ != SERVICES_INITIALIZING) { |
| 133 LOG(WARNING) << "HandleServiceStateChange: state!=SERVICES_INITIALIZING" |
| 134 << (int)state_; |
111 // Ignore service updates until after our GAIA credentials are validated. | 135 // Ignore service updates until after our GAIA credentials are validated. |
112 return; | 136 return; |
113 } | 137 } |
114 // Wait until all of our services are logged in. For now this just means sync. | 138 // Wait until all of our services are logged in. For now this just means sync. |
115 // Long term, we should separate out service auth failures from the signin | 139 // Long term, we should separate out service auth failures from the signin |
116 // process, but for the current UI flow we'll validate service signin status | 140 // process, but for the current UI flow we'll validate service signin status |
117 // also. | 141 // also. |
118 // TODO(atwilson): Move the code to wait for app notification oauth tokens out | 142 // TODO(atwilson): Move the code to wait for app notification oauth tokens out |
119 // of ProfileSyncService and over to here (http://crbug.com/114209). | 143 // of ProfileSyncService and over to here (http://crbug.com/114209). |
120 ProfileSyncService* service = | 144 ProfileSyncService* service = |
121 ProfileSyncServiceFactory::GetForProfile(profile_); | 145 ProfileSyncServiceFactory::GetForProfile(profile_); |
122 if (service->waiting_for_auth()) { | 146 if (service->waiting_for_auth()) { |
| 147 LOG(WARNING) << "HandleServiceStateChange: service is waiting for auth"; |
123 // Still waiting for an auth token to come in so stay in the INITIALIZING | 148 // Still waiting for an auth token to come in so stay in the INITIALIZING |
124 // state (we do this to avoid triggering an early signin error in the case | 149 // state (we do this to avoid triggering an early signin error in the case |
125 // where there's a previous auth error in the sync service that hasn't | 150 // where there's a previous auth error in the sync service that hasn't |
126 // been cleared yet). | 151 // been cleared yet). |
127 return; | 152 return; |
128 } | 153 } |
129 // If we haven't loaded all our service tokens yet, just exit (we'll be called | 154 // If we haven't loaded all our service tokens yet, just exit (we'll be called |
130 // again when another token is loaded, or will transition to SigninFailed if | 155 // again when another token is loaded, or will transition to SigninFailed if |
131 // the loading fails). | 156 // the loading fails). |
132 if (!AreServiceTokensLoaded(profile_)) | 157 if (!AreServiceTokensLoaded(profile_)) { |
| 158 LOG(WARNING) << "HandleServiceStateChange: ServiceToken not loaded"; |
133 return; | 159 return; |
| 160 } |
134 if (!AreServicesSignedIn(profile_)) { | 161 if (!AreServicesSignedIn(profile_)) { |
| 162 LOG(WARNING) << "HandleServiceStateChange: not signed in to services"; |
135 state_ = WAITING_FOR_GAIA_VALIDATION; | 163 state_ = WAITING_FOR_GAIA_VALIDATION; |
136 observer_->SigninFailed(service->GetAuthError()); | 164 observer_->SigninFailed(service->GetAuthError()); |
137 } else if (service->sync_initialized()) { | 165 } else if (service->sync_initialized()) { |
| 166 LOG(WARNING) << "HandleServiceStateChange: sign in complete!"; |
138 state_ = SIGNIN_COMPLETE; | 167 state_ = SIGNIN_COMPLETE; |
139 observer_->SigninSuccess(); | 168 observer_->SigninSuccess(); |
140 } | 169 } |
141 } | 170 } |
142 | 171 |
143 // static | 172 // static |
144 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { | 173 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { |
145 // See if we have all of the tokens required. | 174 // See if we have all of the tokens required. |
146 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); | 175 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); |
147 for (int i = 0; i < kNumSignedInServices; ++i) { | 176 for (int i = 0; i < kNumSignedInServices; ++i) { |
148 if (!token_service->HasTokenForService(kSignedInServices[i])) { | 177 if (!token_service->HasTokenForService(kSignedInServices[i])) { |
| 178 LOG(WARNING) << "Token not available for service " |
| 179 << kSignedInServices[i]; |
149 // Don't have a token for one of our signed-in services. | 180 // Don't have a token for one of our signed-in services. |
150 return false; | 181 return false; |
151 } | 182 } |
152 } | 183 } |
153 return true; | 184 return true; |
154 } | 185 } |
155 | 186 |
156 // static | 187 // static |
157 bool SigninTracker::AreServicesSignedIn(Profile* profile) { | 188 bool SigninTracker::AreServicesSignedIn(Profile* profile) { |
158 if (!AreServiceTokensLoaded(profile)) | 189 if (!AreServiceTokensLoaded(profile)) |
159 return false; | 190 return false; |
160 ProfileSyncService* service = | 191 ProfileSyncService* service = |
161 ProfileSyncServiceFactory::GetForProfile(profile); | 192 ProfileSyncServiceFactory::GetForProfile(profile); |
| 193 LOG(WARNING) << "AreServicesSignedIn: AreCredentialsAvailable() : " |
| 194 << (service->AreCredentialsAvailable() ? "YES" : "NO"); |
| 195 LOG(WARNING) << "AreServicesSignedIn: GetAuthError() : " |
| 196 << (int)service->GetAuthError().state(); |
| 197 LOG(WARNING) << "AreServicesSignedIn: unrecoverable error? : " |
| 198 << (service->unrecoverable_error_detected() ? "YES" : "NO"); |
162 return (service->AreCredentialsAvailable() && | 199 return (service->AreCredentialsAvailable() && |
163 service->GetAuthError().state() == GoogleServiceAuthError::NONE && | 200 service->GetAuthError().state() == GoogleServiceAuthError::NONE && |
164 !service->unrecoverable_error_detected()); | 201 !service->unrecoverable_error_detected()); |
165 } | 202 } |
166 | |
OLD | NEW |