| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/common/net/url_fetcher.h" | 5 #include "chrome/common/net/url_fetcher.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 25 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 26 #include "net/url_request/url_request_throttler_manager.h" | 26 #include "net/url_request/url_request_throttler_manager.h" |
| 27 | 27 |
| 28 static const int kBufferSize = 4096; | 28 static const int kBufferSize = 4096; |
| 29 | 29 |
| 30 bool URLFetcher::g_interception_enabled = false; | 30 bool URLFetcher::g_interception_enabled = false; |
| 31 | 31 |
| 32 class URLFetcher::Core | 32 class URLFetcher::Core |
| 33 : public base::RefCountedThreadSafe<URLFetcher::Core>, | 33 : public base::RefCountedThreadSafe<URLFetcher::Core>, |
| 34 public URLRequest::Delegate { | 34 public net::URLRequest::Delegate { |
| 35 public: | 35 public: |
| 36 // For POST requests, set |content_type| to the MIME type of the content | 36 // For POST requests, set |content_type| to the MIME type of the content |
| 37 // and set |content| to the data to upload. |flags| are flags to apply to | 37 // and set |content| to the data to upload. |flags| are flags to apply to |
| 38 // the load operation--these should be one or more of the LOAD_* flags | 38 // the load operation--these should be one or more of the LOAD_* flags |
| 39 // defined in url_request.h. | 39 // defined in url_request.h. |
| 40 Core(URLFetcher* fetcher, | 40 Core(URLFetcher* fetcher, |
| 41 const GURL& original_url, | 41 const GURL& original_url, |
| 42 RequestType request_type, | 42 RequestType request_type, |
| 43 URLFetcher::Delegate* d); | 43 URLFetcher::Delegate* d); |
| 44 | 44 |
| 45 // Starts the load. It's important that this not happen in the constructor | 45 // Starts the load. It's important that this not happen in the constructor |
| 46 // because it causes the IO thread to begin AddRef()ing and Release()ing | 46 // because it causes the IO thread to begin AddRef()ing and Release()ing |
| 47 // us. If our caller hasn't had time to fully construct us and take a | 47 // us. If our caller hasn't had time to fully construct us and take a |
| 48 // reference, the IO thread could interrupt things, run a task, Release() | 48 // reference, the IO thread could interrupt things, run a task, Release() |
| 49 // us, and destroy us, leaving the caller with an already-destroyed object | 49 // us, and destroy us, leaving the caller with an already-destroyed object |
| 50 // when construction finishes. | 50 // when construction finishes. |
| 51 void Start(); | 51 void Start(); |
| 52 | 52 |
| 53 // Stops any in-progress load and ensures no callback will happen. It is | 53 // Stops any in-progress load and ensures no callback will happen. It is |
| 54 // safe to call this multiple times. | 54 // safe to call this multiple times. |
| 55 void Stop(); | 55 void Stop(); |
| 56 | 56 |
| 57 // Reports that the received content was malformed. | 57 // Reports that the received content was malformed. |
| 58 void ReceivedContentWasMalformed(); | 58 void ReceivedContentWasMalformed(); |
| 59 | 59 |
| 60 // URLRequest::Delegate implementation. | 60 // Overridden from net::URLRequest::Delegate: |
| 61 virtual void OnResponseStarted(URLRequest* request); | 61 virtual void OnResponseStarted(net::URLRequest* request); |
| 62 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | 62 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
| 63 | 63 |
| 64 URLFetcher::Delegate* delegate() const { return delegate_; } | 64 URLFetcher::Delegate* delegate() const { return delegate_; } |
| 65 | 65 |
| 66 static void CancelAll(); | 66 static void CancelAll(); |
| 67 | 67 |
| 68 private: | 68 private: |
| 69 friend class base::RefCountedThreadSafe<URLFetcher::Core>; | 69 friend class base::RefCountedThreadSafe<URLFetcher::Core>; |
| 70 | 70 |
| 71 class Registry { | 71 class Registry { |
| 72 public: | 72 public: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 GURL original_url_; // The URL we were asked to fetch | 106 GURL original_url_; // The URL we were asked to fetch |
| 107 GURL url_; // The URL we eventually wound up at | 107 GURL url_; // The URL we eventually wound up at |
| 108 RequestType request_type_; // What type of request is this? | 108 RequestType request_type_; // What type of request is this? |
| 109 URLFetcher::Delegate* delegate_; // Object to notify on completion | 109 URLFetcher::Delegate* delegate_; // Object to notify on completion |
| 110 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; | 110 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; |
| 111 // Message loop proxy of the creating | 111 // Message loop proxy of the creating |
| 112 // thread. | 112 // thread. |
| 113 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 113 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 114 // The message loop proxy for the thread | 114 // The message loop proxy for the thread |
| 115 // on which the request IO happens. | 115 // on which the request IO happens. |
| 116 scoped_ptr<URLRequest> request_; // The actual request this wraps | 116 scoped_ptr<net::URLRequest> request_; // The actual request this wraps |
| 117 int load_flags_; // Flags for the load operation | 117 int load_flags_; // Flags for the load operation |
| 118 int response_code_; // HTTP status code for the request | 118 int response_code_; // HTTP status code for the request |
| 119 std::string data_; // Results of the request | 119 std::string data_; // Results of the request |
| 120 scoped_refptr<net::IOBuffer> buffer_; | 120 scoped_refptr<net::IOBuffer> buffer_; |
| 121 // Read buffer | 121 // Read buffer |
| 122 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 122 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| 123 // Cookie/cache info for the request | 123 // Cookie/cache info for the request |
| 124 ResponseCookies cookies_; // Response cookies | 124 ResponseCookies cookies_; // Response cookies |
| 125 net::HttpRequestHeaders extra_request_headers_; | 125 net::HttpRequestHeaders extra_request_headers_; |
| 126 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 126 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 if (io_message_loop_proxy_.get()) { | 257 if (io_message_loop_proxy_.get()) { |
| 258 io_message_loop_proxy_->PostTask( | 258 io_message_loop_proxy_->PostTask( |
| 259 FROM_HERE, NewRunnableMethod(this, &Core::NotifyMalformedContent)); | 259 FROM_HERE, NewRunnableMethod(this, &Core::NotifyMalformedContent)); |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 | 262 |
| 263 void URLFetcher::Core::CancelAll() { | 263 void URLFetcher::Core::CancelAll() { |
| 264 g_registry.Get().CancelAll(); | 264 g_registry.Get().CancelAll(); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void URLFetcher::Core::OnResponseStarted(URLRequest* request) { | 267 void URLFetcher::Core::OnResponseStarted(net::URLRequest* request) { |
| 268 DCHECK_EQ(request, request_.get()); | 268 DCHECK_EQ(request, request_.get()); |
| 269 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 269 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 270 if (request_->status().is_success()) { | 270 if (request_->status().is_success()) { |
| 271 response_code_ = request_->GetResponseCode(); | 271 response_code_ = request_->GetResponseCode(); |
| 272 response_headers_ = request_->response_headers(); | 272 response_headers_ = request_->response_headers(); |
| 273 } | 273 } |
| 274 | 274 |
| 275 int bytes_read = 0; | 275 int bytes_read = 0; |
| 276 // Some servers may treat HEAD requests as GET requests. To free up the | 276 // Some servers may treat HEAD requests as GET requests. To free up the |
| 277 // network connection as soon as possible, signal that the request has | 277 // network connection as soon as possible, signal that the request has |
| 278 // completed immediately, without trying to read any data back (all we care | 278 // completed immediately, without trying to read any data back (all we care |
| 279 // about is the response code and headers, which we already have). | 279 // about is the response code and headers, which we already have). |
| 280 if (request_->status().is_success() && (request_type_ != HEAD)) | 280 if (request_->status().is_success() && (request_type_ != HEAD)) |
| 281 request_->Read(buffer_, kBufferSize, &bytes_read); | 281 request_->Read(buffer_, kBufferSize, &bytes_read); |
| 282 OnReadCompleted(request_.get(), bytes_read); | 282 OnReadCompleted(request_.get(), bytes_read); |
| 283 } | 283 } |
| 284 | 284 |
| 285 void URLFetcher::Core::OnReadCompleted(URLRequest* request, int bytes_read) { | 285 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, |
| 286 int bytes_read) { |
| 286 DCHECK(request == request_); | 287 DCHECK(request == request_); |
| 287 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 288 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 288 | 289 |
| 289 url_ = request->url(); | 290 url_ = request->url(); |
| 290 url_throttler_entry_ = | 291 url_throttler_entry_ = |
| 291 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 292 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
| 292 | 293 |
| 293 do { | 294 do { |
| 294 if (!request_->status().is_success() || bytes_read <= 0) | 295 if (!request_->status().is_success() || bytes_read <= 0) |
| 295 break; | 296 break; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 321 if (was_cancelled_) { | 322 if (was_cancelled_) { |
| 322 // Since StartURLRequest() is posted as a *delayed* task, it may | 323 // Since StartURLRequest() is posted as a *delayed* task, it may |
| 323 // run after the URLFetcher was already stopped. | 324 // run after the URLFetcher was already stopped. |
| 324 return; | 325 return; |
| 325 } | 326 } |
| 326 | 327 |
| 327 CHECK(request_context_getter_); | 328 CHECK(request_context_getter_); |
| 328 DCHECK(!request_.get()); | 329 DCHECK(!request_.get()); |
| 329 | 330 |
| 330 g_registry.Get().AddURLFetcherCore(this); | 331 g_registry.Get().AddURLFetcherCore(this); |
| 331 request_.reset(new URLRequest(original_url_, this)); | 332 request_.reset(new net::URLRequest(original_url_, this)); |
| 332 int flags = request_->load_flags() | load_flags_; | 333 int flags = request_->load_flags() | load_flags_; |
| 333 if (!g_interception_enabled) { | 334 if (!g_interception_enabled) { |
| 334 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 335 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 335 } | 336 } |
| 336 request_->set_load_flags(flags); | 337 request_->set_load_flags(flags); |
| 337 request_->set_context(request_context_getter_->GetURLRequestContext()); | 338 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 338 | 339 |
| 339 switch (request_type_) { | 340 switch (request_type_) { |
| 340 case GET: | 341 case GET: |
| 341 break; | 342 break; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 } | 517 } |
| 517 | 518 |
| 518 // static | 519 // static |
| 519 void URLFetcher::CancelAll() { | 520 void URLFetcher::CancelAll() { |
| 520 Core::CancelAll(); | 521 Core::CancelAll(); |
| 521 } | 522 } |
| 522 | 523 |
| 523 URLFetcher::Delegate* URLFetcher::delegate() const { | 524 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 524 return core_->delegate(); | 525 return core_->delegate(); |
| 525 } | 526 } |
| OLD | NEW |