OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
6 #define CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 6 #define CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <iosfwd> | 9 #include <iosfwd> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
15 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/threading/thread_checker.h" |
16 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
17 #include "chrome/browser/sync/syncable/syncable_id.h" | 19 #include "chrome/browser/sync/syncable/syncable_id.h" |
18 #include "chrome/common/net/http_return.h" | 20 #include "chrome/common/net/http_return.h" |
19 | 21 |
20 namespace syncable { | 22 namespace syncable { |
21 class WriteTransaction; | 23 class WriteTransaction; |
22 class DirectoryManager; | 24 class DirectoryManager; |
23 } | 25 } |
24 | 26 |
25 namespace sync_pb { | 27 namespace sync_pb { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 private: | 133 private: |
132 ServerConnectionManager* const conn_mgr_; | 134 ServerConnectionManager* const conn_mgr_; |
133 HttpResponse* const response_; | 135 HttpResponse* const response_; |
134 bool server_reachable_; | 136 bool server_reachable_; |
135 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); | 137 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); |
136 }; | 138 }; |
137 | 139 |
138 // Use this class to interact with the sync server. | 140 // Use this class to interact with the sync server. |
139 // The ServerConnectionManager currently supports POSTing protocol buffers. | 141 // The ServerConnectionManager currently supports POSTing protocol buffers. |
140 // | 142 // |
141 class ServerConnectionManager : public base::NonThreadSafe { | 143 class ServerConnectionManager { |
142 public: | 144 public: |
143 // buffer_in - will be POSTed | 145 // buffer_in - will be POSTed |
144 // buffer_out - string will be overwritten with response | 146 // buffer_out - string will be overwritten with response |
145 struct PostBufferParams { | 147 struct PostBufferParams { |
146 std::string buffer_in; | 148 std::string buffer_in; |
147 std::string buffer_out; | 149 std::string buffer_out; |
148 HttpResponse response; | 150 HttpResponse response; |
149 }; | 151 }; |
150 | 152 |
151 // Abstract class providing network-layer functionality to the | 153 // Abstract class providing network-layer functionality to the |
152 // ServerConnectionManager. Subclasses implement this using an HTTP stack of | 154 // ServerConnectionManager. Subclasses implement this using an HTTP stack of |
153 // their choice. | 155 // their choice. |
154 class Post { | 156 class Connection { |
155 public: | 157 public: |
156 explicit Post(ServerConnectionManager* scm) : scm_(scm) { | 158 explicit Connection(ServerConnectionManager* scm); |
157 } | 159 virtual ~Connection(); |
158 virtual ~Post() { } | |
159 | 160 |
160 // Called to initialize and perform an HTTP POST. | 161 // Called to initialize and perform an HTTP POST. |
161 virtual bool Init(const char* path, | 162 virtual bool Init(const char* path, |
162 const std::string& auth_token, | 163 const std::string& auth_token, |
163 const std::string& payload, | 164 const std::string& payload, |
164 HttpResponse* response) = 0; | 165 HttpResponse* response) = 0; |
165 | 166 |
| 167 // Immediately abandons a pending HTTP POST request and unblocks caller |
| 168 // in Init. |
| 169 virtual void Abort() = 0; |
| 170 |
166 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response, | 171 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response, |
167 bool require_response); | 172 bool require_response); |
168 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out); | 173 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out); |
169 | 174 |
170 protected: | 175 protected: |
171 std::string MakeConnectionURL(const std::string& sync_server, | 176 std::string MakeConnectionURL(const std::string& sync_server, |
172 const std::string& path, | 177 const std::string& path, |
173 bool use_ssl) const; | 178 bool use_ssl) const; |
174 | 179 |
175 void GetServerParams(std::string* server, | 180 void GetServerParams(std::string* server, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 220 |
216 // Updates status and broadcasts events on change. | 221 // Updates status and broadcasts events on change. |
217 bool CheckServerReachable(); | 222 bool CheckServerReachable(); |
218 | 223 |
219 void AddListener(ServerConnectionEventListener* listener); | 224 void AddListener(ServerConnectionEventListener* listener); |
220 void RemoveListener(ServerConnectionEventListener* listener); | 225 void RemoveListener(ServerConnectionEventListener* listener); |
221 | 226 |
222 inline std::string user_agent() const { return user_agent_; } | 227 inline std::string user_agent() const { return user_agent_; } |
223 | 228 |
224 inline HttpResponse::ServerConnectionCode server_status() const { | 229 inline HttpResponse::ServerConnectionCode server_status() const { |
225 DCHECK(CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
226 return server_status_; | 231 return server_status_; |
227 } | 232 } |
228 | 233 |
229 inline bool server_reachable() const { return server_reachable_; } | 234 inline bool server_reachable() const { return server_reachable_; } |
230 | 235 |
231 const std::string client_id() const { return client_id_; } | 236 const std::string client_id() const { return client_id_; } |
232 | 237 |
233 // This changes the server info used by the connection manager. This allows | 238 // This changes the server info used by the connection manager. This allows |
234 // a single client instance to talk to different backing servers. This is | 239 // a single client instance to talk to different backing servers. This is |
235 // typically called during / after authentication so that the server url | 240 // typically called during / after authentication so that the server url |
236 // can be a function of the user's login id. | 241 // can be a function of the user's login id. |
237 void SetServerParameters(const std::string& server_url, | 242 void SetServerParameters(const std::string& server_url, |
238 int port, | 243 int port, |
239 bool use_ssl); | 244 bool use_ssl); |
240 | 245 |
241 // Returns the current server parameters in server_url, port and use_ssl. | 246 // Returns the current server parameters in server_url, port and use_ssl. |
242 void GetServerParameters(std::string* server_url, | 247 void GetServerParameters(std::string* server_url, |
243 int* port, | 248 int* port, |
244 bool* use_ssl) const; | 249 bool* use_ssl) const; |
245 | 250 |
246 std::string GetServerHost() const; | 251 std::string GetServerHost() const; |
247 | 252 |
248 // Factory method to create a Post object we can use for communication with | 253 // Factory method to create an Connection object we can use for |
249 // the server. | 254 // communication with the server. |
250 virtual Post* MakePost(); | 255 virtual Connection* MakeConnection(); |
| 256 |
| 257 // Aborts any active HTTP POST request. |
| 258 // We expect this to get called on a different thread than the valid |
| 259 // ThreadChecker thread, as we want to kill any pending http traffic without |
| 260 // having to wait for the request to complete. |
| 261 void TerminateAllIO(); |
251 | 262 |
252 void set_client_id(const std::string& client_id) { | 263 void set_client_id(const std::string& client_id) { |
253 DCHECK(CalledOnValidThread()); | 264 DCHECK(thread_checker_.CalledOnValidThread()); |
254 DCHECK(client_id_.empty()); | 265 DCHECK(client_id_.empty()); |
255 client_id_.assign(client_id); | 266 client_id_.assign(client_id); |
256 } | 267 } |
257 | 268 |
258 // Returns true if the auth token is succesfully set and false otherwise. | 269 // Returns true if the auth token is succesfully set and false otherwise. |
259 bool set_auth_token(const std::string& auth_token) { | 270 bool set_auth_token(const std::string& auth_token) { |
260 DCHECK(CalledOnValidThread()); | 271 DCHECK(thread_checker_.CalledOnValidThread()); |
261 if (previously_invalidated_token != auth_token) { | 272 if (previously_invalidated_token != auth_token) { |
262 auth_token_.assign(auth_token); | 273 auth_token_.assign(auth_token); |
263 previously_invalidated_token = std::string(); | 274 previously_invalidated_token = std::string(); |
264 return true; | 275 return true; |
265 } | 276 } |
266 return false; | 277 return false; |
267 } | 278 } |
268 | 279 |
269 void InvalidateAndClearAuthToken() { | 280 void InvalidateAndClearAuthToken() { |
270 DCHECK(CalledOnValidThread()); | 281 DCHECK(thread_checker_.CalledOnValidThread()); |
271 // Copy over the token to previous invalid token. | 282 // Copy over the token to previous invalid token. |
272 if (!auth_token_.empty()) { | 283 if (!auth_token_.empty()) { |
273 previously_invalidated_token.assign(auth_token_); | 284 previously_invalidated_token.assign(auth_token_); |
274 auth_token_ = std::string(); | 285 auth_token_ = std::string(); |
275 } | 286 } |
276 } | 287 } |
277 | 288 |
278 const std::string auth_token() const { | 289 const std::string auth_token() const { |
279 DCHECK(CalledOnValidThread()); | 290 DCHECK(thread_checker_.CalledOnValidThread()); |
280 return auth_token_; | 291 return auth_token_; |
281 } | 292 } |
282 | 293 |
283 protected: | 294 protected: |
284 inline std::string proto_sync_path() const { | 295 inline std::string proto_sync_path() const { |
285 return proto_sync_path_; | 296 return proto_sync_path_; |
286 } | 297 } |
287 | 298 |
288 std::string get_time_path() const { | 299 std::string get_time_path() const { |
289 return get_time_path_; | 300 return get_time_path_; |
290 } | 301 } |
291 | 302 |
292 // Called wherever a failure should be taken as an indication that we may | 303 // Called wherever a failure should be taken as an indication that we may |
293 // be experiencing connection difficulties. | 304 // be experiencing connection difficulties. |
294 virtual bool IncrementErrorCount(); | 305 virtual bool IncrementErrorCount(); |
295 | 306 |
296 // NOTE: Tests rely on this protected function being virtual. | 307 // NOTE: Tests rely on this protected function being virtual. |
297 // | 308 // |
298 // Internal PostBuffer base function. | 309 // Internal PostBuffer base function. |
299 virtual bool PostBufferToPath(PostBufferParams*, | 310 virtual bool PostBufferToPath(PostBufferParams*, |
300 const std::string& path, | 311 const std::string& path, |
301 const std::string& auth_token, | 312 const std::string& auth_token, |
302 ScopedServerStatusWatcher* watcher); | 313 ScopedServerStatusWatcher* watcher); |
303 | 314 |
| 315 // Helper to check terminated flags and build a Connection object, installing |
| 316 // it as the |active_connection_|. If this ServerConnectionManager has been |
| 317 // terminated, this will return NULL. |
| 318 Connection* MakeActiveConnection(); |
| 319 |
| 320 // Called by Connection objects as they are destroyed to allow the |
| 321 // ServerConnectionManager to cleanup active connections. |
| 322 void OnConnectionDestroyed(Connection* connection); |
| 323 |
304 // The sync_server_ is the server that requests will be made to. | 324 // The sync_server_ is the server that requests will be made to. |
305 std::string sync_server_; | 325 std::string sync_server_; |
306 | 326 |
307 // The sync_server_port_ is the port that HTTP requests will be made on. | 327 // The sync_server_port_ is the port that HTTP requests will be made on. |
308 int sync_server_port_; | 328 int sync_server_port_; |
309 | 329 |
310 // The unique id of the user's client. | 330 // The unique id of the user's client. |
311 std::string client_id_; | 331 std::string client_id_; |
312 | 332 |
313 // The user-agent string for HTTP. | 333 // The user-agent string for HTTP. |
(...skipping 12 matching lines...) Expand all Loading... |
326 // The previous auth token that is invalid now. | 346 // The previous auth token that is invalid now. |
327 std::string previously_invalidated_token; | 347 std::string previously_invalidated_token; |
328 | 348 |
329 int error_count_; // Tracks the number of connection errors. | 349 int error_count_; // Tracks the number of connection errors. |
330 | 350 |
331 ObserverList<ServerConnectionEventListener> listeners_; | 351 ObserverList<ServerConnectionEventListener> listeners_; |
332 | 352 |
333 HttpResponse::ServerConnectionCode server_status_; | 353 HttpResponse::ServerConnectionCode server_status_; |
334 bool server_reachable_; | 354 bool server_reachable_; |
335 | 355 |
| 356 base::ThreadChecker thread_checker_; |
| 357 |
| 358 // Protects all variables below to allow bailing out of active connections. |
| 359 base::Lock terminate_connection_lock_; |
| 360 |
| 361 // If true, we've been told to terminate IO and expect to be destroyed |
| 362 // shortly. No future network requests will be made. |
| 363 bool terminated_; |
| 364 |
| 365 // A non-owning pointer to any active http connection, so that we can abort |
| 366 // it if necessary. |
| 367 Connection* active_connection_; |
| 368 |
336 private: | 369 private: |
337 friend class Post; | 370 friend class Connection; |
338 friend class ScopedServerStatusWatcher; | 371 friend class ScopedServerStatusWatcher; |
339 | 372 |
| 373 // A class to help deal with cleaning up active Connection objects when (for |
| 374 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper |
| 375 // informs the ServerConnectionManager before the Connection object it takes |
| 376 // ownership of is destroyed. |
| 377 class ScopedConnectionHelper { |
| 378 public: |
| 379 // |manager| must outlive this. Takes ownership of |connection|. |
| 380 ScopedConnectionHelper(ServerConnectionManager* manager, |
| 381 Connection* connection); |
| 382 ~ScopedConnectionHelper(); |
| 383 Connection* get(); |
| 384 private: |
| 385 ServerConnectionManager* manager_; |
| 386 scoped_ptr<Connection> connection_; |
| 387 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); |
| 388 }; |
| 389 |
340 void NotifyStatusChanged(); | 390 void NotifyStatusChanged(); |
341 | 391 |
342 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 392 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
343 }; | 393 }; |
344 | 394 |
345 // Fills a ClientToServerMessage with the appropriate share and birthday | 395 // Fills a ClientToServerMessage with the appropriate share and birthday |
346 // settings. | 396 // settings. |
347 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, | 397 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, |
348 syncable::DirectoryManager* manager, | 398 syncable::DirectoryManager* manager, |
349 const std::string& share); | 399 const std::string& share); |
350 | 400 |
351 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 401 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
352 | 402 |
353 } // namespace browser_sync | 403 } // namespace browser_sync |
354 | 404 |
355 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 405 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
OLD | NEW |