| 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 |
| 45 |
| 46 // By default we don't allow access to all file:// urls on ChromeOS but we do on |
| 47 // other platforms. |
| 48 #if defined(OS_CHROMEOS) |
| 49 bool URLRequestFileJob::g_allow_file_access_ = false; |
| 50 #else |
| 51 bool URLRequestFileJob::g_allow_file_access_ = true; |
| 52 #endif |
| 53 |
| 44 class URLRequestFileJob::AsyncResolver | 54 class URLRequestFileJob::AsyncResolver |
| 45 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { | 55 : public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { |
| 46 public: | 56 public: |
| 47 explicit AsyncResolver(URLRequestFileJob* owner) | 57 explicit AsyncResolver(URLRequestFileJob* owner) |
| 48 : owner_(owner), owner_loop_(MessageLoop::current()) { | 58 : owner_(owner), owner_loop_(MessageLoop::current()) { |
| 49 } | 59 } |
| 50 | 60 |
| 51 void Resolve(const FilePath& file_path) { | 61 void Resolve(const FilePath& file_path) { |
| 52 base::PlatformFileInfo file_info; | 62 base::PlatformFileInfo file_info; |
| 53 bool exists = file_util::GetFileInfo(file_path, &file_info); | 63 bool exists = file_util::GetFileInfo(file_path, &file_info); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 : URLRequestJob(request), | 97 : URLRequestJob(request), |
| 88 file_path_(file_path), | 98 file_path_(file_path), |
| 89 stream_(NULL), | 99 stream_(NULL), |
| 90 is_directory_(false), | 100 is_directory_(false), |
| 91 remaining_bytes_(0) { | 101 remaining_bytes_(0) { |
| 92 } | 102 } |
| 93 | 103 |
| 94 // static | 104 // static |
| 95 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, | 105 URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, |
| 96 const std::string& scheme) { | 106 const std::string& scheme) { |
| 97 | |
| 98 FilePath file_path; | 107 FilePath file_path; |
| 99 const bool is_file = FileURLToFilePath(request->url(), &file_path); | 108 const bool is_file = FileURLToFilePath(request->url(), &file_path); |
| 100 | 109 |
| 101 #if defined(OS_CHROMEOS) | 110 // Check file access permissions. |
| 102 // Check file access. | 111 if (!IsFileAccessAllowed(*request, file_path)) |
| 103 if (AccessDisabled(file_path)) | |
| 104 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); | 112 return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); |
| 105 #endif | |
| 106 | 113 |
| 107 // We need to decide whether to create URLRequestFileJob for file access or | 114 // We need to decide whether to create URLRequestFileJob for file access or |
| 108 // URLRequestFileDirJob for directory access. To avoid accessing the | 115 // URLRequestFileDirJob for directory access. To avoid accessing the |
| 109 // filesystem, we only look at the path string here. | 116 // filesystem, we only look at the path string here. |
| 110 // The code in the URLRequestFileJob::Start() method discovers that a path, | 117 // 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, | 118 // which doesn't end with a slash, should really be treated as a directory, |
| 112 // and it then redirects to the URLRequestFileDirJob. | 119 // and it then redirects to the URLRequestFileDirJob. |
| 113 if (is_file && | 120 if (is_file && |
| 114 file_util::EndsWithSeparator(file_path) && | 121 file_util::EndsWithSeparator(file_path) && |
| 115 file_path.IsAbsolute()) | 122 file_path.IsAbsolute()) |
| 116 return new URLRequestFileDirJob(request, file_path); | 123 return new URLRequestFileDirJob(request, file_path); |
| 117 | 124 |
| 118 // Use a regular file request job for all non-directories (including invalid | 125 // Use a regular file request job for all non-directories (including invalid |
| 119 // file names). | 126 // file names). |
| 120 return new URLRequestFileJob(request, file_path); | 127 return new URLRequestFileJob(request, file_path); |
| 121 } | 128 } |
| 122 | 129 |
| 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() { | 130 void URLRequestFileJob::Start() { |
| 153 DCHECK(!async_resolver_); | 131 DCHECK(!async_resolver_); |
| 154 async_resolver_ = new AsyncResolver(this); | 132 async_resolver_ = new AsyncResolver(this); |
| 155 base::WorkerPool::PostTask( | 133 base::WorkerPool::PostTask( |
| 156 FROM_HERE, | 134 FROM_HERE, |
| 157 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), | 135 base::Bind(&AsyncResolver::Resolve, async_resolver_.get(), file_path_), |
| 158 true); | 136 true); |
| 159 } | 137 } |
| 160 | 138 |
| 161 void URLRequestFileJob::Kill() { | 139 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. | 252 // because we need to do multipart encoding here. |
| 275 // TODO(hclam): decide whether we want to support multiple range | 253 // TODO(hclam): decide whether we want to support multiple range |
| 276 // requests. | 254 // requests. |
| 277 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 255 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 278 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 256 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 279 } | 257 } |
| 280 } | 258 } |
| 281 } | 259 } |
| 282 } | 260 } |
| 283 | 261 |
| 262 // static |
| 263 void URLRequestFileJob::AllowAccessToAllFiles() { |
| 264 g_allow_file_access_ = true; |
| 265 } |
| 266 |
| 267 // static |
| 268 bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, |
| 269 const FilePath& path) { |
| 270 if (g_allow_file_access_) |
| 271 return true; |
| 272 const URLRequestContext* context = request.context(); |
| 273 if (!context) |
| 274 return false; |
| 275 const NetworkDelegate* delegate = context->network_delegate(); |
| 276 if (delegate) |
| 277 return delegate->NotifyFileAccessRequested(request, path); |
| 278 return false; |
| 279 } |
| 280 |
| 284 URLRequestFileJob::~URLRequestFileJob() { | 281 URLRequestFileJob::~URLRequestFileJob() { |
| 285 DCHECK(!async_resolver_); | 282 DCHECK(!async_resolver_); |
| 286 } | 283 } |
| 287 | 284 |
| 288 void URLRequestFileJob::DidResolve( | 285 void URLRequestFileJob::DidResolve( |
| 289 bool exists, const base::PlatformFileInfo& file_info) { | 286 bool exists, const base::PlatformFileInfo& file_info) { |
| 290 async_resolver_ = NULL; | 287 async_resolver_ = NULL; |
| 291 | 288 |
| 292 // We may have been orphaned... | 289 // We may have been orphaned... |
| 293 if (!request_) | 290 if (!request_) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 357 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 361 } | 358 } |
| 362 | 359 |
| 363 remaining_bytes_ -= result; | 360 remaining_bytes_ -= result; |
| 364 DCHECK_GE(remaining_bytes_, 0); | 361 DCHECK_GE(remaining_bytes_, 0); |
| 365 | 362 |
| 366 NotifyReadComplete(result); | 363 NotifyReadComplete(result); |
| 367 } | 364 } |
| 368 | 365 |
| 369 } // namespace net | 366 } // namespace net |
| OLD | NEW |