| 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 18 matching lines...) Expand all Loading... |
| 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" | 38 #include "net/url_request/url_request.h" |
| 39 #include "net/url_request/url_request_context.h" |
| 39 #include "net/url_request/url_request_error_job.h" | 40 #include "net/url_request/url_request_error_job.h" |
| 40 #include "net/url_request/url_request_file_dir_job.h" | 41 #include "net/url_request/url_request_file_dir_job.h" |
| 41 | 42 |
| 42 namespace net { | 43 namespace net { |
| 43 | 44 |
| 44 class URLRequestFileJob::AsyncResolver | 45 class URLRequestFileJob::AsyncResolver |
| 45 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { | 46 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { |
| 46 public: | 47 public: |
| 47 explicit AsyncResolver(URLRequestFileJob* owner) | 48 explicit AsyncResolver(URLRequestFileJob* owner) |
| 48 : owner_(owner), owner_loop_(MessageLoop::current()) { | 49 : owner_(owner), owner_loop_(MessageLoop::current()) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 : URLRequestJob(request), | 88 : URLRequestJob(request), |
| 88 file_path_(file_path), | 89 file_path_(file_path), |
| 89 stream_(NULL), | 90 stream_(NULL), |
| 90 is_directory_(false), | 91 is_directory_(false), |
| 91 remaining_bytes_(0) { | 92 remaining_bytes_(0) { |
| 92 } | 93 } |
| 93 | 94 |
| 94 // static | 95 // static |
| 95 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 96 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
| 96 const std::string& scheme) { | 97 const std::string& scheme) { |
| 97 | |
| 98 FilePath file_path; | 98 FilePath file_path; |
| 99 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 99 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
| 100 | 100 |
| 101 #if defined(OS_CHROMEOS) | 101 // Check file access permissions. |
| 102 // Check file access. | 102 if (!IsFileAccessAllowed(*request, file_path)) |
| 103 if (AccessDisabled(file_path)) | |
| 104 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); | 103 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); |
| 105 #endif | |
| 106 | 104 |
| 107 // We need to decide whether to create URLRequestFileJob for file access or | 105 // We need to decide whether to create URLRequestFileJob for file access or |
| 108 // URLRequestFileDirJob for directory access. To avoid accessing the | 106 // URLRequestFileDirJob for directory access. To avoid accessing the |
| 109 // filesystem, we only look at the path string here. | 107 // filesystem, we only look at the path string here. |
| 110 // The code in the URLRequestFileJob::Start() method discovers that a path, | 108 // 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, | 109 // which doesn't end with a slash, should really be treated as a directory, |
| 112 // and it then redirects to the URLRequestFileDirJob. | 110 // and it then redirects to the URLRequestFileDirJob. |
| 113 if (is_file && | 111 if (is_file && |
| 114 file_util::EndsWithSeparator(file_path) && | 112 file_util::EndsWithSeparator(file_path) && |
| 115 file_path.IsAbsolute()) | 113 file_path.IsAbsolute()) |
| 116 return new URLRequestFileDirJob(request, file_path); | 114 return new URLRequestFileDirJob(request, file_path); |
| 117 | 115 |
| 118 // Use a regular file request job for all non-directories (including invalid | 116 // Use a regular file request job for all non-directories (including invalid |
| 119 // file names). | 117 // file names). |
| 120 return new URLRequestFileJob(request, file_path); | 118 return new URLRequestFileJob(request, file_path); |
| 121 } | 119 } |
| 122 | 120 |
| 123 #if defined(OS_CHROMEOS) | |
| 124 static const char* const kLocalAccessWhiteList[] = { | |
| 125 "/home/chronos/user/Downloads", | |
| 126 "/home/chronos/user/log", | |
| 127 "/media", | |
| 128 "/opt/oem", | |
| 129 "/usr/share/chromeos-assets", | |
| 130 "/tmp", | |
| 131 "/var/log", | |
| 132 }; | |
| 133 | |
| 134 // static | |
| 135 bool URLRequestFileJob::AccessDisabled(const FilePath& file_path) { | |
| 136 if (URLRequest::IsFileAccessAllowed()) { // for tests. | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) { | |
| 141 const FilePath white_listed_path(kLocalAccessWhiteList[i]); | |
| 142 // FilePath::operator== should probably handle trailing seperators. | |
| 143 if (white_listed_path == file_path.StripTrailingSeparators() || | |
| 144 white_listed_path.IsParent(file_path)) { | |
| 145 return false; | |
| 146 } | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 #endif // OS_CHROMEOS | |
| 151 | |
| 152 void URLRequestFileJob::Start() { | 121 void URLRequestFileJob::Start() { |
| 153 DCHECK(!async_resolver_); | 122 DCHECK(!async_resolver_); |
| 154 async_resolver_ = new AsyncResolver(this); | 123 async_resolver_ = new AsyncResolver(this); |
| 155 base::WorkerPool::PostTask( | 124 base::WorkerPool::PostTask( |
| 156 FROM_HERE, | 125 FROM_HERE, |
| 157 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 126 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 158 true); | 127 true); |
| 159 } | 128 } |
| 160 | 129 |
| 161 void URLRequestFileJob::Kill() { | 130 void URLRequestFileJob::Kill() { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // because we need to do multipart encoding here. | 243 // because we need to do multipart encoding here. |
| 275 // TODO(hclam): decide whether we want to support multiple range | 244 // TODO(hclam): decide whether we want to support multiple range |
| 276 // requests. | 245 // requests. |
| 277 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 246 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 278 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 247 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 279 } | 248 } |
| 280 } | 249 } |
| 281 } | 250 } |
| 282 } | 251 } |
| 283 | 252 |
| 253 // static |
| 254 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, |
| 255 const FilePath& path) { |
| 256 const URLRequestContext* context = request.context(); |
| 257 if (!context) |
| 258 return false; |
| 259 const NetworkDelegate* delegate = context->network_delegate(); |
| 260 if (delegate) |
| 261 return delegate->CanAccessFile(request, path); |
| 262 return false; |
| 263 } |
| 264 |
| 284 URLRequestFileJob::~URLRequestFileJob() { | 265 URLRequestFileJob::~URLRequestFileJob() { |
| 285 DCHECK(!async_resolver_); | 266 DCHECK(!async_resolver_); |
| 286 } | 267 } |
| 287 | 268 |
| 288 void URLRequestFileJob::DidResolve( | 269 void URLRequestFileJob::DidResolve( |
| 289 bool exists, const base::PlatformFileInfo& file_info) { | 270 bool exists, const base::PlatformFileInfo& file_info) { |
| 290 async_resolver_ = NULL; | 271 async_resolver_ = NULL; |
| 291 | 272 |
| 292 // We may have been orphaned... | 273 // We may have been orphaned... |
| 293 if (!request_) | 274 if (!request_) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 341 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 361 } | 342 } |
| 362 | 343 |
| 363 remaining_bytes_ -= result; | 344 remaining_bytes_ -= result; |
| 364 DCHECK_GE(remaining_bytes_, 0); | 345 DCHECK_GE(remaining_bytes_, 0); |
| 365 | 346 |
| 366 NotifyReadComplete(result); | 347 NotifyReadComplete(result); |
| 367 } | 348 } |
| 368 | 349 |
| 369 } // namespace net | 350 } // namespace net |
| OLD | NEW |