diff options
author | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-20 20:30:46 +0000 |
---|---|---|
committer | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-20 20:30:46 +0000 |
commit | 65dcdc57900aa995316f626d00e563dde7d1159d (patch) | |
tree | 7f22b347d398d58bf6b761a4537230421c15b446 /net/url_request | |
parent | 37cfa2aa6671ac042d83b1f7fb9dd1806503f31c (diff) | |
download | chromium_src-65dcdc57900aa995316f626d00e563dde7d1159d.zip chromium_src-65dcdc57900aa995316f626d00e563dde7d1159d.tar.gz chromium_src-65dcdc57900aa995316f626d00e563dde7d1159d.tar.bz2 |
Replaced static URLRequestFileJob factory with non-static protocol handler for File jobs
This is a refactor and shouldn't have any visible behavioral change.
BUG=crbug.com/142945
TEST=browser_tests --single_process --gtest_filter=FullscreenControllerTest.FullscreenFileURL
Review URL: https://chromiumcodereview.appspot.com/10700117
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152381 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/file_protocol_handler.cc | 48 | ||||
-rw-r--r-- | net/url_request/file_protocol_handler.h | 33 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.cc | 22 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.h | 10 |
4 files changed, 93 insertions, 20 deletions
diff --git a/net/url_request/file_protocol_handler.cc b/net/url_request/file_protocol_handler.cc new file mode 100644 index 0000000..ec77772 --- /dev/null +++ b/net/url_request/file_protocol_handler.cc @@ -0,0 +1,48 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/url_request/file_protocol_handler.h" + +#include "base/logging.h" +#include "net/base/net_errors.h" +#include "net/base/net_util.h" +#include "net/url_request/url_request_error_job.h" +#include "net/url_request/url_request_file_dir_job.h" +#include "net/url_request/url_request_file_job.h" + +namespace net { + +FileProtocolHandler::FileProtocolHandler( + NetworkDelegate* network_delegate) + : network_delegate_(network_delegate) { +} + +URLRequestJob* FileProtocolHandler::MaybeCreateJob(URLRequest* request) const { + FilePath file_path; + const bool is_file = FileURLToFilePath(request->url(), &file_path); + + // Check file access permissions. + if (!network_delegate_ || + !network_delegate_->CanAccessFile(*request, file_path)) { + return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); + } + + // We need to decide whether to create URLRequestFileJob for file access or + // URLRequestFileDirJob for directory access. To avoid accessing the + // filesystem, we only look at the path string here. + // The code in the URLRequestFileJob::Start() method discovers that a path, + // which doesn't end with a slash, should really be treated as a directory, + // and it then redirects to the URLRequestFileDirJob. + if (is_file && + file_util::EndsWithSeparator(file_path) && + file_path.IsAbsolute()) { + return new URLRequestFileDirJob(request, file_path); + } + + // Use a regular file request job for all non-directories (including invalid + // file names). + return new URLRequestFileJob(request, file_path, network_delegate_); +} + +} // namespace net diff --git a/net/url_request/file_protocol_handler.h b/net/url_request/file_protocol_handler.h new file mode 100644 index 0000000..6c93ab2 --- /dev/null +++ b/net/url_request/file_protocol_handler.h @@ -0,0 +1,33 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_URL_REQUEST_FILE_PROTOCOL_HANDLER_H_ +#define NET_URL_REQUEST_FILE_PROTOCOL_HANDLER_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "net/url_request/url_request_job_factory.h" + +namespace net { + +class NetworkDelegate; +class URLRequestJob; + +// Implements a ProtocolHandler for File jobs. If |network_delegate_| is NULL, +// then all file requests will fail with ERR_ACCESS_DENIED. +class NET_EXPORT FileProtocolHandler : + public URLRequestJobFactory::ProtocolHandler { + public: + explicit FileProtocolHandler(NetworkDelegate* network_delegate); + virtual URLRequestJob* MaybeCreateJob(URLRequest* request) const OVERRIDE; + + private: + NetworkDelegate* network_delegate_; + + DISALLOW_COPY_AND_ASSIGN(FileProtocolHandler); +}; + +} // namespace net + +#endif // NET_URL_REQUEST_FILE_PROTOCOL_HANDLER_H_ diff --git a/net/url_request/url_request_file_job.cc b/net/url_request/url_request_file_job.cc index c9378dd..f07e92fc 100644 --- a/net/url_request/url_request_file_job.cc +++ b/net/url_request/url_request_file_job.cc @@ -84,8 +84,9 @@ class URLRequestFileJob::AsyncResolver }; URLRequestFileJob::URLRequestFileJob(URLRequest* request, - const FilePath& file_path) - : URLRequestJob(request, request->context()->network_delegate()), + const FilePath& file_path, + NetworkDelegate* network_delegate) + : URLRequestJob(request, network_delegate), file_path_(file_path), stream_(NULL), is_directory_(false), @@ -99,8 +100,11 @@ URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, const bool is_file = FileURLToFilePath(request->url(), &file_path); // Check file access permissions. - if (!IsFileAccessAllowed(*request, file_path)) + if (!request->context()->network_delegate() || + !request->context()->network_delegate()->CanAccessFile( + *request, file_path)) { return new URLRequestErrorJob(request, ERR_ACCESS_DENIED); + } // We need to decide whether to create URLRequestFileJob for file access or // URLRequestFileDirJob for directory access. To avoid accessing the @@ -115,7 +119,8 @@ URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, // Use a regular file request job for all non-directories (including invalid // file names). - return new URLRequestFileJob(request, file_path); + return new URLRequestFileJob( + request, file_path, request->context()->network_delegate()); } void URLRequestFileJob::Start() { @@ -250,15 +255,6 @@ void URLRequestFileJob::SetExtraRequestHeaders( } } -// static -bool URLRequestFileJob::IsFileAccessAllowed(const URLRequest& request, - const FilePath& path) { - const NetworkDelegate* delegate = request.context()->network_delegate(); - if (delegate) - return delegate->CanAccessFile(request, path); - return false; -} - URLRequestFileJob::~URLRequestFileJob() { DCHECK(!async_resolver_); } diff --git a/net/url_request/url_request_file_job.h b/net/url_request/url_request_file_job.h index e2b39a58..011a894 100644 --- a/net/url_request/url_request_file_job.h +++ b/net/url_request/url_request_file_job.h @@ -24,7 +24,9 @@ namespace net { // A request job that handles reading file URLs class NET_EXPORT URLRequestFileJob : public URLRequestJob { public: - URLRequestFileJob(URLRequest* request, const FilePath& file_path); + URLRequestFileJob(URLRequest* request, + const FilePath& file_path, + NetworkDelegate* network_delegate); static URLRequest::ProtocolFactory Factory; @@ -52,12 +54,6 @@ class NET_EXPORT URLRequestFileJob : public URLRequestJob { FilePath file_path_; private: - // Tests to see if access to |path| is allowed. If g_allow_file_access_ is - // true, then this will return true. If the NetworkDelegate associated with - // the |request| says it's OK, then this will also return true. - static bool IsFileAccessAllowed(const URLRequest& request, - const FilePath& path); - // Callback after fetching file info on a background thread. void DidResolve(bool exists, const base::PlatformFileInfo& file_info); |