| OLD | NEW |
| 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 // |
| 11 // Since overlapped reads require a 'static' buffer for the duration of the | 11 // Since overlapped reads require a 'static' buffer for the duration of the |
| 12 // asynchronous read, the URLRequestFileJob keeps a buffer as a member var. In | 12 // asynchronous read, the URLRequestFileJob keeps a buffer as a member var. In |
| 13 // URLRequestFileJob::Read, data is simply copied from the object's buffer into | 13 // URLRequestFileJob::Read, data is simply copied from the object's buffer into |
| 14 // the given buffer. If there is no data to copy, the URLRequestFileJob | 14 // the given buffer. If there is no data to copy, the URLRequestFileJob |
| 15 // attempts to read more from the file to fill its buffer. If reading from the | 15 // attempts to read more from the file to fill its buffer. If reading from the |
| 16 // file does not complete synchronously, then the URLRequestFileJob waits for a | 16 // file does not complete synchronously, then the URLRequestFileJob waits for a |
| 17 // signal from the OS that the overlapped read has completed. It does so by | 17 // signal from the OS that the overlapped read has completed. It does so by |
| 18 // leveraging the MessageLoop::WatchObject API. | 18 // leveraging the MessageLoop::WatchObject API. |
| 19 | 19 |
| 20 #include "net/url_request/url_request_file_job.h" | 20 #include "net/url_request/url_request_file_job.h" |
| 21 | 21 |
| 22 #include "base/bind.h" | 22 #include "base/bind.h" |
| 23 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
| 24 #include "base/files/file_util.h" | 24 #include "base/files/file_util.h" |
| 25 #include "base/message_loop/message_loop.h" | 25 #include "base/message_loop/message_loop.h" |
| 26 #include "base/profiler/scoped_profile.h" | 26 #include "base/profiler/scoped_tracker.h" |
| 27 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
| 28 #include "base/synchronization/lock.h" | 28 #include "base/synchronization/lock.h" |
| 29 #include "base/task_runner.h" | 29 #include "base/task_runner.h" |
| 30 #include "base/threading/thread_restrictions.h" | 30 #include "base/threading/thread_restrictions.h" |
| 31 #include "build/build_config.h" | 31 #include "build/build_config.h" |
| 32 #include "net/base/file_stream.h" | 32 #include "net/base/file_stream.h" |
| 33 #include "net/base/filename_util.h" | 33 #include "net/base/filename_util.h" |
| 34 #include "net/base/io_buffer.h" | 34 #include "net/base/io_buffer.h" |
| 35 #include "net/base/load_flags.h" | 35 #include "net/base/load_flags.h" |
| 36 #include "net/base/mime_util.h" | 36 #include "net/base/mime_util.h" |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 base::File::FLAG_READ | | 243 base::File::FLAG_READ | |
| 244 base::File::FLAG_ASYNC; | 244 base::File::FLAG_ASYNC; |
| 245 int rv = stream_->Open(file_path_, flags, | 245 int rv = stream_->Open(file_path_, flags, |
| 246 base::Bind(&URLRequestFileJob::DidOpen, | 246 base::Bind(&URLRequestFileJob::DidOpen, |
| 247 weak_ptr_factory_.GetWeakPtr())); | 247 weak_ptr_factory_.GetWeakPtr())); |
| 248 if (rv != ERR_IO_PENDING) | 248 if (rv != ERR_IO_PENDING) |
| 249 DidOpen(rv); | 249 DidOpen(rv); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void URLRequestFileJob::DidOpen(int result) { | 252 void URLRequestFileJob::DidOpen(int result) { |
| 253 // TODO(vadimt): Remove ScopedProfile below once crbug.com/423948 is fixed. | 253 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 254 tracked_objects::ScopedProfile tracking_profile( | 254 tracked_objects::ScopedTracker tracking_profile( |
| 255 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequestFileJob::DidOpen")); | 255 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequestFileJob::DidOpen")); |
| 256 | 256 |
| 257 if (result != OK) { | 257 if (result != OK) { |
| 258 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 258 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 259 return; | 259 return; |
| 260 } | 260 } |
| 261 | 261 |
| 262 if (!byte_range_.ComputeBounds(meta_info_.file_size)) { | 262 if (!byte_range_.ComputeBounds(meta_info_.file_size)) { |
| 263 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 263 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 264 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 264 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 265 return; | 265 return; |
| 266 } | 266 } |
| 267 | 267 |
| 268 remaining_bytes_ = byte_range_.last_byte_position() - | 268 remaining_bytes_ = byte_range_.last_byte_position() - |
| 269 byte_range_.first_byte_position() + 1; | 269 byte_range_.first_byte_position() + 1; |
| 270 DCHECK_GE(remaining_bytes_, 0); | 270 DCHECK_GE(remaining_bytes_, 0); |
| 271 | 271 |
| 272 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) { | 272 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) { |
| 273 // TODO(vadimt): Remove ScopedProfile below once crbug.com/423948 is fixed. | 273 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 274 tracked_objects::ScopedProfile tracking_profile1( | 274 tracked_objects::ScopedTracker tracking_profile1( |
| 275 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 275 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 276 "423948 URLRequestFileJob::DidOpen 1")); | 276 "423948 URLRequestFileJob::DidOpen 1")); |
| 277 | 277 |
| 278 int rv = stream_->Seek(base::File::FROM_BEGIN, | 278 int rv = stream_->Seek(base::File::FROM_BEGIN, |
| 279 byte_range_.first_byte_position(), | 279 byte_range_.first_byte_position(), |
| 280 base::Bind(&URLRequestFileJob::DidSeek, | 280 base::Bind(&URLRequestFileJob::DidSeek, |
| 281 weak_ptr_factory_.GetWeakPtr())); | 281 weak_ptr_factory_.GetWeakPtr())); |
| 282 if (rv != ERR_IO_PENDING) { | 282 if (rv != ERR_IO_PENDING) { |
| 283 // stream_->Seek() failed, so pass an intentionally erroneous value | 283 // stream_->Seek() failed, so pass an intentionally erroneous value |
| 284 // into DidSeek(). | 284 // into DidSeek(). |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 if (result == 0) { | 317 if (result == 0) { |
| 318 NotifyDone(URLRequestStatus()); | 318 NotifyDone(URLRequestStatus()); |
| 319 } else if (result < 0) { | 319 } else if (result < 0) { |
| 320 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 320 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 321 } | 321 } |
| 322 | 322 |
| 323 NotifyReadComplete(result); | 323 NotifyReadComplete(result); |
| 324 } | 324 } |
| 325 | 325 |
| 326 } // namespace net | 326 } // namespace net |
| OLD | NEW |