Upstream version 5.34.104.0
[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/metrics/user_metrics.h"
8 #include "base/metrics/user_metrics_action.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/sync/profile_sync_service.h"
13 #include "chrome/browser/sync/profile_sync_service_factory.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "google_apis/gaia/gaia_auth_util.h"
17
18 class Profile;
19 class ProfileSyncService;
20
21 namespace chromeos {
22
23 AuthSyncObserver::AuthSyncObserver(Profile* profile)
24     : profile_(profile) {
25 }
26
27 AuthSyncObserver::~AuthSyncObserver() {
28 }
29
30 void AuthSyncObserver::StartObserving() {
31   ProfileSyncService* sync_service =
32       ProfileSyncServiceFactory::GetForProfile(profile_);
33   if (sync_service)
34     sync_service->AddObserver(this);
35 }
36
37 void AuthSyncObserver::Shutdown() {
38   ProfileSyncService* sync_service =
39       ProfileSyncServiceFactory::GetForProfile(profile_);
40   if (sync_service)
41     sync_service->RemoveObserver(this);
42 }
43
44 void AuthSyncObserver::OnStateChanged() {
45   DCHECK(UserManager::Get()->IsLoggedInAsRegularUser() ||
46          UserManager::Get()->IsLoggedInAsLocallyManagedUser());
47   ProfileSyncService* sync_service =
48       ProfileSyncServiceFactory::GetForProfile(profile_);
49   User* user = UserManager::Get()->GetUserByProfile(profile_);
50   GoogleServiceAuthError::State state =
51       sync_service->GetAuthError().state();
52   if (state != GoogleServiceAuthError::NONE &&
53       state != GoogleServiceAuthError::CONNECTION_FAILED &&
54       state != GoogleServiceAuthError::SERVICE_UNAVAILABLE &&
55       state != GoogleServiceAuthError::REQUEST_CANCELED) {
56     // Invalidate OAuth2 refresh token to force Gaia sign-in flow. This is
57     // needed because sign-out/sign-in solution is suggested to the user.
58     // TODO(nkostylev): Remove after crosbug.com/25978 is implemented.
59     LOG(WARNING) << "Invalidate OAuth token because of a sync error: "
60                  << sync_service->GetAuthError().ToString();
61     std::string email = user->email();
62     DCHECK(!email.empty());
63     // TODO(nkostyelv): Change observer after active user has changed.
64     User::OAuthTokenStatus old_status = user->oauth_token_status();
65     UserManager::Get()->SaveUserOAuthStatus(email,
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     }
88   }
89 }
90
91 void AuthSyncObserver::OnSupervisedTokenLoaded(const std::string& token) {
92   UserManager::Get()->GetSupervisedUserManager()->ConfigureSyncWithToken(
93       profile_, token);
94 }
95
96 }  // namespace chromeos