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/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 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 CancelationSignal* signaller, scoped_ptr<Connection> connection) |
118 : manager_(manager), connection_(connection) {} | 119 : cancelation_signal_(signaller), connection_(connection.Pass()) { |
| 120 // Special early return for tests. |
| 121 if (!connection_.get()) |
| 122 return; |
| 123 |
| 124 if (!cancelation_signal_->TryRegisterHandler(this)) { |
| 125 connection_.reset(); |
| 126 } |
| 127 } |
| 128 |
| 129 // This function may be called from another thread. |
| 130 void ServerConnectionManager::ScopedConnectionHelper::OnStopRequested() { |
| 131 DCHECK(connection_); |
| 132 connection_->Abort(); |
| 133 } |
119 | 134 |
120 ServerConnectionManager::ScopedConnectionHelper::~ScopedConnectionHelper() { | 135 ServerConnectionManager::ScopedConnectionHelper::~ScopedConnectionHelper() { |
121 if (connection_) | 136 // We should be registered iff connection_.get() != NULL. |
122 manager_->OnConnectionDestroyed(connection_.get()); | 137 if (connection_.get()) { |
123 connection_.reset(); | 138 // It is important that this be called before this destructor completes. |
| 139 // Until the unregistration is complete, it's possible that the virtual |
| 140 // OnStopRequested() function may be called from a different thread. We |
| 141 // need to unregister it before destruction modifies our vptr. |
| 142 cancelation_signal_->UnregisterHandler(this); |
| 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 CancelationSignal* cancelation_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 cancelation_signal_(cancelation_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(cancelation_signal_, MakeConnection()); |
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 DCHECK(thread_checker_.CalledOnValidThread()); | 333 DCHECK(thread_checker_.CalledOnValidThread()); |
337 listeners_.AddObserver(listener); | 334 listeners_.AddObserver(listener); |
338 } | 335 } |
339 | 336 |
340 void ServerConnectionManager::RemoveListener( | 337 void ServerConnectionManager::RemoveListener( |
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 scoped_ptr<ServerConnectionManager::Connection> |
| 344 ServerConnectionManager::MakeConnection() |
347 { | 345 { |
348 return NULL; // For testing. | 346 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 } | 347 } |
361 | 348 |
362 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 349 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
363 s << " Response Code (bogus on error): " << hr.response_code; | 350 s << " Response Code (bogus on error): " << hr.response_code; |
364 s << " Content-Length (bogus on error): " << hr.content_length; | 351 s << " Content-Length (bogus on error): " << hr.content_length; |
365 s << " Server Status: " | 352 s << " Server Status: " |
366 << HttpResponse::GetServerConnectionCodeString(hr.server_status); | 353 << HttpResponse::GetServerConnectionCodeString(hr.server_status); |
367 return s; | 354 return s; |
368 } | 355 } |
369 | 356 |
370 } // namespace syncer | 357 } // namespace syncer |
OLD | NEW |