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