Chromium Code Reviews| 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/cancellation_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 CancellationSignal; | |
| 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 CancellationSignal* cancellation_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 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 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 Connection* MakeConnection(); |
| 217 | 219 |
| 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 | |
| 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 |
| 233 // Our out-of-band invalidations channel can encounter auth errors, | 229 // Our out-of-band invalidations channel can encounter auth errors, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 | 297 |
| 302 // The previous auth token that is invalid now. | 298 // The previous auth token that is invalid now. |
| 303 std::string previously_invalidated_token; | 299 std::string previously_invalidated_token; |
| 304 | 300 |
| 305 ObserverList<ServerConnectionEventListener> listeners_; | 301 ObserverList<ServerConnectionEventListener> listeners_; |
| 306 | 302 |
| 307 HttpResponse::ServerConnectionCode server_status_; | 303 HttpResponse::ServerConnectionCode server_status_; |
| 308 | 304 |
| 309 base::ThreadChecker thread_checker_; | 305 base::ThreadChecker thread_checker_; |
| 310 | 306 |
| 311 // Protects all variables below to allow bailing out of active connections. | 307 CancellationSignal* cancellation_signal_; |
|
akalin
2013/09/05 16:06:35
const?
rlarocque
2013/09/05 22:37:06
Done.
| |
| 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 | |
| 322 private: | 308 private: |
| 323 friend class Connection; | 309 friend class Connection; |
| 324 friend class ScopedServerStatusWatcher; | 310 friend class ScopedServerStatusWatcher; |
| 325 | 311 |
| 326 // A class to help deal with cleaning up active Connection objects when (for | 312 // A class to help manage the active connection. It handles the registration |
| 327 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper | 313 // and unregistration with the CancellationSignal. It also takes ownership of |
| 328 // informs the ServerConnectionManager before the Connection object it takes | 314 // the connection and will delete it if the abort signal was sent early or |
| 329 // ownership of is destroyed. | 315 // when this class goes out of scope. |
| 330 class ScopedConnectionHelper { | 316 class ScopedConnectionHelper : public CancellationObserver { |
| 331 public: | 317 public: |
| 332 // |manager| must outlive this. Takes ownership of |connection|. | 318 // Takes ownership of |connection|. |
|
akalin
2013/09/05 16:06:35
make this take scoped_ptr<Connection> instead of t
rlarocque
2013/09/05 22:37:06
Done.
| |
| 333 ScopedConnectionHelper(ServerConnectionManager* manager, | 319 ScopedConnectionHelper(CancellationSignal* cancellation_signal, |
| 334 Connection* connection); | 320 Connection* connection); |
| 335 ~ScopedConnectionHelper(); | 321 virtual ~ScopedConnectionHelper(); |
| 336 Connection* get(); | 322 Connection* get(); |
| 323 | |
| 324 // Called from a different thread when the CancellationObserver's | |
| 325 // RequestStop() is called and this class has been registered as a handler. | |
| 326 // | |
| 327 // Marked final because there's no way to safely override it. See comment | |
| 328 // in this class' destructor. | |
| 329 virtual void OnStopRequested() OVERRIDE FINAL; | |
|
akalin
2013/09/05 16:06:35
newline before private:
rlarocque
2013/09/05 22:37:06
Done.
| |
| 337 private: | 330 private: |
| 338 ServerConnectionManager* manager_; | 331 CancellationSignal* cancellation_signal_; |
|
akalin
2013/09/05 16:06:35
const?
rlarocque
2013/09/05 22:37:06
Done.
| |
| 339 scoped_ptr<Connection> connection_; | 332 scoped_ptr<Connection> connection_; |
| 340 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); | 333 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); |
|
akalin
2013/09/05 16:06:35
i believe there should be a newline before DISALLO
rlarocque
2013/09/05 22:37:06
Done.
| |
| 341 }; | 334 }; |
| 342 | 335 |
| 343 void NotifyStatusChanged(); | 336 void NotifyStatusChanged(); |
| 344 | 337 |
| 345 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 338 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
| 346 }; | 339 }; |
| 347 | 340 |
| 348 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 341 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
| 349 | 342 |
| 350 } // namespace syncer | 343 } // namespace syncer |
| 351 | 344 |
| 352 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 345 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
| OLD | NEW |