OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_ | 5 #ifndef GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_ | 6 #define GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
| 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
13 #include "components/signin/core/browser/signin_error_controller.h" | |
14 #include "components/signin/core/browser/signin_manager_base.h" | |
15 #include "google_apis/gaia/gaia_oauth_client.h" | 14 #include "google_apis/gaia/gaia_oauth_client.h" |
| 15 #include "google_apis/gaia/identity_provider.h" |
16 #include "google_apis/gaia/oauth2_token_service.h" | 16 #include "google_apis/gaia/oauth2_token_service.h" |
17 | 17 |
18 class GoogleServiceAuthError; | 18 class GoogleServiceAuthError; |
19 class Profile; | |
20 | 19 |
21 namespace extensions { | 20 namespace net { |
| 21 class URLRequestContextGetter; |
| 22 } |
| 23 |
| 24 namespace gaia { |
22 | 25 |
23 struct AccountIds { | 26 struct AccountIds { |
24 std::string account_key; // The account ID used by OAuth2TokenService. | 27 std::string account_key; // The account ID used by OAuth2TokenService. |
25 std::string gaia; | 28 std::string gaia; |
26 std::string email; | 29 std::string email; |
27 }; | 30 }; |
28 | 31 |
29 class AccountIdFetcher; | 32 class AccountIdFetcher; |
30 | 33 |
31 // The AccountTracker keeps track of what accounts exist on the | 34 // The AccountTracker keeps track of what accounts exist on the |
32 // profile and the state of their credentials. The tracker fetches the | 35 // profile and the state of their credentials. The tracker fetches the |
33 // gaia ID of each account it knows about. | 36 // gaia ID of each account it knows about. |
34 // | 37 // |
35 // The AccountTracker maintains these invariants: | 38 // The AccountTracker maintains these invariants: |
36 // 1. Events are only fired after the gaia ID has been fetched. | 39 // 1. Events are only fired after the gaia ID has been fetched. |
37 // 2. Add/Remove and SignIn/SignOut pairs are always generated in order. | 40 // 2. Add/Remove and SignIn/SignOut pairs are always generated in order. |
38 // 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove. | 41 // 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove. |
39 // 4. If there is no primary account, there are no other accounts. | 42 // 4. If there is no primary account, there are no other accounts. |
40 class AccountTracker : public OAuth2TokenService::Observer, | 43 class AccountTracker : public OAuth2TokenService::Observer, |
41 public SigninErrorController::AuthStatusProvider, | 44 public IdentityProvider::Observer { |
42 public SigninManagerBase::Observer { | |
43 public: | 45 public: |
44 explicit AccountTracker(Profile* profile); | 46 AccountTracker(IdentityProvider* identity_provider, |
| 47 net::URLRequestContextGetter* request_context_getter); |
45 virtual ~AccountTracker(); | 48 virtual ~AccountTracker(); |
46 | 49 |
47 class Observer { | 50 class Observer { |
48 public: | 51 public: |
49 virtual void OnAccountAdded(const AccountIds& ids) = 0; | 52 virtual void OnAccountAdded(const AccountIds& ids) = 0; |
50 virtual void OnAccountRemoved(const AccountIds& ids) = 0; | 53 virtual void OnAccountRemoved(const AccountIds& ids) = 0; |
51 virtual void OnAccountSignInChanged(const AccountIds& ids, | 54 virtual void OnAccountSignInChanged(const AccountIds& ids, |
52 bool is_signed_in) = 0; | 55 bool is_signed_in) = 0; |
53 }; | 56 }; |
54 | 57 |
55 void Shutdown(); | 58 void Shutdown(); |
56 | 59 |
57 void ReportAuthError(const std::string& account_key, | |
58 const GoogleServiceAuthError& error); | |
59 | |
60 void AddObserver(Observer* observer); | 60 void AddObserver(Observer* observer); |
61 void RemoveObserver(Observer* observer); | 61 void RemoveObserver(Observer* observer); |
62 | 62 |
63 // Returns the list of accounts that are signed in, and for which gaia IDs | 63 // Returns the list of accounts that are signed in, and for which gaia IDs |
64 // have been fetched. The primary account for the profile will be first | 64 // have been fetched. The primary account for the profile will be first |
65 // in the vector. Additional accounts will be in order of their gaia IDs. | 65 // in the vector. Additional accounts will be in order of their gaia IDs. |
66 std::vector<AccountIds> GetAccounts() const; | 66 std::vector<AccountIds> GetAccounts() const; |
67 std::string FindAccountKeyByGaiaId(const std::string& gaia_id); | 67 AccountIds FindAccountIdsByGaiaId(const std::string& gaia_id); |
68 | 68 |
69 // OAuth2TokenService::Observer implementation. | 69 // OAuth2TokenService::Observer implementation. |
70 virtual void OnRefreshTokenAvailable(const std::string& account_key) OVERRIDE; | 70 virtual void OnRefreshTokenAvailable(const std::string& account_key) OVERRIDE; |
71 virtual void OnRefreshTokenRevoked(const std::string& account_key) OVERRIDE; | 71 virtual void OnRefreshTokenRevoked(const std::string& account_key) OVERRIDE; |
72 | 72 |
73 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, | 73 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, |
74 const std::string& gaia_id); | 74 const std::string& gaia_id); |
75 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); | 75 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher); |
76 | 76 |
77 // AuthStatusProvider implementation. | 77 // IdentityProvider::Observer implementation. |
78 virtual std::string GetAccountId() const OVERRIDE; | 78 virtual void OnActiveAccountLogin() OVERRIDE; |
79 virtual std::string GetUsername() const OVERRIDE; | 79 virtual void OnActiveAccountLogout() OVERRIDE; |
80 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | |
81 | |
82 // SigninManagerBase::Observer implementation. | |
83 virtual void GoogleSigninSucceeded(const std::string& username, | |
84 const std::string& password) OVERRIDE; | |
85 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; | |
86 | 80 |
87 // Sets the state of an account. Does not fire notifications. | 81 // Sets the state of an account. Does not fire notifications. |
88 void SetAccountStateForTest(AccountIds ids, bool is_signed_in); | 82 void SetAccountStateForTest(AccountIds ids, bool is_signed_in); |
89 | 83 |
| 84 IdentityProvider* identity_provider() { return identity_provider_; } |
| 85 |
90 private: | 86 private: |
91 struct AccountState { | 87 struct AccountState { |
92 AccountIds ids; | 88 AccountIds ids; |
93 bool is_signed_in; | 89 bool is_signed_in; |
94 }; | 90 }; |
95 | 91 |
96 const std::string signin_manager_account_id() const; | |
97 | |
98 void NotifyAccountAdded(const AccountState& account); | 92 void NotifyAccountAdded(const AccountState& account); |
99 void NotifyAccountRemoved(const AccountState& account); | 93 void NotifyAccountRemoved(const AccountState& account); |
100 void NotifySignInChanged(const AccountState& account); | 94 void NotifySignInChanged(const AccountState& account); |
101 | 95 |
102 void ClearAuthError(const std::string& account_key); | 96 void UpdateSignInState(const std::string account_key, bool is_signed_in); |
103 void UpdateSignInState(const std::string& account_key, bool is_signed_in); | |
104 | 97 |
105 void StartTrackingAccount(const std::string& account_key); | 98 void StartTrackingAccount(const std::string account_key); |
106 void StopTrackingAccount(const std::string& account_key); | 99 void StopTrackingAccount(const std::string account_key); |
107 void StopTrackingAllAccounts(); | 100 void StopTrackingAllAccounts(); |
108 void StartFetchingUserInfo(const std::string& account_key); | 101 void StartFetchingUserInfo(const std::string account_key); |
109 void DeleteFetcher(AccountIdFetcher* fetcher); | 102 void DeleteFetcher(AccountIdFetcher* fetcher); |
110 | 103 |
111 Profile* profile_; | 104 IdentityProvider* identity_provider_; // Not owned. |
| 105 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
112 std::map<std::string, AccountIdFetcher*> user_info_requests_; | 106 std::map<std::string, AccountIdFetcher*> user_info_requests_; |
113 std::map<std::string, AccountState> accounts_; | 107 std::map<std::string, AccountState> accounts_; |
114 std::map<std::string, GoogleServiceAuthError> account_errors_; | |
115 ObserverList<Observer> observer_list_; | 108 ObserverList<Observer> observer_list_; |
| 109 bool shutdown_called_; |
116 }; | 110 }; |
117 | 111 |
118 class AccountIdFetcher : public OAuth2TokenService::Consumer, | 112 class AccountIdFetcher : public OAuth2TokenService::Consumer, |
119 public gaia::GaiaOAuthClient::Delegate { | 113 public gaia::GaiaOAuthClient::Delegate { |
120 public: | 114 public: |
121 AccountIdFetcher(Profile* profile, | 115 AccountIdFetcher(OAuth2TokenService* token_service, |
| 116 net::URLRequestContextGetter* request_context_getter, |
122 AccountTracker* tracker, | 117 AccountTracker* tracker, |
123 const std::string& account_key); | 118 const std::string& account_key); |
124 virtual ~AccountIdFetcher(); | 119 virtual ~AccountIdFetcher(); |
125 | 120 |
126 const std::string& account_key() { return account_key_; } | 121 const std::string& account_key() { return account_key_; } |
127 | 122 |
128 void Start(); | 123 void Start(); |
129 | 124 |
130 // OAuth2TokenService::Consumer implementation. | 125 // OAuth2TokenService::Consumer implementation. |
131 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 126 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
132 const std::string& access_token, | 127 const std::string& access_token, |
133 const base::Time& expiration_time) OVERRIDE; | 128 const base::Time& expiration_time) OVERRIDE; |
134 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 129 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
135 const GoogleServiceAuthError& error) OVERRIDE; | 130 const GoogleServiceAuthError& error) OVERRIDE; |
136 | 131 |
137 // gaia::GaiaOAuthClient::Delegate implementation. | 132 // gaia::GaiaOAuthClient::Delegate implementation. |
138 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; | 133 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; |
139 virtual void OnOAuthError() OVERRIDE; | 134 virtual void OnOAuthError() OVERRIDE; |
140 virtual void OnNetworkError(int response_code) OVERRIDE; | 135 virtual void OnNetworkError(int response_code) OVERRIDE; |
141 | 136 |
142 private: | 137 private: |
143 Profile* profile_; | 138 OAuth2TokenService* token_service_; |
| 139 net::URLRequestContextGetter* request_context_getter_; |
144 AccountTracker* tracker_; | 140 AccountTracker* tracker_; |
145 const std::string account_key_; | 141 const std::string account_key_; |
146 | 142 |
147 scoped_ptr<OAuth2TokenService::Request> login_token_request_; | 143 scoped_ptr<OAuth2TokenService::Request> login_token_request_; |
148 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; | 144 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
149 }; | 145 }; |
150 | 146 |
151 } // namespace extensions | 147 } // namespace extensions |
152 | 148 |
153 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_ | 149 #endif // GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_ |
OLD | NEW |