| 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" | |
| 15 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
| 22 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 23 #include "net/http/http_cache.h" | 22 #include "net/http/http_cache.h" |
| 24 #include "net/http/http_network_layer.h" | 23 #include "net/http/http_network_layer.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 http_response_code(-1), | 123 http_response_code(-1), |
| 125 error_code(-1) { | 124 error_code(-1) { |
| 126 } | 125 } |
| 127 HttpBridge::URLFetchState::~URLFetchState() {} | 126 HttpBridge::URLFetchState::~URLFetchState() {} |
| 128 | 127 |
| 129 HttpBridge::HttpBridge( | 128 HttpBridge::HttpBridge( |
| 130 const std::string& user_agent, | 129 const std::string& user_agent, |
| 131 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | 130 const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
| 132 const NetworkTimeUpdateCallback& network_time_update_callback, | 131 const NetworkTimeUpdateCallback& network_time_update_callback, |
| 133 const BindToTrackerCallback& bind_to_tracker_callback) | 132 const BindToTrackerCallback& bind_to_tracker_callback) |
| 134 : created_on_loop_(base::MessageLoop::current()), | 133 : user_agent_(user_agent), |
| 135 user_agent_(user_agent), | |
| 136 http_post_completed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 134 http_post_completed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 137 base::WaitableEvent::InitialState::NOT_SIGNALED), | 135 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 138 request_context_getter_(context_getter), | 136 request_context_getter_(context_getter), |
| 139 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), | 137 network_task_runner_(request_context_getter_->GetNetworkTaskRunner()), |
| 140 network_time_update_callback_(network_time_update_callback), | 138 network_time_update_callback_(network_time_update_callback), |
| 141 bind_to_tracker_callback_(bind_to_tracker_callback) {} | 139 bind_to_tracker_callback_(bind_to_tracker_callback) {} |
| 142 | 140 |
| 143 HttpBridge::~HttpBridge() { | 141 HttpBridge::~HttpBridge() { |
| 144 } | 142 } |
| 145 | 143 |
| 146 void HttpBridge::SetExtraRequestHeaders(const char * headers) { | 144 void HttpBridge::SetExtraRequestHeaders(const char * headers) { |
| 147 DCHECK(extra_headers_.empty()) | 145 DCHECK(extra_headers_.empty()) |
| 148 << "HttpBridge::SetExtraRequestHeaders called twice."; | 146 << "HttpBridge::SetExtraRequestHeaders called twice."; |
| 149 extra_headers_.assign(headers); | 147 extra_headers_.assign(headers); |
| 150 } | 148 } |
| 151 | 149 |
| 152 void HttpBridge::SetURL(const char* url, int port) { | 150 void HttpBridge::SetURL(const char* url, int port) { |
| 153 #if DCHECK_IS_ON() | 151 #if DCHECK_IS_ON() |
| 154 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 { | 153 { |
| 156 base::AutoLock lock(fetch_state_lock_); | 154 base::AutoLock lock(fetch_state_lock_); |
| 157 DCHECK(!fetch_state_.request_completed); | 155 DCHECK(!fetch_state_.request_completed); |
| 158 } | 156 } |
| 159 DCHECK(url_for_request_.is_empty()) | 157 DCHECK(url_for_request_.is_empty()) |
| 160 << "HttpBridge::SetURL called more than once?!"; | 158 << "HttpBridge::SetURL called more than once?!"; |
| 161 #endif | 159 #endif |
| 162 GURL temp(url); | 160 GURL temp(url); |
| 163 GURL::Replacements replacements; | 161 GURL::Replacements replacements; |
| 164 std::string port_str = base::IntToString(port); | 162 std::string port_str = base::IntToString(port); |
| 165 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.length())); | 163 replacements.SetPort(port_str.c_str(), url::Component(0, port_str.length())); |
| 166 url_for_request_ = temp.ReplaceComponents(replacements); | 164 url_for_request_ = temp.ReplaceComponents(replacements); |
| 167 } | 165 } |
| 168 | 166 |
| 169 void HttpBridge::SetPostPayload(const char* content_type, | 167 void HttpBridge::SetPostPayload(const char* content_type, |
| 170 int content_length, | 168 int content_length, |
| 171 const char* content) { | 169 const char* content) { |
| 172 #if DCHECK_IS_ON() | 170 #if DCHECK_IS_ON() |
| 173 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 174 { | 172 { |
| 175 base::AutoLock lock(fetch_state_lock_); | 173 base::AutoLock lock(fetch_state_lock_); |
| 176 DCHECK(!fetch_state_.request_completed); | 174 DCHECK(!fetch_state_.request_completed); |
| 177 } | 175 } |
| 178 DCHECK(content_type_.empty()) << "Bridge payload already set."; | 176 DCHECK(content_type_.empty()) << "Bridge payload already set."; |
| 179 DCHECK_GE(content_length, 0) << "Content length < 0"; | 177 DCHECK_GE(content_length, 0) << "Content length < 0"; |
| 180 #endif | 178 #endif |
| 181 content_type_ = content_type; | 179 content_type_ = content_type; |
| 182 if (!content || (content_length == 0)) { | 180 if (!content || (content_length == 0)) { |
| 183 DCHECK_EQ(content_length, 0); | 181 DCHECK_EQ(content_length, 0); |
| 184 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty | 182 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty |
| 185 // content for POSTs whereas CURL does not, for now | 183 // content for POSTs whereas CURL does not, for now |
| 186 // we hack this to support the sync backend. | 184 // we hack this to support the sync backend. |
| 187 } else { | 185 } else { |
| 188 request_content_.assign(content, content_length); | 186 request_content_.assign(content, content_length); |
| 189 } | 187 } |
| 190 } | 188 } |
| 191 | 189 |
| 192 bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) { | 190 bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) { |
| 193 #if DCHECK_IS_ON() | 191 #if DCHECK_IS_ON() |
| 194 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 192 DCHECK(thread_checker_.CalledOnValidThread()); |
| 195 { | 193 { |
| 196 base::AutoLock lock(fetch_state_lock_); | 194 base::AutoLock lock(fetch_state_lock_); |
| 197 DCHECK(!fetch_state_.request_completed); | 195 DCHECK(!fetch_state_.request_completed); |
| 198 } | 196 } |
| 199 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; | 197 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; |
| 200 DCHECK(!content_type_.empty()) << "Payload not set"; | 198 DCHECK(!content_type_.empty()) << "Payload not set"; |
| 201 #endif | 199 #endif |
| 202 | 200 |
| 203 if (!network_task_runner_->PostTask( | 201 if (!network_task_runner_->PostTask( |
| 204 FROM_HERE, | 202 FROM_HERE, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); | 251 "%s: %s", net::HttpRequestHeaders::kUserAgent, user_agent_.c_str())); |
| 254 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | | 252 fetch_state_.url_poster->SetLoadFlags(net::LOAD_BYPASS_CACHE | |
| 255 net::LOAD_DISABLE_CACHE | | 253 net::LOAD_DISABLE_CACHE | |
| 256 net::LOAD_DO_NOT_SAVE_COOKIES | | 254 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 257 net::LOAD_DO_NOT_SEND_COOKIES); | 255 net::LOAD_DO_NOT_SEND_COOKIES); |
| 258 | 256 |
| 259 fetch_state_.url_poster->Start(); | 257 fetch_state_.url_poster->Start(); |
| 260 } | 258 } |
| 261 | 259 |
| 262 int HttpBridge::GetResponseContentLength() const { | 260 int HttpBridge::GetResponseContentLength() const { |
| 263 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 261 DCHECK(thread_checker_.CalledOnValidThread()); |
| 264 base::AutoLock lock(fetch_state_lock_); | 262 base::AutoLock lock(fetch_state_lock_); |
| 265 DCHECK(fetch_state_.request_completed); | 263 DCHECK(fetch_state_.request_completed); |
| 266 return fetch_state_.response_content.size(); | 264 return fetch_state_.response_content.size(); |
| 267 } | 265 } |
| 268 | 266 |
| 269 const char* HttpBridge::GetResponseContent() const { | 267 const char* HttpBridge::GetResponseContent() const { |
| 270 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | 268 DCHECK(thread_checker_.CalledOnValidThread()); |
| 271 base::AutoLock lock(fetch_state_lock_); | 269 base::AutoLock lock(fetch_state_lock_); |
| 272 DCHECK(fetch_state_.request_completed); | 270 DCHECK(fetch_state_.request_completed); |
| 273 return fetch_state_.response_content.data(); | 271 return fetch_state_.response_content.data(); |
| 274 } | 272 } |
| 275 | 273 |
| 276 const std::string HttpBridge::GetResponseHeaderValue( | 274 const std::string HttpBridge::GetResponseHeaderValue( |
| 277 const std::string& name) const { | 275 const std::string& name) const { |
| 278 | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
| 279 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); | |
| 280 base::AutoLock lock(fetch_state_lock_); | 277 base::AutoLock lock(fetch_state_lock_); |
| 281 DCHECK(fetch_state_.request_completed); | 278 DCHECK(fetch_state_.request_completed); |
| 282 | 279 |
| 283 std::string value; | 280 std::string value; |
| 284 fetch_state_.response_headers->EnumerateHeader(NULL, name, &value); | 281 fetch_state_.response_headers->EnumerateHeader(NULL, name, &value); |
| 285 return value; | 282 return value; |
| 286 } | 283 } |
| 287 | 284 |
| 288 void HttpBridge::Abort() { | 285 void HttpBridge::Abort() { |
| 289 base::AutoLock lock(fetch_state_lock_); | 286 base::AutoLock lock(fetch_state_lock_); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 int64_t sane_time_ms = 0; | 441 int64_t sane_time_ms = 0; |
| 445 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 442 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
| 446 network_time_update_callback_.Run( | 443 network_time_update_callback_.Run( |
| 447 base::Time::FromJsTime(sane_time_ms), | 444 base::Time::FromJsTime(sane_time_ms), |
| 448 base::TimeDelta::FromMilliseconds(1), | 445 base::TimeDelta::FromMilliseconds(1), |
| 449 fetch_state_.end_time - fetch_state_.start_time); | 446 fetch_state_.end_time - fetch_state_.start_time); |
| 450 } | 447 } |
| 451 } | 448 } |
| 452 | 449 |
| 453 } // namespace syncer | 450 } // namespace syncer |
| OLD | NEW |