OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/chromeos/login/auth_sync_observer.h" | 5 #include "chrome/browser/chromeos/login/auth_sync_observer.h" |
6 | 6 |
| 7 #include "base/metrics/user_metrics.h" |
| 8 #include "base/metrics/user_metrics_action.h" |
7 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/chromeos/login/supervised_user_manager.h" | 10 #include "chrome/browser/chromeos/login/supervised_user_manager.h" |
9 #include "chrome/browser/chromeos/login/user_manager.h" | 11 #include "chrome/browser/chromeos/login/user_manager.h" |
10 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
11 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
12 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "content/public/browser/user_metrics.h" |
13 #include "google_apis/gaia/gaia_auth_util.h" | 16 #include "google_apis/gaia/gaia_auth_util.h" |
14 | 17 |
15 class Profile; | 18 class Profile; |
16 class ProfileSyncService; | 19 class ProfileSyncService; |
17 | 20 |
18 namespace chromeos { | 21 namespace chromeos { |
19 | 22 |
20 AuthSyncObserver::AuthSyncObserver(Profile* profile) | 23 AuthSyncObserver::AuthSyncObserver(Profile* profile) |
21 : profile_(profile) { | 24 : profile_(profile) { |
22 } | 25 } |
(...skipping 13 matching lines...) Expand all Loading... |
36 ProfileSyncServiceFactory::GetForProfile(profile_); | 39 ProfileSyncServiceFactory::GetForProfile(profile_); |
37 if (sync_service) | 40 if (sync_service) |
38 sync_service->RemoveObserver(this); | 41 sync_service->RemoveObserver(this); |
39 } | 42 } |
40 | 43 |
41 void AuthSyncObserver::OnStateChanged() { | 44 void AuthSyncObserver::OnStateChanged() { |
42 DCHECK(UserManager::Get()->IsLoggedInAsRegularUser() || | 45 DCHECK(UserManager::Get()->IsLoggedInAsRegularUser() || |
43 UserManager::Get()->IsLoggedInAsLocallyManagedUser()); | 46 UserManager::Get()->IsLoggedInAsLocallyManagedUser()); |
44 ProfileSyncService* sync_service = | 47 ProfileSyncService* sync_service = |
45 ProfileSyncServiceFactory::GetForProfile(profile_); | 48 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 49 User* user = UserManager::Get()->GetUserByProfile(profile_); |
46 GoogleServiceAuthError::State state = | 50 GoogleServiceAuthError::State state = |
47 sync_service->GetAuthError().state(); | 51 sync_service->GetAuthError().state(); |
48 if (state != GoogleServiceAuthError::NONE && | 52 if (state != GoogleServiceAuthError::NONE && |
49 state != GoogleServiceAuthError::CONNECTION_FAILED && | 53 state != GoogleServiceAuthError::CONNECTION_FAILED && |
50 state != GoogleServiceAuthError::SERVICE_UNAVAILABLE && | 54 state != GoogleServiceAuthError::SERVICE_UNAVAILABLE && |
51 state != GoogleServiceAuthError::REQUEST_CANCELED) { | 55 state != GoogleServiceAuthError::REQUEST_CANCELED) { |
52 // Invalidate OAuth2 refresh token to force Gaia sign-in flow. This is | 56 // Invalidate OAuth2 refresh token to force Gaia sign-in flow. This is |
53 // needed because sign-out/sign-in solution is suggested to the user. | 57 // needed because sign-out/sign-in solution is suggested to the user. |
54 // TODO(nkostylev): Remove after crosbug.com/25978 is implemented. | 58 // TODO(nkostylev): Remove after crosbug.com/25978 is implemented. |
55 LOG(WARNING) << "Invalidate OAuth token because of a sync error: " | 59 LOG(WARNING) << "Invalidate OAuth token because of a sync error: " |
56 << sync_service->GetAuthError().ToString(); | 60 << sync_service->GetAuthError().ToString(); |
57 std::string email = profile_->GetProfileName(); | 61 std::string email = user->email(); |
58 if (email.empty() && UserManager::Get()->IsLoggedInAsLocallyManagedUser()) { | |
59 std::string sync_id = | |
60 profile_->GetPrefs()->GetString(prefs::kManagedUserId); | |
61 const User* user = | |
62 UserManager::Get()->GetSupervisedUserManager()->FindBySyncId(sync_id); | |
63 if (user) | |
64 email = user->email(); | |
65 } | |
66 DCHECK(!email.empty()); | 62 DCHECK(!email.empty()); |
67 // TODO(nkostyelv): Change observer after active user has changed. | 63 // TODO(nkostyelv): Change observer after active user has changed. |
68 UserManager::Get()->SaveUserOAuthStatus( | 64 User::OAuthTokenStatus old_status = user->oauth_token_status(); |
69 gaia::CanonicalizeEmail(email), | 65 UserManager::Get()->SaveUserOAuthStatus(email, |
70 User::OAUTH2_TOKEN_STATUS_INVALID); | 66 User::OAUTH2_TOKEN_STATUS_INVALID); |
| 67 if (user->GetType() == User::USER_TYPE_LOCALLY_MANAGED && |
| 68 old_status != User::OAUTH2_TOKEN_STATUS_INVALID) { |
| 69 // Attempt to restore token from file. |
| 70 UserManager::Get()->GetSupervisedUserManager()->LoadSupervisedUserToken( |
| 71 profile_, |
| 72 base::Bind(&AuthSyncObserver::OnSupervisedTokenLoaded, |
| 73 base::Unretained(this))); |
| 74 content::RecordAction( |
| 75 base::UserMetricsAction("ManagedUsers_Chromeos_Sync_Invalidated")); |
| 76 } |
| 77 } else if (state == GoogleServiceAuthError::NONE) { |
| 78 if (user->GetType() == User::USER_TYPE_LOCALLY_MANAGED && |
| 79 user->oauth_token_status() == User::OAUTH2_TOKEN_STATUS_INVALID) { |
| 80 LOG(ERROR) << |
| 81 "Got an incorrectly invalidated token case, restoring token status."; |
| 82 UserManager::Get()->SaveUserOAuthStatus( |
| 83 user->email(), |
| 84 User::OAUTH2_TOKEN_STATUS_VALID); |
| 85 content::RecordAction( |
| 86 base::UserMetricsAction("ManagedUsers_Chromeos_Sync_Recovered")); |
| 87 } |
71 } | 88 } |
72 } | 89 } |
73 | 90 |
| 91 void AuthSyncObserver::OnSupervisedTokenLoaded(const std::string& token) { |
| 92 UserManager::Get()->GetSupervisedUserManager()->ConfigureSyncWithToken( |
| 93 profile_, token); |
| 94 } |
| 95 |
74 } // namespace chromeos | 96 } // namespace chromeos |
OLD | NEW |