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

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

Issue 10701050: net: Implement canceling of all async operations in FileStream. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Update after http://crrev.com/159454 Created 8 years, 2 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
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 // For loading files, we make use of overlapped i/o to ensure that reading from 5 // For loading files, we make use of overlapped i/o to ensure that reading from
6 // the filesystem (e.g., a network filesystem) does not block the calling 6 // the filesystem (e.g., a network filesystem) does not block the calling
7 // thread. An alternative approach would be to use a background thread or pool 7 // thread. An alternative approach would be to use a background thread or pool
8 // of threads, but it seems better to leverage the operating system's ability 8 // of threads, but it seems better to leverage the operating system's ability
9 // to do background file reads for us. 9 // to do background file reads for us.
10 // 10 //
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 base::Lock lock_; 84 base::Lock lock_;
85 MessageLoop* owner_loop_; 85 MessageLoop* owner_loop_;
86 }; 86 };
87 87
88 URLRequestFileJob::URLRequestFileJob(URLRequest* request, 88 URLRequestFileJob::URLRequestFileJob(URLRequest* request,
89 NetworkDelegate* network_delegate, 89 NetworkDelegate* network_delegate,
90 const FilePath& file_path) 90 const FilePath& file_path)
91 : URLRequestJob(request, network_delegate), 91 : URLRequestJob(request, network_delegate),
92 file_path_(file_path), 92 file_path_(file_path),
93 stream_(NULL),
94 is_directory_(false), 93 is_directory_(false),
95 remaining_bytes_(0) { 94 remaining_bytes_(0) {
96 } 95 }
97 96
98 // static 97 // static
99 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, 98 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request,
100 NetworkDelegate* network_delegate, 99 NetworkDelegate* network_delegate,
101 const std::string& scheme) { 100 const std::string& scheme) {
102 FilePath file_path; 101 FilePath file_path;
103 const bool is_file = FileURLToFilePath(request->url(), &file_path); 102 const bool is_file = FileURLToFilePath(request->url(), &file_path);
(...skipping 22 matching lines...) Expand all
126 void URLRequestFileJob::Start() { 125 void URLRequestFileJob::Start() {
127 DCHECK(!async_resolver_); 126 DCHECK(!async_resolver_);
128 async_resolver_ = new AsyncResolver(this); 127 async_resolver_ = new AsyncResolver(this);
129 base::WorkerPool::PostTask( 128 base::WorkerPool::PostTask(
130 FROM_HERE, 129 FROM_HERE,
131 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), 130 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_),
132 true); 131 true);
133 } 132 }
134 133
135 void URLRequestFileJob::Kill() { 134 void URLRequestFileJob::Kill() {
136 // URL requests should not block on the disk! 135 stream_.reset();
137 // http://code.google.com/p/chromium/issues/detail?id=59849
138 base::ThreadRestrictions::ScopedAllowIO allow_io;
139 stream_.CloseSync();
140 136
141 if (async_resolver_) { 137 if (async_resolver_) {
142 async_resolver_->Cancel(); 138 async_resolver_->Cancel();
143 async_resolver_ = NULL; 139 async_resolver_ = NULL;
144 } 140 }
145 141
146 URLRequestJob::Kill(); 142 URLRequestJob::Kill();
147 } 143 }
148 144
149 bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size, 145 bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size,
150 int *bytes_read) { 146 int *bytes_read) {
151 DCHECK_NE(dest_size, 0); 147 DCHECK_NE(dest_size, 0);
152 DCHECK(bytes_read); 148 DCHECK(bytes_read);
153 DCHECK_GE(remaining_bytes_, 0); 149 DCHECK_GE(remaining_bytes_, 0);
154 150
155 if (remaining_bytes_ < dest_size) 151 if (remaining_bytes_ < dest_size)
156 dest_size = static_cast<int>(remaining_bytes_); 152 dest_size = static_cast<int>(remaining_bytes_);
157 153
158 // If we should copy zero bytes because |remaining_bytes_| is zero, short 154 // If we should copy zero bytes because |remaining_bytes_| is zero, short
159 // circuit here. 155 // circuit here.
160 if (!dest_size) { 156 if (!dest_size) {
161 *bytes_read = 0; 157 *bytes_read = 0;
162 return true; 158 return true;
163 } 159 }
164 160
165 int rv = stream_.Read(dest, dest_size, 161 int rv = stream_->Read(dest, dest_size,
166 base::Bind(&URLRequestFileJob::DidRead, 162 base::Bind(&URLRequestFileJob::DidRead,
167 base::Unretained(this))); 163 base::Unretained(this)));
168 if (rv >= 0) { 164 if (rv >= 0) {
169 // Data is immediately available. 165 // Data is immediately available.
170 *bytes_read = rv; 166 *bytes_read = rv;
171 remaining_bytes_ -= rv; 167 remaining_bytes_ -= rv;
172 DCHECK_GE(remaining_bytes_, 0); 168 DCHECK_GE(remaining_bytes_, 0);
173 return true; 169 return true;
174 } 170 }
175 171
176 // Otherwise, a read error occured. We may just need to wait... 172 // Otherwise, a read error occured. We may just need to wait...
177 if (rv == ERR_IO_PENDING) { 173 if (rv == ERR_IO_PENDING) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // trailing slash. 270 // trailing slash.
275 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise, 271 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise,
276 // we will append trailing slash and redirect to FileDirJob. 272 // we will append trailing slash and redirect to FileDirJob.
277 // A special case is "\" on Windows. We should resolve as invalid. 273 // A special case is "\" on Windows. We should resolve as invalid.
278 // However, Windows resolves "\" to "C:\", thus reports it as existent. 274 // However, Windows resolves "\" to "C:\", thus reports it as existent.
279 // So what happens is we append it with trailing slash and redirect it to 275 // So what happens is we append it with trailing slash and redirect it to
280 // FileDirJob where it is resolved as invalid. 276 // FileDirJob where it is resolved as invalid.
281 if (!exists) { 277 if (!exists) {
282 rv = ERR_FILE_NOT_FOUND; 278 rv = ERR_FILE_NOT_FOUND;
283 } else if (!is_directory_) { 279 } else if (!is_directory_) {
280 stream_.reset(new FileStream(NULL));
281
284 // URL requests should not block on the disk! 282 // URL requests should not block on the disk!
285 // http://code.google.com/p/chromium/issues/detail?id=59849 283 // http://code.google.com/p/chromium/issues/detail?id=59849
286 base::ThreadRestrictions::ScopedAllowIO allow_io; 284 base::ThreadRestrictions::ScopedAllowIO allow_io;
287 285
288 int flags = base::PLATFORM_FILE_OPEN | 286 int flags = base::PLATFORM_FILE_OPEN |
289 base::PLATFORM_FILE_READ | 287 base::PLATFORM_FILE_READ |
290 base::PLATFORM_FILE_ASYNC; 288 base::PLATFORM_FILE_ASYNC;
291 rv = stream_.OpenSync(file_path_, flags); 289 rv = stream_->OpenSync(file_path_, flags);
292 } 290 }
293 291
294 if (rv != OK) { 292 if (rv != OK) {
295 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); 293 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
296 return; 294 return;
297 } 295 }
298 296
299 if (!byte_range_.ComputeBounds(file_info.size)) { 297 if (!byte_range_.ComputeBounds(file_info.size)) {
300 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 298 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
301 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 299 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
302 return; 300 return;
303 } 301 }
304 302
305 remaining_bytes_ = byte_range_.last_byte_position() - 303 remaining_bytes_ = byte_range_.last_byte_position() -
306 byte_range_.first_byte_position() + 1; 304 byte_range_.first_byte_position() + 1;
307 DCHECK_GE(remaining_bytes_, 0); 305 DCHECK_GE(remaining_bytes_, 0);
308 306
309 // URL requests should not block on the disk! 307 // URL requests should not block on the disk!
310 // http://code.google.com/p/chromium/issues/detail?id=59849 308 // http://code.google.com/p/chromium/issues/detail?id=59849
311 { 309 {
312 base::ThreadRestrictions::ScopedAllowIO allow_io; 310 base::ThreadRestrictions::ScopedAllowIO allow_io;
313 // Do the seek at the beginning of the request. 311 // Do the seek at the beginning of the request.
314 if (remaining_bytes_ > 0 && 312 if (remaining_bytes_ > 0 &&
315 byte_range_.first_byte_position() != 0 && 313 byte_range_.first_byte_position() != 0 &&
316 byte_range_.first_byte_position() != 314 byte_range_.first_byte_position() !=
317 stream_.SeekSync(FROM_BEGIN, byte_range_.first_byte_position())) { 315 stream_->SeekSync(FROM_BEGIN, byte_range_.first_byte_position())) {
318 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 316 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
319 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 317 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
320 return; 318 return;
321 } 319 }
322 } 320 }
323 321
324 set_expected_content_size(remaining_bytes_); 322 set_expected_content_size(remaining_bytes_);
325 NotifyHeadersComplete(); 323 NotifyHeadersComplete();
326 } 324 }
327 325
328 void URLRequestFileJob::DidRead(int result) { 326 void URLRequestFileJob::DidRead(int result) {
329 if (result > 0) { 327 if (result > 0) {
330 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status 328 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
331 } else if (result == 0) { 329 } else if (result == 0) {
332 NotifyDone(URLRequestStatus()); 330 NotifyDone(URLRequestStatus());
333 } else { 331 } else {
334 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); 332 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
335 } 333 }
336 334
337 remaining_bytes_ -= result; 335 remaining_bytes_ -= result;
338 DCHECK_GE(remaining_bytes_, 0); 336 DCHECK_GE(remaining_bytes_, 0);
339 337
340 NotifyReadComplete(result); 338 NotifyReadComplete(result);
341 } 339 }
342 340
343 } // namespace net 341 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698