- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / auth_sync_observer.cc
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.
4
5 #include "chrome/browser/chromeos/login/auth_sync_observer.h"
6
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"
14
15 class Profile;
16 class ProfileSyncService;
17
18 namespace chromeos {
19
20 AuthSyncObserver::AuthSyncObserver(Profile* profile)
21     : profile_(profile) {
22 }
23
24 AuthSyncObserver::~AuthSyncObserver() {
25 }
26
27 void AuthSyncObserver::StartObserving() {
28   ProfileSyncService* sync_service =
29       ProfileSyncServiceFactory::GetForProfile(profile_);
30   if (sync_service)
31     sync_service->AddObserver(this);
32 }
33
34 void AuthSyncObserver::Shutdown() {
35   ProfileSyncService* sync_service =
36       ProfileSyncServiceFactory::GetForProfile(profile_);
37   if (sync_service)
38     sync_service->RemoveObserver(this);
39 }
40
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()) {
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());
67     // TODO(nkostyelv): Change observer after active user has changed.
68     UserManager::Get()->SaveUserOAuthStatus(
69         gaia::CanonicalizeEmail(email),
70         User::OAUTH2_TOKEN_STATUS_INVALID);
71   }
72 }
73
74 }  // namespace chromeos