OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H | 5 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H |
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H | 6 #define COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 #include "components/signin/core/browser/signin_client.h" | 12 #include "components/signin/core/browser/signin_client.h" |
13 #include "google_apis/gaia/gaia_auth_consumer.h" | 13 #include "google_apis/gaia/gaia_auth_consumer.h" |
14 #include "google_apis/gaia/ubertoken_fetcher.h" | 14 #include "google_apis/gaia/ubertoken_fetcher.h" |
15 #include "net/base/backoff_entry.h" | 15 #include "net/base/backoff_entry.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
17 | 17 |
18 class GaiaAuthFetcher; | 18 class GaiaAuthFetcher; |
| 19 class GaiaCookieRequest; |
19 class GoogleServiceAuthError; | 20 class GoogleServiceAuthError; |
20 class OAuth2TokenService; | 21 class OAuth2TokenService; |
21 | 22 |
22 namespace net { | 23 namespace net { |
23 class URLFetcher; | 24 class URLFetcher; |
24 } | 25 } |
25 | 26 |
26 // Merges a Google account known to Chrome into the cookie jar. When merging | 27 // Merges a Google account known to Chrome into the cookie jar. When merging |
27 // multiple accounts, one instance of the helper is better than multiple | 28 // multiple accounts, one instance of the helper is better than multiple |
28 // instances if there is the possibility that they run concurrently, since | 29 // instances if there is the possibility that they run concurrently, since |
29 // changes to the cookie must be serialized. | 30 // changes to the cookie must be serialized. |
30 // | 31 // |
31 // Also checks the External CC result to ensure no services that consume the | 32 // Also checks the External CC result to ensure no services that consume the |
32 // GAIA cookie are blocked (such as youtube). This is executed once for the | 33 // GAIA cookie are blocked (such as youtube). This is executed once for the |
33 // lifetime of this object, when the first call is made to AddAccountToCookie. | 34 // lifetime of this object, when the first call is made to AddAccountToCookie. |
34 class GaiaCookieManagerService : public KeyedService, | 35 class GaiaCookieManagerService : public KeyedService, |
35 public GaiaAuthConsumer, | 36 public GaiaAuthConsumer, |
36 public UbertokenConsumer, | 37 public UbertokenConsumer, |
37 public net::URLFetcherDelegate { | 38 public net::URLFetcherDelegate { |
38 public: | 39 public: |
39 typedef base::Callback<void(const std::string& data, | |
40 const GoogleServiceAuthError& error)> | |
41 ListAccountsCallback; | |
42 | |
43 | |
44 enum GaiaCookieRequestType { | 40 enum GaiaCookieRequestType { |
45 ADD_ACCOUNT, | 41 ADD_ACCOUNT, |
46 LOG_OUT, | 42 LOG_OUT, |
47 LIST_ACCOUNTS | 43 LIST_ACCOUNTS |
48 }; | 44 }; |
49 | 45 |
50 // Contains the information and parameters for any request. | 46 // Contains the information and parameters for any request. |
51 class GaiaCookieRequest { | 47 class GaiaCookieRequest { |
52 public: | 48 public: |
53 ~GaiaCookieRequest(); | 49 ~GaiaCookieRequest(); |
54 | 50 |
55 GaiaCookieRequestType request_type() const { return request_type_; } | 51 GaiaCookieRequestType request_type() const { return request_type_; } |
56 const std::string& account_id() const {return account_id_; } | 52 const std::string& account_id() const {return account_id_; } |
57 const GaiaCookieManagerService::ListAccountsCallback& | |
58 list_accounts_callback() const { | |
59 return list_accounts_callback_; | |
60 } | |
61 | 53 |
62 static GaiaCookieRequest CreateAddAccountRequest( | 54 static GaiaCookieRequest CreateAddAccountRequest( |
63 const std::string& account_id); | 55 const std::string& account_id); |
64 static GaiaCookieRequest CreateLogOutRequest(); | 56 static GaiaCookieRequest CreateLogOutRequest(); |
65 static GaiaCookieRequest CreateListAccountsRequest( | 57 static GaiaCookieRequest CreateListAccountsRequest(); |
66 const GaiaCookieManagerService::ListAccountsCallback& | |
67 list_accounts_callback); | |
68 | 58 |
69 private: | 59 private: |
70 GaiaCookieRequest( | 60 GaiaCookieRequest( |
71 GaiaCookieRequestType request_type, | 61 GaiaCookieRequestType request_type, |
72 const std::string& account_id, | 62 const std::string& account_id); |
73 const GaiaCookieManagerService::ListAccountsCallback& | |
74 list_accounts_callback); | |
75 | 63 |
76 GaiaCookieRequestType request_type_; | 64 GaiaCookieRequestType request_type_; |
77 std::string account_id_; | 65 std::string account_id_; |
78 GaiaCookieManagerService::ListAccountsCallback list_accounts_callback_; | |
79 }; | 66 }; |
80 | 67 |
81 class Observer { | 68 class Observer { |
82 public: | 69 public: |
83 // Called whenever a merge session is completed. The account that was | 70 // Called whenever a merge session is completed. The account that was |
84 // merged is given by |account_id|. If |error| is equal to | 71 // merged is given by |account_id|. If |error| is equal to |
85 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded. | 72 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded. |
86 virtual void OnAddAccountToCookieCompleted( | 73 virtual void OnAddAccountToCookieCompleted( |
87 const std::string& account_id, | 74 const std::string& account_id, |
88 const GoogleServiceAuthError& error) = 0; | 75 const GoogleServiceAuthError& error) {} |
| 76 |
| 77 // Called whenever the GaiaCookieManagerService's list of GAIA accounts is |
| 78 // updated. The GCMS monitors the APISID cookie and triggers a /ListAccounts |
| 79 // call on change. The GCMS will also call ListAccounts upon the first call |
| 80 // to ListAccounts(). The GCMS will delay calling ListAccounts if other |
| 81 // requests are in queue that would modify the APISID cookie. |
| 82 // If the ListAccounts call fails and the GCMS cannot recover, the reason |
| 83 // is passed in |error|. |
| 84 virtual void OnGaiaAccountsInCookieUpdated( |
| 85 const std::vector<std::pair<std::string, bool> >& accounts, |
| 86 const GoogleServiceAuthError& error) {} |
89 | 87 |
90 protected: | 88 protected: |
91 virtual ~Observer() {} | 89 virtual ~Observer() {} |
92 }; | 90 }; |
93 | 91 |
94 // Class to retrieve the external connection check results from gaia. | 92 // Class to retrieve the external connection check results from gaia. |
95 // Declared publicly for unit tests. | 93 // Declared publicly for unit tests. |
96 class ExternalCcResultFetcher : public GaiaAuthConsumer, | 94 class ExternalCcResultFetcher : public GaiaAuthConsumer, |
97 public net::URLFetcherDelegate { | 95 public net::URLFetcherDelegate { |
98 public: | 96 public: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 base::OneShotTimer<ExternalCcResultFetcher> gaia_auth_fetcher_timer_; | 149 base::OneShotTimer<ExternalCcResultFetcher> gaia_auth_fetcher_timer_; |
152 | 150 |
153 DISALLOW_COPY_AND_ASSIGN(ExternalCcResultFetcher); | 151 DISALLOW_COPY_AND_ASSIGN(ExternalCcResultFetcher); |
154 }; | 152 }; |
155 | 153 |
156 GaiaCookieManagerService(OAuth2TokenService* token_service, | 154 GaiaCookieManagerService(OAuth2TokenService* token_service, |
157 const std::string& source, | 155 const std::string& source, |
158 SigninClient* signin_client); | 156 SigninClient* signin_client); |
159 ~GaiaCookieManagerService() override; | 157 ~GaiaCookieManagerService() override; |
160 | 158 |
| 159 void Init(); |
| 160 void Shutdown() override; |
| 161 |
161 void AddAccountToCookie(const std::string& account_id); | 162 void AddAccountToCookie(const std::string& account_id); |
162 | 163 |
163 void ListAccounts(const ListAccountsCallback& callback); | 164 // Returns if the listed accounts are up to date or not (ignore the out |
| 165 // parameter if return is false). The parameter will be assigned the current |
| 166 // cached accounts. If the accounts are not up to date, a ListAccounts fetch |
| 167 // is sent GAIA and Observer::OnGaiaAccountsInCookieUpdated will be called. |
| 168 bool ListAccounts(std::vector<std::pair<std::string,bool> >* accounts); |
164 | 169 |
165 // Add or remove observers of this helper. | 170 // Add or remove observers of this helper. |
166 void AddObserver(Observer* observer); | 171 void AddObserver(Observer* observer); |
167 void RemoveObserver(Observer* observer); | 172 void RemoveObserver(Observer* observer); |
168 | 173 |
169 // Cancel all login requests. | 174 // Cancel all login requests. |
170 void CancelAll(); | 175 void CancelAll(); |
171 | 176 |
172 // Signout all accounts. | 177 // Signout all accounts. |
173 void LogOutAllAccounts(); | 178 void LogOutAllAccounts(); |
174 | 179 |
175 // Call observers when merge session completes. This public so that callers | 180 // Call observers when merge session completes. This public so that callers |
176 // that know that a given account is already in the cookie jar can simply | 181 // that know that a given account is already in the cookie jar can simply |
177 // inform the observers. | 182 // inform the observers. |
178 void SignalComplete(const std::string& account_id, | 183 void SignalComplete(const std::string& account_id, |
179 const GoogleServiceAuthError& error); | 184 const GoogleServiceAuthError& error); |
180 | 185 |
181 // Returns true of there are pending log ins or outs. | 186 // Returns true of there are pending log ins or outs. |
182 bool is_running() const { return requests_.size() > 0; } | 187 bool is_running() const { return requests_.size() > 0; } |
183 | 188 |
184 // Access the internal object during tests. | 189 // Access the internal object during tests. |
185 ExternalCcResultFetcher* external_cc_result_fetcher_for_testing() { | 190 ExternalCcResultFetcher* external_cc_result_fetcher_for_testing() { |
186 return &external_cc_result_fetcher_; | 191 return &external_cc_result_fetcher_; |
187 } | 192 } |
188 | 193 |
| 194 void set_list_accounts_fetched_once_for_testing(bool fetched) { |
| 195 list_accounts_fetched_once_ = fetched; |
| 196 } |
| 197 |
189 private: | 198 private: |
190 net::URLRequestContextGetter* request_context() { | 199 net::URLRequestContextGetter* request_context() { |
191 return signin_client_->GetURLRequestContext(); | 200 return signin_client_->GetURLRequestContext(); |
192 } | 201 } |
193 | 202 |
| 203 // Called when a cookie changes. If the cookie relates to a GAIA APISID |
| 204 // cookie, then we call ListAccounts and fire OnGaiaAccountsInCookieUpdated. |
| 205 void OnCookieChanged(const net::CanonicalCookie& cookie, bool removed); |
| 206 |
194 // Overridden from UbertokenConsumer. | 207 // Overridden from UbertokenConsumer. |
195 void OnUbertokenSuccess(const std::string& token) override; | 208 void OnUbertokenSuccess(const std::string& token) override; |
196 void OnUbertokenFailure(const GoogleServiceAuthError& error) override; | 209 void OnUbertokenFailure(const GoogleServiceAuthError& error) override; |
197 | 210 |
198 // Overridden from GaiaAuthConsumer. | 211 // Overridden from GaiaAuthConsumer. |
199 void OnMergeSessionSuccess(const std::string& data) override; | 212 void OnMergeSessionSuccess(const std::string& data) override; |
200 void OnMergeSessionFailure(const GoogleServiceAuthError& error) override; | 213 void OnMergeSessionFailure(const GoogleServiceAuthError& error) override; |
| 214 void OnListAccountsSuccess(const std::string& data) override; |
| 215 void OnListAccountsFailure(const GoogleServiceAuthError& error) override; |
201 | 216 |
202 // Starts the proess of fetching the uber token and performing a merge session | 217 // Starts the proess of fetching the uber token and performing a merge session |
203 // for the next account. Virtual so that it can be overriden in tests. | 218 // for the next account. Virtual so that it can be overriden in tests. |
204 virtual void StartFetchingUbertoken(); | 219 virtual void StartFetchingUbertoken(); |
205 | 220 |
206 // Virtual for testing purposes. | 221 // Virtual for testing purposes. |
207 virtual void StartFetchingMergeSession(); | 222 virtual void StartFetchingMergeSession(); |
208 | 223 |
209 // Virtual for testing purpose. | 224 // Virtual for testing purpose. |
210 virtual void StartLogOutUrlFetch(); | 225 virtual void StartLogOutUrlFetch(); |
211 | 226 |
| 227 // Virtual for testing purposes. |
| 228 virtual void StartFetchingListAccounts(); |
| 229 |
212 // Start the next request, if needed. | 230 // Start the next request, if needed. |
213 void HandleNextRequest(); | 231 void HandleNextRequest(); |
214 | 232 |
215 // Overridden from URLFetcherDelgate. | 233 // Overridden from URLFetcherDelgate. |
216 void OnURLFetchComplete(const net::URLFetcher* source) override; | 234 void OnURLFetchComplete(const net::URLFetcher* source) override; |
217 | 235 |
218 OAuth2TokenService* token_service_; | 236 OAuth2TokenService* token_service_; |
219 SigninClient* signin_client_; | 237 SigninClient* signin_client_; |
220 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; | 238 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; |
221 scoped_ptr<UbertokenFetcher> uber_token_fetcher_; | 239 scoped_ptr<UbertokenFetcher> uber_token_fetcher_; |
222 ExternalCcResultFetcher external_cc_result_fetcher_; | 240 ExternalCcResultFetcher external_cc_result_fetcher_; |
223 | 241 |
224 // If the GaiaAuthFetcher fails, retry with exponential backoff. | 242 // If the GaiaAuthFetcher fails, retry with exponential backoff. |
225 net::BackoffEntry gaia_auth_fetcher_backoff_; | 243 net::BackoffEntry gaia_auth_fetcher_backoff_; |
226 base::OneShotTimer<GaiaCookieManagerService> gaia_auth_fetcher_timer_; | 244 base::OneShotTimer<GaiaCookieManagerService> gaia_auth_fetcher_timer_; |
227 int gaia_auth_fetcher_retries_; | 245 int gaia_auth_fetcher_retries_; |
228 | 246 |
229 // The last fetched ubertoken, for use in MergeSession retries. | 247 // The last fetched ubertoken, for use in MergeSession retries. |
230 std::string uber_token_; | 248 std::string uber_token_; |
231 | 249 |
| 250 // Subscription to be called whenever the GAIA cookies change. |
| 251 scoped_ptr<SigninClient::CookieChangedSubscription> |
| 252 cookie_changed_subscription_; |
| 253 |
232 // A worklist for this class. Stores any pending requests that couldn't be | 254 // A worklist for this class. Stores any pending requests that couldn't be |
233 // executed right away, since this class only permits one request to be | 255 // executed right away, since this class only permits one request to be |
234 // executed at a time. | 256 // executed at a time. |
235 std::deque<GaiaCookieRequest> requests_; | 257 std::deque<GaiaCookieRequest> requests_; |
236 | 258 |
237 // List of observers to notify when merge session completes. | 259 // List of observers to notify when merge session completes. |
238 // Makes sure list is empty on destruction. | 260 // Makes sure list is empty on destruction. |
239 ObserverList<Observer, true> observer_list_; | 261 ObserverList<Observer, true> observer_list_; |
240 | 262 |
241 // Source to use with GAIA endpoints for accounting. | 263 // Source to use with GAIA endpoints for accounting. |
242 std::string source_; | 264 std::string source_; |
243 | 265 |
244 // True once the ExternalCCResultFetcher has completed once. | 266 // True once the ExternalCCResultFetcher has completed once. |
245 bool external_cc_result_fetched_; | 267 bool external_cc_result_fetched_; |
246 | 268 |
| 269 std::vector<std::pair<std::string, bool> > listed_accounts_; |
| 270 |
| 271 bool list_accounts_fetched_once_; |
| 272 |
247 DISALLOW_COPY_AND_ASSIGN(GaiaCookieManagerService); | 273 DISALLOW_COPY_AND_ASSIGN(GaiaCookieManagerService); |
248 }; | 274 }; |
249 | 275 |
250 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H | 276 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_GAIA_COOKIE_MANAGER_SERVICE_H |
OLD | NEW |