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
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
6 #define GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
10 #include "base/observer_list.h"
11 #include "base/timer/timer.h"
12 #include "google_apis/gaia/gaia_auth_consumer.h"
13 #include "google_apis/gaia/ubertoken_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
16 class GaiaAuthFetcher
;
17 class GoogleServiceAuthError
;
18 class OAuth2TokenService
;
22 class URLRequestContextGetter
;
25 // Merges a Google account known to Chrome into the cookie jar. When merging
26 // multiple accounts, one instance of the helper is better than multiple
27 // instances if there is the possibility that they run concurrently, since
28 // changes to the cookie must be serialized.
30 // By default instances of MergeSessionHelper delete themselves when done.
31 class MergeSessionHelper
: public GaiaAuthConsumer
,
32 public UbertokenConsumer
,
33 public net::URLFetcherDelegate
{
37 // Called whenever a merge session is completed. The account that was
38 // merged is given by |account_id|. If |error| is equal to
39 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded.
40 virtual void MergeSessionCompleted(const std::string
& account_id
,
41 const GoogleServiceAuthError
& error
) = 0;
43 virtual ~Observer() {}
46 // Class to retrieve the external connection check results from gaia.
47 // Declared publicly for unit tests.
48 class ExternalCcResultFetcher
: public GaiaAuthConsumer
,
49 public net::URLFetcherDelegate
{
51 // Maps connection URLs, as returned by StartGetCheckConnectionInfo() to
52 // token and URLFetcher used to fetch the URL.
53 typedef std::map
<GURL
, std::pair
<std::string
, net::URLFetcher
*> >
56 // Maps tokens to the fetched result for that token.
57 typedef std::map
<std::string
, std::string
> ResultMap
;
59 ExternalCcResultFetcher(MergeSessionHelper
* helper
);
60 ~ExternalCcResultFetcher() override
;
62 // Gets the current value of the external connection check result string.
63 std::string
GetExternalCcResult();
65 // Start fetching the external CC result. If a fetch is already in progress
69 // Are external URLs still being checked?
72 // Returns a copy of the internal token to fetcher map.
73 URLToTokenAndFetcher
get_fetcher_map_for_testing() {
77 // Simulate a timeout for tests.
78 void TimeoutForTests();
81 // Overridden from GaiaAuthConsumer.
82 void OnGetCheckConnectionInfoSuccess(const std::string
& data
) override
;
84 // Creates and initializes a URL fetcher for doing a connection check.
85 net::URLFetcher
* CreateFetcher(const GURL
& url
);
87 // Overridden from URLFetcherDelgate.
88 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
90 // Any fetches still ongoing after this call are considered timed out.
93 void CleanupTransientState();
95 MergeSessionHelper
* helper_
;
96 base::OneShotTimer
<ExternalCcResultFetcher
> timer_
;
97 scoped_ptr
<GaiaAuthFetcher
> gaia_auth_fetcher_
;
98 URLToTokenAndFetcher fetchers_
;
101 DISALLOW_COPY_AND_ASSIGN(ExternalCcResultFetcher
);
104 MergeSessionHelper(OAuth2TokenService
* token_service
,
105 const std::string
& source
,
106 net::URLRequestContextGetter
* request_context
,
108 ~MergeSessionHelper() override
;
110 void LogIn(const std::string
& account_id
);
112 // Add or remove observers of this helper.
113 void AddObserver(Observer
* observer
);
114 void RemoveObserver(Observer
* observer
);
116 // Cancel all login requests.
119 // Signout of |account_id| given a list of accounts already signed in.
120 // Since this involves signing out of all accounts and resigning back in,
121 // the order which |accounts| are given is important as it will dictate
122 // the sign in order. |account_id| does not have to be in |accounts|.
123 void LogOut(const std::string
& account_id
,
124 const std::vector
<std::string
>& accounts
);
126 // Signout all accounts.
127 void LogOutAllAccounts();
129 // Call observers when merge session completes. This public so that callers
130 // that know that a given account is already in the cookie jar can simply
131 // inform the observers.
132 void SignalComplete(const std::string
& account_id
,
133 const GoogleServiceAuthError
& error
);
135 // Returns true of there are pending log ins or outs.
136 bool is_running() const { return accounts_
.size() > 0; }
138 // Start the process of fetching the external check connection result so that
139 // its ready when we try to perform a merge session.
140 void StartFetchingExternalCcResult();
142 // Returns true if the helper is still fetching external check connection
144 bool StillFetchingExternalCcResult();
147 net::URLRequestContextGetter
* request_context() { return request_context_
; }
149 // Overridden from UbertokenConsumer.
150 void OnUbertokenSuccess(const std::string
& token
) override
;
151 void OnUbertokenFailure(const GoogleServiceAuthError
& error
) override
;
153 // Overridden from GaiaAuthConsumer.
154 void OnMergeSessionSuccess(const std::string
& data
) override
;
155 void OnMergeSessionFailure(const GoogleServiceAuthError
& error
) override
;
157 void LogOutInternal(const std::string
& account_id
,
158 const std::vector
<std::string
>& accounts
);
160 // Starts the proess of fetching the uber token and performing a merge session
161 // for the next account. Virtual so that it can be overriden in tests.
162 virtual void StartFetching();
164 // Virtual for testing purpose.
165 virtual void StartLogOutUrlFetch();
167 // Start the next merge session, if needed.
168 void HandleNextAccount();
170 // Overridden from URLFetcherDelgate.
171 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
173 OAuth2TokenService
* token_service_
;
174 net::URLRequestContextGetter
* request_context_
;
175 scoped_ptr
<GaiaAuthFetcher
> gaia_auth_fetcher_
;
176 scoped_ptr
<UbertokenFetcher
> uber_token_fetcher_
;
177 ExternalCcResultFetcher result_fetcher_
;
179 // A worklist for this class. Accounts names are stored here if
180 // we are pending a signin action for that account. Empty strings
181 // represent a signout request.
182 std::deque
<std::string
> accounts_
;
184 // List of observers to notify when merge session completes.
185 // Makes sure list is empty on destruction.
186 ObserverList
<Observer
, true> observer_list_
;
188 // Source to use with GAIA endpoints for accounting.
191 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper
);
194 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_