| OLD | NEW |
| (Empty) |
| 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_ | |
| 6 | |
| 7 #include <deque> | |
| 8 #include <functional> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/callback_forward.h" | |
| 16 #include "base/compiler_specific.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/scoped_vector.h" | |
| 19 #include "components/keyed_service/core/keyed_service.h" | |
| 20 #include "components/signin/core/browser/signin_manager.h" | |
| 21 #include "google_apis/gaia/gaia_auth_consumer.h" | |
| 22 #include "google_apis/gaia/google_service_auth_error.h" | |
| 23 #include "google_apis/gaia/merge_session_helper.h" | |
| 24 #include "google_apis/gaia/oauth2_token_service.h" | |
| 25 | |
| 26 class GaiaAuthFetcher; | |
| 27 class Profile; | |
| 28 class SigninClient; | |
| 29 class SigninOAuthHelper; | |
| 30 | |
| 31 namespace net { | |
| 32 class CanonicalCookie; | |
| 33 } | |
| 34 | |
| 35 class AccountReconcilor : public KeyedService, | |
| 36 public GaiaAuthConsumer, | |
| 37 public MergeSessionHelper::Observer, | |
| 38 public OAuth2TokenService::Consumer, | |
| 39 public OAuth2TokenService::Observer, | |
| 40 public SigninManagerBase::Observer { | |
| 41 public: | |
| 42 explicit AccountReconcilor(Profile* profile, SigninClient* client); | |
| 43 virtual ~AccountReconcilor(); | |
| 44 | |
| 45 void Initialize(bool start_reconcile_if_tokens_available); | |
| 46 | |
| 47 // KeyedService implementation. | |
| 48 virtual void Shutdown() OVERRIDE; | |
| 49 | |
| 50 // Add or remove observers for the merge session notification. | |
| 51 void AddMergeSessionObserver(MergeSessionHelper::Observer* observer); | |
| 52 void RemoveMergeSessionObserver(MergeSessionHelper::Observer* observer); | |
| 53 | |
| 54 Profile* profile() { return profile_; } | |
| 55 | |
| 56 private: | |
| 57 // An std::set<> for use with email addresses that uses | |
| 58 // gaia::CanonicalizeEmail() during comparisons. | |
| 59 // TODO(rogerta): this is a workaround for the fact that SigninManager and | |
| 60 // SigninOAuthHelper use the gaia "email" property when adding accounts to | |
| 61 // the token service, whereas gaia::ParseListAccountsData() returns email | |
| 62 // addresses that have been passed through gaia::CanonicalizeEmail(). These | |
| 63 // two types of email addresses are not directly comparable. | |
| 64 class EmailLessFunc : public std::less<std::string> { | |
| 65 public: | |
| 66 bool operator()(const std::string& s1, const std::string& s2) const; | |
| 67 }; | |
| 68 typedef std::set<std::string, EmailLessFunc> EmailSet; | |
| 69 | |
| 70 class RefreshTokenFetcher; | |
| 71 class UserIdFetcher; | |
| 72 | |
| 73 bool IsPeriodicReconciliationRunning() const { | |
| 74 return reconciliation_timer_.IsRunning(); | |
| 75 } | |
| 76 | |
| 77 bool IsRegisteredWithTokenService() const { | |
| 78 return registered_with_token_service_; | |
| 79 } | |
| 80 | |
| 81 bool AreGaiaAccountsSet() const { return are_gaia_accounts_set_; } | |
| 82 | |
| 83 bool AreAllRefreshTokensChecked() const; | |
| 84 | |
| 85 const std::vector<std::pair<std::string, bool> >& | |
| 86 GetGaiaAccountsForTesting() const { | |
| 87 return gaia_accounts_; | |
| 88 } | |
| 89 | |
| 90 const EmailSet& GetValidChromeAccountsForTesting() const { | |
| 91 return valid_chrome_accounts_; | |
| 92 } | |
| 93 | |
| 94 const EmailSet& GetInvalidChromeAccountsForTesting() const { | |
| 95 return invalid_chrome_accounts_; | |
| 96 } | |
| 97 | |
| 98 // Used during GetAccountsFromCookie. | |
| 99 // Stores a callback for the next action to perform. | |
| 100 typedef base::Callback<void( | |
| 101 const GoogleServiceAuthError& error, | |
| 102 const std::vector<std::pair<std::string, bool> >&)> | |
| 103 GetAccountsFromCookieCallback; | |
| 104 | |
| 105 friend class AccountReconcilorTest; | |
| 106 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, SigninManagerRegistration); | |
| 107 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, Reauth); | |
| 108 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, ProfileAlreadyConnected); | |
| 109 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, GetAccountsFromCookieSuccess); | |
| 110 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, GetAccountsFromCookieFailure); | |
| 111 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, ValidateAccountsFromTokens); | |
| 112 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | |
| 113 ValidateAccountsFromTokensFailedUserInfo); | |
| 114 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | |
| 115 ValidateAccountsFromTokensFailedTokenRequest); | |
| 116 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoop); | |
| 117 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoopWithDots); | |
| 118 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileNoopMultiple); | |
| 119 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileAddToCookie); | |
| 120 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileAddToChrome); | |
| 121 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileBadPrimary); | |
| 122 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, StartReconcileOnlyOnce); | |
| 123 FRIEND_TEST_ALL_PREFIXES(AccountReconcilorTest, | |
| 124 StartReconcileWithSessionInfoExpiredDefault); | |
| 125 | |
| 126 // Register and unregister with dependent services. | |
| 127 void RegisterForCookieChanges(); | |
| 128 void UnregisterForCookieChanges(); | |
| 129 void RegisterWithSigninManager(); | |
| 130 void UnregisterWithSigninManager(); | |
| 131 void RegisterWithTokenService(); | |
| 132 void UnregisterWithTokenService(); | |
| 133 | |
| 134 bool IsProfileConnected(); | |
| 135 | |
| 136 void DeleteFetchers(); | |
| 137 | |
| 138 // Start and stop the periodic reconciliation. | |
| 139 void StartPeriodicReconciliation(); | |
| 140 void StopPeriodicReconciliation(); | |
| 141 void PeriodicReconciliation(); | |
| 142 | |
| 143 // All actions with side effects. Virtual so that they can be overridden | |
| 144 // in tests. | |
| 145 virtual void PerformMergeAction(const std::string& account_id); | |
| 146 virtual void PerformAddToChromeAction(const std::string& account_id, | |
| 147 int session_index); | |
| 148 virtual void PerformLogoutAllAccountsAction(); | |
| 149 | |
| 150 // Used to remove an account from chrome and the cookie jar. | |
| 151 virtual void StartRemoveAction(const std::string& account_id); | |
| 152 virtual void FinishRemoveAction( | |
| 153 const std::string& account_id, | |
| 154 const GoogleServiceAuthError& error, | |
| 155 const std::vector<std::pair<std::string, bool> >& accounts); | |
| 156 | |
| 157 // Used during periodic reconciliation. | |
| 158 void StartReconcile(); | |
| 159 void FinishReconcile(); | |
| 160 void AbortReconcile(); | |
| 161 void CalculateIfReconcileIsDone(); | |
| 162 void ScheduleStartReconcileIfChromeAccountsChanged(); | |
| 163 void HandleSuccessfulAccountIdCheck(const std::string& account_id); | |
| 164 void HandleFailedAccountIdCheck(const std::string& account_id); | |
| 165 void HandleRefreshTokenFetched(const std::string& account_id, | |
| 166 const std::string& refresh_token); | |
| 167 | |
| 168 void GetAccountsFromCookie(GetAccountsFromCookieCallback callback); | |
| 169 void ContinueReconcileActionAfterGetGaiaAccounts( | |
| 170 const GoogleServiceAuthError& error, | |
| 171 const std::vector<std::pair<std::string, bool> >& accounts); | |
| 172 void ValidateAccountsFromTokenService(); | |
| 173 | |
| 174 void OnCookieChanged(const net::CanonicalCookie* cookie); | |
| 175 | |
| 176 // Overriden from GaiaAuthConsumer. | |
| 177 virtual void OnListAccountsSuccess(const std::string& data) OVERRIDE; | |
| 178 virtual void OnListAccountsFailure( | |
| 179 const GoogleServiceAuthError& error) OVERRIDE; | |
| 180 | |
| 181 // Overriden from MergeSessionHelper::Observer. | |
| 182 virtual void MergeSessionCompleted( | |
| 183 const std::string& account_id, | |
| 184 const GoogleServiceAuthError& error) OVERRIDE; | |
| 185 | |
| 186 // Overriden from OAuth2TokenService::Consumer. | |
| 187 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 188 const std::string& access_token, | |
| 189 const base::Time& expiration_time) OVERRIDE; | |
| 190 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 191 const GoogleServiceAuthError& error) OVERRIDE; | |
| 192 | |
| 193 // Overriden from OAuth2TokenService::Observer. | |
| 194 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | |
| 195 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; | |
| 196 virtual void OnRefreshTokensLoaded() OVERRIDE; | |
| 197 | |
| 198 // Overriden from SigninManagerBase::Observer. | |
| 199 virtual void GoogleSigninSucceeded(const std::string& username, | |
| 200 const std::string& password) OVERRIDE; | |
| 201 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; | |
| 202 | |
| 203 void MayBeDoNextListAccounts(); | |
| 204 | |
| 205 // The profile that this reconcilor belongs to. | |
| 206 Profile* profile_; | |
| 207 | |
| 208 // The SigninClient associated with this reconcilor. | |
| 209 SigninClient* client_; | |
| 210 | |
| 211 base::RepeatingTimer<AccountReconcilor> reconciliation_timer_; | |
| 212 MergeSessionHelper merge_session_helper_; | |
| 213 scoped_ptr<GaiaAuthFetcher> gaia_fetcher_; | |
| 214 bool registered_with_token_service_; | |
| 215 | |
| 216 // True while the reconcilor is busy checking or managing the accounts in | |
| 217 // this profile. | |
| 218 bool is_reconcile_started_; | |
| 219 | |
| 220 // Used during reconcile action. | |
| 221 // These members are used used to validate the gaia cookie. |gaia_accounts_| | |
| 222 // holds the state of google accounts in the gaia cookie. Each element is | |
| 223 // a pair that holds the email address of the account and a boolean that | |
| 224 // indicates whether the account is valid or not. The accounts in the vector | |
| 225 // are ordered the in same way as the gaia cookie. | |
| 226 bool are_gaia_accounts_set_; | |
| 227 std::vector<std::pair<std::string, bool> > gaia_accounts_; | |
| 228 | |
| 229 // Used during reconcile action. | |
| 230 // These members are used to validate the tokens in OAuth2TokenService. | |
| 231 std::string primary_account_; | |
| 232 std::vector<std::string> chrome_accounts_; | |
| 233 scoped_ptr<OAuth2TokenService::Request>* requests_; | |
| 234 ScopedVector<UserIdFetcher> user_id_fetchers_; | |
| 235 ScopedVector<SigninOAuthHelper> refresh_token_fetchers_; | |
| 236 EmailSet valid_chrome_accounts_; | |
| 237 EmailSet invalid_chrome_accounts_; | |
| 238 std::vector<std::string> add_to_cookie_; | |
| 239 std::vector<std::pair<std::string, int> > add_to_chrome_; | |
| 240 | |
| 241 std::deque<GetAccountsFromCookieCallback> get_gaia_accounts_callbacks_; | |
| 242 | |
| 243 DISALLOW_COPY_AND_ASSIGN(AccountReconcilor); | |
| 244 }; | |
| 245 | |
| 246 #endif // CHROME_BROWSER_SIGNIN_ACCOUNT_RECONCILOR_H_ | |
| OLD | NEW |