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 "google_apis/gaia/gaia_auth_consumer.h"
12 #include "google_apis/gaia/ubertoken_fetcher.h"
14 class GaiaAuthFetcher;
15 class GoogleServiceAuthError;
16 class OAuth2TokenService;
19 class URLRequestContextGetter;
22 // Merges a Google account known to Chrome into the cookie jar. When merging
23 // multiple accounts, one instance of the helper is better than multiple
24 // instances if there is the possibility that they run concurrently, since
25 // changes to the cookie must be serialized.
27 // By default instances of MergeSessionHelper delete themselves when done.
28 class MergeSessionHelper : public GaiaAuthConsumer,
29 public UbertokenConsumer,
30 public net::URLFetcherDelegate {
34 // Called whenever a merge session is completed. The account that was
35 // merged is given by |account_id|. If |error| is equal to
36 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded.
37 virtual void MergeSessionCompleted(const std::string& account_id,
38 const GoogleServiceAuthError& error) = 0;
40 virtual ~Observer() {}
43 MergeSessionHelper(OAuth2TokenService* token_service,
44 net::URLRequestContextGetter* request_context,
46 virtual ~MergeSessionHelper();
48 void LogIn(const std::string& account_id);
50 // Add or remove observers of this helper.
51 void AddObserver(Observer* observer);
52 void RemoveObserver(Observer* observer);
54 // Cancel all login requests.
57 // Signout of |account_id| given a list of accounts already signed in.
58 // Since this involves signing out of all accounts and resigning back in,
59 // the order which |accounts| are given is important as it will dictate
60 // the sign in order. |account_id| does not have to be in |accounts|.
61 void LogOut(const std::string& account_id,
62 const std::vector<std::string>& accounts);
64 // Signout all accounts.
65 void LogOutAllAccounts();
68 // Overridden from UbertokenConsumer.
69 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
70 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
72 // Overridden from GaiaAuthConsumer.
73 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
74 virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
77 void LogOutInternal(const std::string& account_id,
78 const std::vector<std::string>& accounts);
80 // Starts the proess of fetching the uber token and performing a merge session
81 // for the next account. Virtual so that it can be overriden in tests.
82 virtual void StartFetching();
84 // Virtual for testing purpose.
85 virtual void StartLogOutUrlFetch();
87 // Call observer when merge session completes.
88 void SignalComplete(const std::string& account_id,
89 const GoogleServiceAuthError& error);
91 // Start the next merge session, if needed.
92 void HandleNextAccount();
94 // Overridden from URLFetcherDelgate.
95 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
97 OAuth2TokenService* token_service_;
98 net::URLRequestContextGetter* request_context_;
99 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
100 scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
102 // A worklist for this class. Accounts names are stored here if
103 // we are pending a signin action for that account. Empty strings
104 // represent a signout request.
105 std::deque<std::string> accounts_;
107 // List of observers to notify when merge session completes.
108 // Makes sure list is empty on destruction.
109 ObserverList<Observer, true> observer_list_;
111 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
114 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_