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