Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/browser/sync/engine/net/server_connection_manager.h

Issue 7731002: sync: make ServerConnectionManager explicitly NonThreadSafe (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: dcheck Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/sync/engine/net/server_connection_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 11
12 #include "base/atomicops.h" 12 #include "base/atomicops.h"
13 #include "base/observer_list_threadsafe.h" 13 #include "base/observer_list.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/threading/non_thread_safe.h"
15 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
16 #include "chrome/browser/sync/syncable/syncable_id.h" 17 #include "chrome/browser/sync/syncable/syncable_id.h"
17 #include "chrome/common/net/http_return.h" 18 #include "chrome/common/net/http_return.h"
18 19
19 namespace syncable { 20 namespace syncable {
20 class WriteTransaction; 21 class WriteTransaction;
21 class DirectoryManager; 22 class DirectoryManager;
22 } 23 }
23 24
24 namespace sync_pb { 25 namespace sync_pb {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 public: 118 public:
118 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; 119 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0;
119 protected: 120 protected:
120 virtual ~ServerConnectionEventListener() {} 121 virtual ~ServerConnectionEventListener() {}
121 }; 122 };
122 123
123 class ServerConnectionManager; 124 class ServerConnectionManager;
124 // A helper class that automatically notifies when the status changes. 125 // A helper class that automatically notifies when the status changes.
125 // TODO(tim): This class shouldn't be exposed outside of the implementation, 126 // TODO(tim): This class shouldn't be exposed outside of the implementation,
126 // bug 35060. 127 // bug 35060.
127 class ScopedServerStatusWatcher { 128 class ScopedServerStatusWatcher : public base::NonThreadSafe {
128 public: 129 public:
129 ScopedServerStatusWatcher(ServerConnectionManager* conn_mgr, 130 ScopedServerStatusWatcher(ServerConnectionManager* conn_mgr,
130 HttpResponse* response); 131 HttpResponse* response);
131 ~ScopedServerStatusWatcher(); 132 virtual ~ScopedServerStatusWatcher();
132 private: 133 private:
133 ServerConnectionManager* const conn_mgr_; 134 ServerConnectionManager* const conn_mgr_;
134 HttpResponse* const response_; 135 HttpResponse* const response_;
135 // TODO(tim): Should this be Barrier:AtomicIncrement?
136 base::subtle::AtomicWord reset_count_;
137 bool server_reachable_; 136 bool server_reachable_;
138 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); 137 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher);
139 }; 138 };
140 139
141 // Use this class to interact with the sync server. 140 // Use this class to interact with the sync server.
142 // The ServerConnectionManager currently supports POSTing protocol buffers. 141 // The ServerConnectionManager currently supports POSTing protocol buffers.
143 // 142 //
144 // *** This class is thread safe. In fact, you should consider creating only 143 class ServerConnectionManager : public base::NonThreadSafe {
145 // one instance for every server that you need to talk to.
146 class ServerConnectionManager {
147 public: 144 public:
148 // buffer_in - will be POSTed 145 // buffer_in - will be POSTed
149 // buffer_out - string will be overwritten with response 146 // buffer_out - string will be overwritten with response
150 struct PostBufferParams { 147 struct PostBufferParams {
151 const std::string& buffer_in; 148 const std::string& buffer_in;
152 std::string* buffer_out; 149 std::string* buffer_out;
153 HttpResponse* response; 150 HttpResponse* response;
154 RequestTimingInfo* timing_info; 151 RequestTimingInfo* timing_info;
155 }; 152 };
156 153
(...skipping 22 matching lines...) Expand all
179 RequestTimingInfo* timing_info() { return timing_info_; } 176 RequestTimingInfo* timing_info() { return timing_info_; }
180 177
181 protected: 178 protected:
182 std::string MakeConnectionURL(const std::string& sync_server, 179 std::string MakeConnectionURL(const std::string& sync_server,
183 const std::string& path, 180 const std::string& path,
184 bool use_ssl) const; 181 bool use_ssl) const;
185 182
186 void GetServerParams(std::string* server, 183 void GetServerParams(std::string* server,
187 int* server_port, 184 int* server_port,
188 bool* use_ssl) const { 185 bool* use_ssl) const {
189 base::AutoLock lock(scm_->server_parameters_mutex_);
190 server->assign(scm_->sync_server_); 186 server->assign(scm_->sync_server_);
191 *server_port = scm_->sync_server_port_; 187 *server_port = scm_->sync_server_port_;
192 *use_ssl = scm_->use_ssl_; 188 *use_ssl = scm_->use_ssl_;
193 } 189 }
194 190
195 std::string buffer_; 191 std::string buffer_;
196 ServerConnectionManager* scm_; 192 ServerConnectionManager* scm_;
197 193
198 private: 194 private:
199 int ReadResponse(void* buffer, int length); 195 int ReadResponse(void* buffer, int length);
(...skipping 22 matching lines...) Expand all
222 // Returns true if sync_server_ is reachable. This method verifies that the 218 // Returns true if sync_server_ is reachable. This method verifies that the
223 // server is pingable and that traffic can be sent to and from it. 219 // server is pingable and that traffic can be sent to and from it.
224 virtual bool IsServerReachable(); 220 virtual bool IsServerReachable();
225 221
226 // Returns true if user has been successfully authenticated. 222 // Returns true if user has been successfully authenticated.
227 virtual bool IsUserAuthenticated(); 223 virtual bool IsUserAuthenticated();
228 224
229 // Updates status and broadcasts events on change. 225 // Updates status and broadcasts events on change.
230 bool CheckServerReachable(); 226 bool CheckServerReachable();
231 227
232 // Signal the shutdown event to notify listeners.
233 virtual void kill();
234
235 void AddListener(ServerConnectionEventListener* listener); 228 void AddListener(ServerConnectionEventListener* listener);
236 void RemoveListener(ServerConnectionEventListener* listener); 229 void RemoveListener(ServerConnectionEventListener* listener);
237 230
238 inline std::string user_agent() const { return user_agent_; } 231 inline std::string user_agent() const { return user_agent_; }
239 232
240 inline HttpResponse::ServerConnectionCode server_status() const { 233 inline HttpResponse::ServerConnectionCode server_status() const {
234 DCHECK(CalledOnValidThread());
241 return server_status_; 235 return server_status_;
242 } 236 }
243 237
244 inline bool server_reachable() const { return server_reachable_; } 238 inline bool server_reachable() const { return server_reachable_; }
245 239
246 const std::string client_id() const { return client_id_; } 240 const std::string client_id() const { return client_id_; }
247 241
248 // This changes the server info used by the connection manager. This allows 242 // This changes the server info used by the connection manager. This allows
249 // a single client instance to talk to different backing servers. This is 243 // a single client instance to talk to different backing servers. This is
250 // typically called during / after authentication so that the server url 244 // typically called during / after authentication so that the server url
251 // can be a function of the user's login id. A side effect of this call is 245 // can be a function of the user's login id.
252 // that ResetConnection is called.
253 void SetServerParameters(const std::string& server_url, 246 void SetServerParameters(const std::string& server_url,
254 int port, 247 int port,
255 bool use_ssl); 248 bool use_ssl);
256 249
257 // Returns the current server parameters in server_url, port and use_ssl. 250 // Returns the current server parameters in server_url, port and use_ssl.
258 void GetServerParameters(std::string* server_url, 251 void GetServerParameters(std::string* server_url,
259 int* port, 252 int* port,
260 bool* use_ssl) const; 253 bool* use_ssl) const;
261 254
262 std::string GetServerHost() const; 255 std::string GetServerHost() const;
263 256
264 bool terminate_all_io() const {
265 base::AutoLock lock(terminate_all_io_mutex_);
266 return terminate_all_io_;
267 }
268
269 // Factory method to create a Post object we can use for communication with 257 // Factory method to create a Post object we can use for communication with
270 // the server. 258 // the server.
271 virtual Post* MakePost(); 259 virtual Post* MakePost();
272 260
273 void set_client_id(const std::string& client_id) { 261 void set_client_id(const std::string& client_id) {
262 DCHECK(CalledOnValidThread());
274 DCHECK(client_id_.empty()); 263 DCHECK(client_id_.empty());
275 client_id_.assign(client_id); 264 client_id_.assign(client_id);
276 } 265 }
277 266
278 // Returns true if the auth token is succesfully set and false otherwise. 267 // Returns true if the auth token is succesfully set and false otherwise.
279 bool set_auth_token(const std::string& auth_token) { 268 bool set_auth_token(const std::string& auth_token) {
280 // TODO(chron): Consider adding a message loop check here. 269 DCHECK(CalledOnValidThread());
281 base::AutoLock lock(auth_token_mutex_);
282 if (previously_invalidated_token != auth_token) { 270 if (previously_invalidated_token != auth_token) {
283 auth_token_.assign(auth_token); 271 auth_token_.assign(auth_token);
284 previously_invalidated_token = std::string(); 272 previously_invalidated_token = std::string();
285 return true; 273 return true;
286 } 274 }
287 return false; 275 return false;
288 } 276 }
289 277
290 void InvalidateAndClearAuthToken() { 278 void InvalidateAndClearAuthToken() {
279 DCHECK(CalledOnValidThread());
291 // Copy over the token to previous invalid token. 280 // Copy over the token to previous invalid token.
292 base::AutoLock lock(auth_token_mutex_);
293 if (!auth_token_.empty()) { 281 if (!auth_token_.empty()) {
294 previously_invalidated_token.assign(auth_token_); 282 previously_invalidated_token.assign(auth_token_);
295 auth_token_ = std::string(); 283 auth_token_ = std::string();
296 } 284 }
297 } 285 }
298 286
299 const std::string auth_token() const { 287 const std::string auth_token() const {
300 base::AutoLock lock(auth_token_mutex_); 288 DCHECK(CalledOnValidThread());
301 return auth_token_; 289 return auth_token_;
302 } 290 }
303 291
304 protected: 292 protected:
305 inline std::string proto_sync_path() const { 293 inline std::string proto_sync_path() const {
306 base::AutoLock lock(path_mutex_);
307 return proto_sync_path_; 294 return proto_sync_path_;
308 } 295 }
309 296
310 std::string get_time_path() const { 297 std::string get_time_path() const {
311 base::AutoLock lock(path_mutex_);
312 return get_time_path_; 298 return get_time_path_;
313 } 299 }
314 300
315 // Called wherever a failure should be taken as an indication that we may 301 // Called wherever a failure should be taken as an indication that we may
316 // be experiencing connection difficulties. 302 // be experiencing connection difficulties.
317 virtual bool IncrementErrorCount(); 303 virtual bool IncrementErrorCount();
318 304
319 // NOTE: Tests rely on this protected function being virtual. 305 // NOTE: Tests rely on this protected function being virtual.
320 // 306 //
321 // Internal PostBuffer base function. 307 // Internal PostBuffer base function.
322 virtual bool PostBufferToPath(const PostBufferParams*, 308 virtual bool PostBufferToPath(const PostBufferParams*,
323 const std::string& path, 309 const std::string& path,
324 const std::string& auth_token, 310 const std::string& auth_token,
325 ScopedServerStatusWatcher* watcher); 311 ScopedServerStatusWatcher* watcher);
326 312
327 // Protects access to sync_server_, sync_server_port_ and use_ssl_:
328 mutable base::Lock server_parameters_mutex_;
329
330 // The sync_server_ is the server that requests will be made to. 313 // The sync_server_ is the server that requests will be made to.
331 std::string sync_server_; 314 std::string sync_server_;
332 315
333 // The sync_server_port_ is the port that HTTP requests will be made on. 316 // The sync_server_port_ is the port that HTTP requests will be made on.
334 int sync_server_port_; 317 int sync_server_port_;
335 318
336 // The unique id of the user's client. 319 // The unique id of the user's client.
337 std::string client_id_; 320 std::string client_id_;
338 321
339 // The user-agent string for HTTP. 322 // The user-agent string for HTTP.
340 std::string user_agent_; 323 std::string user_agent_;
341 324
342 // Indicates whether or not requests should be made using HTTPS. 325 // Indicates whether or not requests should be made using HTTPS.
343 bool use_ssl_; 326 bool use_ssl_;
344 327
345 // The paths we post to. 328 // The paths we post to.
346 mutable base::Lock path_mutex_;
347 std::string proto_sync_path_; 329 std::string proto_sync_path_;
348 std::string get_time_path_; 330 std::string get_time_path_;
349 331
350 mutable base::Lock auth_token_mutex_;
351 // The auth token to use in authenticated requests. Set by the AuthWatcher. 332 // The auth token to use in authenticated requests. Set by the AuthWatcher.
352 std::string auth_token_; 333 std::string auth_token_;
353 334
354 // The previous auth token that is invalid now. 335 // The previous auth token that is invalid now.
355 std::string previously_invalidated_token; 336 std::string previously_invalidated_token;
356 337
357 base::Lock error_count_mutex_; // Protects error_count_
358 int error_count_; // Tracks the number of connection errors. 338 int error_count_; // Tracks the number of connection errors.
359 339
360 scoped_refptr<ObserverListThreadSafe<ServerConnectionEventListener> > 340 ObserverList<ServerConnectionEventListener> listeners_;
361 listeners_;
362 341
363 // Volatile so various threads can call server_status() without 342 HttpResponse::ServerConnectionCode server_status_;
364 // synchronization.
365 volatile HttpResponse::ServerConnectionCode server_status_;
366 bool server_reachable_; 343 bool server_reachable_;
367 344
368 // A counter that is incremented everytime ResetAuthStatus() is called.
369 volatile base::subtle::AtomicWord reset_count_;
370
371 private: 345 private:
372 friend class Post; 346 friend class Post;
373 friend class ScopedServerStatusWatcher; 347 friend class ScopedServerStatusWatcher;
374 348
375 void NotifyStatusChanged(); 349 void NotifyStatusChanged();
376 void ResetConnection();
377 350
378 mutable base::Lock terminate_all_io_mutex_;
379 bool terminate_all_io_; // When set to true, terminate all connections asap.
380 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); 351 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager);
381 }; 352 };
382 353
383 // Fills a ClientToServerMessage with the appropriate share and birthday 354 // Fills a ClientToServerMessage with the appropriate share and birthday
384 // settings. 355 // settings.
385 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, 356 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm,
386 syncable::DirectoryManager* manager, 357 syncable::DirectoryManager* manager,
387 const std::string& share); 358 const std::string& share);
388 359
389 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); 360 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr);
390 361
391 } // namespace browser_sync 362 } // namespace browser_sync
392 363
393 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ 364 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/sync/engine/net/server_connection_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698