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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 URLFetcher* fetcher_; // Corresponding fetcher object | 105 URLFetcher* fetcher_; // Corresponding fetcher object |
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 MessageLoop* delegate_loop_; // Message loop of the creating thread | 110 MessageLoop* delegate_loop_; // Message loop of the creating thread |
111 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 111 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
112 // The message loop proxy for the thread | 112 // The message loop proxy for the thread |
113 // on which the request IO happens. | 113 // on which the request IO happens. |
114 scoped_ptr<URLRequest> request_; // The actual request this wraps | 114 scoped_ptr<net::URLRequest> request_; // The actual request this wraps |
115 int load_flags_; // Flags for the load operation | 115 int load_flags_; // Flags for the load operation |
116 int response_code_; // HTTP status code for the request | 116 int response_code_; // HTTP status code for the request |
117 std::string data_; // Results of the request | 117 std::string data_; // Results of the request |
118 scoped_refptr<net::IOBuffer> buffer_; | 118 scoped_refptr<net::IOBuffer> buffer_; |
119 // Read buffer | 119 // Read buffer |
120 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 120 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
121 // Cookie/cache info for the request | 121 // Cookie/cache info for the request |
122 ResponseCookies cookies_; // Response cookies | 122 ResponseCookies cookies_; // Response cookies |
123 net::HttpRequestHeaders extra_request_headers_; | 123 net::HttpRequestHeaders extra_request_headers_; |
124 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 124 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 if (io_message_loop_proxy_.get()) { | 255 if (io_message_loop_proxy_.get()) { |
256 io_message_loop_proxy_->PostTask( | 256 io_message_loop_proxy_->PostTask( |
257 FROM_HERE, NewRunnableMethod(this, &Core::NotifyMalformedContent)); | 257 FROM_HERE, NewRunnableMethod(this, &Core::NotifyMalformedContent)); |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 void URLFetcher::Core::CancelAll() { | 261 void URLFetcher::Core::CancelAll() { |
262 g_registry.Get().CancelAll(); | 262 g_registry.Get().CancelAll(); |
263 } | 263 } |
264 | 264 |
265 void URLFetcher::Core::OnResponseStarted(URLRequest* request) { | 265 void URLFetcher::Core::OnResponseStarted(net::URLRequest* request) { |
266 DCHECK_EQ(request, request_.get()); | 266 DCHECK_EQ(request, request_.get()); |
267 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 267 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
268 if (request_->status().is_success()) { | 268 if (request_->status().is_success()) { |
269 response_code_ = request_->GetResponseCode(); | 269 response_code_ = request_->GetResponseCode(); |
270 response_headers_ = request_->response_headers(); | 270 response_headers_ = request_->response_headers(); |
271 } | 271 } |
272 | 272 |
273 int bytes_read = 0; | 273 int bytes_read = 0; |
274 // Some servers may treat HEAD requests as GET requests. To free up the | 274 // Some servers may treat HEAD requests as GET requests. To free up the |
275 // network connection as soon as possible, signal that the request has | 275 // network connection as soon as possible, signal that the request has |
276 // completed immediately, without trying to read any data back (all we care | 276 // completed immediately, without trying to read any data back (all we care |
277 // about is the response code and headers, which we already have). | 277 // about is the response code and headers, which we already have). |
278 if (request_->status().is_success() && (request_type_ != HEAD)) | 278 if (request_->status().is_success() && (request_type_ != HEAD)) |
279 request_->Read(buffer_, kBufferSize, &bytes_read); | 279 request_->Read(buffer_, kBufferSize, &bytes_read); |
280 OnReadCompleted(request_.get(), bytes_read); | 280 OnReadCompleted(request_.get(), bytes_read); |
281 } | 281 } |
282 | 282 |
283 void URLFetcher::Core::OnReadCompleted(URLRequest* request, int bytes_read) { | 283 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, |
| 284 int bytes_read) { |
284 DCHECK(request == request_); | 285 DCHECK(request == request_); |
285 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 286 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
286 | 287 |
287 url_ = request->url(); | 288 url_ = request->url(); |
288 url_throttler_entry_ = | 289 url_throttler_entry_ = |
289 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 290 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
290 | 291 |
291 do { | 292 do { |
292 if (!request_->status().is_success() || bytes_read <= 0) | 293 if (!request_->status().is_success() || bytes_read <= 0) |
293 break; | 294 break; |
(...skipping 19 matching lines...) Expand all Loading... |
313 if (was_cancelled_) { | 314 if (was_cancelled_) { |
314 // Since StartURLRequest() is posted as a *delayed* task, it may | 315 // Since StartURLRequest() is posted as a *delayed* task, it may |
315 // run after the URLFetcher was already stopped. | 316 // run after the URLFetcher was already stopped. |
316 return; | 317 return; |
317 } | 318 } |
318 | 319 |
319 CHECK(request_context_getter_); | 320 CHECK(request_context_getter_); |
320 DCHECK(!request_.get()); | 321 DCHECK(!request_.get()); |
321 | 322 |
322 g_registry.Get().AddURLFetcherCore(this); | 323 g_registry.Get().AddURLFetcherCore(this); |
323 request_.reset(new URLRequest(original_url_, this)); | 324 request_.reset(new net::URLRequest(original_url_, this)); |
324 int flags = request_->load_flags() | load_flags_; | 325 int flags = request_->load_flags() | load_flags_; |
325 if (!g_interception_enabled) { | 326 if (!g_interception_enabled) { |
326 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 327 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
327 } | 328 } |
328 request_->set_load_flags(flags); | 329 request_->set_load_flags(flags); |
329 request_->set_context(request_context_getter_->GetURLRequestContext()); | 330 request_->set_context(request_context_getter_->GetURLRequestContext()); |
330 | 331 |
331 switch (request_type_) { | 332 switch (request_type_) { |
332 case GET: | 333 case GET: |
333 break; | 334 break; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 509 } |
509 | 510 |
510 // static | 511 // static |
511 void URLFetcher::CancelAll() { | 512 void URLFetcher::CancelAll() { |
512 Core::CancelAll(); | 513 Core::CancelAll(); |
513 } | 514 } |
514 | 515 |
515 URLFetcher::Delegate* URLFetcher::delegate() const { | 516 URLFetcher::Delegate* URLFetcher::delegate() const { |
516 return core_->delegate(); | 517 return core_->delegate(); |
517 } | 518 } |
OLD | NEW |