| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 5 #ifndef SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
| 6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <iosfwd> | 8 #include <iosfwd> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/atomicops.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 16 #include "sync/base/sync_export.h" | 18 #include "sync/base/sync_export.h" |
| 17 #include "sync/internal_api/public/base/cancelation_observer.h" | |
| 18 #include "sync/syncable/syncable_id.h" | 19 #include "sync/syncable/syncable_id.h" |
| 19 | 20 |
| 20 namespace sync_pb { | 21 namespace sync_pb { |
| 21 class ClientToServerMessage; | 22 class ClientToServerMessage; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace syncer { | 25 namespace syncer { |
| 25 | 26 |
| 26 class CancelationSignal; | |
| 27 | |
| 28 namespace syncable { | 27 namespace syncable { |
| 29 class Directory; | 28 class Directory; |
| 30 } | 29 } |
| 31 | 30 |
| 32 static const int32 kUnsetResponseCode = -1; | 31 static const int32 kUnsetResponseCode = -1; |
| 33 static const int32 kUnsetContentLength = -1; | 32 static const int32 kUnsetContentLength = -1; |
| 34 static const int32 kUnsetPayloadLength = -1; | 33 static const int32 kUnsetPayloadLength = -1; |
| 35 | 34 |
| 36 // HttpResponse gathers the relevant output properties of an HTTP request. | 35 // HttpResponse gathers the relevant output properties of an HTTP request. |
| 37 // Depending on the value of the server_status code, response_code, and | 36 // Depending on the value of the server_status code, response_code, and |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 ServerConnectionManager* scm_; | 176 ServerConnectionManager* scm_; |
| 178 | 177 |
| 179 private: | 178 private: |
| 180 int ReadResponse(void* buffer, int length); | 179 int ReadResponse(void* buffer, int length); |
| 181 int ReadResponse(std::string* buffer, int length); | 180 int ReadResponse(std::string* buffer, int length); |
| 182 }; | 181 }; |
| 183 | 182 |
| 184 ServerConnectionManager(const std::string& server, | 183 ServerConnectionManager(const std::string& server, |
| 185 int port, | 184 int port, |
| 186 bool use_ssl, | 185 bool use_ssl, |
| 187 bool use_oauth2_token, | 186 bool use_oauth2_token); |
| 188 CancelationSignal* cancelation_signal); | |
| 189 | 187 |
| 190 virtual ~ServerConnectionManager(); | 188 virtual ~ServerConnectionManager(); |
| 191 | 189 |
| 192 // POSTS buffer_in and reads a response into buffer_out. Uses our currently | 190 // POSTS buffer_in and reads a response into buffer_out. Uses our currently |
| 193 // set auth token in our headers. | 191 // set auth token in our headers. |
| 194 // | 192 // |
| 195 // Returns true if executed successfully. | 193 // Returns true if executed successfully. |
| 196 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, | 194 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, |
| 197 ScopedServerStatusWatcher* watcher); | 195 ScopedServerStatusWatcher* watcher); |
| 198 | 196 |
| 199 void AddListener(ServerConnectionEventListener* listener); | 197 void AddListener(ServerConnectionEventListener* listener); |
| 200 void RemoveListener(ServerConnectionEventListener* listener); | 198 void RemoveListener(ServerConnectionEventListener* listener); |
| 201 | 199 |
| 202 inline HttpResponse::ServerConnectionCode server_status() const { | 200 inline HttpResponse::ServerConnectionCode server_status() const { |
| 203 DCHECK(thread_checker_.CalledOnValidThread()); | 201 DCHECK(thread_checker_.CalledOnValidThread()); |
| 204 return server_status_; | 202 return server_status_; |
| 205 } | 203 } |
| 206 | 204 |
| 207 const std::string client_id() const { return client_id_; } | 205 const std::string client_id() const { return client_id_; } |
| 208 | 206 |
| 209 // Returns the current server parameters in server_url, port and use_ssl. | 207 // Returns the current server parameters in server_url, port and use_ssl. |
| 210 void GetServerParameters(std::string* server_url, | 208 void GetServerParameters(std::string* server_url, |
| 211 int* port, | 209 int* port, |
| 212 bool* use_ssl) const; | 210 bool* use_ssl) const; |
| 213 | 211 |
| 214 std::string GetServerHost() const; | 212 std::string GetServerHost() const; |
| 215 | 213 |
| 216 // Factory method to create an Connection object we can use for | 214 // Factory method to create an Connection object we can use for |
| 217 // communication with the server. | 215 // communication with the server. |
| 218 virtual scoped_ptr<Connection> MakeConnection(); | 216 virtual Connection* MakeConnection(); |
| 217 |
| 218 // Aborts any active HTTP POST request. |
| 219 // We expect this to get called on a different thread than the valid |
| 220 // ThreadChecker thread, as we want to kill any pending http traffic without |
| 221 // having to wait for the request to complete. |
| 222 void TerminateAllIO(); |
| 219 | 223 |
| 220 void set_client_id(const std::string& client_id) { | 224 void set_client_id(const std::string& client_id) { |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | 225 DCHECK(thread_checker_.CalledOnValidThread()); |
| 222 DCHECK(client_id_.empty()); | 226 DCHECK(client_id_.empty()); |
| 223 client_id_.assign(client_id); | 227 client_id_.assign(client_id); |
| 224 } | 228 } |
| 225 | 229 |
| 226 // Sets a new auth token and time. | 230 // Sets a new auth token and time. |
| 227 bool SetAuthToken(const std::string& auth_token); | 231 bool SetAuthToken(const std::string& auth_token); |
| 228 | 232 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // An internal helper to clear our auth_token_ and cache the old version | 265 // An internal helper to clear our auth_token_ and cache the old version |
| 262 // in |previously_invalidated_token_| to shelter us from retrying with a | 266 // in |previously_invalidated_token_| to shelter us from retrying with a |
| 263 // known bad token. | 267 // known bad token. |
| 264 void InvalidateAndClearAuthToken(); | 268 void InvalidateAndClearAuthToken(); |
| 265 | 269 |
| 266 // Helper to check terminated flags and build a Connection object, installing | 270 // Helper to check terminated flags and build a Connection object, installing |
| 267 // it as the |active_connection_|. If this ServerConnectionManager has been | 271 // it as the |active_connection_|. If this ServerConnectionManager has been |
| 268 // terminated, this will return NULL. | 272 // terminated, this will return NULL. |
| 269 Connection* MakeActiveConnection(); | 273 Connection* MakeActiveConnection(); |
| 270 | 274 |
| 275 // Called by Connection objects as they are destroyed to allow the |
| 276 // ServerConnectionManager to cleanup active connections. |
| 277 void OnConnectionDestroyed(Connection* connection); |
| 278 |
| 271 // The sync_server_ is the server that requests will be made to. | 279 // The sync_server_ is the server that requests will be made to. |
| 272 std::string sync_server_; | 280 std::string sync_server_; |
| 273 | 281 |
| 274 // The sync_server_port_ is the port that HTTP requests will be made on. | 282 // The sync_server_port_ is the port that HTTP requests will be made on. |
| 275 int sync_server_port_; | 283 int sync_server_port_; |
| 276 | 284 |
| 277 // The unique id of the user's client. | 285 // The unique id of the user's client. |
| 278 std::string client_id_; | 286 std::string client_id_; |
| 279 | 287 |
| 280 // Indicates whether or not requests should be made using HTTPS. | 288 // Indicates whether or not requests should be made using HTTPS. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 293 | 301 |
| 294 // The previous auth token that is invalid now. | 302 // The previous auth token that is invalid now. |
| 295 std::string previously_invalidated_token; | 303 std::string previously_invalidated_token; |
| 296 | 304 |
| 297 ObserverList<ServerConnectionEventListener> listeners_; | 305 ObserverList<ServerConnectionEventListener> listeners_; |
| 298 | 306 |
| 299 HttpResponse::ServerConnectionCode server_status_; | 307 HttpResponse::ServerConnectionCode server_status_; |
| 300 | 308 |
| 301 base::ThreadChecker thread_checker_; | 309 base::ThreadChecker thread_checker_; |
| 302 | 310 |
| 303 CancelationSignal* const cancelation_signal_; | 311 // Protects all variables below to allow bailing out of active connections. |
| 312 base::Lock terminate_connection_lock_; |
| 313 |
| 314 // If true, we've been told to terminate IO and expect to be destroyed |
| 315 // shortly. No future network requests will be made. |
| 316 bool terminated_; |
| 317 |
| 318 // A non-owning pointer to any active http connection, so that we can abort |
| 319 // it if necessary. |
| 320 Connection* active_connection_; |
| 304 | 321 |
| 305 private: | 322 private: |
| 306 friend class Connection; | 323 friend class Connection; |
| 307 friend class ScopedServerStatusWatcher; | 324 friend class ScopedServerStatusWatcher; |
| 308 | 325 |
| 309 // A class to help manage the active connection. It handles the registration | 326 // A class to help deal with cleaning up active Connection objects when (for |
| 310 // and unregistration with the CancelationSignal. It also takes ownership of | 327 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper |
| 311 // the connection and will delete it if the abort signal was sent early or | 328 // informs the ServerConnectionManager before the Connection object it takes |
| 312 // when this class goes out of scope. | 329 // ownership of is destroyed. |
| 313 class ScopedConnectionHelper : public CancelationObserver { | 330 class ScopedConnectionHelper { |
| 314 public: | 331 public: |
| 315 ScopedConnectionHelper(CancelationSignal* cancelation_signal, | 332 // |manager| must outlive this. Takes ownership of |connection|. |
| 316 scoped_ptr<Connection> connection); | 333 ScopedConnectionHelper(ServerConnectionManager* manager, |
| 317 virtual ~ScopedConnectionHelper(); | 334 Connection* connection); |
| 335 ~ScopedConnectionHelper(); |
| 318 Connection* get(); | 336 Connection* get(); |
| 319 | |
| 320 // Called from a different thread when the CancelationObserver's | |
| 321 // RequestStop() is called and this class has been registered as a handler. | |
| 322 // | |
| 323 // Marked final because there's no way to safely override it. See comment | |
| 324 // in this class' destructor. | |
| 325 virtual void OnStopRequested() OVERRIDE FINAL; | |
| 326 | |
| 327 private: | 337 private: |
| 328 CancelationSignal* const cancelation_signal_; | 338 ServerConnectionManager* manager_; |
| 329 scoped_ptr<Connection> connection_; | 339 scoped_ptr<Connection> connection_; |
| 330 | |
| 331 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); | 340 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); |
| 332 }; | 341 }; |
| 333 | 342 |
| 334 void NotifyStatusChanged(); | 343 void NotifyStatusChanged(); |
| 335 | 344 |
| 336 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 345 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
| 337 }; | 346 }; |
| 338 | 347 |
| 339 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 348 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
| 340 | 349 |
| 341 } // namespace syncer | 350 } // namespace syncer |
| 342 | 351 |
| 343 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 352 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
| OLD | NEW |