Upstream version 5.34.104.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   // Signout all accounts.
65   void LogOutAllAccounts();
66
67  private:
68   // Overridden from UbertokenConsumer.
69   virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
70   virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
71
72   // Overridden from GaiaAuthConsumer.
73   virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE;
74   virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error)
75       OVERRIDE;
76
77   void LogOutInternal(const std::string& account_id,
78                       const std::vector<std::string>& accounts);
79
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();
83
84   // Virtual for testing purpose.
85   virtual void StartLogOutUrlFetch();
86
87   // Call observer when merge session completes.
88   void SignalComplete(const std::string& account_id,
89                       const GoogleServiceAuthError& error);
90
91   // Start the next merge session, if needed.
92   void HandleNextAccount();
93
94   // Overridden from URLFetcherDelgate.
95   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
96
97   OAuth2TokenService* token_service_;
98   net::URLRequestContextGetter* request_context_;
99   scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
100   scoped_ptr<UbertokenFetcher> uber_token_fetcher_;
101
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_;
106
107   // List of observers to notify when merge session completes.
108   // Makes sure list is empty on destruction.
109   ObserverList<Observer, true> observer_list_;
110
111   DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper);
112 };
113
114 #endif  // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_