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. |
fgorski
2014/06/17 04:14:00
Did you consider the following:
* calling it accou
Michael Courage
2014/06/18 23:13:17
Considered, but this seemed more straightforward t
| |
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(scoped_ptr<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 std::string FindAccountKeyByGaiaId(const std::string& gaia_id); |
fgorski
2014/06/17 04:14:00
Could you have a method to get AccountIds by GaiaI
Michael Courage
2014/06/18 23:13:17
Done.
| |
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; |
fgorski
2014/06/17 04:14:00
Is active account a new name for primary account?
Roger Tawa OOO till Jul 10th
2014/06/17 15:28:20
No. IdentityProvider has its own concept of one a
fgorski
2014/06/19 13:48:14
Thanks for the explanation... Now I feel like I've
| |
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_.get(); } | |
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; | 92 const std::string active_account_id() const; |
97 | 93 |
98 void NotifyAccountAdded(const AccountState& account); | 94 void NotifyAccountAdded(const AccountState& account); |
99 void NotifyAccountRemoved(const AccountState& account); | 95 void NotifyAccountRemoved(const AccountState& account); |
100 void NotifySignInChanged(const AccountState& account); | 96 void NotifySignInChanged(const AccountState& account); |
101 | 97 |
102 void ClearAuthError(const std::string& account_key); | 98 void UpdateSignInState(const std::string account_key, bool is_signed_in); |
103 void UpdateSignInState(const std::string& account_key, bool is_signed_in); | |
104 | 99 |
105 void StartTrackingAccount(const std::string& account_key); | 100 void StartTrackingAccount(const std::string account_key); |
106 void StopTrackingAccount(const std::string& account_key); | 101 void StopTrackingAccount(const std::string account_key); |
107 void StopTrackingAllAccounts(); | 102 void StopTrackingAllAccounts(); |
108 void StartFetchingUserInfo(const std::string& account_key); | 103 void StartFetchingUserInfo(const std::string account_key); |
109 void DeleteFetcher(AccountIdFetcher* fetcher); | 104 void DeleteFetcher(AccountIdFetcher* fetcher); |
110 | 105 |
111 Profile* profile_; | 106 scoped_ptr<IdentityProvider> identity_provider_; |
fgorski
2014/06/17 04:14:00
Why is AccountTracker taking the ownership of Iden
Michael Courage
2014/06/18 23:13:15
That seemed to match existing IdentityProvider usa
| |
107 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
112 std::map<std::string, AccountIdFetcher*> user_info_requests_; | 108 std::map<std::string, AccountIdFetcher*> user_info_requests_; |
113 std::map<std::string, AccountState> accounts_; | 109 std::map<std::string, AccountState> accounts_; |
114 std::map<std::string, GoogleServiceAuthError> account_errors_; | |
115 ObserverList<Observer> observer_list_; | 110 ObserverList<Observer> observer_list_; |
116 }; | 111 }; |
117 | 112 |
118 class AccountIdFetcher : public OAuth2TokenService::Consumer, | 113 class AccountIdFetcher : public OAuth2TokenService::Consumer, |
119 public gaia::GaiaOAuthClient::Delegate { | 114 public gaia::GaiaOAuthClient::Delegate { |
120 public: | 115 public: |
121 AccountIdFetcher(Profile* profile, | 116 AccountIdFetcher(OAuth2TokenService* token_service, |
117 net::URLRequestContextGetter* request_context_getter, | |
122 AccountTracker* tracker, | 118 AccountTracker* tracker, |
123 const std::string& account_key); | 119 const std::string& account_key); |
124 virtual ~AccountIdFetcher(); | 120 virtual ~AccountIdFetcher(); |
125 | 121 |
126 const std::string& account_key() { return account_key_; } | 122 const std::string& account_key() { return account_key_; } |
127 | 123 |
128 void Start(); | 124 void Start(); |
129 | 125 |
130 // OAuth2TokenService::Consumer implementation. | 126 // OAuth2TokenService::Consumer implementation. |
131 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 127 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
132 const std::string& access_token, | 128 const std::string& access_token, |
133 const base::Time& expiration_time) OVERRIDE; | 129 const base::Time& expiration_time) OVERRIDE; |
134 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 130 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
135 const GoogleServiceAuthError& error) OVERRIDE; | 131 const GoogleServiceAuthError& error) OVERRIDE; |
136 | 132 |
137 // gaia::GaiaOAuthClient::Delegate implementation. | 133 // gaia::GaiaOAuthClient::Delegate implementation. |
138 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; | 134 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; |
139 virtual void OnOAuthError() OVERRIDE; | 135 virtual void OnOAuthError() OVERRIDE; |
140 virtual void OnNetworkError(int response_code) OVERRIDE; | 136 virtual void OnNetworkError(int response_code) OVERRIDE; |
141 | 137 |
142 private: | 138 private: |
143 Profile* profile_; | 139 OAuth2TokenService* token_service_; |
140 net::URLRequestContextGetter* request_context_getter_; | |
144 AccountTracker* tracker_; | 141 AccountTracker* tracker_; |
145 const std::string account_key_; | 142 const std::string account_key_; |
146 | 143 |
147 scoped_ptr<OAuth2TokenService::Request> login_token_request_; | 144 scoped_ptr<OAuth2TokenService::Request> login_token_request_; |
148 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; | 145 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
149 }; | 146 }; |
150 | 147 |
151 } // namespace extensions | 148 } // namespace extensions |
152 | 149 |
153 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_ACCOUNT_TRACKER_H_ | 150 #endif // GOOGLE_APIS_GAIA_ACCOUNT_TRACKER_H_ |
OLD | NEW |