OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
11 | 11 |
12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
13 #include "base/lock.h" | |
14 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/synchronization/lock.h" |
15 #include "chrome/browser/sync/syncable/syncable_id.h" | 15 #include "chrome/browser/sync/syncable/syncable_id.h" |
16 #include "chrome/common/deprecated/event_sys.h" | 16 #include "chrome/common/deprecated/event_sys.h" |
17 #include "chrome/common/deprecated/event_sys-inl.h" | 17 #include "chrome/common/deprecated/event_sys-inl.h" |
18 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
19 | 19 |
20 namespace syncable { | 20 namespace syncable { |
21 class WriteTransaction; | 21 class WriteTransaction; |
22 class DirectoryManager; | 22 class DirectoryManager; |
23 } | 23 } |
24 | 24 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); | 142 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); |
143 }; | 143 }; |
144 | 144 |
145 // Use this class to interact with the sync server. | 145 // Use this class to interact with the sync server. |
146 // The ServerConnectionManager currently supports POSTing protocol buffers. | 146 // The ServerConnectionManager currently supports POSTing protocol buffers. |
147 // | 147 // |
148 // *** This class is thread safe. In fact, you should consider creating only | 148 // *** This class is thread safe. In fact, you should consider creating only |
149 // one instance for every server that you need to talk to. | 149 // one instance for every server that you need to talk to. |
150 class ServerConnectionManager { | 150 class ServerConnectionManager { |
151 public: | 151 public: |
152 typedef EventChannel<ServerConnectionEvent, Lock> Channel; | 152 typedef EventChannel<ServerConnectionEvent, base::Lock> Channel; |
153 | 153 |
154 // buffer_in - will be POSTed | 154 // buffer_in - will be POSTed |
155 // buffer_out - string will be overwritten with response | 155 // buffer_out - string will be overwritten with response |
156 struct PostBufferParams { | 156 struct PostBufferParams { |
157 const std::string& buffer_in; | 157 const std::string& buffer_in; |
158 std::string* buffer_out; | 158 std::string* buffer_out; |
159 HttpResponse* response; | 159 HttpResponse* response; |
160 RequestTimingInfo* timing_info; | 160 RequestTimingInfo* timing_info; |
161 }; | 161 }; |
162 | 162 |
(...skipping 22 matching lines...) Expand all Loading... |
185 RequestTimingInfo* timing_info() { return timing_info_; } | 185 RequestTimingInfo* timing_info() { return timing_info_; } |
186 | 186 |
187 protected: | 187 protected: |
188 std::string MakeConnectionURL(const std::string& sync_server, | 188 std::string MakeConnectionURL(const std::string& sync_server, |
189 const std::string& path, | 189 const std::string& path, |
190 bool use_ssl) const; | 190 bool use_ssl) const; |
191 | 191 |
192 void GetServerParams(std::string* server, | 192 void GetServerParams(std::string* server, |
193 int* server_port, | 193 int* server_port, |
194 bool* use_ssl) const { | 194 bool* use_ssl) const { |
195 AutoLock lock(scm_->server_parameters_mutex_); | 195 base::AutoLock lock(scm_->server_parameters_mutex_); |
196 server->assign(scm_->sync_server_); | 196 server->assign(scm_->sync_server_); |
197 *server_port = scm_->sync_server_port_; | 197 *server_port = scm_->sync_server_port_; |
198 *use_ssl = scm_->use_ssl_; | 198 *use_ssl = scm_->use_ssl_; |
199 } | 199 } |
200 | 200 |
201 std::string buffer_; | 201 std::string buffer_; |
202 ServerConnectionManager* scm_; | 202 ServerConnectionManager* scm_; |
203 | 203 |
204 private: | 204 private: |
205 int ReadResponse(void* buffer, int length); | 205 int ReadResponse(void* buffer, int length); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 bool use_ssl); | 262 bool use_ssl); |
263 | 263 |
264 // Returns the current server parameters in server_url, port and use_ssl. | 264 // Returns the current server parameters in server_url, port and use_ssl. |
265 void GetServerParameters(std::string* server_url, | 265 void GetServerParameters(std::string* server_url, |
266 int* port, | 266 int* port, |
267 bool* use_ssl) const; | 267 bool* use_ssl) const; |
268 | 268 |
269 std::string GetServerHost() const; | 269 std::string GetServerHost() const; |
270 | 270 |
271 bool terminate_all_io() const { | 271 bool terminate_all_io() const { |
272 AutoLock lock(terminate_all_io_mutex_); | 272 base::AutoLock lock(terminate_all_io_mutex_); |
273 return terminate_all_io_; | 273 return terminate_all_io_; |
274 } | 274 } |
275 | 275 |
276 // Factory method to create a Post object we can use for communication with | 276 // Factory method to create a Post object we can use for communication with |
277 // the server. | 277 // the server. |
278 virtual Post* MakePost(); | 278 virtual Post* MakePost(); |
279 | 279 |
280 void set_client_id(const std::string& client_id) { | 280 void set_client_id(const std::string& client_id) { |
281 DCHECK(client_id_.empty()); | 281 DCHECK(client_id_.empty()); |
282 client_id_.assign(client_id); | 282 client_id_.assign(client_id); |
283 } | 283 } |
284 | 284 |
285 void set_auth_token(const std::string& auth_token) { | 285 void set_auth_token(const std::string& auth_token) { |
286 // TODO(chron): Consider adding a message loop check here. | 286 // TODO(chron): Consider adding a message loop check here. |
287 AutoLock lock(auth_token_mutex_); | 287 base::AutoLock lock(auth_token_mutex_); |
288 auth_token_.assign(auth_token); | 288 auth_token_.assign(auth_token); |
289 } | 289 } |
290 | 290 |
291 const std::string auth_token() const { | 291 const std::string auth_token() const { |
292 AutoLock lock(auth_token_mutex_); | 292 base::AutoLock lock(auth_token_mutex_); |
293 return auth_token_; | 293 return auth_token_; |
294 } | 294 } |
295 | 295 |
296 protected: | 296 protected: |
297 inline std::string proto_sync_path() const { | 297 inline std::string proto_sync_path() const { |
298 AutoLock lock(path_mutex_); | 298 base::AutoLock lock(path_mutex_); |
299 return proto_sync_path_; | 299 return proto_sync_path_; |
300 } | 300 } |
301 | 301 |
302 std::string get_time_path() const { | 302 std::string get_time_path() const { |
303 AutoLock lock(path_mutex_); | 303 base::AutoLock lock(path_mutex_); |
304 return get_time_path_; | 304 return get_time_path_; |
305 } | 305 } |
306 | 306 |
307 // Called wherever a failure should be taken as an indication that we may | 307 // Called wherever a failure should be taken as an indication that we may |
308 // be experiencing connection difficulties. | 308 // be experiencing connection difficulties. |
309 virtual bool IncrementErrorCount(); | 309 virtual bool IncrementErrorCount(); |
310 | 310 |
311 // NOTE: Tests rely on this protected function being virtual. | 311 // NOTE: Tests rely on this protected function being virtual. |
312 // | 312 // |
313 // Internal PostBuffer base function. | 313 // Internal PostBuffer base function. |
314 virtual bool PostBufferToPath(const PostBufferParams*, | 314 virtual bool PostBufferToPath(const PostBufferParams*, |
315 const std::string& path, | 315 const std::string& path, |
316 const std::string& auth_token, | 316 const std::string& auth_token, |
317 ScopedServerStatusWatcher* watcher); | 317 ScopedServerStatusWatcher* watcher); |
318 | 318 |
319 // Protects access to sync_server_, sync_server_port_ and use_ssl_: | 319 // Protects access to sync_server_, sync_server_port_ and use_ssl_: |
320 mutable Lock server_parameters_mutex_; | 320 mutable base::Lock server_parameters_mutex_; |
321 | 321 |
322 // The sync_server_ is the server that requests will be made to. | 322 // The sync_server_ is the server that requests will be made to. |
323 std::string sync_server_; | 323 std::string sync_server_; |
324 | 324 |
325 // The sync_server_port_ is the port that HTTP requests will be made on. | 325 // The sync_server_port_ is the port that HTTP requests will be made on. |
326 int sync_server_port_; | 326 int sync_server_port_; |
327 | 327 |
328 // The unique id of the user's client. | 328 // The unique id of the user's client. |
329 std::string client_id_; | 329 std::string client_id_; |
330 | 330 |
331 // The user-agent string for HTTP. | 331 // The user-agent string for HTTP. |
332 std::string user_agent_; | 332 std::string user_agent_; |
333 | 333 |
334 // Indicates whether or not requests should be made using HTTPS. | 334 // Indicates whether or not requests should be made using HTTPS. |
335 bool use_ssl_; | 335 bool use_ssl_; |
336 | 336 |
337 // The paths we post to. | 337 // The paths we post to. |
338 mutable Lock path_mutex_; | 338 mutable base::Lock path_mutex_; |
339 std::string proto_sync_path_; | 339 std::string proto_sync_path_; |
340 std::string get_time_path_; | 340 std::string get_time_path_; |
341 | 341 |
342 mutable Lock auth_token_mutex_; | 342 mutable base::Lock auth_token_mutex_; |
343 // The auth token to use in authenticated requests. Set by the AuthWatcher. | 343 // The auth token to use in authenticated requests. Set by the AuthWatcher. |
344 std::string auth_token_; | 344 std::string auth_token_; |
345 | 345 |
346 Lock error_count_mutex_; // Protects error_count_ | 346 base::Lock error_count_mutex_; // Protects error_count_ |
347 int error_count_; // Tracks the number of connection errors. | 347 int error_count_; // Tracks the number of connection errors. |
348 | 348 |
349 Channel* const channel_; | 349 Channel* const channel_; |
350 | 350 |
351 // Volatile so various threads can call server_status() without | 351 // Volatile so various threads can call server_status() without |
352 // synchronization. | 352 // synchronization. |
353 volatile HttpResponse::ServerConnectionCode server_status_; | 353 volatile HttpResponse::ServerConnectionCode server_status_; |
354 bool server_reachable_; | 354 bool server_reachable_; |
355 | 355 |
356 // A counter that is incremented everytime ResetAuthStatus() is called. | 356 // A counter that is incremented everytime ResetAuthStatus() is called. |
357 volatile base::subtle::AtomicWord reset_count_; | 357 volatile base::subtle::AtomicWord reset_count_; |
358 | 358 |
359 private: | 359 private: |
360 friend class Post; | 360 friend class Post; |
361 friend class ScopedServerStatusWatcher; | 361 friend class ScopedServerStatusWatcher; |
362 | 362 |
363 void NotifyStatusChanged(); | 363 void NotifyStatusChanged(); |
364 void ResetConnection(); | 364 void ResetConnection(); |
365 | 365 |
366 mutable Lock terminate_all_io_mutex_; | 366 mutable base::Lock terminate_all_io_mutex_; |
367 bool terminate_all_io_; // When set to true, terminate all connections asap. | 367 bool terminate_all_io_; // When set to true, terminate all connections asap. |
368 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 368 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
369 }; | 369 }; |
370 | 370 |
371 // Fills a ClientToServerMessage with the appropriate share and birthday | 371 // Fills a ClientToServerMessage with the appropriate share and birthday |
372 // settings. | 372 // settings. |
373 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, | 373 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, |
374 syncable::DirectoryManager* manager, | 374 syncable::DirectoryManager* manager, |
375 const std::string& share); | 375 const std::string& share); |
376 | 376 |
377 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 377 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
378 | 378 |
379 } // namespace browser_sync | 379 } // namespace browser_sync |
380 | 380 |
381 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 381 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
OLD | NEW |