2019-05-11 00:14:48 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. Portions
|
|
|
|
// Copyright (c) 2018 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 "libcef/browser/net_service/url_loader_factory_getter.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/thread_util.h"
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "libcef/common/app_manager.h"
|
2019-05-11 00:14:48 +02:00
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/task/single_thread_task_runner.h"
|
2019-05-11 00:14:48 +02:00
|
|
|
#include "content/browser/devtools/devtools_instrumentation.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "content/browser/renderer_host/render_frame_host_impl.h"
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2019-05-11 00:14:48 +02:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
|
|
|
#include "content/public/common/content_client.h"
|
|
|
|
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
|
|
|
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
|
|
|
|
|
|
|
|
namespace net_service {
|
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
// Based on CreatePendingSharedURLLoaderFactory from
|
2019-05-11 00:14:48 +02:00
|
|
|
// content/browser/download/download_manager_impl.cc.
|
|
|
|
// static
|
|
|
|
scoped_refptr<URLLoaderFactoryGetter> URLLoaderFactoryGetter::Create(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
content::BrowserContext* browser_context) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(browser_context);
|
|
|
|
|
|
|
|
// Call this early because newly created BrowserContexts may need to
|
|
|
|
// initialize additional state, and that should be done on the UI thread
|
|
|
|
// instead of potentially racing with the WillCreateURLLoaderFactory
|
|
|
|
// implementation.
|
2021-06-04 03:34:56 +02:00
|
|
|
auto loader_factory = browser_context->GetDefaultStoragePartition()
|
|
|
|
->GetURLLoaderFactoryForBrowserProcess();
|
2019-05-11 00:14:48 +02:00
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> proxy_factory_remote;
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory>
|
|
|
|
proxy_factory_receiver;
|
2019-05-11 00:14:48 +02:00
|
|
|
|
|
|
|
// Create an intermediate pipe that can be used to proxy the request's
|
|
|
|
// URLLoaderFactory.
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory>
|
|
|
|
maybe_proxy_factory_remote;
|
2019-09-04 17:13:32 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory>
|
2022-03-26 02:12:30 +01:00
|
|
|
maybe_proxy_factory_receiver =
|
|
|
|
maybe_proxy_factory_remote.InitWithNewPipeAndPassReceiver();
|
2019-05-11 00:14:48 +02:00
|
|
|
|
|
|
|
bool should_proxy = false;
|
|
|
|
int render_process_id = -1;
|
|
|
|
|
|
|
|
if (render_frame_host) {
|
|
|
|
render_process_id = render_frame_host->GetProcess()->GetID();
|
|
|
|
|
|
|
|
// Allow DevTools to potentially inject itself into the proxy pipe.
|
|
|
|
should_proxy =
|
|
|
|
content::devtools_instrumentation::WillCreateURLLoaderFactory(
|
|
|
|
static_cast<content::RenderFrameHostImpl*>(render_frame_host),
|
|
|
|
false /* is_navigation */, false /* is_download */,
|
2022-03-26 02:12:30 +01:00
|
|
|
&maybe_proxy_factory_receiver, nullptr /* factory_override */);
|
2019-05-11 00:14:48 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 23:05:36 +02:00
|
|
|
auto browser_client = CefAppManager::Get()->GetContentClient()->browser();
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
// Allow the Content embedder to inject itself if it wants to.
|
2020-06-28 23:05:36 +02:00
|
|
|
should_proxy |= browser_client->WillCreateURLLoaderFactory(
|
2019-05-11 00:14:48 +02:00
|
|
|
browser_context, render_frame_host, render_process_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
content::ContentBrowserClient::URLLoaderFactoryType::kDocumentSubResource,
|
2021-06-04 03:34:56 +02:00
|
|
|
url::Origin(), absl::nullopt /* navigation_id */, ukm::SourceIdObj(),
|
2022-03-26 02:12:30 +01:00
|
|
|
&maybe_proxy_factory_receiver, nullptr /* header_client */,
|
2020-03-04 01:29:39 +01:00
|
|
|
nullptr /* bypass_redirect_checks */, nullptr /* disable_secure_dns */,
|
2023-06-26 12:13:38 +02:00
|
|
|
nullptr /* factory_override */,
|
|
|
|
nullptr /* navigation_response_task_runner */);
|
2019-05-11 00:14:48 +02:00
|
|
|
|
|
|
|
// If anyone above indicated that they care about proxying, pass the
|
|
|
|
// intermediate pipe along to the URLLoaderFactoryGetter.
|
|
|
|
if (should_proxy) {
|
2022-03-26 02:12:30 +01:00
|
|
|
proxy_factory_remote = std::move(maybe_proxy_factory_remote);
|
|
|
|
proxy_factory_receiver = std::move(maybe_proxy_factory_receiver);
|
2019-05-11 00:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return base::WrapRefCounted(new URLLoaderFactoryGetter(
|
2022-03-26 02:12:30 +01:00
|
|
|
loader_factory->Clone(), std::move(proxy_factory_remote),
|
|
|
|
std::move(proxy_factory_receiver)));
|
2019-05-11 00:14:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
// Based on CreateFactory from
|
|
|
|
// content/browser/download/network_download_pending_url_loader_factory.cc.
|
2019-05-11 00:14:48 +02:00
|
|
|
scoped_refptr<network::SharedURLLoaderFactory>
|
|
|
|
URLLoaderFactoryGetter::GetURLLoaderFactory() {
|
|
|
|
// On first call we associate with the current thread.
|
|
|
|
if (!task_runner_) {
|
2023-01-30 18:43:54 +01:00
|
|
|
task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
|
2019-05-11 00:14:48 +02:00
|
|
|
} else {
|
|
|
|
DCHECK(task_runner_->RunsTasksInCurrentSequence());
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (lazy_factory_) {
|
2019-05-11 00:14:48 +02:00
|
|
|
return lazy_factory_;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-05-11 00:14:48 +02:00
|
|
|
|
|
|
|
// Bind on the current thread.
|
|
|
|
auto loader_factory =
|
|
|
|
network::SharedURLLoaderFactory::Create(std::move(loader_factory_info_));
|
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
if (proxy_factory_receiver_.is_valid()) {
|
|
|
|
loader_factory->Clone(std::move(proxy_factory_receiver_));
|
2019-05-11 00:14:48 +02:00
|
|
|
lazy_factory_ =
|
|
|
|
base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
|
2022-03-26 02:12:30 +01:00
|
|
|
std::move(proxy_factory_remote_));
|
2019-05-11 00:14:48 +02:00
|
|
|
} else {
|
|
|
|
lazy_factory_ = loader_factory;
|
|
|
|
}
|
|
|
|
return lazy_factory_;
|
|
|
|
}
|
|
|
|
|
|
|
|
URLLoaderFactoryGetter::URLLoaderFactoryGetter(
|
2020-02-10 18:10:17 +01:00
|
|
|
std::unique_ptr<network::PendingSharedURLLoaderFactory> loader_factory_info,
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> proxy_factory_remote,
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory>
|
|
|
|
proxy_factory_receiver)
|
2019-05-11 00:14:48 +02:00
|
|
|
: loader_factory_info_(std::move(loader_factory_info)),
|
2022-03-26 02:12:30 +01:00
|
|
|
proxy_factory_remote_(std::move(proxy_factory_remote)),
|
|
|
|
proxy_factory_receiver_(std::move(proxy_factory_receiver)) {}
|
2019-05-11 00:14:48 +02:00
|
|
|
|
|
|
|
URLLoaderFactoryGetter::~URLLoaderFactoryGetter() = default;
|
|
|
|
|
|
|
|
void URLLoaderFactoryGetter::DeleteOnCorrectThread() const {
|
|
|
|
if (task_runner_ && !task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
task_runner_->DeleteSoon(FROM_HERE, this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2023-06-26 12:13:38 +02:00
|
|
|
} // namespace net_service
|