• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
11 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h"
13 #include "google_apis/gaia/gaia_auth_consumer.h"
14 #include "google_apis/gaia/oauth2_token_service.h"
15 
16 class GaiaAuthFetcher;
17 class Profile;
18 struct ChromeCookieDetails;
19 
20 class AccountReconcilor : public BrowserContextKeyedService,
21                                  content::NotificationObserver,
22                                  GaiaAuthConsumer,
23                                  OAuth2TokenService::Consumer,
24                                  OAuth2TokenService::Observer {
25  public:
26   explicit AccountReconcilor(Profile* profile);
27   virtual ~AccountReconcilor();
28 
29   // BrowserContextKeyedService implementation.
30   virtual void Shutdown() OVERRIDE;
31 
profile()32   Profile* profile() { return profile_; }
33 
IsPeriodicReconciliationRunning()34   bool IsPeriodicReconciliationRunning() {
35     return reconciliation_timer_.IsRunning();
36   }
37 
38  private:
39   class AccountReconcilorTest;
40 
41   // Register and unregister with dependent services.
42   void RegisterWithCookieMonster();
43   void UnregisterWithCookieMonster();
44   void RegisterWithSigninManager();
45   void UnregisterWithSigninManager();
46   void RegisterWithTokenService();
47   void UnregisterWithTokenService();
48 
49   bool IsProfileConnected();
50 
51   void DeleteAccessTokenRequests();
52 
53   // Start and stop the periodic reconciliation.
54   void StartPeriodicReconciliation();
55   void StopPeriodicReconciliation();
56   void PeriodicReconciliation();
57 
58   void PerformMergeAction(const std::string& account_id);
59   void PerformRemoveAction(const std::string& account_id);
60   void StartReconcileAction();
61   void FinishReconcileAction();
62 
63   void GetAccountsFromCookie();
64   void ValidateAccountsFromTokenService();
65 
66   void OnCookieChanged(ChromeCookieDetails* details);
67 
68   // Overriden from content::NotificationObserver
69   virtual void Observe(int type,
70                        const content::NotificationSource& source,
71                        const content::NotificationDetails& details) OVERRIDE;
72 
73   // Overriden from OAuth2TokenService::Consumer
74   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
75                                  const std::string& access_token,
76                                  const base::Time& expiration_time) OVERRIDE;
77   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
78                                  const GoogleServiceAuthError& error) OVERRIDE;
79 
80   // Overriden from OAuth2TokenService::Observer
81   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
82   virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
83   virtual void OnRefreshTokensLoaded() OVERRIDE;
84 
85   // Overriden from GaiaAuthConsumer
86   virtual void OnListAccountsSuccess(const std::string& data) OVERRIDE;
87   virtual void OnListAccountsFailure(
88       const GoogleServiceAuthError& error) OVERRIDE;
89 
90   // The profile that this reconcilor belongs to.
91   Profile* profile_;
92   content::NotificationRegistrar registrar_;
93   base::RepeatingTimer<AccountReconcilor> reconciliation_timer_;
94 
95   // Used during reconcile action.
96   // These members are used used to validate the gaia cookie.
97   scoped_ptr<GaiaAuthFetcher> gaia_fetcher_;
98   bool are_gaia_accounts_set_;
99   std::vector<std::string> gaia_accounts_;
100 
101   // Used during reconcile action.
102   // These members are used to validate the tokens in OAuth2TokenService.
103   std::string primary_account_;
104   std::vector<std::string> chrome_accounts_;
105   scoped_ptr<OAuth2TokenService::Request>* requests_;
106   std::set<std::string> valid_chrome_accounts_;
107   std::set<std::string> invalid_chrome_accounts_;
108 
109   DISALLOW_COPY_AND_ASSIGN(AccountReconcilor);
110 };
111 
112 #endif  // CHROME_BROWSER_SIGNIN_ACCOUNT_RECONCILOR_H_
113