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

Side by Side Diff: sync/engine/net/server_connection_manager.cc

Issue 23189021: sync: Gracefully handle very early shutdown (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes for first set of review comments Created 7 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) 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 #include "sync/engine/net/server_connection_manager.h" 5 #include "sync/engine/net/server_connection_manager.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
16 #include "net/http/http_status_code.h" 16 #include "net/http/http_status_code.h"
17 #include "sync/engine/net/url_translator.h" 17 #include "sync/engine/net/url_translator.h"
18 #include "sync/engine/syncer.h" 18 #include "sync/engine/syncer.h"
19 #include "sync/internal_api/public/base/cancelation_signal.h"
19 #include "sync/protocol/sync.pb.h" 20 #include "sync/protocol/sync.pb.h"
20 #include "sync/syncable/directory.h" 21 #include "sync/syncable/directory.h"
21 #include "url/gurl.h" 22 #include "url/gurl.h"
22 23
23 namespace syncer { 24 namespace syncer {
24 25
25 using std::ostream; 26 using std::ostream;
26 using std::string; 27 using std::string;
27 using std::vector; 28 using std::vector;
28 29
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 if (bytes_read != response->content_length) { 108 if (bytes_read != response->content_length) {
108 LOG(ERROR) << "Mismatched content lengths, server claimed " << 109 LOG(ERROR) << "Mismatched content lengths, server claimed " <<
109 response->content_length << ", but sent " << bytes_read; 110 response->content_length << ", but sent " << bytes_read;
110 response->server_status = HttpResponse::IO_ERROR; 111 response->server_status = HttpResponse::IO_ERROR;
111 return false; 112 return false;
112 } 113 }
113 return true; 114 return true;
114 } 115 }
115 116
116 ServerConnectionManager::ScopedConnectionHelper::ScopedConnectionHelper( 117 ServerConnectionManager::ScopedConnectionHelper::ScopedConnectionHelper(
117 ServerConnectionManager* manager, Connection* connection) 118 CancelationSignal* signaller, scoped_ptr<Connection> connection)
118 : manager_(manager), connection_(connection) {} 119 : cancelation_signal_(signaller), connection_(connection.Pass()) {
120 if (!cancelation_signal_->TryRegisterHandler(this)) {
121 connection_.reset();
122 }
123 }
124
125 // This function may be called from another thread.
126 void ServerConnectionManager::ScopedConnectionHelper::OnStopRequested() {
127 DCHECK(connection_);
128 connection_->Abort();
129 }
119 130
120 ServerConnectionManager::ScopedConnectionHelper::~ScopedConnectionHelper() { 131 ServerConnectionManager::ScopedConnectionHelper::~ScopedConnectionHelper() {
121 if (connection_) 132 // We should be registered iff connection_.get() != NULL.
tim (not reviewing) 2013/09/06 21:47:34 Is this true in tests as well? The SCM::MakeConne
rlarocque 2013/09/06 22:42:53 Good point. I wasn't thinking about the tests. I
122 manager_->OnConnectionDestroyed(connection_.get()); 133 if (connection_) {
tim (not reviewing) 2013/09/06 21:47:34 I'd rather use connection_.get() so the scoped_ptr
rlarocque 2013/09/06 22:42:53 I've changed this to connection_.get(). What do y
123 connection_.reset(); 134 // It is important that this be called before this destructor completes.
135 // Until the unregistration is complete, it's possible that the virtual
136 // OnStopRequested() function may be called from a different thread. We
137 // need to unregister it before destruction modifies our vptr.
138 cancelation_signal_->UnregisterHandler(this);
139 }
124 } 140 }
125 141
126 ServerConnectionManager::Connection* 142 ServerConnectionManager::Connection*
127 ServerConnectionManager::ScopedConnectionHelper::get() { 143 ServerConnectionManager::ScopedConnectionHelper::get() {
128 return connection_.get(); 144 return connection_.get();
129 } 145 }
130 146
131 namespace { 147 namespace {
132 148
133 string StripTrailingSlash(const string& s) { 149 string StripTrailingSlash(const string& s) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 186 }
171 187
172 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { 188 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() {
173 conn_mgr_->SetServerStatus(response_->server_status); 189 conn_mgr_->SetServerStatus(response_->server_status);
174 } 190 }
175 191
176 ServerConnectionManager::ServerConnectionManager( 192 ServerConnectionManager::ServerConnectionManager(
177 const string& server, 193 const string& server,
178 int port, 194 int port,
179 bool use_ssl, 195 bool use_ssl,
180 bool use_oauth2_token) 196 bool use_oauth2_token,
197 CancelationSignal* cancelation_signal)
181 : sync_server_(server), 198 : sync_server_(server),
182 sync_server_port_(port), 199 sync_server_port_(port),
183 use_ssl_(use_ssl), 200 use_ssl_(use_ssl),
184 use_oauth2_token_(use_oauth2_token), 201 use_oauth2_token_(use_oauth2_token),
185 proto_sync_path_(kSyncServerSyncPath), 202 proto_sync_path_(kSyncServerSyncPath),
186 server_status_(HttpResponse::NONE), 203 server_status_(HttpResponse::NONE),
187 terminated_(false), 204 cancelation_signal_(cancelation_signal) {
188 active_connection_(NULL) {
189 } 205 }
190 206
191 ServerConnectionManager::~ServerConnectionManager() { 207 ServerConnectionManager::~ServerConnectionManager() {
192 } 208 }
193 209
194 ServerConnectionManager::Connection*
195 ServerConnectionManager::MakeActiveConnection() {
196 base::AutoLock lock(terminate_connection_lock_);
197 DCHECK(!active_connection_);
198 if (terminated_)
199 return NULL;
200
201 active_connection_ = MakeConnection();
202 return active_connection_;
203 }
204
205 void ServerConnectionManager::OnConnectionDestroyed(Connection* connection) {
206 DCHECK(connection);
207 base::AutoLock lock(terminate_connection_lock_);
208 // |active_connection_| can be NULL already if it was aborted. Also,
209 // it can legitimately be a different Connection object if a new Connection
210 // was created after a previous one was Aborted and destroyed.
211 if (active_connection_ != connection)
212 return;
213
214 active_connection_ = NULL;
215 }
216
217 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token) { 210 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token) {
218 DCHECK(thread_checker_.CalledOnValidThread()); 211 DCHECK(thread_checker_.CalledOnValidThread());
219 if (previously_invalidated_token != auth_token) { 212 if (previously_invalidated_token != auth_token) {
220 auth_token_.assign(auth_token); 213 auth_token_.assign(auth_token);
221 previously_invalidated_token = std::string(); 214 previously_invalidated_token = std::string();
222 return true; 215 return true;
223 } 216 }
224 return false; 217 return false;
225 } 218 }
226 219
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // workaround for M29 blocker to avoid sending RPC to sync with known invalid 264 // workaround for M29 blocker to avoid sending RPC to sync with known invalid
272 // token but instead to trigger refreshing token in ProfileSyncService. Need 265 // token but instead to trigger refreshing token in ProfileSyncService. Need
273 // to clean it. 266 // to clean it.
274 if (auth_token.empty() || auth_token == "credentials_lost") { 267 if (auth_token.empty() || auth_token == "credentials_lost") {
275 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR; 268 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR;
276 return false; 269 return false;
277 } 270 }
278 271
279 // When our connection object falls out of scope, it clears itself from 272 // When our connection object falls out of scope, it clears itself from
280 // active_connection_. 273 // active_connection_.
281 ScopedConnectionHelper post(this, MakeActiveConnection()); 274 ScopedConnectionHelper post(cancelation_signal_, MakeConnection());
282 if (!post.get()) { 275 if (!post.get()) {
283 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; 276 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE;
284 return false; 277 return false;
285 } 278 }
286 279
287 // Note that |post| may be aborted by now, which will just cause Init to fail 280 // Note that |post| may be aborted by now, which will just cause Init to fail
288 // with CONNECTION_UNAVAILABLE. 281 // with CONNECTION_UNAVAILABLE.
289 bool ok = post.get()->Init( 282 bool ok = post.get()->Init(
290 path.c_str(), auth_token, params->buffer_in, &params->response); 283 path.c_str(), auth_token, params->buffer_in, &params->response);
291 284
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 DCHECK(thread_checker_.CalledOnValidThread()); 329 DCHECK(thread_checker_.CalledOnValidThread());
337 listeners_.AddObserver(listener); 330 listeners_.AddObserver(listener);
338 } 331 }
339 332
340 void ServerConnectionManager::RemoveListener( 333 void ServerConnectionManager::RemoveListener(
341 ServerConnectionEventListener* listener) { 334 ServerConnectionEventListener* listener) {
342 DCHECK(thread_checker_.CalledOnValidThread()); 335 DCHECK(thread_checker_.CalledOnValidThread());
343 listeners_.RemoveObserver(listener); 336 listeners_.RemoveObserver(listener);
344 } 337 }
345 338
346 ServerConnectionManager::Connection* ServerConnectionManager::MakeConnection() 339 scoped_ptr<ServerConnectionManager::Connection>
340 ServerConnectionManager::MakeConnection()
347 { 341 {
348 return NULL; // For testing. 342 return scoped_ptr<Connection>(); // For testing.
349 }
350
351 void ServerConnectionManager::TerminateAllIO() {
352 base::AutoLock lock(terminate_connection_lock_);
353 terminated_ = true;
354 if (active_connection_)
355 active_connection_->Abort();
356
357 // Sever our ties to this connection object. Note that it still may exist,
358 // since we don't own it, but it has been neutered.
359 active_connection_ = NULL;
360 } 343 }
361 344
362 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { 345 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) {
363 s << " Response Code (bogus on error): " << hr.response_code; 346 s << " Response Code (bogus on error): " << hr.response_code;
364 s << " Content-Length (bogus on error): " << hr.content_length; 347 s << " Content-Length (bogus on error): " << hr.content_length;
365 s << " Server Status: " 348 s << " Server Status: "
366 << HttpResponse::GetServerConnectionCodeString(hr.server_status); 349 << HttpResponse::GetServerConnectionCodeString(hr.server_status);
367 return s; 350 return s;
368 } 351 }
369 352
370 } // namespace syncer 353 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698