| 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 // |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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/message_loop.h" | 24 #include "base/message_loop.h" |
| 25 #include "base/platform_file.h" | 25 #include "base/platform_file.h" |
| 26 #include "base/string_util.h" | 26 #include "base/string_util.h" |
| 27 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
| 28 #include "base/threading/worker_pool.h" | 28 #include "base/threading/worker_pool.h" |
| 29 #include "base/threading/thread_restrictions.h" | 29 #include "base/threading/thread_restrictions.h" |
| 30 #include "build/build_config.h" | 30 #include "build/build_config.h" |
| 31 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
| 32 #include "net/base/file_stream.h" |
| 32 #include "net/base/io_buffer.h" | 33 #include "net/base/io_buffer.h" |
| 33 #include "net/base/load_flags.h" | 34 #include "net/base/load_flags.h" |
| 34 #include "net/base/mime_util.h" | 35 #include "net/base/mime_util.h" |
| 35 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 36 #include "net/base/net_util.h" | 37 #include "net/base/net_util.h" |
| 37 #include "net/http/http_util.h" | 38 #include "net/http/http_util.h" |
| 38 #include "net/url_request/url_request_error_job.h" | 39 #include "net/url_request/url_request_error_job.h" |
| 39 #include "net/url_request/url_request_file_dir_job.h" | 40 #include "net/url_request/url_request_file_dir_job.h" |
| 40 | 41 |
| 41 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 base::Lock lock_; | 85 base::Lock lock_; |
| 85 MessageLoop* owner_loop_; | 86 MessageLoop* owner_loop_; |
| 86 }; | 87 }; |
| 87 | 88 |
| 88 URLRequestFileJob::URLRequestFileJob(URLRequest* request, | 89 URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
| 89 NetworkDelegate* network_delegate, | 90 NetworkDelegate* network_delegate, |
| 90 const FilePath& file_path) | 91 const FilePath& file_path) |
| 91 : URLRequestJob(request, network_delegate), | 92 : URLRequestJob(request, network_delegate), |
| 92 file_path_(file_path), | 93 file_path_(file_path), |
| 93 stream_(NULL), | |
| 94 is_directory_(false), | 94 is_directory_(false), |
| 95 remaining_bytes_(0) { | 95 remaining_bytes_(0) { |
| 96 } | 96 } |
| 97 | 97 |
| 98 // static | 98 // static |
| 99 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 99 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
| 100 NetworkDelegate* network_delegate, | 100 NetworkDelegate* network_delegate, |
| 101 const std::string& scheme) { | 101 const std::string& scheme) { |
| 102 FilePath file_path; | 102 FilePath file_path; |
| 103 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 103 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 126 void URLRequestFileJob::Start() { | 126 void URLRequestFileJob::Start() { |
| 127 DCHECK(!async_resolver_); | 127 DCHECK(!async_resolver_); |
| 128 async_resolver_ = new AsyncResolver(this); | 128 async_resolver_ = new AsyncResolver(this); |
| 129 base::WorkerPool::PostTask( | 129 base::WorkerPool::PostTask( |
| 130 FROM_HERE, | 130 FROM_HERE, |
| 131 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 131 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 132 true); | 132 true); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void URLRequestFileJob::Kill() { | 135 void URLRequestFileJob::Kill() { |
| 136 // URL requests should not block on the disk! | 136 stream_.reset(); |
| 137 // http://code.google.com/p/chromium/issues/detail?id=59849 | |
| 138 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 139 stream_.CloseSync(); | |
| 140 | 137 |
| 141 if (async_resolver_) { | 138 if (async_resolver_) { |
| 142 async_resolver_->Cancel(); | 139 async_resolver_->Cancel(); |
| 143 async_resolver_ = NULL; | 140 async_resolver_ = NULL; |
| 144 } | 141 } |
| 145 | 142 |
| 146 URLRequestJob::Kill(); | 143 URLRequestJob::Kill(); |
| 147 } | 144 } |
| 148 | 145 |
| 149 bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size, | 146 bool URLRequestFileJob::ReadRawData(IOBuffer* dest, int dest_size, |
| 150 int *bytes_read) { | 147 int *bytes_read) { |
| 151 DCHECK_NE(dest_size, 0); | 148 DCHECK_NE(dest_size, 0); |
| 152 DCHECK(bytes_read); | 149 DCHECK(bytes_read); |
| 153 DCHECK_GE(remaining_bytes_, 0); | 150 DCHECK_GE(remaining_bytes_, 0); |
| 154 | 151 |
| 155 if (remaining_bytes_ < dest_size) | 152 if (remaining_bytes_ < dest_size) |
| 156 dest_size = static_cast<int>(remaining_bytes_); | 153 dest_size = static_cast<int>(remaining_bytes_); |
| 157 | 154 |
| 158 // If we should copy zero bytes because |remaining_bytes_| is zero, short | 155 // If we should copy zero bytes because |remaining_bytes_| is zero, short |
| 159 // circuit here. | 156 // circuit here. |
| 160 if (!dest_size) { | 157 if (!dest_size) { |
| 161 *bytes_read = 0; | 158 *bytes_read = 0; |
| 162 return true; | 159 return true; |
| 163 } | 160 } |
| 164 | 161 |
| 165 int rv = stream_.Read(dest, dest_size, | 162 int rv = stream_->Read(dest, dest_size, |
| 166 base::Bind(&URLRequestFileJob::DidRead, | 163 base::Bind(&URLRequestFileJob::DidRead, |
| 167 base::Unretained(this))); | 164 base::Unretained(this))); |
| 168 if (rv >= 0) { | 165 if (rv >= 0) { |
| 169 // Data is immediately available. | 166 // Data is immediately available. |
| 170 *bytes_read = rv; | 167 *bytes_read = rv; |
| 171 remaining_bytes_ -= rv; | 168 remaining_bytes_ -= rv; |
| 172 DCHECK_GE(remaining_bytes_, 0); | 169 DCHECK_GE(remaining_bytes_, 0); |
| 173 return true; | 170 return true; |
| 174 } | 171 } |
| 175 | 172 |
| 176 // Otherwise, a read error occured. We may just need to wait... | 173 // Otherwise, a read error occured. We may just need to wait... |
| 177 if (rv == ERR_IO_PENDING) { | 174 if (rv == ERR_IO_PENDING) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // trailing slash. | 271 // trailing slash. |
| 275 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise, | 272 // If a directory does not exist, we return ERR_FILE_NOT_FOUND. Otherwise, |
| 276 // we will append trailing slash and redirect to FileDirJob. | 273 // we will append trailing slash and redirect to FileDirJob. |
| 277 // A special case is "\" on Windows. We should resolve as invalid. | 274 // A special case is "\" on Windows. We should resolve as invalid. |
| 278 // However, Windows resolves "\" to "C:\", thus reports it as existent. | 275 // 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 | 276 // So what happens is we append it with trailing slash and redirect it to |
| 280 // FileDirJob where it is resolved as invalid. | 277 // FileDirJob where it is resolved as invalid. |
| 281 if (!exists) { | 278 if (!exists) { |
| 282 rv = ERR_FILE_NOT_FOUND; | 279 rv = ERR_FILE_NOT_FOUND; |
| 283 } else if (!is_directory_) { | 280 } else if (!is_directory_) { |
| 281 stream_.reset(new FileStream(NULL)); |
| 282 |
| 284 // URL requests should not block on the disk! | 283 // URL requests should not block on the disk! |
| 285 // http://code.google.com/p/chromium/issues/detail?id=59849 | 284 // http://code.google.com/p/chromium/issues/detail?id=59849 |
| 286 base::ThreadRestrictions::ScopedAllowIO allow_io; | 285 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 287 | 286 |
| 288 int flags = base::PLATFORM_FILE_OPEN | | 287 int flags = base::PLATFORM_FILE_OPEN | |
| 289 base::PLATFORM_FILE_READ | | 288 base::PLATFORM_FILE_READ | |
| 290 base::PLATFORM_FILE_ASYNC; | 289 base::PLATFORM_FILE_ASYNC; |
| 291 rv = stream_.OpenSync(file_path_, flags); | 290 rv = stream_->OpenSync(file_path_, flags); |
| 292 } | 291 } |
| 293 | 292 |
| 294 if (rv != OK) { | 293 if (rv != OK) { |
| 295 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 294 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 296 return; | 295 return; |
| 297 } | 296 } |
| 298 | 297 |
| 299 if (!byte_range_.ComputeBounds(file_info.size)) { | 298 if (!byte_range_.ComputeBounds(file_info.size)) { |
| 300 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 299 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 301 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 300 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 302 return; | 301 return; |
| 303 } | 302 } |
| 304 | 303 |
| 305 remaining_bytes_ = byte_range_.last_byte_position() - | 304 remaining_bytes_ = byte_range_.last_byte_position() - |
| 306 byte_range_.first_byte_position() + 1; | 305 byte_range_.first_byte_position() + 1; |
| 307 DCHECK_GE(remaining_bytes_, 0); | 306 DCHECK_GE(remaining_bytes_, 0); |
| 308 | 307 |
| 309 // URL requests should not block on the disk! | 308 // URL requests should not block on the disk! |
| 310 // http://code.google.com/p/chromium/issues/detail?id=59849 | 309 // http://code.google.com/p/chromium/issues/detail?id=59849 |
| 311 { | 310 { |
| 312 base::ThreadRestrictions::ScopedAllowIO allow_io; | 311 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 313 // Do the seek at the beginning of the request. | 312 // Do the seek at the beginning of the request. |
| 314 if (remaining_bytes_ > 0 && | 313 if (remaining_bytes_ > 0 && |
| 315 byte_range_.first_byte_position() != 0 && | 314 byte_range_.first_byte_position() != 0 && |
| 316 byte_range_.first_byte_position() != | 315 byte_range_.first_byte_position() != |
| 317 stream_.SeekSync(FROM_BEGIN, byte_range_.first_byte_position())) { | 316 stream_->SeekSync(FROM_BEGIN, byte_range_.first_byte_position())) { |
| 318 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 317 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 319 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 318 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 320 return; | 319 return; |
| 321 } | 320 } |
| 322 } | 321 } |
| 323 | 322 |
| 324 set_expected_content_size(remaining_bytes_); | 323 set_expected_content_size(remaining_bytes_); |
| 325 NotifyHeadersComplete(); | 324 NotifyHeadersComplete(); |
| 326 } | 325 } |
| 327 | 326 |
| 328 void URLRequestFileJob::DidRead(int result) { | 327 void URLRequestFileJob::DidRead(int result) { |
| 329 if (result > 0) { | 328 if (result > 0) { |
| 330 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status | 329 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status |
| 331 } else if (result == 0) { | 330 } else if (result == 0) { |
| 332 NotifyDone(URLRequestStatus()); | 331 NotifyDone(URLRequestStatus()); |
| 333 } else { | 332 } else { |
| 334 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 333 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 335 } | 334 } |
| 336 | 335 |
| 337 remaining_bytes_ -= result; | 336 remaining_bytes_ -= result; |
| 338 DCHECK_GE(remaining_bytes_, 0); | 337 DCHECK_GE(remaining_bytes_, 0); |
| 339 | 338 |
| 340 NotifyReadComplete(result); | 339 NotifyReadComplete(result); |
| 341 } | 340 } |
| 342 | 341 |
| 343 } // namespace net | 342 } // namespace net |
| OLD | NEW |