| 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 "sync/internal_api/public/http_bridge.h" | 5 #include "sync/internal_api/public/http_bridge.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <vector> | 10 #include <vector> |
| 8 | 11 |
| 9 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 13 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 16 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 40 void LogTimeout(bool timed_out) { | 43 void LogTimeout(bool timed_out) { |
| 41 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); | 44 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); |
| 42 } | 45 } |
| 43 | 46 |
| 44 bool IsSyncHttpContentCompressionEnabled() { | 47 bool IsSyncHttpContentCompressionEnabled() { |
| 45 const std::string group_name = | 48 const std::string group_name = |
| 46 base::FieldTrialList::FindFullName("SyncHttpContentCompression"); | 49 base::FieldTrialList::FindFullName("SyncHttpContentCompression"); |
| 47 return StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); | 50 return StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); |
| 48 } | 51 } |
| 49 | 52 |
| 50 void RecordSyncRequestContentLengthHistograms(int64 compressed_content_length, | 53 void RecordSyncRequestContentLengthHistograms(int64_t compressed_content_length, |
| 51 int64 original_content_length) { | 54 int64_t original_content_length) { |
| 52 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Compressed", | 55 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Compressed", |
| 53 compressed_content_length); | 56 compressed_content_length); |
| 54 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Original", | 57 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Original", |
| 55 original_content_length); | 58 original_content_length); |
| 56 } | 59 } |
| 57 | 60 |
| 58 void RecordSyncResponseContentLengthHistograms(int64 compressed_content_length, | 61 void RecordSyncResponseContentLengthHistograms( |
| 59 int64 original_content_length) { | 62 int64_t compressed_content_length, |
| 63 int64_t original_content_length) { |
| 60 UMA_HISTOGRAM_COUNTS("Sync.ResponseContentLength.Compressed", | 64 UMA_HISTOGRAM_COUNTS("Sync.ResponseContentLength.Compressed", |
| 61 compressed_content_length); | 65 compressed_content_length); |
| 62 UMA_HISTOGRAM_COUNTS("Sync.ResponseContentLength.Original", | 66 UMA_HISTOGRAM_COUNTS("Sync.ResponseContentLength.Original", |
| 63 original_content_length); | 67 original_content_length); |
| 64 } | 68 } |
| 65 | 69 |
| 66 // ----------------------------------------------------------------------------- | 70 // ----------------------------------------------------------------------------- |
| 67 // The rest of the code in the anon namespace is copied from | 71 // The rest of the code in the anon namespace is copied from |
| 68 // components/compression/compression_utils.cc | 72 // components/compression/compression_utils.cc |
| 69 // TODO(gangwu): crbug.com/515695. The following code is copied from | 73 // TODO(gangwu): crbug.com/515695. The following code is copied from |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 DCHECK(request_context_getter_.get()); | 323 DCHECK(request_context_getter_.get()); |
| 320 fetch_state_.start_time = base::Time::Now(); | 324 fetch_state_.start_time = base::Time::Now(); |
| 321 fetch_state_.url_poster = | 325 fetch_state_.url_poster = |
| 322 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) | 326 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) |
| 323 .release(); | 327 .release(); |
| 324 if (!bind_to_tracker_callback_.is_null()) | 328 if (!bind_to_tracker_callback_.is_null()) |
| 325 bind_to_tracker_callback_.Run(fetch_state_.url_poster); | 329 bind_to_tracker_callback_.Run(fetch_state_.url_poster); |
| 326 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); | 330 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); |
| 327 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 331 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
| 328 | 332 |
| 329 int64 compressed_content_size = 0; | 333 int64_t compressed_content_size = 0; |
| 330 if (IsSyncHttpContentCompressionEnabled()) { | 334 if (IsSyncHttpContentCompressionEnabled()) { |
| 331 std::string compressed_request_content; | 335 std::string compressed_request_content; |
| 332 GzipCompress(request_content_, &compressed_request_content); | 336 GzipCompress(request_content_, &compressed_request_content); |
| 333 compressed_content_size = compressed_request_content.size(); | 337 compressed_content_size = compressed_request_content.size(); |
| 334 fetch_state_.url_poster->SetUploadData(content_type_, | 338 fetch_state_.url_poster->SetUploadData(content_type_, |
| 335 compressed_request_content); | 339 compressed_request_content); |
| 336 fetch_state_.url_poster->AddExtraRequestHeader("Content-Encoding: gzip"); | 340 fetch_state_.url_poster->AddExtraRequestHeader("Content-Encoding: gzip"); |
| 337 } else { | 341 } else { |
| 338 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); | 342 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); |
| 339 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( | 343 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 // Use a real (non-debug) log to facilitate troubleshooting in the wild. | 449 // Use a real (non-debug) log to facilitate troubleshooting in the wild. |
| 446 VLOG(2) << "HttpBridge::OnURLFetchComplete for: " | 450 VLOG(2) << "HttpBridge::OnURLFetchComplete for: " |
| 447 << fetch_state_.url_poster->GetURL().spec(); | 451 << fetch_state_.url_poster->GetURL().spec(); |
| 448 VLOG(1) << "HttpBridge received response code: " | 452 VLOG(1) << "HttpBridge received response code: " |
| 449 << fetch_state_.http_response_code; | 453 << fetch_state_.http_response_code; |
| 450 | 454 |
| 451 source->GetResponseAsString(&fetch_state_.response_content); | 455 source->GetResponseAsString(&fetch_state_.response_content); |
| 452 fetch_state_.response_headers = source->GetResponseHeaders(); | 456 fetch_state_.response_headers = source->GetResponseHeaders(); |
| 453 UpdateNetworkTime(); | 457 UpdateNetworkTime(); |
| 454 | 458 |
| 455 int64 compressed_content_length = fetch_state_.response_content.size(); | 459 int64_t compressed_content_length = fetch_state_.response_content.size(); |
| 456 int64 original_content_length = compressed_content_length; | 460 int64_t original_content_length = compressed_content_length; |
| 457 if (fetch_state_.response_headers && | 461 if (fetch_state_.response_headers && |
| 458 fetch_state_.response_headers->HasHeaderValue("content-encoding", | 462 fetch_state_.response_headers->HasHeaderValue("content-encoding", |
| 459 "gzip")) { | 463 "gzip")) { |
| 460 compressed_content_length = | 464 compressed_content_length = |
| 461 fetch_state_.response_headers->GetContentLength(); | 465 fetch_state_.response_headers->GetContentLength(); |
| 462 } | 466 } |
| 463 RecordSyncResponseContentLengthHistograms(compressed_content_length, | 467 RecordSyncResponseContentLengthHistograms(compressed_content_length, |
| 464 original_content_length); | 468 original_content_length); |
| 465 | 469 |
| 466 // End of the line for url_poster_. It lives only on the IO loop. | 470 // End of the line for url_poster_. It lives only on the IO loop. |
| 467 // We defer deletion because we're inside a callback from a component of the | 471 // We defer deletion because we're inside a callback from a component of the |
| 468 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. | 472 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. |
| 469 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); | 473 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); |
| 470 fetch_state_.url_poster = NULL; | 474 fetch_state_.url_poster = NULL; |
| 471 | 475 |
| 472 // Wake the blocked syncer thread in MakeSynchronousPost. | 476 // Wake the blocked syncer thread in MakeSynchronousPost. |
| 473 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 477 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 474 http_post_completed_.Signal(); | 478 http_post_completed_.Signal(); |
| 475 } | 479 } |
| 476 | 480 |
| 477 void HttpBridge::OnURLFetchDownloadProgress(const net::URLFetcher* source, | 481 void HttpBridge::OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 478 int64 current, int64 total) { | 482 int64_t current, |
| 483 int64_t total) { |
| 479 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 484 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 480 // Reset the delay when forward progress is made. | 485 // Reset the delay when forward progress is made. |
| 481 base::AutoLock lock(fetch_state_lock_); | 486 base::AutoLock lock(fetch_state_lock_); |
| 482 if (fetch_state_.http_request_timeout_timer.get()) | 487 if (fetch_state_.http_request_timeout_timer.get()) |
| 483 fetch_state_.http_request_timeout_timer->Reset(); | 488 fetch_state_.http_request_timeout_timer->Reset(); |
| 484 } | 489 } |
| 485 | 490 |
| 486 void HttpBridge::OnURLFetchUploadProgress(const net::URLFetcher* source, | 491 void HttpBridge::OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 487 int64 current, int64 total) { | 492 int64_t current, |
| 493 int64_t total) { |
| 488 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 494 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 489 // Reset the delay when forward progress is made. | 495 // Reset the delay when forward progress is made. |
| 490 base::AutoLock lock(fetch_state_lock_); | 496 base::AutoLock lock(fetch_state_lock_); |
| 491 if (fetch_state_.http_request_timeout_timer.get()) | 497 if (fetch_state_.http_request_timeout_timer.get()) |
| 492 fetch_state_.http_request_timeout_timer->Reset(); | 498 fetch_state_.http_request_timeout_timer->Reset(); |
| 493 } | 499 } |
| 494 | 500 |
| 495 void HttpBridge::OnURLFetchTimedOut() { | 501 void HttpBridge::OnURLFetchTimedOut() { |
| 496 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 502 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 497 | 503 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 void HttpBridge::UpdateNetworkTime() { | 536 void HttpBridge::UpdateNetworkTime() { |
| 531 std::string sane_time_str; | 537 std::string sane_time_str; |
| 532 if (!fetch_state_.request_succeeded || fetch_state_.start_time.is_null() || | 538 if (!fetch_state_.request_succeeded || fetch_state_.start_time.is_null() || |
| 533 fetch_state_.end_time < fetch_state_.start_time || | 539 fetch_state_.end_time < fetch_state_.start_time || |
| 534 !fetch_state_.response_headers || | 540 !fetch_state_.response_headers || |
| 535 !fetch_state_.response_headers->EnumerateHeader(NULL, "Sane-Time-Millis", | 541 !fetch_state_.response_headers->EnumerateHeader(NULL, "Sane-Time-Millis", |
| 536 &sane_time_str)) { | 542 &sane_time_str)) { |
| 537 return; | 543 return; |
| 538 } | 544 } |
| 539 | 545 |
| 540 int64 sane_time_ms = 0; | 546 int64_t sane_time_ms = 0; |
| 541 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 547 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
| 542 network_time_update_callback_.Run( | 548 network_time_update_callback_.Run( |
| 543 base::Time::FromJsTime(sane_time_ms), | 549 base::Time::FromJsTime(sane_time_ms), |
| 544 base::TimeDelta::FromMilliseconds(1), | 550 base::TimeDelta::FromMilliseconds(1), |
| 545 fetch_state_.end_time - fetch_state_.start_time); | 551 fetch_state_.end_time - fetch_state_.start_time); |
| 546 } | 552 } |
| 547 } | 553 } |
| 548 | 554 |
| 549 } // namespace syncer | 555 } // namespace syncer |
| OLD | NEW |