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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/login/auth_sync_observer.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
9 #include "chrome/browser/chromeos/login/user_manager.h"
10 #include "chrome/browser/sync/profile_sync_service.h"
11 #include "chrome/browser/sync/profile_sync_service_factory.h"
12 #include "chrome/common/pref_names.h"
13 #include "google_apis/gaia/gaia_auth_util.h"
16 class ProfileSyncService;
20 AuthSyncObserver::AuthSyncObserver(Profile* profile)
24 AuthSyncObserver::~AuthSyncObserver() {
27 void AuthSyncObserver::StartObserving() {
28 ProfileSyncService* sync_service =
29 ProfileSyncServiceFactory::GetForProfile(profile_);
31 sync_service->AddObserver(this);
34 void AuthSyncObserver::Shutdown() {
35 ProfileSyncService* sync_service =
36 ProfileSyncServiceFactory::GetForProfile(profile_);
38 sync_service->RemoveObserver(this);
41 void AuthSyncObserver::OnStateChanged() {
42 DCHECK(UserManager::Get()->IsLoggedInAsRegularUser() ||
43 UserManager::Get()->IsLoggedInAsLocallyManagedUser());
44 ProfileSyncService* sync_service =
45 ProfileSyncServiceFactory::GetForProfile(profile_);
46 GoogleServiceAuthError::State state =
47 sync_service->GetAuthError().state();
48 if (state != GoogleServiceAuthError::NONE &&
49 state != GoogleServiceAuthError::CONNECTION_FAILED &&
50 state != GoogleServiceAuthError::SERVICE_UNAVAILABLE &&
51 state != GoogleServiceAuthError::REQUEST_CANCELED) {
52 // 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.
54 // TODO(nkostylev): Remove after crosbug.com/25978 is implemented.
55 LOG(WARNING) << "Invalidate OAuth token because of a sync error: "
56 << sync_service->GetAuthError().ToString();
57 std::string email = profile_->GetProfileName();
58 if (email.empty() && UserManager::Get()->IsLoggedInAsLocallyManagedUser()) {
60 profile_->GetPrefs()->GetString(prefs::kManagedUserId);
62 UserManager::Get()->GetSupervisedUserManager()->FindBySyncId(sync_id);
64 email = user->email();
66 DCHECK(!email.empty());
67 // TODO(nkostyelv): Change observer after active user has changed.
68 UserManager::Get()->SaveUserOAuthStatus(
69 gaia::CanonicalizeEmail(email),
70 User::OAUTH2_TOKEN_STATUS_INVALID);
74 } // namespace chromeos