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 ProfileSyncServiceFactory::GetForProfile(profile_)->RemoveObserver(this); |
| 45 } |
| 46 |
| 47 void SigninTracker::Initialize() { |
30 DCHECK(observer_); | 48 DCHECK(observer_); |
31 // Register for notifications from the SigninManager. | 49 // Register for notifications from the SigninManager. |
32 registrar_.Add(this, | 50 registrar_.Add(this, |
33 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 51 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
34 content::Source<Profile>(profile_)); | 52 content::Source<Profile>(profile_)); |
35 registrar_.Add(this, | 53 registrar_.Add(this, |
36 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 54 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
37 content::Source<Profile>(profile_)); | 55 content::Source<Profile>(profile_)); |
38 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 56 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
39 registrar_.Add(this, | 57 registrar_.Add(this, |
40 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 58 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
41 content::Source<TokenService>(token_service)); | 59 content::Source<TokenService>(token_service)); |
42 registrar_.Add(this, | 60 registrar_.Add(this, |
43 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 61 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
44 content::Source<TokenService>(token_service)); | 62 content::Source<TokenService>(token_service)); |
45 | 63 |
46 // Also listen for notifications from the various signed in services (only | 64 // Also listen for notifications from the various signed in services (only |
47 // sync for now). | 65 // sync for now). |
48 ProfileSyncService* service = | 66 ProfileSyncService* service = |
49 ProfileSyncServiceFactory::GetForProfile(profile_); | 67 ProfileSyncServiceFactory::GetForProfile(profile_); |
50 service->AddObserver(this); | 68 service->AddObserver(this); |
51 } | |
52 | 69 |
53 SigninTracker::~SigninTracker() { | 70 if (state_ == SERVICES_INITIALIZING) |
54 ProfileSyncService* service = | 71 HandleServiceStateChange(); |
55 ProfileSyncServiceFactory::GetForProfile(profile_); | |
56 service->RemoveObserver(this); | |
57 } | 72 } |
58 | 73 |
59 void SigninTracker::Observe(int type, | 74 void SigninTracker::Observe(int type, |
60 const content::NotificationSource& source, | 75 const content::NotificationSource& source, |
61 const content::NotificationDetails& details) { | 76 const content::NotificationDetails& details) { |
62 // We should not get more than one of these notifications. | 77 // We should not get more than one of these notifications. |
63 switch (type) { | 78 switch (type) { |
64 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: | 79 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: |
65 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 80 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
66 state_ = SERVICES_INITIALIZING; | 81 state_ = SERVICES_INITIALIZING; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // static | 171 // static |
157 bool SigninTracker::AreServicesSignedIn(Profile* profile) { | 172 bool SigninTracker::AreServicesSignedIn(Profile* profile) { |
158 if (!AreServiceTokensLoaded(profile)) | 173 if (!AreServiceTokensLoaded(profile)) |
159 return false; | 174 return false; |
160 ProfileSyncService* service = | 175 ProfileSyncService* service = |
161 ProfileSyncServiceFactory::GetForProfile(profile); | 176 ProfileSyncServiceFactory::GetForProfile(profile); |
162 return (service->AreCredentialsAvailable() && | 177 return (service->AreCredentialsAvailable() && |
163 service->GetAuthError().state() == GoogleServiceAuthError::NONE && | 178 service->GetAuthError().state() == GoogleServiceAuthError::NONE && |
164 !service->unrecoverable_error_detected()); | 179 !service->unrecoverable_error_detected()); |
165 } | 180 } |
166 | |
OLD | NEW |