Chromium Code Reviews| 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 (!IsFileAccessAllowed( |
| 104 *request, file_path, request->context()->network_delegate())) { | |
| 103 return new URLRequestErrorJob( | 105 return new URLRequestErrorJob( |
| 104 request, request->context()->network_delegate(), ERR_ACCESS_DENIED); | 106 request, request->context()->network_delegate(), ERR_ACCESS_DENIED); |
| 105 } | 107 } |
| 106 | 108 |
| 107 // 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 |
| 108 // URLRequestFileDirJob for directory access. To avoid accessing the | 110 // URLRequestFileDirJob for directory access. To avoid accessing the |
| 109 // filesystem, we only look at the path string here. | 111 // filesystem, we only look at the path string here. |
| 110 // The code in the URLRequestFileJob::Start() method discovers that a path, | 112 // The code in the URLRequestFileJob::Start() method discovers that a path, |
| 111 // 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, |
| 112 // and it then redirects to the URLRequestFileDirJob. | 114 // and it then redirects to the URLRequestFileDirJob. |
| 113 if (is_file && | 115 if (is_file && |
| 114 file_util::EndsWithSeparator(file_path) && | 116 file_util::EndsWithSeparator(file_path) && |
| 115 file_path.IsAbsolute()) | 117 file_path.IsAbsolute()) |
| 116 return new URLRequestFileDirJob(request, file_path); | 118 return new URLRequestFileDirJob(request, |
| 119 request->context()->network_delegate(), | |
| 120 file_path); | |
| 117 | 121 |
| 118 // Use a regular file request job for all non-directories (including invalid | 122 // Use a regular file request job for all non-directories (including invalid |
| 119 // file names). | 123 // file names). |
| 120 return new URLRequestFileJob(request, file_path); | 124 return new URLRequestFileJob( |
| 125 request, file_path, request->context()->network_delegate()); | |
| 121 } | 126 } |
| 122 | 127 |
| 123 void URLRequestFileJob::Start() { | 128 void URLRequestFileJob::Start() { |
| 124 DCHECK(!async_resolver_); | 129 DCHECK(!async_resolver_); |
| 125 async_resolver_ = new AsyncResolver(this); | 130 async_resolver_ = new AsyncResolver(this); |
| 126 base::WorkerPool::PostTask( | 131 base::WorkerPool::PostTask( |
| 127 FROM_HERE, | 132 FROM_HERE, |
| 128 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 133 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 129 true); | 134 true); |
| 130 } | 135 } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 // requests. | 252 // requests. |
| 248 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 253 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 249 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 254 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 250 } | 255 } |
| 251 } | 256 } |
| 252 } | 257 } |
| 253 } | 258 } |
| 254 | 259 |
| 255 // static | 260 // static |
| 256 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, | 261 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, |
| 257 const FilePath& path) { | 262 const FilePath& path, |
| 258 const URLRequestContext* context = request.context(); | 263 NetworkDelegate* delegate) { |
| 259 if (!context) | |
| 260 return false; | |
| 261 const NetworkDelegate* delegate = context->network_delegate(); | |
| 262 if (delegate) | 264 if (delegate) |
| 263 return delegate->CanAccessFile(request, path); | 265 return delegate->CanAccessFile(request, path); |
|
erikwright (departed)
2012/07/13 14:50:36
I would just inline this at this point. Replace ca
shalev
2012/07/17 19:40:16
Done.
| |
| 264 return false; | 266 return false; |
| 265 } | 267 } |
| 266 | 268 |
| 267 URLRequestFileJob::~URLRequestFileJob() { | 269 URLRequestFileJob::~URLRequestFileJob() { |
| 268 DCHECK(!async_resolver_); | 270 DCHECK(!async_resolver_); |
| 269 } | 271 } |
| 270 | 272 |
| 271 void URLRequestFileJob::DidResolve( | 273 void URLRequestFileJob::DidResolve( |
| 272 bool exists, const base::PlatformFileInfo& file_info) { | 274 bool exists, const base::PlatformFileInfo& file_info) { |
| 273 async_resolver_ = NULL; | 275 async_resolver_ = NULL; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 345 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 344 } | 346 } |
| 345 | 347 |
| 346 remaining_bytes_ -= result; | 348 remaining_bytes_ -= result; |
| 347 DCHECK_GE(remaining_bytes_, 0); | 349 DCHECK_GE(remaining_bytes_, 0); |
| 348 | 350 |
| 349 NotifyReadComplete(result); | 351 NotifyReadComplete(result); |
| 350 } | 352 } |
| 351 | 353 |
| 352 } // namespace net | 354 } // namespace net |
| OLD | NEW |