| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 SYNC_SERVER_ERROR, | 53 SYNC_SERVER_ERROR, |
| 54 | 54 |
| 55 // SYNC_AUTH_ERROR is returned when the HTTP status code indicates that an | 55 // SYNC_AUTH_ERROR is returned when the HTTP status code indicates that an |
| 56 // auth error has occured (i.e. a 401 or sync-specific AUTH_INVALID | 56 // auth error has occured (i.e. a 401 or sync-specific AUTH_INVALID |
| 57 // response) | 57 // response) |
| 58 // TODO(tim): Caring about AUTH_INVALID is a layering violation. But | 58 // TODO(tim): Caring about AUTH_INVALID is a layering violation. But |
| 59 // this app-specific logic is being added as a stable branch hotfix so | 59 // this app-specific logic is being added as a stable branch hotfix so |
| 60 // minimal changes prevail for the moment. Fix this! Bug 35060. | 60 // minimal changes prevail for the moment. Fix this! Bug 35060. |
| 61 SYNC_AUTH_ERROR, | 61 SYNC_AUTH_ERROR, |
| 62 | 62 |
| 63 // All the following connection codes are valid responses from the server. | |
| 64 // Means the server is up. If you update this list, be sure to also update | |
| 65 // IsGoodReplyFromServer(). | |
| 66 | |
| 67 // SERVER_CONNECTION_OK is returned when request was handled correctly. | 63 // SERVER_CONNECTION_OK is returned when request was handled correctly. |
| 68 SERVER_CONNECTION_OK, | 64 SERVER_CONNECTION_OK, |
| 69 | 65 |
| 70 // RETRY is returned when a Commit request fails with a RETRY response from | 66 // RETRY is returned when a Commit request fails with a RETRY response from |
| 71 // the server. | 67 // the server. |
| 72 // | 68 // |
| 73 // TODO(idana): the server no longer returns RETRY so we should remove this | 69 // TODO(idana): the server no longer returns RETRY so we should remove this |
| 74 // value. | 70 // value. |
| 75 RETRY, | 71 RETRY, |
| 76 }; | 72 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 89 | 85 |
| 90 // Identifies the type of failure, if any. | 86 // Identifies the type of failure, if any. |
| 91 ServerConnectionCode server_status; | 87 ServerConnectionCode server_status; |
| 92 | 88 |
| 93 HttpResponse(); | 89 HttpResponse(); |
| 94 | 90 |
| 95 static const char* GetServerConnectionCodeString( | 91 static const char* GetServerConnectionCodeString( |
| 96 ServerConnectionCode code); | 92 ServerConnectionCode code); |
| 97 }; | 93 }; |
| 98 | 94 |
| 99 inline bool IsGoodReplyFromServer(HttpResponse::ServerConnectionCode code) { | |
| 100 return code >= HttpResponse::SERVER_CONNECTION_OK; | |
| 101 } | |
| 102 | |
| 103 struct ServerConnectionEvent { | 95 struct ServerConnectionEvent { |
| 104 HttpResponse::ServerConnectionCode connection_code; | 96 HttpResponse::ServerConnectionCode connection_code; |
| 105 bool server_reachable; | 97 explicit ServerConnectionEvent(HttpResponse::ServerConnectionCode code) : |
| 106 ServerConnectionEvent(HttpResponse::ServerConnectionCode code, | 98 connection_code(code) {} |
| 107 bool server_reachable) : | |
| 108 connection_code(code), server_reachable(server_reachable) {} | |
| 109 }; | 99 }; |
| 110 | 100 |
| 111 class ServerConnectionEventListener { | 101 class ServerConnectionEventListener { |
| 112 public: | 102 public: |
| 113 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; | 103 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; |
| 114 protected: | 104 protected: |
| 115 virtual ~ServerConnectionEventListener() {} | 105 virtual ~ServerConnectionEventListener() {} |
| 116 }; | 106 }; |
| 117 | 107 |
| 118 class ServerConnectionManager; | 108 class ServerConnectionManager; |
| 119 // A helper class that automatically notifies when the status changes. | 109 // A helper class that automatically notifies when the status changes. |
| 120 // TODO(tim): This class shouldn't be exposed outside of the implementation, | 110 // TODO(tim): This class shouldn't be exposed outside of the implementation, |
| 121 // bug 35060. | 111 // bug 35060. |
| 122 class ScopedServerStatusWatcher : public base::NonThreadSafe { | 112 class ScopedServerStatusWatcher : public base::NonThreadSafe { |
| 123 public: | 113 public: |
| 124 ScopedServerStatusWatcher(ServerConnectionManager* conn_mgr, | 114 ScopedServerStatusWatcher(ServerConnectionManager* conn_mgr, |
| 125 HttpResponse* response); | 115 HttpResponse* response); |
| 126 virtual ~ScopedServerStatusWatcher(); | 116 virtual ~ScopedServerStatusWatcher(); |
| 127 private: | 117 private: |
| 128 ServerConnectionManager* const conn_mgr_; | 118 ServerConnectionManager* const conn_mgr_; |
| 129 HttpResponse* const response_; | 119 HttpResponse* const response_; |
| 130 bool server_reachable_; | |
| 131 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); | 120 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); |
| 132 }; | 121 }; |
| 133 | 122 |
| 134 // Use this class to interact with the sync server. | 123 // Use this class to interact with the sync server. |
| 135 // The ServerConnectionManager currently supports POSTing protocol buffers. | 124 // The ServerConnectionManager currently supports POSTing protocol buffers. |
| 136 // | 125 // |
| 137 class ServerConnectionManager { | 126 class ServerConnectionManager { |
| 138 public: | 127 public: |
| 139 // buffer_in - will be POSTed | 128 // buffer_in - will be POSTed |
| 140 // buffer_out - string will be overwritten with response | 129 // buffer_out - string will be overwritten with response |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 183 |
| 195 virtual ~ServerConnectionManager(); | 184 virtual ~ServerConnectionManager(); |
| 196 | 185 |
| 197 // POSTS buffer_in and reads a response into buffer_out. Uses our currently | 186 // POSTS buffer_in and reads a response into buffer_out. Uses our currently |
| 198 // set auth token in our headers. | 187 // set auth token in our headers. |
| 199 // | 188 // |
| 200 // Returns true if executed successfully. | 189 // Returns true if executed successfully. |
| 201 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, | 190 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, |
| 202 ScopedServerStatusWatcher* watcher); | 191 ScopedServerStatusWatcher* watcher); |
| 203 | 192 |
| 204 // Checks the time on the server. Returns false if the request failed. |time| | |
| 205 // is an out parameter that stores the value returned from the server. | |
| 206 virtual bool CheckTime(int32* out_time); | |
| 207 | |
| 208 // Returns true if sync_server_ is reachable. This method verifies that the | |
| 209 // server is pingable and that traffic can be sent to and from it. | |
| 210 virtual bool IsServerReachable(); | |
| 211 | |
| 212 // Returns true if user has been successfully authenticated. | |
| 213 virtual bool IsUserAuthenticated(); | |
| 214 | |
| 215 // Updates status and broadcasts events on change. | |
| 216 bool CheckServerReachable(); | |
| 217 | |
| 218 void AddListener(ServerConnectionEventListener* listener); | 193 void AddListener(ServerConnectionEventListener* listener); |
| 219 void RemoveListener(ServerConnectionEventListener* listener); | 194 void RemoveListener(ServerConnectionEventListener* listener); |
| 220 | 195 |
| 221 inline std::string user_agent() const { return user_agent_; } | 196 inline std::string user_agent() const { return user_agent_; } |
| 222 | 197 |
| 223 inline HttpResponse::ServerConnectionCode server_status() const { | 198 inline HttpResponse::ServerConnectionCode server_status() const { |
| 224 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 225 return server_status_; | 200 return server_status_; |
| 226 } | 201 } |
| 227 | 202 |
| 228 inline bool server_reachable() const { return server_reachable_; } | |
| 229 | |
| 230 const std::string client_id() const { return client_id_; } | 203 const std::string client_id() const { return client_id_; } |
| 231 | 204 |
| 232 // This changes the server info used by the connection manager. This allows | |
| 233 // a single client instance to talk to different backing servers. This is | |
| 234 // typically called during / after authentication so that the server url | |
| 235 // can be a function of the user's login id. | |
| 236 void SetServerParameters(const std::string& server_url, | |
| 237 int port, | |
| 238 bool use_ssl); | |
| 239 | |
| 240 // Returns the current server parameters in server_url, port and use_ssl. | 205 // Returns the current server parameters in server_url, port and use_ssl. |
| 241 void GetServerParameters(std::string* server_url, | 206 void GetServerParameters(std::string* server_url, |
| 242 int* port, | 207 int* port, |
| 243 bool* use_ssl) const; | 208 bool* use_ssl) const; |
| 244 | 209 |
| 245 std::string GetServerHost() const; | 210 std::string GetServerHost() const; |
| 246 | 211 |
| 247 // Factory method to create an Connection object we can use for | 212 // Factory method to create an Connection object we can use for |
| 248 // communication with the server. | 213 // communication with the server. |
| 249 virtual Connection* MakeConnection(); | 214 virtual Connection* MakeConnection(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 273 | 238 |
| 274 void InvalidateAndClearAuthToken() { | 239 void InvalidateAndClearAuthToken() { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 240 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 // Copy over the token to previous invalid token. | 241 // Copy over the token to previous invalid token. |
| 277 if (!auth_token_.empty()) { | 242 if (!auth_token_.empty()) { |
| 278 previously_invalidated_token.assign(auth_token_); | 243 previously_invalidated_token.assign(auth_token_); |
| 279 auth_token_ = std::string(); | 244 auth_token_ = std::string(); |
| 280 } | 245 } |
| 281 } | 246 } |
| 282 | 247 |
| 248 bool HasInvalidAuthToken() { |
| 249 return auth_token_.empty(); |
| 250 } |
| 251 |
| 283 const std::string auth_token() const { | 252 const std::string auth_token() const { |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | 253 DCHECK(thread_checker_.CalledOnValidThread()); |
| 285 return auth_token_; | 254 return auth_token_; |
| 286 } | 255 } |
| 287 | 256 |
| 288 protected: | 257 protected: |
| 289 inline std::string proto_sync_path() const { | 258 inline std::string proto_sync_path() const { |
| 290 return proto_sync_path_; | 259 return proto_sync_path_; |
| 291 } | 260 } |
| 292 | 261 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 323 // The user-agent string for HTTP. | 292 // The user-agent string for HTTP. |
| 324 std::string user_agent_; | 293 std::string user_agent_; |
| 325 | 294 |
| 326 // Indicates whether or not requests should be made using HTTPS. | 295 // Indicates whether or not requests should be made using HTTPS. |
| 327 bool use_ssl_; | 296 bool use_ssl_; |
| 328 | 297 |
| 329 // The paths we post to. | 298 // The paths we post to. |
| 330 std::string proto_sync_path_; | 299 std::string proto_sync_path_; |
| 331 std::string get_time_path_; | 300 std::string get_time_path_; |
| 332 | 301 |
| 333 // The auth token to use in authenticated requests. Set by the AuthWatcher. | 302 // The auth token to use in authenticated requests. |
| 334 std::string auth_token_; | 303 std::string auth_token_; |
| 335 | 304 |
| 336 // The previous auth token that is invalid now. | 305 // The previous auth token that is invalid now. |
| 337 std::string previously_invalidated_token; | 306 std::string previously_invalidated_token; |
| 338 | 307 |
| 339 ObserverList<ServerConnectionEventListener> listeners_; | 308 ObserverList<ServerConnectionEventListener> listeners_; |
| 340 | 309 |
| 341 HttpResponse::ServerConnectionCode server_status_; | 310 HttpResponse::ServerConnectionCode server_status_; |
| 342 bool server_reachable_; | |
| 343 | 311 |
| 344 base::ThreadChecker thread_checker_; | 312 base::ThreadChecker thread_checker_; |
| 345 | 313 |
| 346 // Protects all variables below to allow bailing out of active connections. | 314 // Protects all variables below to allow bailing out of active connections. |
| 347 base::Lock terminate_connection_lock_; | 315 base::Lock terminate_connection_lock_; |
| 348 | 316 |
| 349 // If true, we've been told to terminate IO and expect to be destroyed | 317 // If true, we've been told to terminate IO and expect to be destroyed |
| 350 // shortly. No future network requests will be made. | 318 // shortly. No future network requests will be made. |
| 351 bool terminated_; | 319 bool terminated_; |
| 352 | 320 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // settings. | 352 // settings. |
| 385 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, | 353 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, |
| 386 syncable::Directory* manager, | 354 syncable::Directory* manager, |
| 387 const std::string& share); | 355 const std::string& share); |
| 388 | 356 |
| 389 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 357 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
| 390 | 358 |
| 391 } // namespace browser_sync | 359 } // namespace browser_sync |
| 392 | 360 |
| 393 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 361 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
| OLD | NEW |