| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/glue/http_bridge.h" | 5 #include "chrome/browser/sync/glue/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 "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 HttpBridge::RequestContext::~RequestContext() { | 110 HttpBridge::RequestContext::~RequestContext() { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 delete http_transaction_factory(); | 112 delete http_transaction_factory(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 HttpBridge::URLFetchState::URLFetchState() : url_poster(NULL), | 115 HttpBridge::URLFetchState::URLFetchState() : url_poster(NULL), |
| 116 aborted(false), | 116 aborted(false), |
| 117 request_completed(false), | 117 request_completed(false), |
| 118 request_succeeded(false), | 118 request_succeeded(false), |
| 119 http_response_code(-1), | 119 http_response_code(-1), |
| 120 os_error_code(-1) {} | 120 error_code(-1) {} |
| 121 HttpBridge::URLFetchState::~URLFetchState() {} | 121 HttpBridge::URLFetchState::~URLFetchState() {} |
| 122 | 122 |
| 123 HttpBridge::HttpBridge(HttpBridge::RequestContextGetter* context_getter) | 123 HttpBridge::HttpBridge(HttpBridge::RequestContextGetter* context_getter) |
| 124 : context_getter_for_request_(context_getter), | 124 : context_getter_for_request_(context_getter), |
| 125 created_on_loop_(MessageLoop::current()), | 125 created_on_loop_(MessageLoop::current()), |
| 126 http_post_completed_(false, false) { | 126 http_post_completed_(false, false) { |
| 127 } | 127 } |
| 128 | 128 |
| 129 HttpBridge::~HttpBridge() { | 129 HttpBridge::~HttpBridge() { |
| 130 } | 130 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 if (!content || (content_length == 0)) { | 174 if (!content || (content_length == 0)) { |
| 175 DCHECK_EQ(content_length, 0); | 175 DCHECK_EQ(content_length, 0); |
| 176 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty | 176 request_content_ = " "; // TODO(timsteele): URLFetcher requires non-empty |
| 177 // content for POSTs whereas CURL does not, for now | 177 // content for POSTs whereas CURL does not, for now |
| 178 // we hack this to support the sync backend. | 178 // we hack this to support the sync backend. |
| 179 } else { | 179 } else { |
| 180 request_content_.assign(content, content_length); | 180 request_content_.assign(content, content_length); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 bool HttpBridge::MakeSynchronousPost(int* os_error_code, int* response_code) { | 184 bool HttpBridge::MakeSynchronousPost(int* error_code, int* response_code) { |
| 185 DCHECK_EQ(MessageLoop::current(), created_on_loop_); | 185 DCHECK_EQ(MessageLoop::current(), created_on_loop_); |
| 186 if (DCHECK_IS_ON()) { | 186 if (DCHECK_IS_ON()) { |
| 187 base::AutoLock lock(fetch_state_lock_); | 187 base::AutoLock lock(fetch_state_lock_); |
| 188 DCHECK(!fetch_state_.request_completed); | 188 DCHECK(!fetch_state_.request_completed); |
| 189 } | 189 } |
| 190 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; | 190 DCHECK(url_for_request_.is_valid()) << "Invalid URL for request"; |
| 191 DCHECK(!content_type_.empty()) << "Payload not set"; | 191 DCHECK(!content_type_.empty()) << "Payload not set"; |
| 192 | 192 |
| 193 if (!BrowserThread::PostTask( | 193 if (!BrowserThread::PostTask( |
| 194 BrowserThread::IO, FROM_HERE, | 194 BrowserThread::IO, FROM_HERE, |
| 195 NewRunnableMethod(this, &HttpBridge::CallMakeAsynchronousPost))) { | 195 NewRunnableMethod(this, &HttpBridge::CallMakeAsynchronousPost))) { |
| 196 // This usually happens when we're in a unit test. | 196 // This usually happens when we're in a unit test. |
| 197 LOG(WARNING) << "Could not post CallMakeAsynchronousPost task"; | 197 LOG(WARNING) << "Could not post CallMakeAsynchronousPost task"; |
| 198 return false; | 198 return false; |
| 199 } | 199 } |
| 200 | 200 |
| 201 if (!http_post_completed_.Wait()) // Block until network request completes | 201 if (!http_post_completed_.Wait()) // Block until network request completes |
| 202 NOTREACHED(); // or is aborted. See OnURLFetchComplete | 202 NOTREACHED(); // or is aborted. See OnURLFetchComplete |
| 203 // and Abort. | 203 // and Abort. |
| 204 | 204 |
| 205 base::AutoLock lock(fetch_state_lock_); | 205 base::AutoLock lock(fetch_state_lock_); |
| 206 DCHECK(fetch_state_.request_completed || fetch_state_.aborted); | 206 DCHECK(fetch_state_.request_completed || fetch_state_.aborted); |
| 207 *os_error_code = fetch_state_.os_error_code; | 207 *error_code = fetch_state_.error_code; |
| 208 *response_code = fetch_state_.http_response_code; | 208 *response_code = fetch_state_.http_response_code; |
| 209 return fetch_state_.request_succeeded; | 209 return fetch_state_.request_succeeded; |
| 210 } | 210 } |
| 211 | 211 |
| 212 void HttpBridge::MakeAsynchronousPost() { | 212 void HttpBridge::MakeAsynchronousPost() { |
| 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 214 base::AutoLock lock(fetch_state_lock_); | 214 base::AutoLock lock(fetch_state_lock_); |
| 215 DCHECK(!fetch_state_.request_completed); | 215 DCHECK(!fetch_state_.request_completed); |
| 216 if (fetch_state_.aborted) | 216 if (fetch_state_.aborted) |
| 217 return; | 217 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 void HttpBridge::Abort() { | 254 void HttpBridge::Abort() { |
| 255 base::AutoLock lock(fetch_state_lock_); | 255 base::AutoLock lock(fetch_state_lock_); |
| 256 DCHECK(!fetch_state_.aborted); | 256 DCHECK(!fetch_state_.aborted); |
| 257 if (fetch_state_.aborted || fetch_state_.request_completed) | 257 if (fetch_state_.aborted || fetch_state_.request_completed) |
| 258 return; | 258 return; |
| 259 | 259 |
| 260 fetch_state_.aborted = true; | 260 fetch_state_.aborted = true; |
| 261 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 261 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 262 fetch_state_.url_poster); | 262 fetch_state_.url_poster); |
| 263 fetch_state_.url_poster = NULL; | 263 fetch_state_.url_poster = NULL; |
| 264 fetch_state_.os_error_code = net::ERR_ABORTED; | 264 fetch_state_.error_code = net::ERR_ABORTED; |
| 265 http_post_completed_.Signal(); | 265 http_post_completed_.Signal(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void HttpBridge::OnURLFetchComplete(const URLFetcher *source, | 268 void HttpBridge::OnURLFetchComplete(const URLFetcher *source, |
| 269 const GURL &url, | 269 const GURL &url, |
| 270 const net::URLRequestStatus &status, | 270 const net::URLRequestStatus &status, |
| 271 int response_code, | 271 int response_code, |
| 272 const net::ResponseCookies &cookies, | 272 const net::ResponseCookies &cookies, |
| 273 const std::string &data) { | 273 const std::string &data) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 275 base::AutoLock lock(fetch_state_lock_); | 275 base::AutoLock lock(fetch_state_lock_); |
| 276 if (fetch_state_.aborted) | 276 if (fetch_state_.aborted) |
| 277 return; | 277 return; |
| 278 | 278 |
| 279 fetch_state_.request_completed = true; | 279 fetch_state_.request_completed = true; |
| 280 fetch_state_.request_succeeded = | 280 fetch_state_.request_succeeded = |
| 281 (net::URLRequestStatus::SUCCESS == status.status()); | 281 (net::URLRequestStatus::SUCCESS == status.status()); |
| 282 fetch_state_.http_response_code = response_code; | 282 fetch_state_.http_response_code = response_code; |
| 283 fetch_state_.os_error_code = status.os_error(); | 283 fetch_state_.error_code = status.error(); |
| 284 | 284 |
| 285 fetch_state_.response_content = data; | 285 fetch_state_.response_content = data; |
| 286 fetch_state_.response_headers = source->response_headers(); | 286 fetch_state_.response_headers = source->response_headers(); |
| 287 | 287 |
| 288 // End of the line for url_poster_. It lives only on the IO loop. | 288 // End of the line for url_poster_. It lives only on the IO loop. |
| 289 // We defer deletion because we're inside a callback from a component of the | 289 // We defer deletion because we're inside a callback from a component of the |
| 290 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. | 290 // URLFetcher, so it seems most natural / "polite" to let the stack unwind. |
| 291 MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); | 291 MessageLoop::current()->DeleteSoon(FROM_HERE, fetch_state_.url_poster); |
| 292 fetch_state_.url_poster = NULL; | 292 fetch_state_.url_poster = NULL; |
| 293 | 293 |
| 294 // Wake the blocked syncer thread in MakeSynchronousPost. | 294 // Wake the blocked syncer thread in MakeSynchronousPost. |
| 295 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! | 295 // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted! |
| 296 http_post_completed_.Signal(); | 296 http_post_completed_.Signal(); |
| 297 } | 297 } |
| 298 | 298 |
| 299 } // namespace browser_sync | 299 } // namespace browser_sync |
| OLD | NEW |