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 "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 http_response_code(-1), | 133 http_response_code(-1), |
134 error_code(-1) {} | 134 error_code(-1) {} |
135 HttpBridge::URLFetchState::~URLFetchState() {} | 135 HttpBridge::URLFetchState::~URLFetchState() {} |
136 | 136 |
137 HttpBridge::HttpBridge( | 137 HttpBridge::HttpBridge( |
138 HttpBridge::RequestContextGetter* context_getter, | 138 HttpBridge::RequestContextGetter* context_getter, |
139 const NetworkTimeUpdateCallback& network_time_update_callback) | 139 const NetworkTimeUpdateCallback& network_time_update_callback) |
140 : context_getter_for_request_(context_getter), | 140 : context_getter_for_request_(context_getter), |
141 network_task_runner_( | 141 network_task_runner_( |
142 context_getter_for_request_->GetNetworkTaskRunner()), | 142 context_getter_for_request_->GetNetworkTaskRunner()), |
143 created_on_loop_(MessageLoop::current()), | 143 created_on_loop_(base::MessageLoop::current()), |
144 http_post_completed_(false, false), | 144 http_post_completed_(false, false), |
145 network_time_update_callback_(network_time_update_callback) { | 145 network_time_update_callback_(network_time_update_callback) { |
146 } | 146 } |
147 | 147 |
148 HttpBridge::~HttpBridge() { | 148 HttpBridge::~HttpBridge() { |
149 } | 149 } |
150 | 150 |
151 void HttpBridge::SetExtraRequestHeaders(const char * headers) { | 151 void HttpBridge::SetExtraRequestHeaders(const char * headers) { |
152 DCHECK(extra_headers_.empty()) | 152 DCHECK(extra_headers_.empty()) |
153 << "HttpBridge::SetExtraRequestHeaders called twice."; | 153 << "HttpBridge::SetExtraRequestHeaders called twice."; |
154 extra_headers_.assign(headers); | 154 extra_headers_.assign(headers); |
155 } | 155 } |
156 | 156 |
157 void HttpBridge::SetURL(const char* url, int port) { | 157 void HttpBridge::SetURL(const char* url, int port) { |
158 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 158 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
159 if (DCHECK_IS_ON()) { | 159 if (DCHECK_IS_ON()) { |
160 base::AutoLock lock(fetch_state_lock_); | 160 base::AutoLock lock(fetch_state_lock_); |
161 DCHECK(!fetch_state_.request_completed); | 161 DCHECK(!fetch_state_.request_completed); |
162 } | 162 } |
163 DCHECK(url_for_request_.is_empty()) | 163 DCHECK(url_for_request_.is_empty()) |
164 << "HttpBridge::SetURL called more than once?!"; | 164 << "HttpBridge::SetURL called more than once?!"; |
165 GURL temp(url); | 165 GURL temp(url); |
166 GURL::Replacements replacements; | 166 GURL::Replacements replacements; |
167 std::string port_str = base::IntToString(port); | 167 std::string port_str = base::IntToString(port); |
168 replacements.SetPort(port_str.c_str(), | 168 replacements.SetPort(port_str.c_str(), |
169 url_parse::Component(0, port_str.length())); | 169 url_parse::Component(0, port_str.length())); |
170 url_for_request_ = temp.ReplaceComponents(replacements); | 170 url_for_request_ = temp.ReplaceComponents(replacements); |
171 } | 171 } |
172 | 172 |
173 void HttpBridge::SetPostPayload(const char* content_type, | 173 void HttpBridge::SetPostPayload(const char* content_type, |
174 int content_length, | 174 int content_length, |
175 const char* content) { | 175 const char* content) { |
176 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 176 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
177 if (DCHECK_IS_ON()) { | 177 if (DCHECK_IS_ON()) { |
178 base::AutoLock lock(fetch_state_lock_); | 178 base::AutoLock lock(fetch_state_lock_); |
179 DCHECK(!fetch_state_.request_completed); | 179 DCHECK(!fetch_state_.request_completed); |
180 } | 180 } |
181 DCHECK(content_type_.empty()) << "Bridge payload already set."; | 181 DCHECK(content_type_.empty()) << "Bridge payload already set."; |
182 DCHECK_GE(content_length, 0) << "Content length < 0"; | 182 DCHECK_GE(content_length, 0) << "Content length < 0"; |
183 content_type_ = content_type; | 183 content_type_ = content_type; |
184 if (!content || (content_length == 0)) { | 184 if (!content || (content_length == 0)) { |
185 DCHECK_EQ(content_length, 0); | 185 DCHECK_EQ(content_length, 0); |
186 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty | 186 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty |
187 // content for POSTs whereas CURL does not, for now | 187 // content for POSTs whereas CURL does not, for now |
188 // we hack this to support the sync backend. | 188 // we hack this to support the sync backend. |
189 } else { | 189 } else { |
190 request_content_.assign(content, content_length); | 190 request_content_.assign(content, content_length); |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) { | 194 bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) { |
195 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 195 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
196 if (DCHECK_IS_ON()) { | 196 if (DCHECK_IS_ON()) { |
197 base::AutoLock lock(fetch_state_lock_); | 197 base::AutoLock lock(fetch_state_lock_); |
198 DCHECK(!fetch_state_.request_completed); | 198 DCHECK(!fetch_state_.request_completed); |
199 } | 199 } |
200 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; | 200 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; |
201 DCHECK(!content_type_.empty()) << "Payload not set"; | 201 DCHECK(!content_type_.empty()) << "Payload not set"; |
202 | 202 |
203 if (!network_task_runner_->PostTask( | 203 if (!network_task_runner_->PostTask( |
204 FROM_HERE, | 204 FROM_HERE, |
205 base::Bind(&HttpBridge::CallMakeAsynchronousPost, this))) { | 205 base::Bind(&HttpBridge::CallMakeAsynchronousPost, this))) { |
(...skipping 24 matching lines...) Expand all Loading... |
230 url_for_request_, net::URLFetcher::POST, this); | 230 url_for_request_, net::URLFetcher::POST, this); |
231 fetch_state_.url_poster->SetRequestContext(context_getter_for_request_); | 231 fetch_state_.url_poster->SetRequestContext(context_getter_for_request_); |
232 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); | 232 fetch_state_.url_poster->SetUploadData(content_type_, request_content_); |
233 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); | 233 fetch_state_.url_poster->SetExtraRequestHeaders(extra_headers_); |
234 fetch_state_.url_poster->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES); | 234 fetch_state_.url_poster->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES); |
235 fetch_state_.start_time = base::Time::Now(); | 235 fetch_state_.start_time = base::Time::Now(); |
236 fetch_state_.url_poster->Start(); | 236 fetch_state_.url_poster->Start(); |
237 } | 237 } |
238 | 238 |
239 int HttpBridge::GetResponseContentLength() const { | 239 int HttpBridge::GetResponseContentLength() const { |
240 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 240 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
241 base::AutoLock lock(fetch_state_lock_); | 241 base::AutoLock lock(fetch_state_lock_); |
242 DCHECK(fetch_state_.request_completed); | 242 DCHECK(fetch_state_.request_completed); |
243 return fetch_state_.response_content.size(); | 243 return fetch_state_.response_content.size(); |
244 } | 244 } |
245 | 245 |
246 const char* HttpBridge::GetResponseContent() const { | 246 const char* HttpBridge::GetResponseContent() const { |
247 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 247 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
248 base::AutoLock lock(fetch_state_lock_); | 248 base::AutoLock lock(fetch_state_lock_); |
249 DCHECK(fetch_state_.request_completed); | 249 DCHECK(fetch_state_.request_completed); |
250 return fetch_state_.response_content.data(); | 250 return fetch_state_.response_content.data(); |
251 } | 251 } |
252 | 252 |
253 const std::string HttpBridge::GetResponseHeaderValue( | 253 const std::string HttpBridge::GetResponseHeaderValue( |
254 const std::string& name) const { | 254 const std::string& name) const { |
255 | 255 |
256 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 256 DCHECK_EQ(base::MessageLoop::current(), created_on_loop_); |
257 base::AutoLock lock(fetch_state_lock_); | 257 base::AutoLock lock(fetch_state_lock_); |
258 DCHECK(fetch_state_.request_completed); | 258 DCHECK(fetch_state_.request_completed); |
259 | 259 |
260 std::string value; | 260 std::string value; |
261 fetch_state_.response_headers->EnumerateHeader(NULL, name, &value); | 261 fetch_state_.response_headers->EnumerateHeader(NULL, name, &value); |
262 return value; | 262 return value; |
263 } | 263 } |
264 | 264 |
265 void HttpBridge::Abort() { | 265 void HttpBridge::Abort() { |
266 base::AutoLock lock(fetch_state_lock_); | 266 base::AutoLock lock(fetch_state_lock_); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 VLOG(1) << "HttpBridge received response code: " | 306 VLOG(1) << "HttpBridge received response code: " |
307 << fetch_state_.http_response_code; | 307 << fetch_state_.http_response_code; |
308 | 308 |
309 source->GetResponseAsString(&fetch_state_.response_content); | 309 source->GetResponseAsString(&fetch_state_.response_content); |
310 fetch_state_.response_headers = source->GetResponseHeaders(); | 310 fetch_state_.response_headers = source->GetResponseHeaders(); |
311 UpdateNetworkTime(); | 311 UpdateNetworkTime(); |
312 | 312 |
313 // End of the line for url_poster_. It lives only on the IO loop. | 313 // End of the line for url_poster_. It lives only on the IO loop. |
314 // We defer deletion because we're inside a callback from a component of the | 314 // We defer deletion because we're inside a callback from a component of the |
315 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. | 315 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. |
316 MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); | 316 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); |
317 fetch_state_.url_poster = NULL; | 317 fetch_state_.url_poster = NULL; |
318 | 318 |
319 // Wake the blocked syncer thread in MakeSynchronousPost. | 319 // Wake the blocked syncer thread in MakeSynchronousPost. |
320 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 320 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
321 http_post_completed_.Signal(); | 321 http_post_completed_.Signal(); |
322 } | 322 } |
323 | 323 |
324 net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest() | 324 net::URLRequestContextGetter* HttpBridge::GetRequestContextGetterForTest() |
325 const { | 325 const { |
326 return context_getter_for_request_; | 326 return context_getter_for_request_; |
(...skipping 11 matching lines...) Expand all Loading... |
338 int64 sane_time_ms = 0; | 338 int64 sane_time_ms = 0; |
339 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { | 339 if (base::StringToInt64(sane_time_str, &sane_time_ms)) { |
340 network_time_update_callback_.Run( | 340 network_time_update_callback_.Run( |
341 base::Time::FromJsTime(sane_time_ms), | 341 base::Time::FromJsTime(sane_time_ms), |
342 base::TimeDelta::FromMilliseconds(1), | 342 base::TimeDelta::FromMilliseconds(1), |
343 fetch_state_.end_time - fetch_state_.start_time); | 343 fetch_state_.end_time - fetch_state_.start_time); |
344 } | 344 } |
345 } | 345 } |
346 | 346 |
347 } // namespace syncer | 347 } // namespace syncer |
OLD | NEW |