Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: net/url_request/url_fetcher_core.cc

Issue 10915307: URLFetcher uses a TaskRunner instead of a message loop. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Changes to url_fetcher.h Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/url_fetcher_core.h ('k') | net/url_request/url_fetcher_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "net/url_request/url_fetcher_core.h" 5 #include "net/url_request/url_fetcher_core.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util_proxy.h" 8 #include "base/file_util_proxy.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 void URLFetcherCore::Registry::CancelAll() { 49 void URLFetcherCore::Registry::CancelAll() {
50 while (!fetchers_.empty()) 50 while (!fetchers_.empty())
51 (*fetchers_.begin())->CancelURLRequest(); 51 (*fetchers_.begin())->CancelURLRequest();
52 } 52 }
53 53
54 54
55 // URLFetcherCore::FileWriter ------------------------------------------------- 55 // URLFetcherCore::FileWriter -------------------------------------------------
56 56
57 URLFetcherCore::FileWriter::FileWriter( 57 URLFetcherCore::FileWriter::FileWriter(
58 URLFetcherCore* core, 58 URLFetcherCore* core,
59 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) 59 scoped_refptr<base::TaskRunner> file_task_runner)
60 : core_(core), 60 : core_(core),
61 error_code_(base::PLATFORM_FILE_OK), 61 error_code_(base::PLATFORM_FILE_OK),
62 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 62 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
63 file_task_runner_(file_task_runner), 63 file_task_runner_(file_task_runner),
64 file_handle_(base::kInvalidPlatformFileValue) { 64 file_handle_(base::kInvalidPlatformFileValue) {
65 } 65 }
66 66
67 URLFetcherCore::FileWriter::~FileWriter() { 67 URLFetcherCore::FileWriter::~FileWriter() {
68 RemoveFile(); 68 CloseAndDeleteFile();
69 } 69 }
70 70
71 void URLFetcherCore::FileWriter::CreateFileAtPath( 71 void URLFetcherCore::FileWriter::CreateFileAtPath(
72 const FilePath& file_path) { 72 const FilePath& file_path) {
73 DCHECK(core_->network_task_runner_->BelongsToCurrentThread()); 73 DCHECK(core_->network_task_runner_->BelongsToCurrentThread());
74 DCHECK(file_task_runner_.get()); 74 DCHECK(file_task_runner_.get());
75 base::FileUtilProxy::CreateOrOpen( 75 base::FileUtilProxy::CreateOrOpen(
76 file_task_runner_, 76 file_task_runner_,
77 file_path, 77 file_path,
78 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, 78 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 117
118 // Every code path that resets |core_->request_| should reset 118 // Every code path that resets |core_->request_| should reset
119 // |core->file_writer_| or cause the file writer to disown the file. In the 119 // |core->file_writer_| or cause the file writer to disown the file. In the
120 // former case, this callback can not be called, because the weak pointer to 120 // former case, this callback can not be called, because the weak pointer to
121 // |this| will be NULL. In the latter case, the check of |file_handle_| at the 121 // |this| will be NULL. In the latter case, the check of |file_handle_| at the
122 // start of this method ensures that we can not reach this point. 122 // start of this method ensures that we can not reach this point.
123 CHECK(core_->request_.get()); 123 CHECK(core_->request_.get());
124 124
125 if (base::PLATFORM_FILE_OK != error_code) { 125 if (base::PLATFORM_FILE_OK != error_code) {
126 error_code_ = error_code; 126 error_code_ = error_code;
127 RemoveFile(); 127 CloseAndDeleteFile();
128 core_->delegate_task_runner_->PostTask( 128 core_->delegate_task_runner_->PostTask(
129 FROM_HERE, 129 FROM_HERE,
130 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_)); 130 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_));
131 return; 131 return;
132 } 132 }
133 133
134 total_bytes_written_ += bytes_written; 134 total_bytes_written_ += bytes_written;
135 buffer_offset_ += bytes_written; 135 buffer_offset_ += bytes_written;
136 pending_bytes_ -= bytes_written; 136 pending_bytes_ -= bytes_written;
137 137
(...skipping 26 matching lines...) Expand all
164 164
165 if (file_handle_ != base::kInvalidPlatformFileValue) { 165 if (file_handle_ != base::kInvalidPlatformFileValue) {
166 base::FileUtilProxy::Close( 166 base::FileUtilProxy::Close(
167 file_task_runner_, file_handle_, 167 file_task_runner_, file_handle_,
168 base::Bind(&URLFetcherCore::FileWriter::DidCloseFile, 168 base::Bind(&URLFetcherCore::FileWriter::DidCloseFile,
169 weak_factory_.GetWeakPtr())); 169 weak_factory_.GetWeakPtr()));
170 file_handle_ = base::kInvalidPlatformFileValue; 170 file_handle_ = base::kInvalidPlatformFileValue;
171 } 171 }
172 } 172 }
173 173
174 void URLFetcherCore::FileWriter::RemoveFile() { 174 void URLFetcherCore::FileWriter::CloseAndDeleteFile() {
175 DCHECK(core_->network_task_runner_->BelongsToCurrentThread()); 175 DCHECK(core_->network_task_runner_->BelongsToCurrentThread());
176 176
177 if (file_handle_ == base::kInvalidPlatformFileValue) {
178 DeleteFile(base::PLATFORM_FILE_OK);
179 return;
180 }
177 // Close the file if it is open. 181 // Close the file if it is open.
178 if (file_handle_ != base::kInvalidPlatformFileValue) { 182 base::FileUtilProxy::Close(
179 base::FileUtilProxy::Close( 183 file_task_runner_, file_handle_,
180 file_task_runner_, file_handle_, 184 base::Bind(&URLFetcherCore::FileWriter::DeleteFile,
181 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. 185 weak_factory_.GetWeakPtr()));
182 file_handle_ = base::kInvalidPlatformFileValue; 186 file_handle_ = base::kInvalidPlatformFileValue;
183 } 187 }
184 188
185 if (!file_path_.empty()) { 189 void URLFetcherCore::FileWriter::DeleteFile(
186 base::FileUtilProxy::Delete( 190 base::PlatformFileError error_code) {
187 file_task_runner_, file_path_, 191 DCHECK(core_->network_task_runner_->BelongsToCurrentThread());
188 false, // No need to recurse, as the path is to a file. 192 if (file_path_.empty())
189 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. 193 return;
190 DisownFile(); 194
191 } 195 base::FileUtilProxy::Delete(
196 file_task_runner_, file_path_,
197 false, // No need to recurse, as the path is to a file.
198 base::FileUtilProxy::StatusCallback());
199 DisownFile();
192 } 200 }
193 201
194 void URLFetcherCore::FileWriter::DidCreateFile( 202 void URLFetcherCore::FileWriter::DidCreateFile(
195 const FilePath& file_path, 203 const FilePath& file_path,
196 base::PlatformFileError error_code, 204 base::PlatformFileError error_code,
197 base::PassPlatformFile file_handle, 205 base::PassPlatformFile file_handle,
198 bool created) { 206 bool created) {
199 DidCreateFileInternal(file_path, error_code, file_handle); 207 DidCreateFileInternal(file_path, error_code, file_handle);
200 } 208 }
201 209
202 void URLFetcherCore::FileWriter::DidCreateTempFile( 210 void URLFetcherCore::FileWriter::DidCreateTempFile(
203 base::PlatformFileError error_code, 211 base::PlatformFileError error_code,
204 base::PassPlatformFile file_handle, 212 base::PassPlatformFile file_handle,
205 const FilePath& file_path) { 213 const FilePath& file_path) {
206 DidCreateFileInternal(file_path, error_code, file_handle); 214 DidCreateFileInternal(file_path, error_code, file_handle);
207 } 215 }
208 216
209 void URLFetcherCore::FileWriter::DidCreateFileInternal( 217 void URLFetcherCore::FileWriter::DidCreateFileInternal(
210 const FilePath& file_path, 218 const FilePath& file_path,
211 base::PlatformFileError error_code, 219 base::PlatformFileError error_code,
212 base::PassPlatformFile file_handle) { 220 base::PassPlatformFile file_handle) {
213 DCHECK(core_->network_task_runner_->BelongsToCurrentThread()); 221 DCHECK(core_->network_task_runner_->BelongsToCurrentThread());
214 222
215 if (base::PLATFORM_FILE_OK != error_code) { 223 if (base::PLATFORM_FILE_OK != error_code) {
216 error_code_ = error_code; 224 error_code_ = error_code;
217 RemoveFile(); 225 CloseAndDeleteFile();
218 core_->delegate_task_runner_->PostTask( 226 core_->delegate_task_runner_->PostTask(
219 FROM_HERE, 227 FROM_HERE,
220 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_)); 228 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_));
221 return; 229 return;
222 } 230 }
223 231
224 file_path_ = file_path; 232 file_path_ = file_path;
225 file_handle_ = file_handle.ReleaseValue(); 233 file_handle_ = file_handle.ReleaseValue();
226 total_bytes_written_ = 0; 234 total_bytes_written_ = 0;
227 235
228 core_->network_task_runner_->PostTask( 236 core_->network_task_runner_->PostTask(
229 FROM_HERE, 237 FROM_HERE,
230 base::Bind(&URLFetcherCore::StartURLRequestWhenAppropriate, core_)); 238 base::Bind(&URLFetcherCore::StartURLRequestWhenAppropriate, core_));
231 } 239 }
232 240
233 void URLFetcherCore::FileWriter::DidCloseFile( 241 void URLFetcherCore::FileWriter::DidCloseFile(
234 base::PlatformFileError error_code) { 242 base::PlatformFileError error_code) {
235 DCHECK(core_->network_task_runner_->BelongsToCurrentThread()); 243 DCHECK(core_->network_task_runner_->BelongsToCurrentThread());
236 244
237 if (base::PLATFORM_FILE_OK != error_code) { 245 if (base::PLATFORM_FILE_OK != error_code) {
238 error_code_ = error_code; 246 error_code_ = error_code;
239 RemoveFile(); 247 CloseAndDeleteFile();
240 core_->delegate_task_runner_->PostTask( 248 core_->delegate_task_runner_->PostTask(
241 FROM_HERE, 249 FROM_HERE,
242 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_)); 250 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, core_));
243 return; 251 return;
244 } 252 }
245 253
246 // If the file was successfully closed, then the URL request is complete. 254 // If the file was successfully closed, then the URL request is complete.
247 core_->RetryOrCompleteUrlFetch(); 255 core_->RetryOrCompleteUrlFetch();
248 } 256 }
249 257
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 int URLFetcherCore::GetMaxRetries() const { 413 int URLFetcherCore::GetMaxRetries() const {
406 return max_retries_; 414 return max_retries_;
407 } 415 }
408 416
409 base::TimeDelta URLFetcherCore::GetBackoffDelay() const { 417 base::TimeDelta URLFetcherCore::GetBackoffDelay() const {
410 return backoff_delay_; 418 return backoff_delay_;
411 } 419 }
412 420
413 void URLFetcherCore::SaveResponseToFileAtPath( 421 void URLFetcherCore::SaveResponseToFileAtPath(
414 const FilePath& file_path, 422 const FilePath& file_path,
415 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { 423 scoped_refptr<base::TaskRunner> file_task_runner) {
416 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); 424 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
417 file_task_runner_ = file_task_runner; 425 file_task_runner_ = file_task_runner;
418 response_destination_ = URLFetcherCore::PERMANENT_FILE; 426 response_destination_ = URLFetcherCore::PERMANENT_FILE;
419 response_destination_file_path_ = file_path; 427 response_destination_file_path_ = file_path;
420 } 428 }
421 429
422 void URLFetcherCore::SaveResponseToTemporaryFile( 430 void URLFetcherCore::SaveResponseToTemporaryFile(
423 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { 431 scoped_refptr<base::TaskRunner> file_task_runner) {
424 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); 432 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
425 file_task_runner_ = file_task_runner; 433 file_task_runner_ = file_task_runner;
426 response_destination_ = URLFetcherCore::TEMP_FILE; 434 response_destination_ = URLFetcherCore::TEMP_FILE;
427 } 435 }
428 436
429 HttpResponseHeaders* URLFetcherCore::GetResponseHeaders() const { 437 HttpResponseHeaders* URLFetcherCore::GetResponseHeaders() const {
430 return response_headers_; 438 return response_headers_;
431 } 439 }
432 440
433 // TODO(panayiotis): socket_address_ is written in the IO thread, 441 // TODO(panayiotis): socket_address_ is written in the IO thread,
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 } 1004 }
997 1005
998 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( 1006 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread(
999 scoped_ptr<std::string> download_data) { 1007 scoped_ptr<std::string> download_data) {
1000 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); 1008 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
1001 if (delegate_) 1009 if (delegate_)
1002 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); 1010 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass());
1003 } 1011 }
1004 1012
1005 } // namespace net 1013 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_fetcher_core.h ('k') | net/url_request/url_fetcher_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698