Support policy registration using a preobtained access token.
[chromium-blink-merge.git] / google_apis / gaia / merge_session_helper.h
blob76bad63b15af3a259d5b012d99b674f79062b790
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_
8 #include <deque>
10 #include "base/observer_list.h"
11 #include "google_apis/gaia/gaia_auth_consumer.h"
12 #include "google_apis/gaia/ubertoken_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h"
15 class GaiaAuthFetcher;
16 class GoogleServiceAuthError;
17 class OAuth2TokenService;
19 namespace net {
20 class URLRequestContextGetter;
23 // Merges a Google account known to Chrome into the cookie jar. When merging
24 // multiple accounts, one instance of the helper is better than multiple
25 // instances if there is the possibility that they run concurrently, since
26 // changes to the cookie must be serialized.
28 // By default instances of MergeSessionHelper delete themselves when done.
29 class MergeSessionHelper : public GaiaAuthConsumer,
30 public UbertokenConsumer,
31 public net::URLFetcherDelegate {
32 public:
33 class Observer {
34 public:
35 // Called whenever a merge session is completed. The account that was
36 // merged is given by |account_id|. If |error| is equal to
37 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded.
38 virtual void MergeSessionCompleted(const std::string& account_id,
39 const GoogleServiceAuthError& error) = 0;
40 protected:
41 virtual ~Observer() {}
44 MergeSessionHelper(OAuth2TokenService* token_service,
45 net::URLRequestContextGetter* request_context,
46 Observer* observer);
47 virtual ~MergeSessionHelper();
49 void LogIn(const std::string& account_id);
51 // Add or remove observers of this helper.
52 void AddObserver(Observer* observer);
53 void RemoveObserver(Observer* observer);
55 // Cancel all login requests.
56 void CancelAll();
58 // Signout of |account_id| given a list of accounts already signed in.
59 // Since this involves signing out of all accounts and resigning back in,
60 // the order which |accounts| are given is important as it will dictate
61 // the sign in order. |account_id| does not have to be in |accounts|.
62 void LogOut(const std::string& account_id,
63 const std::vector<std::string>& accounts);
65 // Signout all accounts.
66 void LogOutAllAccounts();
68 private:
69 // Overridden from UbertokenConsumer.
70 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
71 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
73 // Overridden from GaiaAuthConsumer.
74 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
75 virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
76 OVERRIDE;
78 void LogOutInternal(const std::string& account_id,
79 const std::vector<std::string>& accounts);
81 // Starts the proess of fetching the uber token and performing a merge session
82 // for the next account. Virtual so that it can be overriden in tests.
83 virtual void StartFetching();
85 // Virtual for testing purpose.
86 virtual void StartLogOutUrlFetch();
88 // Call observer when merge session completes.
89 void SignalComplete(const std::string& account_id,
90 const GoogleServiceAuthError& error);
92 // Start the next merge session, if needed.
93 void HandleNextAccount();
95 // Overridden from URLFetcherDelgate.
96 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
98 OAuth2TokenService* token_service_;
99 net::URLRequestContextGetter* request_context_;
100 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
101 scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
103 // A worklist for this class. Accounts names are stored here if
104 // we are pending a signin action for that account. Empty strings
105 // represent a signout request.
106 std::deque<std::string> accounts_;
108 // List of observers to notify when merge session completes.
109 // Makes sure list is empty on destruction.
110 ObserverList<Observer, true> observer_list_;
112 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
115 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_