| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 owner_->DidResolve(exists, file_info); | 77 owner_->DidResolve(exists, file_info); |
| 78 } | 78 } |
| 79 | 79 |
| 80 URLRequestFileJob* owner_; | 80 URLRequestFileJob* owner_; |
| 81 | 81 |
| 82 base::Lock lock_; | 82 base::Lock lock_; |
| 83 MessageLoop* owner_loop_; | 83 MessageLoop* owner_loop_; |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 URLRequestFileJob::URLRequestFileJob(URLRequest* request, | 86 URLRequestFileJob::URLRequestFileJob(URLRequest* request, |
| 87 const FilePath& file_path) | 87 const FilePath& file_path, |
| 88 : URLRequestJob(request, request->context()->network_delegate()), | 88 NetworkDelegate* network_delegate) |
| 89 : URLRequestJob(request, network_delegate), |
| 89 file_path_(file_path), | 90 file_path_(file_path), |
| 90 stream_(NULL), | 91 stream_(NULL), |
| 91 is_directory_(false), | 92 is_directory_(false), |
| 92 remaining_bytes_(0) { | 93 remaining_bytes_(0) { |
| 93 } | 94 } |
| 94 | 95 |
| 95 // static | 96 // static |
| 96 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 97 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
| 97 const std::string& scheme) { | 98 const std::string& scheme) { |
| 98 FilePath file_path; | 99 FilePath file_path; |
| 99 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 100 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
| 100 | 101 |
| 101 // Check file access permissions. | 102 // Check file access permissions. |
| 102 if (!IsFileAccessAllowed(*request, file_path)) | 103 if (!request->context()->network_delegate() || |
| 104 !request->context()->network_delegate()->CanAccessFile( |
| 105 *request, file_path)) { |
| 103 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); | 106 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); |
| 107 } |
| 104 | 108 |
| 105 // We need to decide whether to create URLRequestFileJob for file access or | 109 // We need to decide whether to create URLRequestFileJob for file access or |
| 106 // URLRequestFileDirJob for directory access. To avoid accessing the | 110 // URLRequestFileDirJob for directory access. To avoid accessing the |
| 107 // filesystem, we only look at the path string here. | 111 // filesystem, we only look at the path string here. |
| 108 // The code in the URLRequestFileJob::Start() method discovers that a path, | 112 // The code in the URLRequestFileJob::Start() method discovers that a path, |
| 109 // which doesn't end with a slash, should really be treated as a directory, | 113 // which doesn't end with a slash, should really be treated as a directory, |
| 110 // and it then redirects to the URLRequestFileDirJob. | 114 // and it then redirects to the URLRequestFileDirJob. |
| 111 if (is_file && | 115 if (is_file && |
| 112 file_util::EndsWithSeparator(file_path) && | 116 file_util::EndsWithSeparator(file_path) && |
| 113 file_path.IsAbsolute()) | 117 file_path.IsAbsolute()) |
| 114 return new URLRequestFileDirJob(request, file_path); | 118 return new URLRequestFileDirJob(request, file_path); |
| 115 | 119 |
| 116 // Use a regular file request job for all non-directories (including invalid | 120 // Use a regular file request job for all non-directories (including invalid |
| 117 // file names). | 121 // file names). |
| 118 return new URLRequestFileJob(request, file_path); | 122 return new URLRequestFileJob( |
| 123 request, file_path, request->context()->network_delegate()); |
| 119 } | 124 } |
| 120 | 125 |
| 121 void URLRequestFileJob::Start() { | 126 void URLRequestFileJob::Start() { |
| 122 DCHECK(!async_resolver_); | 127 DCHECK(!async_resolver_); |
| 123 async_resolver_ = new AsyncResolver(this); | 128 async_resolver_ = new AsyncResolver(this); |
| 124 base::WorkerPool::PostTask( | 129 base::WorkerPool::PostTask( |
| 125 FROM_HERE, | 130 FROM_HERE, |
| 126 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 131 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 127 true); | 132 true); |
| 128 } | 133 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // because we need to do multipart encoding here. | 248 // because we need to do multipart encoding here. |
| 244 // TODO(hclam): decide whether we want to support multiple range | 249 // TODO(hclam): decide whether we want to support multiple range |
| 245 // requests. | 250 // requests. |
| 246 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 251 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 247 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 252 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 } | 255 } |
| 251 } | 256 } |
| 252 | 257 |
| 253 // static | |
| 254 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, | |
| 255 const FilePath& path) { | |
| 256 const NetworkDelegate* delegate = request.context()->network_delegate(); | |
| 257 if (delegate) | |
| 258 return delegate->CanAccessFile(request, path); | |
| 259 return false; | |
| 260 } | |
| 261 | |
| 262 URLRequestFileJob::~URLRequestFileJob() { | 258 URLRequestFileJob::~URLRequestFileJob() { |
| 263 DCHECK(!async_resolver_); | 259 DCHECK(!async_resolver_); |
| 264 } | 260 } |
| 265 | 261 |
| 266 void URLRequestFileJob::DidResolve( | 262 void URLRequestFileJob::DidResolve( |
| 267 bool exists, const base::PlatformFileInfo& file_info) { | 263 bool exists, const base::PlatformFileInfo& file_info) { |
| 268 async_resolver_ = NULL; | 264 async_resolver_ = NULL; |
| 269 | 265 |
| 270 // We may have been orphaned... | 266 // We may have been orphaned... |
| 271 if (!request_) | 267 if (!request_) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 334 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 339 } | 335 } |
| 340 | 336 |
| 341 remaining_bytes_ -= result; | 337 remaining_bytes_ -= result; |
| 342 DCHECK_GE(remaining_bytes_, 0); | 338 DCHECK_GE(remaining_bytes_, 0); |
| 343 | 339 |
| 344 NotifyReadComplete(result); | 340 NotifyReadComplete(result); |
| 345 } | 341 } |
| 346 | 342 |
| 347 } // namespace net | 343 } // namespace net |
| OLD | NEW |