| 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 COMPONENTS_SYNC_ENGINE_IMPL_NET_SERVER_CONNECTION_MANAGER_H_ |
| 6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 6 #define COMPONENTS_SYNC_ENGINE_IMPL_NET_SERVER_CONNECTION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <iosfwd> | 10 #include <iosfwd> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/atomicops.h" | 14 #include "base/atomicops.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "sync/base/sync_export.h" | 21 #include "components/sync/base/cancelation_observer.h" |
| 22 #include "sync/internal_api/public/base/cancelation_observer.h" | 22 #include "components/sync/base/sync_export.h" |
| 23 #include "sync/syncable/syncable_id.h" | 23 #include "components/sync/syncable/syncable_id.h" |
| 24 | 24 |
| 25 namespace sync_pb { | 25 namespace sync_pb { |
| 26 class ClientToServerMessage; | 26 class ClientToServerMessage; |
| 27 } | 27 } |
| 28 | 28 |
| 29 namespace syncer { | 29 namespace syncer { |
| 30 | 30 |
| 31 class CancelationSignal; | 31 class CancelationSignal; |
| 32 | 32 |
| 33 namespace syncable { | 33 namespace syncable { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 int64_t content_length; | 82 int64_t content_length; |
| 83 | 83 |
| 84 // The size of a download request's payload. | 84 // The size of a download request's payload. |
| 85 int64_t payload_length; | 85 int64_t payload_length; |
| 86 | 86 |
| 87 // Identifies the type of failure, if any. | 87 // Identifies the type of failure, if any. |
| 88 ServerConnectionCode server_status; | 88 ServerConnectionCode server_status; |
| 89 | 89 |
| 90 HttpResponse(); | 90 HttpResponse(); |
| 91 | 91 |
| 92 static const char* GetServerConnectionCodeString( | 92 static const char* GetServerConnectionCodeString(ServerConnectionCode code); |
| 93 ServerConnectionCode code); | |
| 94 }; | 93 }; |
| 95 | 94 |
| 96 struct ServerConnectionEvent { | 95 struct ServerConnectionEvent { |
| 97 HttpResponse::ServerConnectionCode connection_code; | 96 HttpResponse::ServerConnectionCode connection_code; |
| 98 explicit ServerConnectionEvent(HttpResponse::ServerConnectionCode code) : | 97 explicit ServerConnectionEvent(HttpResponse::ServerConnectionCode code) |
| 99 connection_code(code) {} | 98 : connection_code(code) {} |
| 100 }; | 99 }; |
| 101 | 100 |
| 102 class SYNC_EXPORT ServerConnectionEventListener { | 101 class SYNC_EXPORT ServerConnectionEventListener { |
| 103 public: | 102 public: |
| 104 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; | 103 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; |
| 104 |
| 105 protected: | 105 protected: |
| 106 virtual ~ServerConnectionEventListener() {} | 106 virtual ~ServerConnectionEventListener() {} |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 // Use this class to interact with the sync server. | 109 // Use this class to interact with the sync server. |
| 110 // The ServerConnectionManager currently supports POSTing protocol buffers. | 110 // The ServerConnectionManager currently supports POSTing protocol buffers. |
| 111 // | 111 // |
| 112 class SYNC_EXPORT ServerConnectionManager : public CancelationObserver { | 112 class SYNC_EXPORT ServerConnectionManager : public CancelationObserver { |
| 113 public: | 113 public: |
| 114 // buffer_in - will be POSTed | 114 // buffer_in - will be POSTed |
| (...skipping 15 matching lines...) Expand all Loading... |
| 130 // Called to initialize and perform an HTTP POST. | 130 // Called to initialize and perform an HTTP POST. |
| 131 virtual bool Init(const char* path, | 131 virtual bool Init(const char* path, |
| 132 const std::string& auth_token, | 132 const std::string& auth_token, |
| 133 const std::string& payload, | 133 const std::string& payload, |
| 134 HttpResponse* response) = 0; | 134 HttpResponse* response) = 0; |
| 135 | 135 |
| 136 // Immediately abandons a pending HTTP POST request and unblocks caller | 136 // Immediately abandons a pending HTTP POST request and unblocks caller |
| 137 // in Init. | 137 // in Init. |
| 138 virtual void Abort() = 0; | 138 virtual void Abort() = 0; |
| 139 | 139 |
| 140 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response, | 140 bool ReadBufferResponse(std::string* buffer_out, |
| 141 HttpResponse* response, |
| 141 bool require_response); | 142 bool require_response); |
| 142 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out); | 143 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out); |
| 143 | 144 |
| 144 protected: | 145 protected: |
| 145 std::string MakeConnectionURL(const std::string& sync_server, | 146 std::string MakeConnectionURL(const std::string& sync_server, |
| 146 const std::string& path, | 147 const std::string& path, |
| 147 bool use_ssl) const; | 148 bool use_ssl) const; |
| 148 | 149 |
| 149 void GetServerParams(std::string* server, | 150 void GetServerParams(std::string* server, |
| 150 int* server_port, | 151 int* server_port, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 197 |
| 197 void set_client_id(const std::string& client_id) { | 198 void set_client_id(const std::string& client_id) { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 DCHECK(client_id_.empty()); | 200 DCHECK(client_id_.empty()); |
| 200 client_id_.assign(client_id); | 201 client_id_.assign(client_id); |
| 201 } | 202 } |
| 202 | 203 |
| 203 // Sets a new auth token and time. | 204 // Sets a new auth token and time. |
| 204 bool SetAuthToken(const std::string& auth_token); | 205 bool SetAuthToken(const std::string& auth_token); |
| 205 | 206 |
| 206 bool HasInvalidAuthToken() { | 207 bool HasInvalidAuthToken() { return auth_token_.empty(); } |
| 207 return auth_token_.empty(); | |
| 208 } | |
| 209 | 208 |
| 210 const std::string auth_token() const { | 209 const std::string auth_token() const { |
| 211 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 212 return auth_token_; | 211 return auth_token_; |
| 213 } | 212 } |
| 214 | 213 |
| 215 protected: | 214 protected: |
| 216 inline std::string proto_sync_path() const { | 215 inline std::string proto_sync_path() const { return proto_sync_path_; } |
| 217 return proto_sync_path_; | |
| 218 } | |
| 219 | 216 |
| 220 // Updates server_status_ and notifies listeners if server_status_ changed | 217 // Updates server_status_ and notifies listeners if server_status_ changed |
| 221 void SetServerStatus(HttpResponse::ServerConnectionCode server_status); | 218 void SetServerStatus(HttpResponse::ServerConnectionCode server_status); |
| 222 | 219 |
| 223 // NOTE: Tests rely on this protected function being virtual. | 220 // NOTE: Tests rely on this protected function being virtual. |
| 224 // | 221 // |
| 225 // Internal PostBuffer base function. | 222 // Internal PostBuffer base function. |
| 226 virtual bool PostBufferToPath(PostBufferParams*, | 223 virtual bool PostBufferToPath(PostBufferParams*, |
| 227 const std::string& path, | 224 const std::string& path, |
| 228 const std::string& auth_token); | 225 const std::string& auth_token); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper | 281 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper |
| 285 // informs the ServerConnectionManager before the Connection object it takes | 282 // informs the ServerConnectionManager before the Connection object it takes |
| 286 // ownership of is destroyed. | 283 // ownership of is destroyed. |
| 287 class ScopedConnectionHelper { | 284 class ScopedConnectionHelper { |
| 288 public: | 285 public: |
| 289 // |manager| must outlive this. Takes ownership of |connection|. | 286 // |manager| must outlive this. Takes ownership of |connection|. |
| 290 ScopedConnectionHelper(ServerConnectionManager* manager, | 287 ScopedConnectionHelper(ServerConnectionManager* manager, |
| 291 Connection* connection); | 288 Connection* connection); |
| 292 ~ScopedConnectionHelper(); | 289 ~ScopedConnectionHelper(); |
| 293 Connection* get(); | 290 Connection* get(); |
| 291 |
| 294 private: | 292 private: |
| 295 ServerConnectionManager* manager_; | 293 ServerConnectionManager* manager_; |
| 296 std::unique_ptr<Connection> connection_; | 294 std::unique_ptr<Connection> connection_; |
| 297 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); | 295 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); |
| 298 }; | 296 }; |
| 299 | 297 |
| 300 void NotifyStatusChanged(); | 298 void NotifyStatusChanged(); |
| 301 | 299 |
| 302 CancelationSignal* const cancelation_signal_; | 300 CancelationSignal* const cancelation_signal_; |
| 303 bool signal_handler_registered_; | 301 bool signal_handler_registered_; |
| 304 | 302 |
| 305 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 303 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
| 306 }; | 304 }; |
| 307 | 305 |
| 308 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 306 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
| 309 | 307 |
| 310 } // namespace syncer | 308 } // namespace syncer |
| 311 | 309 |
| 312 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 310 #endif // COMPONENTS_SYNC_ENGINE_IMPL_NET_SERVER_CONNECTION_MANAGER_H_ |
| OLD | NEW |