Commit 379e1982 authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

Make ContentDirectoryLoaderFactory always owned by its |receivers_|.

This CL introduces ContentDirectoryLoaderFactory::CreateAndBind static
method that allows creating an ContentDirectoryLoaderFactory that is
owned by its |receivers_| and will self-delete when the last receiver
disconnects.

This CL removes the ability to directly construct and own a
std::unique_ptr<ContentDirectoryLoaderFactory>, because this ability
means that the factory can be destructed while receivers bound via the
Clone method are still alive (see the associated bug).  In particular,
this CL stops exposing ContentDirectoryLoaderFactory constructor as a
public member, which forces construction to go via the new CreateAndBind
static method.

This CL mostly just follows the pattern established earlier by
https://crrev.com/c/2337411.

Bug: 1106995
Change-Id: I5ed88648637eed43f23dfab234115bfa33281ccc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2357964
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812152}
parent 1f7da9ee
...@@ -350,8 +350,23 @@ class ContentDirectoryURLLoader : public network::mojom::URLLoader { ...@@ -350,8 +350,23 @@ class ContentDirectoryURLLoader : public network::mojom::URLLoader {
} // namespace } // namespace
ContentDirectoryLoaderFactory::ContentDirectoryLoaderFactory() // static
: task_runner_(base::ThreadPool::CreateSequencedTaskRunner( mojo::PendingRemote<network::mojom::URLLoaderFactory>
ContentDirectoryLoaderFactory::Create() {
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote;
// The ContentDirectoryLoaderFactory will delete itself when there are no more
// receivers - see the NonNetworkURLLoaderFactoryBase::OnDisconnect method.
new ContentDirectoryLoaderFactory(
pending_remote.InitWithNewPipeAndPassReceiver());
return pending_remote;
}
ContentDirectoryLoaderFactory::ContentDirectoryLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver)
: content::NonNetworkURLLoaderFactoryBase(std::move(factory_receiver)),
task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})) {} base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})) {}
...@@ -445,11 +460,6 @@ void ContentDirectoryLoaderFactory::CreateLoaderAndStart( ...@@ -445,11 +460,6 @@ void ContentDirectoryLoaderFactory::CreateLoaderAndStart(
base::Passed(std::move(metadata_handle)))); base::Passed(std::move(metadata_handle))));
} }
void ContentDirectoryLoaderFactory::Clone(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader) {
receivers_.Add(this, std::move(loader));
}
void ContentDirectoryLoaderFactory::SetContentDirectoriesForTest( void ContentDirectoryLoaderFactory::SetContentDirectoriesForTest(
std::vector<fuchsia::web::ContentDirectoryProvider> directories) { std::vector<fuchsia::web::ContentDirectoryProvider> directories) {
ContentDirectoriesMap* current_process_directories = GetContentDirectories(); ContentDirectoriesMap* current_process_directories = GetContentDirectories();
......
...@@ -12,25 +12,36 @@ ...@@ -12,25 +12,36 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "content/public/browser/non_network_url_loader_factory_base.h"
#include "fuchsia/engine/web_engine_export.h" #include "fuchsia/engine/web_engine_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h" #include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h" #include "services/network/public/mojom/url_loader_factory.mojom.h"
// Creates a URLLoaderFactory which services requests for resources stored // URLLoaderFactory which services requests for resources stored under named
// under named directories. The directories are accessed using the // directories. The directories are accessed using the fuchsia-dir:// scheme.
// fuchsia-dir:// scheme. class ContentDirectoryLoaderFactory
class ContentDirectoryLoaderFactory : public network::mojom::URLLoaderFactory { : public content::NonNetworkURLLoaderFactoryBase {
public: public:
ContentDirectoryLoaderFactory(); // Returns mojo::PendingRemote to a newly constructed
~ContentDirectoryLoaderFactory() override; // ContentDirectoryLoaderFactory. The factory is self-owned - it will delete
// itself once there are no more receivers (including the receiver associated
// with the returned mojo::PendingRemote and the receivers bound by the Clone
// method).
static mojo::PendingRemote<network::mojom::URLLoaderFactory> Create();
// Sets the list of content directories for the duration of the process. // Sets the list of content directories for the duration of the process.
// Can be called multiple times for clearing or replacing the list. // Can be called multiple times for clearing or replacing the list.
static WEB_ENGINE_EXPORT void SetContentDirectoriesForTest( static WEB_ENGINE_EXPORT void SetContentDirectoriesForTest(
std::vector<fuchsia::web::ContentDirectoryProvider> directories); std::vector<fuchsia::web::ContentDirectoryProvider> directories);
private:
explicit ContentDirectoryLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver);
~ContentDirectoryLoaderFactory() override;
// network::mojom::URLLoaderFactory: // network::mojom::URLLoaderFactory:
void CreateLoaderAndStart( void CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> loader, mojo::PendingReceiver<network::mojom::URLLoader> loader,
...@@ -40,10 +51,7 @@ class ContentDirectoryLoaderFactory : public network::mojom::URLLoaderFactory { ...@@ -40,10 +51,7 @@ class ContentDirectoryLoaderFactory : public network::mojom::URLLoaderFactory {
const network::ResourceRequest& request, const network::ResourceRequest& request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client, mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) final; const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) final;
void Clone(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader) final;
private:
net::Error OpenFileFromDirectory( net::Error OpenFileFromDirectory(
const std::string& directory_name, const std::string& directory_name,
base::FilePath path, base::FilePath path,
...@@ -52,8 +60,6 @@ class ContentDirectoryLoaderFactory : public network::mojom::URLLoaderFactory { ...@@ -52,8 +60,6 @@ class ContentDirectoryLoaderFactory : public network::mojom::URLLoaderFactory {
// Used for executing blocking URLLoader routines. // Used for executing blocking URLLoader routines.
const scoped_refptr<base::SequencedTaskRunner> task_runner_; const scoped_refptr<base::SequencedTaskRunner> task_runner_;
mojo::ReceiverSet<network::mojom::URLLoaderFactory> receivers_;
DISALLOW_COPY_AND_ASSIGN(ContentDirectoryLoaderFactory); DISALLOW_COPY_AND_ASSIGN(ContentDirectoryLoaderFactory);
}; };
......
...@@ -136,8 +136,8 @@ void WebEngineContentBrowserClient:: ...@@ -136,8 +136,8 @@ void WebEngineContentBrowserClient::
NonNetworkURLLoaderFactoryMap* factories) { NonNetworkURLLoaderFactoryMap* factories) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kContentDirectories)) { switches::kContentDirectories)) {
(*uniquely_owned_factories)[cr_fuchsia::kFuchsiaDirScheme] = factories->emplace(cr_fuchsia::kFuchsiaDirScheme,
std::make_unique<ContentDirectoryLoaderFactory>(); ContentDirectoryLoaderFactory::Create());
} }
} }
...@@ -149,8 +149,8 @@ void WebEngineContentBrowserClient:: ...@@ -149,8 +149,8 @@ void WebEngineContentBrowserClient::
NonNetworkURLLoaderFactoryMap* factories) { NonNetworkURLLoaderFactoryMap* factories) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kContentDirectories)) { switches::kContentDirectories)) {
(*uniquely_owned_factories)[cr_fuchsia::kFuchsiaDirScheme] = factories->emplace(cr_fuchsia::kFuchsiaDirScheme,
std::make_unique<ContentDirectoryLoaderFactory>(); ContentDirectoryLoaderFactory::Create());
} }
} }
......
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