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);
65 // Overridden from UbertokenConsumer.
66 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
67 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
69 // Overridden from GaiaAuthConsumer.
70 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
71 virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
74 // Starts the proess of fetching the uber token and performing a merge session
75 // for the next account. Virtual so that it can be overriden in tests.
76 virtual void StartFetching();
78 // Virtual for testing purpose.
79 virtual void StartLogOutUrlFetch();
81 // Call observer when merge session completes.
82 void SignalComplete(const std::string& account_id,
83 const GoogleServiceAuthError& error);
85 // Start the next merge session, if needed.
86 void HandleNextAccount();
88 // Overridden from URLFetcherDelgate.
89 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
91 OAuth2TokenService* token_service_;
92 net::URLRequestContextGetter* request_context_;
93 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
94 scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
96 // A worklist for this class. Accounts names are stored here if
97 // we are pending a signin action for that account. Empty strings
98 // represent a signout request.
99 std::deque<std::string> accounts_;
101 // List of observers to notify when merge session completes.
102 // Makes sure list is empty on destruction.
103 ObserverList<Observer, true> observer_list_;
105 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
108 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_