OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/net/url_fetcher.h" | 5 #include "chrome/browser/net/url_fetcher.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/thread.h" | 9 #include "base/thread.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chrome_thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/base/io_buffer.h" |
| 15 |
| 16 static const int kBufferSize = 4096; |
14 | 17 |
15 URLFetcher::URLFetcher(const GURL& url, | 18 URLFetcher::URLFetcher(const GURL& url, |
16 RequestType request_type, | 19 RequestType request_type, |
17 Delegate* d) | 20 Delegate* d) |
18 : ALLOW_THIS_IN_INITIALIZER_LIST( | 21 : ALLOW_THIS_IN_INITIALIZER_LIST( |
19 core_(new Core(this, url, request_type, d))) { | 22 core_(new Core(this, url, request_type, d))) { |
20 } | 23 } |
21 | 24 |
22 URLFetcher::~URLFetcher() { | 25 URLFetcher::~URLFetcher() { |
23 core_->Stop(); | 26 core_->Stop(); |
24 } | 27 } |
25 | 28 |
26 URLFetcher::Core::Core(URLFetcher* fetcher, | 29 URLFetcher::Core::Core(URLFetcher* fetcher, |
27 const GURL& original_url, | 30 const GURL& original_url, |
28 RequestType request_type, | 31 RequestType request_type, |
29 URLFetcher::Delegate* d) | 32 URLFetcher::Delegate* d) |
30 : fetcher_(fetcher), | 33 : fetcher_(fetcher), |
31 original_url_(original_url), | 34 original_url_(original_url), |
32 request_type_(request_type), | 35 request_type_(request_type), |
33 delegate_(d), | 36 delegate_(d), |
34 delegate_loop_(MessageLoop::current()), | 37 delegate_loop_(MessageLoop::current()), |
35 io_loop_(ChromeThread::GetMessageLoop(ChromeThread::IO)), | 38 io_loop_(ChromeThread::GetMessageLoop(ChromeThread::IO)), |
36 request_(NULL), | 39 request_(NULL), |
37 load_flags_(net::LOAD_NORMAL), | 40 load_flags_(net::LOAD_NORMAL), |
38 response_code_(-1), | 41 response_code_(-1), |
| 42 buffer_(new net::IOBuffer(kBufferSize)), |
39 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( | 43 protect_entry_(URLFetcherProtectManager::GetInstance()->Register( |
40 original_url_.host())), | 44 original_url_.host())), |
41 num_retries_(0) { | 45 num_retries_(0) { |
42 } | 46 } |
43 | 47 |
44 void URLFetcher::Core::Start() { | 48 void URLFetcher::Core::Start() { |
45 DCHECK(delegate_loop_); | 49 DCHECK(delegate_loop_); |
46 DCHECK(io_loop_); | 50 DCHECK(io_loop_); |
47 DCHECK(request_context_) << "We need an URLRequestContext!"; | 51 DCHECK(request_context_) << "We need an URLRequestContext!"; |
48 io_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod( | 52 io_loop_->PostDelayedTask(FROM_HERE, NewRunnableMethod( |
(...skipping 15 matching lines...) Expand all Loading... |
64 response_code_ = request_->GetResponseCode(); | 68 response_code_ = request_->GetResponseCode(); |
65 response_headers_ = request_->response_headers(); | 69 response_headers_ = request_->response_headers(); |
66 } | 70 } |
67 | 71 |
68 int bytes_read = 0; | 72 int bytes_read = 0; |
69 // Some servers may treat HEAD requests as GET requests. To free up the | 73 // Some servers may treat HEAD requests as GET requests. To free up the |
70 // network connection as soon as possible, signal that the request has | 74 // network connection as soon as possible, signal that the request has |
71 // completed immediately, without trying to read any data back (all we care | 75 // completed immediately, without trying to read any data back (all we care |
72 // about is the response code and headers, which we already have). | 76 // about is the response code and headers, which we already have). |
73 if (request_->status().is_success() && (request_type_ != HEAD)) | 77 if (request_->status().is_success() && (request_type_ != HEAD)) |
74 request_->Read(buffer_, sizeof(buffer_), &bytes_read); | 78 request_->Read(buffer_, kBufferSize, &bytes_read); |
75 OnReadCompleted(request_, bytes_read); | 79 OnReadCompleted(request_, bytes_read); |
76 } | 80 } |
77 | 81 |
78 void URLFetcher::Core::OnReadCompleted(URLRequest* request, int bytes_read) { | 82 void URLFetcher::Core::OnReadCompleted(URLRequest* request, int bytes_read) { |
79 DCHECK(request == request_); | 83 DCHECK(request == request_); |
80 DCHECK(MessageLoop::current() == io_loop_); | 84 DCHECK(MessageLoop::current() == io_loop_); |
81 | 85 |
82 url_ = request->url(); | 86 url_ = request->url(); |
83 | 87 |
84 do { | 88 do { |
85 if (!request_->status().is_success() || bytes_read <= 0) | 89 if (!request_->status().is_success() || bytes_read <= 0) |
86 break; | 90 break; |
87 data_.append(buffer_, bytes_read); | 91 data_.append(buffer_->data(), bytes_read); |
88 } while (request_->Read(buffer_, sizeof(buffer_), &bytes_read)); | 92 } while (request_->Read(buffer_, kBufferSize, &bytes_read)); |
89 | 93 |
90 if (request_->status().is_success()) | 94 if (request_->status().is_success()) |
91 request_->GetResponseCookies(&cookies_); | 95 request_->GetResponseCookies(&cookies_); |
92 | 96 |
93 // See comments re: HEAD requests in OnResponseStarted(). | 97 // See comments re: HEAD requests in OnResponseStarted(). |
94 if (!request_->status().is_io_pending() || (request_type_ == HEAD)) { | 98 if (!request_->status().is_io_pending() || (request_type_ == HEAD)) { |
95 delegate_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 99 delegate_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
96 this, &Core::OnCompletedURLRequest, request_->status())); | 100 this, &Core::OnCompletedURLRequest, request_->status())); |
97 delete request_; | 101 delete request_; |
98 request_ = NULL; | 102 request_ = NULL; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 cookies_, data_); | 177 cookies_, data_); |
174 } | 178 } |
175 } | 179 } |
176 } else { | 180 } else { |
177 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); | 181 protect_entry_->UpdateBackoff(URLFetcherProtectEntry::SUCCESS); |
178 if (delegate_) | 182 if (delegate_) |
179 delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, | 183 delegate_->OnURLFetchComplete(fetcher_, url_, status, response_code_, |
180 cookies_, data_); | 184 cookies_, data_); |
181 } | 185 } |
182 } | 186 } |
OLD | NEW |