| 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 17 matching lines...) Expand all Loading... |
| 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/io_buffer.h" | 32 #include "net/base/io_buffer.h" |
| 33 #include "net/base/load_flags.h" | 33 #include "net/base/load_flags.h" |
| 34 #include "net/base/mime_util.h" | 34 #include "net/base/mime_util.h" |
| 35 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 36 #include "net/base/net_util.h" | 36 #include "net/base/net_util.h" |
| 37 #include "net/http/http_util.h" | 37 #include "net/http/http_util.h" |
| 38 #include "net/url_request/url_request.h" | |
| 39 #include "net/url_request/url_request_context.h" | |
| 40 #include "net/url_request/url_request_error_job.h" | 38 #include "net/url_request/url_request_error_job.h" |
| 41 #include "net/url_request/url_request_file_dir_job.h" | 39 #include "net/url_request/url_request_file_dir_job.h" |
| 42 | 40 |
| 43 namespace net { | 41 namespace net { |
| 44 | 42 |
| 45 class URLRequestFileJob::AsyncResolver | 43 class URLRequestFileJob::AsyncResolver |
| 46 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { | 44 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { |
| 47 public: | 45 public: |
| 48 explicit AsyncResolver(URLRequestFileJob* owner) | 46 explicit AsyncResolver(URLRequestFileJob* owner) |
| 49 : owner_(owner), owner_loop_(MessageLoop::current()) { | 47 : owner_(owner), owner_loop_(MessageLoop::current()) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 77 owner_->DidResolve(exists, file_info); | 75 owner_->DidResolve(exists, file_info); |
| 78 } | 76 } |
| 79 | 77 |
| 80 URLRequestFileJob* owner_; | 78 URLRequestFileJob* owner_; |
| 81 | 79 |
| 82 base::Lock lock_; | 80 base::Lock lock_; |
| 83 MessageLoop* owner_loop_; | 81 MessageLoop* owner_loop_; |
| 84 }; | 82 }; |
| 85 | 83 |
| 86 URLRequestFileJob::URLRequestFileJob(URLRequest* request, | 84 URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
| 87 const FilePath& file_path, | 85 NetworkDelegate* network_delegate, |
| 88 NetworkDelegate* network_delegate) | 86 const FilePath& file_path) |
| 89 : URLRequestJob(request, network_delegate), | 87 : URLRequestJob(request, network_delegate), |
| 90 file_path_(file_path), | 88 file_path_(file_path), |
| 91 stream_(NULL), | 89 stream_(NULL), |
| 92 is_directory_(false), | 90 is_directory_(false), |
| 93 remaining_bytes_(0) { | 91 remaining_bytes_(0) { |
| 94 } | 92 } |
| 95 | 93 |
| 96 // static | 94 // static |
| 97 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 95 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
| 96 NetworkDelegate* network_delegate, |
| 98 const std::string& scheme) { | 97 const std::string& scheme) { |
| 99 FilePath file_path; | 98 FilePath file_path; |
| 100 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 99 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
| 101 | 100 |
| 102 // Check file access permissions. | 101 // Check file access permissions. |
| 103 if (!request->context()->network_delegate() || | 102 if (!network_delegate || |
| 104 !request->context()->network_delegate()->CanAccessFile( | 103 !network_delegate->CanAccessFile(*request, file_path)) { |
| 105 *request, file_path)) { | 104 return new URLRequestErrorJob(request, network_delegate, ERR_ACCESS_DENIED); |
| 106 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); | |
| 107 } | 105 } |
| 108 | |
| 109 // We need to decide whether to create URLRequestFileJob for file access or | 106 // We need to decide whether to create URLRequestFileJob for file access or |
| 110 // URLRequestFileDirJob for directory access. To avoid accessing the | 107 // URLRequestFileDirJob for directory access. To avoid accessing the |
| 111 // filesystem, we only look at the path string here. | 108 // filesystem, we only look at the path string here. |
| 112 // The code in the URLRequestFileJob::Start() method discovers that a path, | 109 // The code in the URLRequestFileJob::Start() method discovers that a path, |
| 113 // which doesn't end with a slash, should really be treated as a directory, | 110 // which doesn't end with a slash, should really be treated as a directory, |
| 114 // and it then redirects to the URLRequestFileDirJob. | 111 // and it then redirects to the URLRequestFileDirJob. |
| 115 if (is_file && | 112 if (is_file && |
| 116 file_util::EndsWithSeparator(file_path) && | 113 file_util::EndsWithSeparator(file_path) && |
| 117 file_path.IsAbsolute()) | 114 file_path.IsAbsolute()) |
| 118 return new URLRequestFileDirJob(request, file_path); | 115 return new URLRequestFileDirJob(request, network_delegate, file_path); |
| 119 | 116 |
| 120 // Use a regular file request job for all non-directories (including invalid | 117 // Use a regular file request job for all non-directories (including invalid |
| 121 // file names). | 118 // file names). |
| 122 return new URLRequestFileJob( | 119 return new URLRequestFileJob(request, network_delegate, file_path); |
| 123 request, file_path, request->context()->network_delegate()); | |
| 124 } | 120 } |
| 125 | 121 |
| 126 void URLRequestFileJob::Start() { | 122 void URLRequestFileJob::Start() { |
| 127 DCHECK(!async_resolver_); | 123 DCHECK(!async_resolver_); |
| 128 async_resolver_ = new AsyncResolver(this); | 124 async_resolver_ = new AsyncResolver(this); |
| 129 base::WorkerPool::PostTask( | 125 base::WorkerPool::PostTask( |
| 130 FROM_HERE, | 126 FROM_HERE, |
| 131 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 127 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 132 true); | 128 true); |
| 133 } | 129 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 330 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 335 } | 331 } |
| 336 | 332 |
| 337 remaining_bytes_ -= result; | 333 remaining_bytes_ -= result; |
| 338 DCHECK_GE(remaining_bytes_, 0); | 334 DCHECK_GE(remaining_bytes_, 0); |
| 339 | 335 |
| 340 NotifyReadComplete(result); | 336 NotifyReadComplete(result); |
| 341 } | 337 } |
| 342 | 338 |
| 343 } // namespace net | 339 } // namespace net |
| OLD | NEW |