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

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

Issue 7792022: sync: abort active HTTP requests on shutdown. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review 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
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.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/threading/non_thread_safe.h"
16 #include "base/threading/thread_checker.h"
16 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
17 #include "chrome/browser/sync/syncable/syncable_id.h" 18 #include "chrome/browser/sync/syncable/syncable_id.h"
18 #include "chrome/common/net/http_return.h" 19 #include "chrome/common/net/http_return.h"
19 20
20 namespace syncable { 21 namespace syncable {
21 class WriteTransaction; 22 class WriteTransaction;
22 class DirectoryManager; 23 class DirectoryManager;
23 } 24 }
24 25
25 namespace sync_pb { 26 namespace sync_pb {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 private: 134 private:
134 ServerConnectionManager* const conn_mgr_; 135 ServerConnectionManager* const conn_mgr_;
135 HttpResponse* const response_; 136 HttpResponse* const response_;
136 bool server_reachable_; 137 bool server_reachable_;
137 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); 138 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher);
138 }; 139 };
139 140
140 // Use this class to interact with the sync server. 141 // Use this class to interact with the sync server.
141 // The ServerConnectionManager currently supports POSTing protocol buffers. 142 // The ServerConnectionManager currently supports POSTing protocol buffers.
142 // 143 //
143 class ServerConnectionManager : public base::NonThreadSafe { 144 class ServerConnectionManager {
144 public: 145 public:
145 // buffer_in - will be POSTed 146 // buffer_in - will be POSTed
146 // buffer_out - string will be overwritten with response 147 // buffer_out - string will be overwritten with response
147 struct PostBufferParams { 148 struct PostBufferParams {
148 const std::string& buffer_in; 149 const std::string& buffer_in;
149 std::string* buffer_out; 150 std::string* buffer_out;
150 HttpResponse* response; 151 HttpResponse* response;
151 RequestTimingInfo* timing_info; 152 RequestTimingInfo* timing_info;
152 }; 153 };
153 154
154 // Abstract class providing network-layer functionality to the 155 // Abstract class providing network-layer functionality to the
155 // ServerConnectionManager. Subclasses implement this using an HTTP stack of 156 // ServerConnectionManager. Subclasses implement this using an HTTP stack of
156 // their choice. 157 // their choice.
157 class Post { 158 class Connection {
158 public: 159 public:
159 explicit Post(ServerConnectionManager* scm) : scm_(scm), timing_info_(0) { 160 explicit Connection(ServerConnectionManager* scm);
160 } 161 virtual ~Connection();
161 virtual ~Post() { }
162 162
163 // Called to initialize and perform an HTTP POST. 163 // Called to initialize and perform an HTTP POST.
164 virtual bool Init(const char* path, 164 virtual bool Init(const char* path,
165 const std::string& auth_token, 165 const std::string& auth_token,
166 const std::string& payload, 166 const std::string& payload,
167 HttpResponse* response) = 0; 167 HttpResponse* response) = 0;
168 168
169 // Immediately abandons a pending HTTP POST request and unblocks caller
170 // in Init.
171 virtual void Abort() = 0;
172
169 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response, 173 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response,
170 bool require_response); 174 bool require_response);
171 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out); 175 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out);
172 176
173 void set_timing_info(RequestTimingInfo* timing_info) { 177 void set_timing_info(RequestTimingInfo* timing_info) {
174 timing_info_ = timing_info; 178 timing_info_ = timing_info;
175 } 179 }
176 RequestTimingInfo* timing_info() { return timing_info_; } 180 RequestTimingInfo* timing_info() { return timing_info_; }
177 181
178 protected: 182 protected:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 228
225 // Updates status and broadcasts events on change. 229 // Updates status and broadcasts events on change.
226 bool CheckServerReachable(); 230 bool CheckServerReachable();
227 231
228 void AddListener(ServerConnectionEventListener* listener); 232 void AddListener(ServerConnectionEventListener* listener);
229 void RemoveListener(ServerConnectionEventListener* listener); 233 void RemoveListener(ServerConnectionEventListener* listener);
230 234
231 inline std::string user_agent() const { return user_agent_; } 235 inline std::string user_agent() const { return user_agent_; }
232 236
233 inline HttpResponse::ServerConnectionCode server_status() const { 237 inline HttpResponse::ServerConnectionCode server_status() const {
234 DCHECK(CalledOnValidThread()); 238 DCHECK(thread_checker_.CalledOnValidThread());
235 return server_status_; 239 return server_status_;
236 } 240 }
237 241
238 inline bool server_reachable() const { return server_reachable_; } 242 inline bool server_reachable() const { return server_reachable_; }
239 243
240 const std::string client_id() const { return client_id_; } 244 const std::string client_id() const { return client_id_; }
241 245
242 // This changes the server info used by the connection manager. This allows 246 // This changes the server info used by the connection manager. This allows
243 // a single client instance to talk to different backing servers. This is 247 // a single client instance to talk to different backing servers. This is
244 // typically called during / after authentication so that the server url 248 // typically called during / after authentication so that the server url
245 // can be a function of the user's login id. 249 // can be a function of the user's login id.
246 void SetServerParameters(const std::string& server_url, 250 void SetServerParameters(const std::string& server_url,
247 int port, 251 int port,
248 bool use_ssl); 252 bool use_ssl);
249 253
250 // Returns the current server parameters in server_url, port and use_ssl. 254 // Returns the current server parameters in server_url, port and use_ssl.
251 void GetServerParameters(std::string* server_url, 255 void GetServerParameters(std::string* server_url,
252 int* port, 256 int* port,
253 bool* use_ssl) const; 257 bool* use_ssl) const;
254 258
255 std::string GetServerHost() const; 259 std::string GetServerHost() const;
256 260
257 // Factory method to create a Post object we can use for communication with 261 // Factory method to create an Connection object we can use for
258 // the server. 262 // communication with the server.
259 virtual Post* MakePost(); 263 virtual Connection* MakeConnection();
264
265 // Aborts any active HTTP POST request.
266 // We expect this to get called on a different thread than the valid
267 // ThreadChecker thread, as we want to kill any pending http traffic without
268 // having to wait for the request to complete.
269 void TerminateAllIO();
260 270
261 void set_client_id(const std::string& client_id) { 271 void set_client_id(const std::string& client_id) {
262 DCHECK(CalledOnValidThread()); 272 DCHECK(thread_checker_.CalledOnValidThread());
263 DCHECK(client_id_.empty()); 273 DCHECK(client_id_.empty());
264 client_id_.assign(client_id); 274 client_id_.assign(client_id);
265 } 275 }
266 276
267 // Returns true if the auth token is succesfully set and false otherwise. 277 // Returns true if the auth token is succesfully set and false otherwise.
268 bool set_auth_token(const std::string& auth_token) { 278 bool set_auth_token(const std::string& auth_token) {
269 DCHECK(CalledOnValidThread()); 279 DCHECK(thread_checker_.CalledOnValidThread());
270 if (previously_invalidated_token != auth_token) { 280 if (previously_invalidated_token != auth_token) {
271 auth_token_.assign(auth_token); 281 auth_token_.assign(auth_token);
272 previously_invalidated_token = std::string(); 282 previously_invalidated_token = std::string();
273 return true; 283 return true;
274 } 284 }
275 return false; 285 return false;
276 } 286 }
277 287
278 void InvalidateAndClearAuthToken() { 288 void InvalidateAndClearAuthToken() {
279 DCHECK(CalledOnValidThread()); 289 DCHECK(thread_checker_.CalledOnValidThread());
280 // Copy over the token to previous invalid token. 290 // Copy over the token to previous invalid token.
281 if (!auth_token_.empty()) { 291 if (!auth_token_.empty()) {
282 previously_invalidated_token.assign(auth_token_); 292 previously_invalidated_token.assign(auth_token_);
283 auth_token_ = std::string(); 293 auth_token_ = std::string();
284 } 294 }
285 } 295 }
286 296
287 const std::string auth_token() const { 297 const std::string auth_token() const {
288 DCHECK(CalledOnValidThread()); 298 DCHECK(thread_checker_.CalledOnValidThread());
289 return auth_token_; 299 return auth_token_;
290 } 300 }
291 301
292 protected: 302 protected:
293 inline std::string proto_sync_path() const { 303 inline std::string proto_sync_path() const {
294 return proto_sync_path_; 304 return proto_sync_path_;
295 } 305 }
296 306
297 std::string get_time_path() const { 307 std::string get_time_path() const {
298 return get_time_path_; 308 return get_time_path_;
299 } 309 }
300 310
301 // Called wherever a failure should be taken as an indication that we may 311 // Called wherever a failure should be taken as an indication that we may
302 // be experiencing connection difficulties. 312 // be experiencing connection difficulties.
303 virtual bool IncrementErrorCount(); 313 virtual bool IncrementErrorCount();
304 314
305 // NOTE: Tests rely on this protected function being virtual. 315 // NOTE: Tests rely on this protected function being virtual.
306 // 316 //
307 // Internal PostBuffer base function. 317 // Internal PostBuffer base function.
308 virtual bool PostBufferToPath(const PostBufferParams*, 318 virtual bool PostBufferToPath(const PostBufferParams*,
309 const std::string& path, 319 const std::string& path,
310 const std::string& auth_token, 320 const std::string& auth_token,
311 ScopedServerStatusWatcher* watcher); 321 ScopedServerStatusWatcher* watcher);
312 322
323 // Helper to check terminated flags and build a Connection object, installing
324 // it as the |active_connection_|. If this ServerConnectionManager has been
325 // terminated, this will return NULL.
326 Connection* MakeActiveConnection();
327
328 // Called by Connection objects as they are destroyed to allow the
329 // ServerConnectionManager to cleanup active connections.
330 void OnConnectionDestroyed(Connection* connection);
331
313 // The sync_server_ is the server that requests will be made to. 332 // The sync_server_ is the server that requests will be made to.
314 std::string sync_server_; 333 std::string sync_server_;
315 334
316 // The sync_server_port_ is the port that HTTP requests will be made on. 335 // The sync_server_port_ is the port that HTTP requests will be made on.
317 int sync_server_port_; 336 int sync_server_port_;
318 337
319 // The unique id of the user's client. 338 // The unique id of the user's client.
320 std::string client_id_; 339 std::string client_id_;
321 340
322 // The user-agent string for HTTP. 341 // The user-agent string for HTTP.
(...skipping 12 matching lines...) Expand all
335 // The previous auth token that is invalid now. 354 // The previous auth token that is invalid now.
336 std::string previously_invalidated_token; 355 std::string previously_invalidated_token;
337 356
338 int error_count_; // Tracks the number of connection errors. 357 int error_count_; // Tracks the number of connection errors.
339 358
340 ObserverList<ServerConnectionEventListener> listeners_; 359 ObserverList<ServerConnectionEventListener> listeners_;
341 360
342 HttpResponse::ServerConnectionCode server_status_; 361 HttpResponse::ServerConnectionCode server_status_;
343 bool server_reachable_; 362 bool server_reachable_;
344 363
364 base::ThreadChecker thread_checker_;
365
366 // Protects all variables below to allow bailing out of active connections.
367 base::Lock terminate_connection_lock_;
368
369 // If true, we've been told to terminate IO and expect to be destroyed
370 // shortly. No future network requests will be made.
371 bool terminated_;
372
373 // A non-owning pointer to any active http connection, so that we can abort
374 // it if necessary.
375 Connection* active_connection_;
376
345 private: 377 private:
346 friend class Post; 378 friend class Connection;
347 friend class ScopedServerStatusWatcher; 379 friend class ScopedServerStatusWatcher;
348 380
349 void NotifyStatusChanged(); 381 void NotifyStatusChanged();
350 382
351 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); 383 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager);
352 }; 384 };
353 385
354 // Fills a ClientToServerMessage with the appropriate share and birthday 386 // Fills a ClientToServerMessage with the appropriate share and birthday
355 // settings. 387 // settings.
356 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, 388 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm,
357 syncable::DirectoryManager* manager, 389 syncable::DirectoryManager* manager,
358 const std::string& share); 390 const std::string& share);
359 391
360 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); 392 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr);
361 393
362 } // namespace browser_sync 394 } // namespace browser_sync
363 395
364 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ 396 #endif // CHROME_BROWSER_SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698