OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ | 5 #ifndef GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ |
6 #define GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ | 6 #define GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 11 #include "base/timer/timer.h" |
11 #include "google_apis/gaia/gaia_auth_consumer.h" | 12 #include "google_apis/gaia/gaia_auth_consumer.h" |
12 #include "google_apis/gaia/ubertoken_fetcher.h" | 13 #include "google_apis/gaia/ubertoken_fetcher.h" |
13 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
14 | 15 |
15 class GaiaAuthFetcher; | 16 class GaiaAuthFetcher; |
16 class GoogleServiceAuthError; | 17 class GoogleServiceAuthError; |
17 class OAuth2TokenService; | 18 class OAuth2TokenService; |
18 | 19 |
19 namespace net { | 20 namespace net { |
| 21 class URLFetcher; |
20 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
21 } | 23 } |
22 | 24 |
23 // Merges a Google account known to Chrome into the cookie jar. When merging | 25 // 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 | 26 // multiple accounts, one instance of the helper is better than multiple |
25 // instances if there is the possibility that they run concurrently, since | 27 // instances if there is the possibility that they run concurrently, since |
26 // changes to the cookie must be serialized. | 28 // changes to the cookie must be serialized. |
27 // | 29 // |
28 // By default instances of MergeSessionHelper delete themselves when done. | 30 // By default instances of MergeSessionHelper delete themselves when done. |
29 class MergeSessionHelper : public GaiaAuthConsumer, | 31 class MergeSessionHelper : public GaiaAuthConsumer, |
30 public UbertokenConsumer, | 32 public UbertokenConsumer, |
31 public net::URLFetcherDelegate { | 33 public net::URLFetcherDelegate { |
32 public: | 34 public: |
33 class Observer { | 35 class Observer { |
34 public: | 36 public: |
35 // Called whenever a merge session is completed. The account that was | 37 // Called whenever a merge session is completed. The account that was |
36 // merged is given by |account_id|. If |error| is equal to | 38 // merged is given by |account_id|. If |error| is equal to |
37 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded. | 39 // GoogleServiceAuthError::AuthErrorNone() then the merge succeeeded. |
38 virtual void MergeSessionCompleted(const std::string& account_id, | 40 virtual void MergeSessionCompleted(const std::string& account_id, |
39 const GoogleServiceAuthError& error) = 0; | 41 const GoogleServiceAuthError& error) = 0; |
40 protected: | 42 protected: |
41 virtual ~Observer() {} | 43 virtual ~Observer() {} |
42 }; | 44 }; |
43 | 45 |
| 46 // Class to retrieve the external connection check results from gaia. |
| 47 class ExternalCcResultFetcher : public GaiaAuthConsumer, |
| 48 public net::URLFetcherDelegate { |
| 49 public: |
| 50 // Maps connection URLs, as returned by StartGetCheckConnectionInfo() to |
| 51 // token and URLFetcher used to fetch the URL. |
| 52 typedef std::map<GURL, std::pair<std::string, net::URLFetcher*> > |
| 53 URLToTokenAndFetcher; |
| 54 |
| 55 // Maps tokens to the fetched result for that token. |
| 56 typedef std::map<std::string, std::string> ResultMap; |
| 57 |
| 58 ExternalCcResultFetcher(MergeSessionHelper* helper); |
| 59 virtual ~ExternalCcResultFetcher(); |
| 60 |
| 61 // Gets the current value of the external connection check result string. |
| 62 std::string GetExternalCcResult(); |
| 63 |
| 64 // Start fetching the external CC result. If a fetch is already in progress |
| 65 // it is canceled. |
| 66 void Start(); |
| 67 |
| 68 // Are external URLs still being checked? |
| 69 bool IsRunning(); |
| 70 |
| 71 // Returns a copy of the internal token to fetcher map. |
| 72 URLToTokenAndFetcher get_fetcher_map_for_testing() { |
| 73 return fetchers_; |
| 74 } |
| 75 |
| 76 // Simulate a timeout for tests. |
| 77 void TimeoutForTests() { |
| 78 Timeout(); |
| 79 } |
| 80 |
| 81 private: |
| 82 // Overridden from GaiaAuthConsumer. |
| 83 virtual void OnGetCheckConnectionInfoSuccess( |
| 84 const std::string& data) OVERRIDE; |
| 85 |
| 86 // Creates and initializes a URL fetcher for doing a connection check. |
| 87 net::URLFetcher* CreateFetcher(const GURL& url); |
| 88 |
| 89 // Overridden from URLFetcherDelgate. |
| 90 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 91 |
| 92 // Any fetches still ongoing after this call are considered timed out. |
| 93 void Timeout(); |
| 94 |
| 95 void CleanupTransientState(); |
| 96 |
| 97 MergeSessionHelper* helper_; |
| 98 base::OneShotTimer<ExternalCcResultFetcher> timer_; |
| 99 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; |
| 100 URLToTokenAndFetcher fetchers_; |
| 101 ResultMap results_; |
| 102 }; |
| 103 |
44 MergeSessionHelper(OAuth2TokenService* token_service, | 104 MergeSessionHelper(OAuth2TokenService* token_service, |
45 net::URLRequestContextGetter* request_context, | 105 net::URLRequestContextGetter* request_context, |
46 Observer* observer); | 106 Observer* observer); |
47 virtual ~MergeSessionHelper(); | 107 virtual ~MergeSessionHelper(); |
48 | 108 |
49 void LogIn(const std::string& account_id); | 109 void LogIn(const std::string& account_id); |
50 | 110 |
51 // Add or remove observers of this helper. | 111 // Add or remove observers of this helper. |
52 void AddObserver(Observer* observer); | 112 void AddObserver(Observer* observer); |
53 void RemoveObserver(Observer* observer); | 113 void RemoveObserver(Observer* observer); |
(...skipping 13 matching lines...) Expand all Loading... |
67 | 127 |
68 // Call observers when merge session completes. This public so that callers | 128 // Call observers when merge session completes. This public so that callers |
69 // that know that a given account is already in the cookie jar can simply | 129 // that know that a given account is already in the cookie jar can simply |
70 // inform the observers. | 130 // inform the observers. |
71 void SignalComplete(const std::string& account_id, | 131 void SignalComplete(const std::string& account_id, |
72 const GoogleServiceAuthError& error); | 132 const GoogleServiceAuthError& error); |
73 | 133 |
74 // Returns true of there are pending log ins or outs. | 134 // Returns true of there are pending log ins or outs. |
75 bool is_running() const { return accounts_.size() > 0; } | 135 bool is_running() const { return accounts_.size() > 0; } |
76 | 136 |
| 137 // Start the process of fetching the external check connection result so that |
| 138 // its ready when we try to perform a merge session. |
| 139 void StartFetchingExternalCcResult(); |
| 140 |
| 141 // Returns true if the helper is still fetching external check connection |
| 142 // results. |
| 143 bool StillFetchingExternalCcResult(); |
| 144 |
77 private: | 145 private: |
| 146 net::URLRequestContextGetter* request_context() { return request_context_; } |
| 147 |
78 // Overridden from UbertokenConsumer. | 148 // Overridden from UbertokenConsumer. |
79 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; | 149 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; |
80 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 150 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
81 | 151 |
82 // Overridden from GaiaAuthConsumer. | 152 // Overridden from GaiaAuthConsumer. |
83 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE; | 153 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE; |
84 virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error) | 154 virtual void OnMergeSessionFailure(const GoogleServiceAuthError& error) |
85 OVERRIDE; | 155 OVERRIDE; |
86 | 156 |
87 void LogOutInternal(const std::string& account_id, | 157 void LogOutInternal(const std::string& account_id, |
88 const std::vector<std::string>& accounts); | 158 const std::vector<std::string>& accounts); |
89 | 159 |
90 // Starts the proess of fetching the uber token and performing a merge session | 160 // Starts the proess of fetching the uber token and performing a merge session |
91 // for the next account. Virtual so that it can be overriden in tests. | 161 // for the next account. Virtual so that it can be overriden in tests. |
92 virtual void StartFetching(); | 162 virtual void StartFetching(); |
93 | 163 |
94 // Virtual for testing purpose. | 164 // Virtual for testing purpose. |
95 virtual void StartLogOutUrlFetch(); | 165 virtual void StartLogOutUrlFetch(); |
96 | 166 |
97 // Start the next merge session, if needed. | 167 // Start the next merge session, if needed. |
98 void HandleNextAccount(); | 168 void HandleNextAccount(); |
99 | 169 |
100 // Overridden from URLFetcherDelgate. | 170 // Overridden from URLFetcherDelgate. |
101 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 171 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
102 | 172 |
103 OAuth2TokenService* token_service_; | 173 OAuth2TokenService* token_service_; |
104 net::URLRequestContextGetter* request_context_; | 174 net::URLRequestContextGetter* request_context_; |
105 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; | 175 scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_; |
106 scoped_ptr<UbertokenFetcher> uber_token_fetcher_; | 176 scoped_ptr<UbertokenFetcher> uber_token_fetcher_; |
| 177 ExternalCcResultFetcher result_fetcher_; |
107 | 178 |
108 // A worklist for this class. Accounts names are stored here if | 179 // A worklist for this class. Accounts names are stored here if |
109 // we are pending a signin action for that account. Empty strings | 180 // we are pending a signin action for that account. Empty strings |
110 // represent a signout request. | 181 // represent a signout request. |
111 std::deque<std::string> accounts_; | 182 std::deque<std::string> accounts_; |
112 | 183 |
113 // List of observers to notify when merge session completes. | 184 // List of observers to notify when merge session completes. |
114 // Makes sure list is empty on destruction. | 185 // Makes sure list is empty on destruction. |
115 ObserverList<Observer, true> observer_list_; | 186 ObserverList<Observer, true> observer_list_; |
116 | 187 |
117 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper); | 188 DISALLOW_COPY_AND_ASSIGN(MergeSessionHelper); |
118 }; | 189 }; |
119 | 190 |
120 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ | 191 #endif // GOOGLE_APIS_GAIA_MERGE_SESSION_HELPER_H_ |
OLD | NEW |