| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "components/sync/engine/net/http_bridge.h" | 5 #include "components/sync/engine/net/http_bridge.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 104 } |
| 105 | 105 |
| 106 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { | 106 void HttpBridgeFactory::Destroy(HttpPostProviderInterface* http) { |
| 107 static_cast<HttpBridge*>(http)->Release(); | 107 static_cast<HttpBridge*>(http)->Release(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void HttpBridgeFactory::OnSignalReceived() { | 110 void HttpBridgeFactory::OnSignalReceived() { |
| 111 base::AutoLock lock(request_context_getter_lock_); | 111 base::AutoLock lock(request_context_getter_lock_); |
| 112 // Release |request_context_getter_| as soon as possible so that it | 112 // Release |request_context_getter_| as soon as possible so that it |
| 113 // is destroyed in the right order on its network task runner. | 113 // is destroyed in the right order on its network task runner. |
| 114 request_context_getter_ = NULL; | 114 request_context_getter_ = nullptr; |
| 115 } | 115 } |
| 116 | 116 |
| 117 HttpBridge::URLFetchState::URLFetchState() | 117 HttpBridge::URLFetchState::URLFetchState() |
| 118 : url_poster(NULL), | 118 : url_poster(nullptr), |
| 119 aborted(false), | 119 aborted(false), |
| 120 request_completed(false), | 120 request_completed(false), |
| 121 request_succeeded(false), | 121 request_succeeded(false), |
| 122 http_response_code(-1), | 122 http_response_code(-1), |
| 123 error_code(-1) {} | 123 error_code(-1) {} |
| 124 HttpBridge::URLFetchState::~URLFetchState() {} | 124 HttpBridge::URLFetchState::~URLFetchState() {} |
| 125 | 125 |
| 126 HttpBridge::HttpBridge( | 126 HttpBridge::HttpBridge( |
| 127 const std::string& user_agent, | 127 const std::string& user_agent, |
| 128 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | 128 const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 return fetch_state_.response_content.data(); | 267 return fetch_state_.response_content.data(); |
| 268 } | 268 } |
| 269 | 269 |
| 270 const std::string HttpBridge::GetResponseHeaderValue( | 270 const std::string HttpBridge::GetResponseHeaderValue( |
| 271 const std::string& name) const { | 271 const std::string& name) const { |
| 272 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK(thread_checker_.CalledOnValidThread()); |
| 273 base::AutoLock lock(fetch_state_lock_); | 273 base::AutoLock lock(fetch_state_lock_); |
| 274 DCHECK(fetch_state_.request_completed); | 274 DCHECK(fetch_state_.request_completed); |
| 275 | 275 |
| 276 std::string value; | 276 std::string value; |
| 277 fetch_state_.response_headers->EnumerateHeader(NULL, name, &value); | 277 fetch_state_.response_headers->EnumerateHeader(nullptr, name, &value); |
| 278 return value; | 278 return value; |
| 279 } | 279 } |
| 280 | 280 |
| 281 void HttpBridge::Abort() { | 281 void HttpBridge::Abort() { |
| 282 base::AutoLock lock(fetch_state_lock_); | 282 base::AutoLock lock(fetch_state_lock_); |
| 283 | 283 |
| 284 // Release |request_context_getter_| as soon as possible so that it is | 284 // Release |request_context_getter_| as soon as possible so that it is |
| 285 // destroyed in the right order on its network task runner. | 285 // destroyed in the right order on its network task runner. |
| 286 request_context_getter_ = NULL; | 286 request_context_getter_ = nullptr; |
| 287 | 287 |
| 288 DCHECK(!fetch_state_.aborted); | 288 DCHECK(!fetch_state_.aborted); |
| 289 if (fetch_state_.aborted || fetch_state_.request_completed) | 289 if (fetch_state_.aborted || fetch_state_.request_completed) |
| 290 return; | 290 return; |
| 291 | 291 |
| 292 fetch_state_.aborted = true; | 292 fetch_state_.aborted = true; |
| 293 if (!network_task_runner_->PostTask( | 293 if (!network_task_runner_->PostTask( |
| 294 FROM_HERE, | 294 FROM_HERE, |
| 295 base::Bind(&HttpBridge::DestroyURLFetcherOnIOThread, this, | 295 base::Bind(&HttpBridge::DestroyURLFetcherOnIOThread, this, |
| 296 fetch_state_.url_poster, | 296 fetch_state_.url_poster, |
| 297 fetch_state_.http_request_timeout_timer.release()))) { | 297 fetch_state_.http_request_timeout_timer.release()))) { |
| 298 // Madness ensues. | 298 // Madness ensues. |
| 299 NOTREACHED() << "Could not post task to delete URLFetcher"; | 299 NOTREACHED() << "Could not post task to delete URLFetcher"; |
| 300 } | 300 } |
| 301 | 301 |
| 302 fetch_state_.url_poster = NULL; | 302 fetch_state_.url_poster = nullptr; |
| 303 fetch_state_.error_code = net::ERR_ABORTED; | 303 fetch_state_.error_code = net::ERR_ABORTED; |
| 304 http_post_completed_.Signal(); | 304 http_post_completed_.Signal(); |
| 305 } | 305 } |
| 306 | 306 |
| 307 void HttpBridge::DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher, | 307 void HttpBridge::DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher, |
| 308 base::Timer* fetch_timer) { | 308 base::Timer* fetch_timer) { |
| 309 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 309 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 310 if (fetch_timer) | 310 if (fetch_timer) |
| 311 delete fetch_timer; | 311 delete fetch_timer; |
| 312 delete fetcher; | 312 delete fetcher; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 int64_t compressed_content_length = | 354 int64_t compressed_content_length = |
| 355 source->GetReceivedResponseContentLength(); | 355 source->GetReceivedResponseContentLength(); |
| 356 RecordSyncResponseContentLengthHistograms(compressed_content_length, | 356 RecordSyncResponseContentLengthHistograms(compressed_content_length, |
| 357 original_content_length); | 357 original_content_length); |
| 358 | 358 |
| 359 // End of the line for url_poster_. It lives only on the IO loop. | 359 // End of the line for url_poster_. It lives only on the IO loop. |
| 360 // We defer deletion because we're inside a callback from a component of the | 360 // We defer deletion because we're inside a callback from a component of the |
| 361 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. | 361 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. |
| 362 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, | 362 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, |
| 363 fetch_state_.url_poster); | 363 fetch_state_.url_poster); |
| 364 fetch_state_.url_poster = NULL; | 364 fetch_state_.url_poster = nullptr; |
| 365 | 365 |
| 366 // Wake the blocked syncer thread in MakeSynchronousPost. | 366 // Wake the blocked syncer thread in MakeSynchronousPost. |
| 367 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 367 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 368 http_post_completed_.Signal(); | 368 http_post_completed_.Signal(); |
| 369 } | 369 } |
| 370 | 370 |
| 371 void HttpBridge::OnURLFetchDownloadProgress(const net::URLFetcher* source, | 371 void HttpBridge::OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 372 int64_t current, | 372 int64_t current, |
| 373 int64_t total, | 373 int64_t total, |
| 374 int64_t current_network_bytes) { | 374 int64_t current_network_bytes) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 401 | 401 |
| 402 fetch_state_.end_time = base::Time::Now(); | 402 fetch_state_.end_time = base::Time::Now(); |
| 403 fetch_state_.request_completed = true; | 403 fetch_state_.request_completed = true; |
| 404 fetch_state_.request_succeeded = false; | 404 fetch_state_.request_succeeded = false; |
| 405 fetch_state_.http_response_code = -1; | 405 fetch_state_.http_response_code = -1; |
| 406 fetch_state_.error_code = net::ERR_TIMED_OUT; | 406 fetch_state_.error_code = net::ERR_TIMED_OUT; |
| 407 | 407 |
| 408 // This method is called by the timer, not the url fetcher implementation, | 408 // This method is called by the timer, not the url fetcher implementation, |
| 409 // so it's safe to delete the fetcher here. | 409 // so it's safe to delete the fetcher here. |
| 410 delete fetch_state_.url_poster; | 410 delete fetch_state_.url_poster; |
| 411 fetch_state_.url_poster = NULL; | 411 fetch_state_.url_poster = nullptr; |
| 412 | 412 |
| 413 // Timer is smart enough to handle being deleted as part of the invoked task. | 413 // Timer is smart enough to handle being deleted as part of the invoked task. |
| 414 fetch_state_.http_request_timeout_timer.reset(); | 414 fetch_state_.http_request_timeout_timer.reset(); |
| 415 | 415 |
| 416 // Wake the blocked syncer thread in MakeSynchronousPost. | 416 // Wake the blocked syncer thread in MakeSynchronousPost. |
| 417 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 417 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 418 http_post_completed_.Signal(); | 418 http_post_completed_.Signal(); |
| 419 } | 419 } |
| 420 | 420 |
| 421 net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest() | 421 net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest() |
| 422 const { | 422 const { |
| 423 base::AutoLock lock(fetch_state_lock_); | 423 base::AutoLock lock(fetch_state_lock_); |
| 424 return request_context_getter_.get(); | 424 return request_context_getter_.get(); |
| 425 } | 425 } |
| 426 | 426 |
| 427 void HttpBridge::UpdateNetworkTime() { | 427 void HttpBridge::UpdateNetworkTime() { |
| 428 std::string sane_time_str; | 428 std::string sane_time_str; |
| 429 if (!fetch_state_.request_succeeded || fetch_state_.start_time.is_null() || | 429 if (!fetch_state_.request_succeeded || fetch_state_.start_time.is_null() || |
| 430 fetch_state_.end_time < fetch_state_.start_time || | 430 fetch_state_.end_time < fetch_state_.start_time || |
| 431 !fetch_state_.response_headers || | 431 !fetch_state_.response_headers || |
| 432 !fetch_state_.response_headers->EnumerateHeader(NULL, "Sane-Time-Millis", | 432 !fetch_state_.response_headers->EnumerateHeader( |
| 433 &sane_time_str)) { | 433 nullptr, "Sane-Time-Millis", &sane_time_str)) { |
| 434 return; | 434 return; |
| 435 } | 435 } |
| 436 | 436 |
| 437 int64_t sane_time_ms = 0; | 437 int64_t sane_time_ms = 0; |
| 438 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 438 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
| 439 network_time_update_callback_.Run( | 439 network_time_update_callback_.Run( |
| 440 base::Time::FromJsTime(sane_time_ms), | 440 base::Time::FromJsTime(sane_time_ms), |
| 441 base::TimeDelta::FromMilliseconds(1), | 441 base::TimeDelta::FromMilliseconds(1), |
| 442 fetch_state_.end_time - fetch_state_.start_time); | 442 fetch_state_.end_time - fetch_state_.start_time); |
| 443 } | 443 } |
| 444 } | 444 } |
| 445 | 445 |
| 446 } // namespace syncer | 446 } // namespace syncer |
| OLD | NEW |