Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / google_apis / gaia / merge_session_helper.h
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.
4
5 #ifndef GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
6 #define GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_
7
8 #include <deque>
9
10 #include "base/observer_list.h"
11 #include "google_apis/gaia/gaia_auth_consumer.h"
12 #include "google_apis/gaia/ubertoken_fetcher.h"
13
14 class GaiaAuthFetcher;
15 class GoogleServiceAuthError;
16 class OAuth2TokenService;
17
18 namespace net {
19 class URLRequestContextGetter;
20 }
21
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.
26 //
27 // By default instances of MergeSessionHelper delete themselves when done.
28 class MergeSessionHelper : public GaiaAuthConsumer,
29                            public UbertokenConsumer,
30                            public net::URLFetcherDelegate {
31  public:
32   class Observer {
33    public:
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;
39    protected:
40     virtual ~Observer() {}
41   };
42
43   MergeSessionHelper(OAuth2TokenService* token_service,
44                      net::URLRequestContextGetter* request_context,
45                      Observer* observer);
46   virtual ~MergeSessionHelper();
47
48   void LogIn(const std::string& account_id);
49
50   // Add or remove observers of this helper.
51   void AddObserver(Observer* observer);
52   void RemoveObserver(Observer* observer);
53
54   // Cancel all login requests.
55   void CancelAll();
56
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);
63
64  private:
65   // Overridden from UbertokenConsumer.
66   virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
67   virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
68
69   // Overridden from GaiaAuthConsumer.
70   virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
71   virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
72       OVERRIDE;
73
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();
77
78   // Virtual for testing purpose.
79   virtual void StartLogOutUrlFetch();
80
81   // Call observer when merge session completes.
82   void SignalComplete(const std::string& account_id,
83                       const GoogleServiceAuthError& error);
84
85   // Start the next merge session, if needed.
86   void HandleNextAccount();
87
88   // Overridden from URLFetcherDelgate.
89   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
90
91   OAuth2TokenService* token_service_;
92   net::URLRequestContextGetter* request_context_;
93   scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
94   scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
95
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_;
100
101   // List of observers to notify when merge session completes.
102   // Makes sure list is empty on destruction.
103   ObserverList<Observer, true> observer_list_;
104
105   DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
106 };
107
108 #endif  // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_