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 #ifndef CHROME_BROWSER_SIGNIN_ACCOUNT_RECONCILOR_H_
5 #define CHROME_BROWSER_SIGNIN_ACCOUNT_RECONCILOR_H_
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/scoped_vector.h"
11 #include "chrome/browser/signin/google_auto_login_helper.h"
12 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "google_apis/gaia/gaia_auth_consumer.h"
16 #include "google_apis/gaia/oauth2_token_service.h"
18 class GaiaAuthFetcher
;
20 struct ChromeCookieDetails
;
22 class AccountReconcilor
: public BrowserContextKeyedService
,
23 public content::NotificationObserver
,
24 public GaiaAuthConsumer
,
25 public OAuth2TokenService::Consumer
,
26 public OAuth2TokenService::Observer
{
28 explicit AccountReconcilor(Profile
* profile
);
29 virtual ~AccountReconcilor();
31 // BrowserContextKeyedService implementation.
32 virtual void Shutdown() OVERRIDE
;
34 // Add or remove observers for the merge session notification.
35 void AddMergeSessionObserver(GoogleAutoLoginHelper::Observer
* observer
);
36 void RemoveMergeSessionObserver(GoogleAutoLoginHelper::Observer
* observer
);
38 Profile
* profile() { return profile_
; }
40 bool IsPeriodicReconciliationRunning() const {
41 return reconciliation_timer_
.IsRunning();
44 bool IsRegisteredWithTokenService() const {
45 return registered_with_token_service_
;
48 bool AreGaiaAccountsSet() const { return are_gaia_accounts_set_
; }
50 bool AreAllRefreshTokensChecked() const;
52 const std::vector
<std::string
>& GetGaiaAccountsForTesting() const {
53 return gaia_accounts_
;
56 const std::set
<std::string
>& GetValidChromeAccountsForTesting() const {
57 return valid_chrome_accounts_
;
60 const std::set
<std::string
>& GetInvalidChromeAccountsForTesting() const {
61 return invalid_chrome_accounts_
;
65 class AccountReconcilorTest
;
66 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, GetAccountsFromCookieSuccess
);
67 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, GetAccountsFromCookieFailure
);
68 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, ValidateAccountsFromTokens
);
69 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
70 ValidateAccountsFromTokensFailedUserInfo
);
71 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
,
72 ValidateAccountsFromTokensFailedTokenRequest
);
73 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest
, StartReconcileAction
);
77 // Register and unregister with dependent services.
78 void RegisterWithCookieMonster();
79 void UnregisterWithCookieMonster();
80 void RegisterWithSigninManager();
81 void UnregisterWithSigninManager();
82 void RegisterWithTokenService();
83 void UnregisterWithTokenService();
85 bool IsProfileConnected();
87 void DeleteAccessTokenRequestsAndUserIdFetchers();
89 // Start and stop the periodic reconciliation.
90 void StartPeriodicReconciliation();
91 void StopPeriodicReconciliation();
92 void PeriodicReconciliation();
94 void PerformMergeAction(const std::string
& account_id
);
95 void PerformRemoveAction(const std::string
& account_id
);
97 // Used during period reconciliation.
98 void StartReconcileAction();
99 void FinishReconcileAction();
100 void HandleSuccessfulAccountIdCheck(const std::string
& account_id
);
101 void HandleFailedAccountIdCheck(const std::string
& account_id
);
103 void GetAccountsFromCookie();
104 void ValidateAccountsFromTokenService();
106 void OnCookieChanged(ChromeCookieDetails
* details
);
108 // Overriden from content::NotificationObserver.
109 virtual void Observe(int type
,
110 const content::NotificationSource
& source
,
111 const content::NotificationDetails
& details
) OVERRIDE
;
113 // Overriden from OAuth2TokenService::Consumer.
114 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
115 const std::string
& access_token
,
116 const base::Time
& expiration_time
) OVERRIDE
;
117 virtual void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
118 const GoogleServiceAuthError
& error
) OVERRIDE
;
120 // Overriden from OAuth2TokenService::Observer.
121 virtual void OnRefreshTokenAvailable(const std::string
& account_id
) OVERRIDE
;
122 virtual void OnRefreshTokenRevoked(const std::string
& account_id
) OVERRIDE
;
123 virtual void OnRefreshTokensLoaded() OVERRIDE
;
125 // Overriden from GaiaAuthConsumer.
126 virtual void OnListAccountsSuccess(const std::string
& data
) OVERRIDE
;
127 virtual void OnListAccountsFailure(
128 const GoogleServiceAuthError
& error
) OVERRIDE
;
130 // The profile that this reconcilor belongs to.
132 content::NotificationRegistrar registrar_
;
133 base::RepeatingTimer
<AccountReconcilor
> reconciliation_timer_
;
134 GoogleAutoLoginHelper merge_session_helper_
;
135 bool registered_with_token_service_
;
137 // Used during reconcile action.
138 // These members are used used to validate the gaia cookie.
139 scoped_ptr
<GaiaAuthFetcher
> gaia_fetcher_
;
140 bool are_gaia_accounts_set_
;
141 std::vector
<std::string
> gaia_accounts_
;
143 // Used during reconcile action.
144 // These members are used to validate the tokens in OAuth2TokenService.
145 std::string primary_account_
;
146 std::vector
<std::string
> chrome_accounts_
;
147 scoped_ptr
<OAuth2TokenService::Request
>* requests_
;
148 ScopedVector
<UserIdFetcher
> user_id_fetchers_
;
149 std::set
<std::string
> valid_chrome_accounts_
;
150 std::set
<std::string
> invalid_chrome_accounts_
;
152 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor
);
155 #endif // CHROME_BROWSER_SIGNIN_ACCOUNT_RECONCILOR_H_