Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/cancellation_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 Loading... | |
| 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 CancellationSignal* signaller, Connection* connection) |
| 118 : manager_(manager), connection_(connection) {} | 119 : cancellation_signal_(signaller), connection_(connection) { |
| 120 if (!cancellation_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. |
| 122 manager_->OnConnectionDestroyed(connection_.get()); | 133 if (connection_) { |
| 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 cancellation_signal_->UnregisterHandler(this); | |
| 139 } | |
| 140 | |
| 141 if (connection_) { | |
|
akalin
2013/09/05 15:35:28
i don't think this clause is needed
rlarocque
2013/09/05 22:37:06
You're right. Fixed.
| |
| 142 connection_.reset(); | |
| 143 } | |
| 124 } | 144 } |
| 125 | 145 |
| 126 ServerConnectionManager::Connection* | 146 ServerConnectionManager::Connection* |
| 127 ServerConnectionManager::ScopedConnectionHelper::get() { | 147 ServerConnectionManager::ScopedConnectionHelper::get() { |
| 128 return connection_.get(); | 148 return connection_.get(); |
| 129 } | 149 } |
| 130 | 150 |
| 131 namespace { | 151 namespace { |
| 132 | 152 |
| 133 string StripTrailingSlash(const string& s) { | 153 string StripTrailingSlash(const string& s) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 } | 190 } |
| 171 | 191 |
| 172 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { | 192 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { |
| 173 conn_mgr_->SetServerStatus(response_->server_status); | 193 conn_mgr_->SetServerStatus(response_->server_status); |
| 174 } | 194 } |
| 175 | 195 |
| 176 ServerConnectionManager::ServerConnectionManager( | 196 ServerConnectionManager::ServerConnectionManager( |
| 177 const string& server, | 197 const string& server, |
| 178 int port, | 198 int port, |
| 179 bool use_ssl, | 199 bool use_ssl, |
| 180 bool use_oauth2_token) | 200 bool use_oauth2_token, |
| 201 CancellationSignal* cancellation_signal) | |
| 181 : sync_server_(server), | 202 : sync_server_(server), |
| 182 sync_server_port_(port), | 203 sync_server_port_(port), |
| 183 use_ssl_(use_ssl), | 204 use_ssl_(use_ssl), |
| 184 use_oauth2_token_(use_oauth2_token), | 205 use_oauth2_token_(use_oauth2_token), |
| 185 proto_sync_path_(kSyncServerSyncPath), | 206 proto_sync_path_(kSyncServerSyncPath), |
| 186 server_status_(HttpResponse::NONE), | 207 server_status_(HttpResponse::NONE), |
| 187 terminated_(false), | 208 cancellation_signal_(cancellation_signal) { |
| 188 active_connection_(NULL) { | |
| 189 } | 209 } |
| 190 | 210 |
| 191 ServerConnectionManager::~ServerConnectionManager() { | 211 ServerConnectionManager::~ServerConnectionManager() { |
| 192 } | 212 } |
| 193 | 213 |
| 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) { | 214 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token) { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | 215 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 if (previously_invalidated_token != auth_token) { | 216 if (previously_invalidated_token != auth_token) { |
| 220 auth_token_.assign(auth_token); | 217 auth_token_.assign(auth_token); |
| 221 previously_invalidated_token = std::string(); | 218 previously_invalidated_token = std::string(); |
| 222 return true; | 219 return true; |
| 223 } | 220 } |
| 224 return false; | 221 return false; |
| 225 } | 222 } |
| 226 | 223 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 // workaround for M29 blocker to avoid sending RPC to sync with known invalid | 268 // workaround for M29 blocker to avoid sending RPC to sync with known invalid |
| 272 // token but instead to trigger refreshing token in ProfileSyncService. Need | 269 // token but instead to trigger refreshing token in ProfileSyncService. Need |
| 273 // to clean it. | 270 // to clean it. |
| 274 if (auth_token.empty() || auth_token == "credentials_lost") { | 271 if (auth_token.empty() || auth_token == "credentials_lost") { |
| 275 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR; | 272 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR; |
| 276 return false; | 273 return false; |
| 277 } | 274 } |
| 278 | 275 |
| 279 // When our connection object falls out of scope, it clears itself from | 276 // When our connection object falls out of scope, it clears itself from |
| 280 // active_connection_. | 277 // active_connection_. |
| 281 ScopedConnectionHelper post(this, MakeActiveConnection()); | 278 ScopedConnectionHelper post(cancellation_signal_, MakeConnection()); |
|
akalin
2013/09/05 16:06:35
ideally, MakeConnection() would return a scoped_pt
rlarocque
2013/09/05 22:37:06
Done.
| |
| 282 if (!post.get()) { | 279 if (!post.get()) { |
| 283 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; | 280 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; |
| 284 return false; | 281 return false; |
| 285 } | 282 } |
| 286 | 283 |
| 287 // Note that |post| may be aborted by now, which will just cause Init to fail | 284 // Note that |post| may be aborted by now, which will just cause Init to fail |
| 288 // with CONNECTION_UNAVAILABLE. | 285 // with CONNECTION_UNAVAILABLE. |
| 289 bool ok = post.get()->Init( | 286 bool ok = post.get()->Init( |
| 290 path.c_str(), auth_token, params->buffer_in, ¶ms->response); | 287 path.c_str(), auth_token, params->buffer_in, ¶ms->response); |
| 291 | 288 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 ServerConnectionEventListener* listener) { | 338 ServerConnectionEventListener* listener) { |
| 342 DCHECK(thread_checker_.CalledOnValidThread()); | 339 DCHECK(thread_checker_.CalledOnValidThread()); |
| 343 listeners_.RemoveObserver(listener); | 340 listeners_.RemoveObserver(listener); |
| 344 } | 341 } |
| 345 | 342 |
| 346 ServerConnectionManager::Connection* ServerConnectionManager::MakeConnection() | 343 ServerConnectionManager::Connection* ServerConnectionManager::MakeConnection() |
| 347 { | 344 { |
| 348 return NULL; // For testing. | 345 return NULL; // For testing. |
| 349 } | 346 } |
| 350 | 347 |
| 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 } | |
| 361 | |
| 362 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 348 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
| 363 s << " Response Code (bogus on error): " << hr.response_code; | 349 s << " Response Code (bogus on error): " << hr.response_code; |
| 364 s << " Content-Length (bogus on error): " << hr.content_length; | 350 s << " Content-Length (bogus on error): " << hr.content_length; |
| 365 s << " Server Status: " | 351 s << " Server Status: " |
| 366 << HttpResponse::GetServerConnectionCodeString(hr.server_status); | 352 << HttpResponse::GetServerConnectionCodeString(hr.server_status); |
| 367 return s; | 353 return s; |
| 368 } | 354 } |
| 369 | 355 |
| 370 } // namespace syncer | 356 } // namespace syncer |
| OLD | NEW |