Commit 44819b64 authored by Tsuyoshi Horo's avatar Tsuyoshi Horo Committed by Commit Bot

Refactor BundledExchangesSource

Bug: 966753
Change-Id: Id51e9b7ffe3ca34b950c7bd2adcbd05af8acc776
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1784418Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarKunihiko Sakamoto <ksakamoto@chromium.org>
Commit-Queue: Tsuyoshi Horo <horo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#694151}
parent 1f146124
...@@ -1916,13 +1916,9 @@ void NavigationRequest::OnStartChecksComplete( ...@@ -1916,13 +1916,9 @@ void NavigationRequest::OnStartChecksComplete(
const base::FilePath specified_path = const base::FilePath specified_path =
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
switches::kTrustableBundledExchangesFile); switches::kTrustableBundledExchangesFile);
base::FilePath url_path; if (net::FilePathToFileURL(specified_path) == common_params_->url) {
if (net::FileURLToFilePath(common_params_->url, &url_path) && bundled_exchanges_handle_ = std::make_unique<BundledExchangesHandle>(
url_path == specified_path) { BundledExchangesSource::CreateFromTrustedFile(specified_path));
BundledExchangesSource source(specified_path);
source.is_trusted = true;
bundled_exchanges_handle_ =
std::make_unique<BundledExchangesHandle>(source);
} }
} else if (base::FeatureList::IsEnabled(features::kBundledHTTPExchanges)) { } else if (base::FeatureList::IsEnabled(features::kBundledHTTPExchanges)) {
// Production path behind the feature flag. // Production path behind the feature flag.
......
...@@ -152,19 +152,16 @@ class BundledExchangesHandle::PrimaryURLRedirectLoader final ...@@ -152,19 +152,16 @@ class BundledExchangesHandle::PrimaryURLRedirectLoader final
DISALLOW_COPY_AND_ASSIGN(PrimaryURLRedirectLoader); DISALLOW_COPY_AND_ASSIGN(PrimaryURLRedirectLoader);
}; };
BundledExchangesHandle::BundledExchangesHandle() BundledExchangesHandle::BundledExchangesHandle() {}
: BundledExchangesHandle(BundledExchangesSource()) {}
BundledExchangesHandle::BundledExchangesHandle( BundledExchangesHandle::BundledExchangesHandle(
const BundledExchangesSource& bundled_exchanges_source) std::unique_ptr<BundledExchangesSource> bundled_exchanges_source)
: source_(bundled_exchanges_source) { : source_(std::move(bundled_exchanges_source)),
reader_(std::make_unique<BundledExchangesReader>(*source_)) {
DCHECK(source_->is_trusted());
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (bundled_exchanges_source.IsValid()) { reader_->ReadMetadata(base::BindOnce(&BundledExchangesHandle::OnMetadataReady,
reader_ = weak_factory_.GetWeakPtr()));
std::make_unique<BundledExchangesReader>(bundled_exchanges_source);
reader_->ReadMetadata(base::BindOnce(
&BundledExchangesHandle::OnMetadataReady, weak_factory_.GetWeakPtr()));
}
} }
BundledExchangesHandle::~BundledExchangesHandle() { BundledExchangesHandle::~BundledExchangesHandle() {
...@@ -175,10 +172,9 @@ std::unique_ptr<NavigationLoaderInterceptor> ...@@ -175,10 +172,9 @@ std::unique_ptr<NavigationLoaderInterceptor>
BundledExchangesHandle::CreateInterceptor() { BundledExchangesHandle::CreateInterceptor() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
return std::make_unique<Interceptor>( return std::make_unique<Interceptor>(
source_.IsValid() reader_ ? base::BindRepeating(&BundledExchangesHandle::CreateURLLoader,
? base::BindRepeating(&BundledExchangesHandle::CreateURLLoader, weak_factory_.GetWeakPtr())
weak_factory_.GetWeakPtr()) : base::NullCallback());
: base::NullCallback());
} }
void BundledExchangesHandle::CreateURLLoaderFactory( void BundledExchangesHandle::CreateURLLoaderFactory(
...@@ -199,9 +195,7 @@ void BundledExchangesHandle::CreateURLLoader( ...@@ -199,9 +195,7 @@ void BundledExchangesHandle::CreateURLLoader(
const network::ResourceRequest& resource_request, const network::ResourceRequest& resource_request,
network::mojom::URLLoaderRequest request, network::mojom::URLLoaderRequest request,
network::mojom::URLLoaderClientPtr client) { network::mojom::URLLoaderClientPtr client) {
DCHECK(source_.is_trusted);
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (metadata_error_) { if (metadata_error_) {
client->OnComplete( client->OnComplete(
network::URLLoaderCompletionStatus(net::ERR_INVALID_BUNDLED_EXCHANGES)); network::URLLoaderCompletionStatus(net::ERR_INVALID_BUNDLED_EXCHANGES));
...@@ -210,7 +204,7 @@ void BundledExchangesHandle::CreateURLLoader( ...@@ -210,7 +204,7 @@ void BundledExchangesHandle::CreateURLLoader(
if (!url_loader_factory_) { if (!url_loader_factory_) {
// This must be the first request to the bundled exchange file. // This must be the first request to the bundled exchange file.
DCHECK(source_.Match(resource_request.url)); DCHECK_EQ(source_->url(), resource_request.url);
pending_create_url_loader_task_ = base::Bind( pending_create_url_loader_task_ = base::Bind(
&BundledExchangesHandle::CreateURLLoader, base::Unretained(this), &BundledExchangesHandle::CreateURLLoader, base::Unretained(this),
resource_request, base::Passed(&request), base::Passed(&client)); resource_request, base::Passed(&request), base::Passed(&client));
...@@ -220,7 +214,7 @@ void BundledExchangesHandle::CreateURLLoader( ...@@ -220,7 +214,7 @@ void BundledExchangesHandle::CreateURLLoader(
// Currently |source_| must be a local file. And the bundle's primary URL // Currently |source_| must be a local file. And the bundle's primary URL
// can't be a local file URL. So while handling redirected request to the // can't be a local file URL. So while handling redirected request to the
// primary URL, |resource_request.url| must not be same as the |source|'s URL. // primary URL, |resource_request.url| must not be same as the |source|'s URL.
if (!source_.Match(resource_request.url)) { if (source_->url() != resource_request.url) {
url_loader_factory_->CreateLoaderAndStart( url_loader_factory_->CreateLoaderAndStart(
std::move(request), /*routing_id=*/0, /*request_id=*/0, /*options=*/0, std::move(request), /*routing_id=*/0, /*request_id=*/0, /*options=*/0,
resource_request, std::move(client), resource_request, std::move(client),
......
...@@ -30,7 +30,8 @@ class NavigationLoaderInterceptor; ...@@ -30,7 +30,8 @@ class NavigationLoaderInterceptor;
class BundledExchangesHandle final { class BundledExchangesHandle final {
public: public:
BundledExchangesHandle(); BundledExchangesHandle();
explicit BundledExchangesHandle(const BundledExchangesSource& source); explicit BundledExchangesHandle(
std::unique_ptr<BundledExchangesSource> bundled_exchanges_source);
~BundledExchangesHandle(); ~BundledExchangesHandle();
// Creates a NavigationLoaderInterceptor instance to handle the request for // Creates a NavigationLoaderInterceptor instance to handle the request for
...@@ -54,10 +55,9 @@ class BundledExchangesHandle final { ...@@ -54,10 +55,9 @@ class BundledExchangesHandle final {
network::mojom::URLLoaderClientPtr client); network::mojom::URLLoaderClientPtr client);
void OnMetadataReady(data_decoder::mojom::BundleMetadataParseErrorPtr error); void OnMetadataReady(data_decoder::mojom::BundleMetadataParseErrorPtr error);
const BundledExchangesSource source_;
base::OnceClosure pending_create_url_loader_task_; base::OnceClosure pending_create_url_loader_task_;
std::unique_ptr<BundledExchangesSource> source_;
std::unique_ptr<BundledExchangesReader> reader_; std::unique_ptr<BundledExchangesReader> reader_;
std::unique_ptr<BundledExchangesURLLoaderFactory> url_loader_factory_; std::unique_ptr<BundledExchangesURLLoaderFactory> url_loader_factory_;
GURL primary_url_; GURL primary_url_;
......
...@@ -142,7 +142,7 @@ BundledExchangesReader::BundledExchangesReader( ...@@ -142,7 +142,7 @@ BundledExchangesReader::BundledExchangesReader(
: parser_(ServiceManagerConnection::GetForProcess() : parser_(ServiceManagerConnection::GetForProcess()
? ServiceManagerConnection::GetForProcess()->GetConnector() ? ServiceManagerConnection::GetForProcess()->GetConnector()
: nullptr), : nullptr),
file_(base::MakeRefCounted<SharedFile>(source.file_path)) {} file_(base::MakeRefCounted<SharedFile>(source.file_path())) {}
BundledExchangesReader::~BundledExchangesReader() { BundledExchangesReader::~BundledExchangesReader() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
namespace content { namespace content {
struct BundledExchangesSource; class BundledExchangesSource;
// A class to handle a BundledExchanges that is specified by |source|. // A class to handle a BundledExchanges that is specified by |source|.
// It asks the utility process to parse metadata and response structures, and // It asks the utility process to parse metadata and response structures, and
......
...@@ -3,49 +3,28 @@ ...@@ -3,49 +3,28 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/web_package/bundled_exchanges_source.h" #include "content/browser/web_package/bundled_exchanges_source.h"
#include "base/memory/ptr_util.h"
#include "net/base/filename_util.h" #include "net/base/filename_util.h"
#include "net/base/url_util.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace content {
namespace { // static
std::unique_ptr<BundledExchangesSource>
bool MatchByFilePath(const GURL& url, const base::FilePath file_path) { BundledExchangesSource::CreateFromTrustedFile(const base::FilePath& file_path) {
if (!url.SchemeIsFile()) return base::WrapUnique(new BundledExchangesSource(
return false; true, file_path, net::FilePathToFileURL(file_path)));
base::FilePath url_file_path;
return net::FileURLToFilePath(url, &url_file_path) &&
file_path == url_file_path;
} }
} // namespace std::unique_ptr<BundledExchangesSource> BundledExchangesSource::Clone() const {
return base::WrapUnique(
BundledExchangesSource::BundledExchangesSource() {} new BundledExchangesSource(is_trusted_, file_path_, url_));
BundledExchangesSource::BundledExchangesSource(const base::FilePath& path)
: file_path(path) {
DCHECK(!file_path.empty());
} }
BundledExchangesSource::BundledExchangesSource( BundledExchangesSource::BundledExchangesSource(bool is_trusted,
const BundledExchangesSource& src) = default; const base::FilePath& file_path,
const GURL& url)
bool BundledExchangesSource::Match(const GURL& url) const { : is_trusted_(is_trusted), file_path_(file_path), url_(url) {}
if (!IsValid())
return false;
GURL request_url = net::SimplifyUrlForRequest(url);
if (!file_path.empty())
return MatchByFilePath(request_url, file_path);
NOTREACHED();
return false;
}
bool BundledExchangesSource::IsValid() const {
return !file_path.empty();
}
} // namespace content } // namespace content
...@@ -8,37 +8,43 @@ ...@@ -8,37 +8,43 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/macros.h" #include "base/macros.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "url/gurl.h"
class GURL;
namespace content { namespace content {
// A struct to abstract required information to access a BundledExchanges. // A class to abstract required information to access a BundledExchanges.
struct CONTENT_EXPORT BundledExchangesSource { class CONTENT_EXPORT BundledExchangesSource {
// Constructs an invalid instance that does not match any. public:
BundledExchangesSource(); static std::unique_ptr<BundledExchangesSource> CreateFromTrustedFile(
const base::FilePath& file_path);
// Constructs a valid instance that match file URL for the given |file_path|.
explicit BundledExchangesSource(const base::FilePath& file_path);
// Copy constructor.
explicit BundledExchangesSource(const BundledExchangesSource& src);
// Checks if this instance is valid and can match a URL. std::unique_ptr<BundledExchangesSource> Clone() const;
bool IsValid() const;
// Checks if the given |url| is for the BundledExchanges itself that this ~BundledExchangesSource() = default;
// instance represents. Note that this does not mean the |url| is for an
// exchange provided by the BundledExchanges.
bool Match(const GURL& url) const;
// A flag to represent if this source can be trusted, i.e. using the URL in // A flag to represent if this source can be trusted, i.e. using the URL in
// the BundledExchanges as the origin for the content. Otherwise, we will use // the BundledExchanges as the origin for the content. Otherwise, we will use
// the origin that serves the BundledExchanges itself. For instance, if the // the origin that serves the BundledExchanges itself. For instance, if the
// BundledExchanges is in a local file system, file:// should be the origin. // BundledExchanges is in a local file system, file:// should be the origin.
bool is_trusted = false; //
// Currently this flag is always true because we only support
// trustable-bundled-exchanges-file loading. TODO(horo): Implement
// untrusted bundled exchanges file loading.
bool is_trusted() const { return is_trusted_; }
const base::FilePath& file_path() const { return file_path_; }
const GURL& url() const { return url_; }
private:
BundledExchangesSource(bool is_trusted,
const base::FilePath& file_path,
const GURL& url);
const bool is_trusted_;
const base::FilePath file_path_;
const GURL url_;
const base::FilePath file_path; DISALLOW_COPY_AND_ASSIGN(BundledExchangesSource);
}; };
} // namespace content } // namespace content
......
...@@ -158,8 +158,9 @@ class MockBundledExchangesReaderFactoryImpl final ...@@ -158,8 +158,9 @@ class MockBundledExchangesReaderFactoryImpl final
return nullptr; return nullptr;
} }
BundledExchangesSource source(temp_file_path_); auto source =
auto reader = std::make_unique<BundledExchangesReader>(source); BundledExchangesSource::CreateFromTrustedFile(temp_file_path_);
auto reader = std::make_unique<BundledExchangesReader>(*source);
std::unique_ptr<MockParserFactory> factory = std::unique_ptr<MockParserFactory> factory =
std::make_unique<MockParserFactory>(); std::make_unique<MockParserFactory>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment