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 "components/sync/core/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/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
15 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "components/sync/base/cancelation_signal.h" |
20 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
22 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
23 #include "net/http/http_network_layer.h" | 24 #include "net/http/http_network_layer.h" |
24 #include "net/http/http_request_headers.h" | 25 #include "net/http/http_request_headers.h" |
25 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
26 #include "net/url_request/static_http_user_agent_settings.h" | 27 #include "net/url_request/static_http_user_agent_settings.h" |
27 #include "net/url_request/url_fetcher.h" | 28 #include "net/url_request/url_fetcher.h" |
28 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
29 #include "net/url_request/url_request_job_factory_impl.h" | 30 #include "net/url_request/url_request_job_factory_impl.h" |
30 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
31 #include "sync/internal_api/public/base/cancelation_signal.h" | |
32 | 32 |
33 namespace syncer { | 33 namespace syncer { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // It's possible for an http request to be silently stalled. We set a time | 37 // It's possible for an http request to be silently stalled. We set a time |
38 // limit for all http requests, beyond which the request is cancelled and | 38 // limit for all http requests, beyond which the request is cancelled and |
39 // treated as a transient failure. | 39 // treated as a transient failure. |
40 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. | 40 const int kMaxHttpRequestTimeSeconds = 60 * 5; // 5 minutes. |
41 | 41 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // is destroyed in the right order on its network task runner. | 114 // is destroyed in the right order on its network task runner. |
115 request_context_getter_ = NULL; | 115 request_context_getter_ = NULL; |
116 } | 116 } |
117 | 117 |
118 HttpBridge::URLFetchState::URLFetchState() | 118 HttpBridge::URLFetchState::URLFetchState() |
119 : url_poster(NULL), | 119 : url_poster(NULL), |
120 aborted(false), | 120 aborted(false), |
121 request_completed(false), | 121 request_completed(false), |
122 request_succeeded(false), | 122 request_succeeded(false), |
123 http_response_code(-1), | 123 http_response_code(-1), |
124 error_code(-1) { | 124 error_code(-1) {} |
125 } | |
126 HttpBridge::URLFetchState::~URLFetchState() {} | 125 HttpBridge::URLFetchState::~URLFetchState() {} |
127 | 126 |
128 HttpBridge::HttpBridge( | 127 HttpBridge::HttpBridge( |
129 const std::string& user_agent, | 128 const std::string& user_agent, |
130 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | 129 const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
131 const NetworkTimeUpdateCallback& network_time_update_callback, | 130 const NetworkTimeUpdateCallback& network_time_update_callback, |
132 const BindToTrackerCallback& bind_to_tracker_callback) | 131 const BindToTrackerCallback& bind_to_tracker_callback) |
133 : user_agent_(user_agent), | 132 : user_agent_(user_agent), |
134 http_post_completed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 133 http_post_completed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
135 base::WaitableEvent::InitialState::NOT_SIGNALED), | 134 base::WaitableEvent::InitialState::NOT_SIGNALED), |
136 request_context_getter_(context_getter), | 135 request_context_getter_(context_getter), |
137 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), | 136 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), |
138 network_time_update_callback_(network_time_update_callback), | 137 network_time_update_callback_(network_time_update_callback), |
139 bind_to_tracker_callback_(bind_to_tracker_callback) {} | 138 bind_to_tracker_callback_(bind_to_tracker_callback) {} |
140 | 139 |
141 HttpBridge::~HttpBridge() { | 140 HttpBridge::~HttpBridge() {} |
142 } | |
143 | 141 |
144 void HttpBridge::SetExtraRequestHeaders(const char * headers) { | 142 void HttpBridge::SetExtraRequestHeaders(const char* headers) { |
145 DCHECK(extra_headers_.empty()) | 143 DCHECK(extra_headers_.empty()) |
146 << "HttpBridge::SetExtraRequestHeaders called twice."; | 144 << "HttpBridge::SetExtraRequestHeaders called twice."; |
147 extra_headers_.assign(headers); | 145 extra_headers_.assign(headers); |
148 } | 146 } |
149 | 147 |
150 void HttpBridge::SetURL(const char* url, int port) { | 148 void HttpBridge::SetURL(const char* url, int port) { |
151 #if DCHECK_IS_ON() | 149 #if DCHECK_IS_ON() |
152 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
153 { | 151 { |
154 base::AutoLock lock(fetch_state_lock_); | 152 base::AutoLock lock(fetch_state_lock_); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 DCHECK(thread_checker_.CalledOnValidThread()); | 190 DCHECK(thread_checker_.CalledOnValidThread()); |
193 { | 191 { |
194 base::AutoLock lock(fetch_state_lock_); | 192 base::AutoLock lock(fetch_state_lock_); |
195 DCHECK(!fetch_state_.request_completed); | 193 DCHECK(!fetch_state_.request_completed); |
196 } | 194 } |
197 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; | 195 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; |
198 DCHECK(!content_type_.empty()) << "Payload not set"; | 196 DCHECK(!content_type_.empty()) << "Payload not set"; |
199 #endif | 197 #endif |
200 | 198 |
201 if (!network_task_runner_->PostTask( | 199 if (!network_task_runner_->PostTask( |
202 FROM_HERE, | 200 FROM_HERE, base::Bind(&HttpBridge::CallMakeAsynchronousPost, this))) { |
203 base::Bind(&HttpBridge::CallMakeAsynchronousPost, this))) { | |
204 // This usually happens when we're in a unit test. | 201 // This usually happens when we're in a unit test. |
205 LOG(WARNING) << "Could not post CallMakeAsynchronousPost task"; | 202 LOG(WARNING) << "Could not post CallMakeAsynchronousPost task"; |
206 return false; | 203 return false; |
207 } | 204 } |
208 | 205 |
209 // Block until network request completes or is aborted. See | 206 // Block until network request completes or is aborted. See |
210 // OnURLFetchComplete and Abort. | 207 // OnURLFetchComplete and Abort. |
211 http_post_completed_.Wait(); | 208 http_post_completed_.Wait(); |
212 | 209 |
213 base::AutoLock lock(fetch_state_lock_); | 210 base::AutoLock lock(fetch_state_lock_); |
(...skipping 28 matching lines...) Expand all Loading... |
242 bind_to_tracker_callback_.Run(fetch_state_.url_poster); | 239 bind_to_tracker_callback_.Run(fetch_state_.url_poster); |
243 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); | 240 fetch_state_.url_poster->SetRequestContext(request_context_getter_.get()); |
244 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 241 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
245 | 242 |
246 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); | 243 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); |
247 RecordSyncRequestContentLengthHistograms(request_content_.size(), | 244 RecordSyncRequestContentLengthHistograms(request_content_.size(), |
248 request_content_.size()); | 245 request_content_.size()); |
249 | 246 |
250 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( | 247 fetch_state_.url_poster->AddExtraRequestHeader(base::StringPrintf( |
251 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); | 248 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); |
252 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 249 fetch_state_.url_poster->SetLoadFlags( |
253 net::LOAD_DISABLE_CACHE | | 250 net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | |
254 net::LOAD_DO_NOT_SAVE_COOKIES | | 251 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES); |
255 net::LOAD_DO_NOT_SEND_COOKIES); | |
256 | 252 |
257 fetch_state_.url_poster->Start(); | 253 fetch_state_.url_poster->Start(); |
258 } | 254 } |
259 | 255 |
260 int HttpBridge::GetResponseContentLength() const { | 256 int HttpBridge::GetResponseContentLength() const { |
261 DCHECK(thread_checker_.CalledOnValidThread()); | 257 DCHECK(thread_checker_.CalledOnValidThread()); |
262 base::AutoLock lock(fetch_state_lock_); | 258 base::AutoLock lock(fetch_state_lock_); |
263 DCHECK(fetch_state_.request_completed); | 259 DCHECK(fetch_state_.request_completed); |
264 return fetch_state_.response_content.size(); | 260 return fetch_state_.response_content.size(); |
265 } | 261 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 fetch_state_.http_request_timeout_timer.release()))) { | 297 fetch_state_.http_request_timeout_timer.release()))) { |
302 // Madness ensues. | 298 // Madness ensues. |
303 NOTREACHED() << "Could not post task to delete URLFetcher"; | 299 NOTREACHED() << "Could not post task to delete URLFetcher"; |
304 } | 300 } |
305 | 301 |
306 fetch_state_.url_poster = NULL; | 302 fetch_state_.url_poster = NULL; |
307 fetch_state_.error_code = net::ERR_ABORTED; | 303 fetch_state_.error_code = net::ERR_ABORTED; |
308 http_post_completed_.Signal(); | 304 http_post_completed_.Signal(); |
309 } | 305 } |
310 | 306 |
311 void HttpBridge::DestroyURLFetcherOnIOThread( | 307 void HttpBridge::DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher, |
312 net::URLFetcher* fetcher, | 308 base::Timer* fetch_timer) { |
313 base::Timer* fetch_timer) { | |
314 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 309 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
315 if (fetch_timer) | 310 if (fetch_timer) |
316 delete fetch_timer; | 311 delete fetch_timer; |
317 delete fetcher; | 312 delete fetcher; |
318 } | 313 } |
319 | 314 |
320 void HttpBridge::OnURLFetchComplete(const net::URLFetcher* source) { | 315 void HttpBridge::OnURLFetchComplete(const net::URLFetcher* source) { |
321 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 316 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
322 | 317 |
323 base::AutoLock lock(fetch_state_lock_); | 318 base::AutoLock lock(fetch_state_lock_); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 int64_t sane_time_ms = 0; | 436 int64_t sane_time_ms = 0; |
442 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 437 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
443 network_time_update_callback_.Run( | 438 network_time_update_callback_.Run( |
444 base::Time::FromJsTime(sane_time_ms), | 439 base::Time::FromJsTime(sane_time_ms), |
445 base::TimeDelta::FromMilliseconds(1), | 440 base::TimeDelta::FromMilliseconds(1), |
446 fetch_state_.end_time - fetch_state_.start_time); | 441 fetch_state_.end_time - fetch_state_.start_time); |
447 } | 442 } |
448 } | 443 } |
449 | 444 |
450 } // namespace syncer | 445 } // namespace syncer |
OLD | NEW |