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> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bit_cast.h" | 12 #include "base/bit_cast.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/metrics/field_trial.h" | |
16 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
17 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
18 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | |
21 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
23 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
24 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
25 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
26 #include "net/http/http_network_layer.h" | 24 #include "net/http/http_network_layer.h" |
27 #include "net/http/http_request_headers.h" | 25 #include "net/http/http_request_headers.h" |
28 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
29 #include "net/url_request/static_http_user_agent_settings.h" | 27 #include "net/url_request/static_http_user_agent_settings.h" |
30 #include "net/url_request/url_fetcher.h" | 28 #include "net/url_request/url_fetcher.h" |
31 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
32 #include "net/url_request/url_request_job_factory_impl.h" | 30 #include "net/url_request/url_request_job_factory_impl.h" |
33 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
34 #include "sync/internal_api/public/base/cancelation_signal.h" | 32 #include "sync/internal_api/public/base/cancelation_signal.h" |
35 | 33 |
36 namespace syncer { | 34 namespace syncer { |
37 | 35 |
38 namespace { | 36 namespace { |
39 | 37 |
40 // It's possible for an http request to be silently stalled. We set a time | 38 // It's possible for an http request to be silently stalled. We set a time |
41 // limit for all http requests, beyond which the request is cancelled and | 39 // limit for all http requests, beyond which the request is cancelled and |
42 // treated as a transient failure. | 40 // treated as a transient failure. |
43 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. | 41 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. |
44 | 42 |
45 // Helper method for logging timeouts via UMA. | 43 // Helper method for logging timeouts via UMA. |
46 void LogTimeout(bool timed_out) { | 44 void LogTimeout(bool timed_out) { |
47 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); | 45 UMA_HISTOGRAM_BOOLEAN("Sync.URLFetchTimedOut", timed_out); |
48 } | 46 } |
49 | 47 |
50 bool IsSyncHttpContentCompressionEnabled() { | |
51 const std::string group_name = | |
52 base::FieldTrialList::FindFullName("SyncHttpContentCompression"); | |
53 return StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); | |
54 } | |
55 | |
56 void RecordSyncRequestContentLengthHistograms(int64_t compressed_content_length, | 48 void RecordSyncRequestContentLengthHistograms(int64_t compressed_content_length, |
57 int64_t original_content_length) { | 49 int64_t original_content_length) { |
58 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Compressed", | 50 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Compressed", |
59 compressed_content_length); | 51 compressed_content_length); |
60 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Original", | 52 UMA_HISTOGRAM_COUNTS("Sync.RequestContentLength.Original", |
61 original_content_length); | 53 original_content_length); |
62 } | 54 } |
63 | 55 |
64 void RecordSyncResponseContentLengthHistograms( | 56 void RecordSyncResponseContentLengthHistograms( |
65 int64_t compressed_content_length, | 57 int64_t compressed_content_length, |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 DCHECK(request_context_getter_.get()); | 238 DCHECK(request_context_getter_.get()); |
247 fetch_state_.start_time = base::Time::Now(); | 239 fetch_state_.start_time = base::Time::Now(); |
248 fetch_state_.url_poster = | 240 fetch_state_.url_poster = |
249 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) | 241 net::URLFetcher::Create(url_for_request_, net::URLFetcher::POST, this) |
250 .release(); | 242 .release(); |
251 if (!bind_to_tracker_callback_.is_null()) | 243 if (!bind_to_tracker_callback_.is_null()) |
252 bind_to_tracker_callback_.Run(fetch_state_.url_poster); | 244 bind_to_tracker_callback_.Run(fetch_state_.url_poster); |
253 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); | 245 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); |
254 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 246 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
255 | 247 |
256 if (!IsSyncHttpContentCompressionEnabled()) { | |
257 // We set "accept-encoding" here to avoid URLRequestHttpJob adding "gzip" | |
258 // into "accept-encoding" later. | |
259 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( | |
260 "%s: %s", net::HttpRequestHeaders::kAcceptEncoding, "deflate")); | |
261 } | |
262 | |
263 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); | 248 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); |
264 RecordSyncRequestContentLengthHistograms(request_content_.size(), | 249 RecordSyncRequestContentLengthHistograms(request_content_.size(), |
265 request_content_.size()); | 250 request_content_.size()); |
266 | 251 |
267 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( | 252 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( |
268 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); | 253 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); |
269 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 254 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
270 net::LOAD_DISABLE_CACHE | | 255 net::LOAD_DISABLE_CACHE | |
271 net::LOAD_DO_NOT_SAVE_COOKIES | | 256 net::LOAD_DO_NOT_SAVE_COOKIES | |
272 net::LOAD_DO_NOT_SEND_COOKIES); | 257 net::LOAD_DO_NOT_SEND_COOKIES); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 int64_t sane_time_ms = 0; | 444 int64_t sane_time_ms = 0; |
460 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 445 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
461 network_time_update_callback_.Run( | 446 network_time_update_callback_.Run( |
462 base::Time::FromJsTime(sane_time_ms), | 447 base::Time::FromJsTime(sane_time_ms), |
463 base::TimeDelta::FromMilliseconds(1), | 448 base::TimeDelta::FromMilliseconds(1), |
464 fetch_state_.end_time - fetch_state_.start_time); | 449 fetch_state_.end_time - fetch_state_.start_time); |
465 } | 450 } |
466 } | 451 } |
467 | 452 |
468 } // namespace syncer | 453 } // namespace syncer |
OLD | NEW |