feat: migrate protocol module to NetworkService (Part 8) (#18361)

* Add ProxyingURLLoaderFactory

* Intercept file:// protocol to support asar archives
This commit is contained in:
Cheng Zhao 2019-05-22 10:43:37 +09:00 коммит произвёл GitHub
Родитель 287345c778
Коммит e1a2cc7f36
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 145 добавлений и 0 удалений

Просмотреть файл

@ -30,6 +30,7 @@
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/net/network_context_service.h" #include "atom/browser/net/network_context_service.h"
#include "atom/browser/net/network_context_service_factory.h" #include "atom/browser/net/network_context_service_factory.h"
#include "atom/browser/net/proxying_url_loader_factory.h"
#include "atom/browser/notifications/notification_presenter.h" #include "atom/browser/notifications/notification_presenter.h"
#include "atom/browser/notifications/platform_notification_service.h" #include "atom/browser/notifications/platform_notification_service.h"
#include "atom/browser/session_preferences.h" #include "atom/browser/session_preferences.h"
@ -958,6 +959,29 @@ void AtomBrowserClient::RegisterNonNetworkSubresourceURLLoaderFactories(
protocol->RegisterURLLoaderFactories(factories); protocol->RegisterURLLoaderFactories(factories);
} }
bool AtomBrowserClient::WillCreateURLLoaderFactory(
content::BrowserContext* browser_context,
content::RenderFrameHost* frame_host,
int render_process_id,
bool is_navigation,
bool is_download,
const url::Origin& request_initiator,
network::mojom::URLLoaderFactoryRequest* factory_request,
network::mojom::TrustedURLLoaderHeaderClientPtrInfo* header_client,
bool* bypass_redirect_checks) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame_host);
if (!web_contents)
return false;
auto proxied_request = std::move(*factory_request);
network::mojom::URLLoaderFactoryPtrInfo target_factory_info;
*factory_request = mojo::MakeRequest(&target_factory_info);
new ProxyingURLLoaderFactory(std::move(proxied_request),
std::move(target_factory_info));
return true;
}
std::string AtomBrowserClient::GetApplicationLocale() { std::string AtomBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO)) if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get(); return g_io_thread_application_locale.Get();

Просмотреть файл

@ -167,6 +167,16 @@ class AtomBrowserClient : public content::ContentBrowserClient,
int render_process_id, int render_process_id,
int render_frame_id, int render_frame_id,
NonNetworkURLLoaderFactoryMap* factories) override; NonNetworkURLLoaderFactoryMap* factories) override;
bool WillCreateURLLoaderFactory(
content::BrowserContext* browser_context,
content::RenderFrameHost* frame,
int render_process_id,
bool is_navigation,
bool is_download,
const url::Origin& request_initiator,
network::mojom::URLLoaderFactoryRequest* factory_request,
network::mojom::TrustedURLLoaderHeaderClientPtrInfo* header_client,
bool* bypass_redirect_checks) override;
// content::RenderProcessHostObserver: // content::RenderProcessHostObserver:
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

Просмотреть файл

@ -0,0 +1,62 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/proxying_url_loader_factory.h"
#include <utility>
#include "atom/browser/net/asar/asar_url_loader.h"
namespace atom {
ProxyingURLLoaderFactory::ProxyingURLLoaderFactory(
network::mojom::URLLoaderFactoryRequest loader_request,
network::mojom::URLLoaderFactoryPtrInfo target_factory_info)
: weak_factory_(this) {
target_factory_.Bind(std::move(target_factory_info));
target_factory_.set_connection_error_handler(base::BindOnce(
&ProxyingURLLoaderFactory::OnTargetFactoryError, base::Unretained(this)));
proxy_bindings_.AddBinding(this, std::move(loader_request));
proxy_bindings_.set_connection_error_handler(base::BindRepeating(
&ProxyingURLLoaderFactory::OnProxyBindingError, base::Unretained(this)));
}
ProxyingURLLoaderFactory::~ProxyingURLLoaderFactory() = default;
void ProxyingURLLoaderFactory::CreateLoaderAndStart(
network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
// Intercept file:// protocol to support asar archives.
if (request.url.SchemeIsFile()) {
asar::CreateAsarURLLoader(request, std::move(loader), std::move(client),
nullptr);
return;
}
// Pass-through to the original factory.
target_factory_->CreateLoaderAndStart(std::move(loader), routing_id,
request_id, options, request,
std::move(client), traffic_annotation);
}
void ProxyingURLLoaderFactory::Clone(
network::mojom::URLLoaderFactoryRequest loader_request) {
proxy_bindings_.AddBinding(this, std::move(loader_request));
}
void ProxyingURLLoaderFactory::OnTargetFactoryError() {
delete this;
}
void ProxyingURLLoaderFactory::OnProxyBindingError() {
if (proxy_bindings_.empty())
delete this;
}
} // namespace atom

Просмотреть файл

@ -0,0 +1,47 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_
#define ATOM_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
namespace atom {
class ProxyingURLLoaderFactory : public network::mojom::URLLoaderFactory {
public:
ProxyingURLLoaderFactory(
network::mojom::URLLoaderFactoryRequest loader_request,
network::mojom::URLLoaderFactoryPtrInfo target_factory_info);
~ProxyingURLLoaderFactory() override;
// network::mojom::URLLoaderFactory:
void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag&
traffic_annotation) override;
void Clone(network::mojom::URLLoaderFactoryRequest request) override;
private:
void OnTargetFactoryError();
void OnProxyBindingError();
mojo::BindingSet<network::mojom::URLLoaderFactory> proxy_bindings_;
network::mojom::URLLoaderFactoryPtr target_factory_;
base::WeakPtrFactory<ProxyingURLLoaderFactory> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ProxyingURLLoaderFactory);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_

Просмотреть файл

@ -335,6 +335,8 @@ filenames = {
"atom/browser/net/atom_url_request_job_factory.h", "atom/browser/net/atom_url_request_job_factory.h",
"atom/browser/net/http_protocol_handler.cc", "atom/browser/net/http_protocol_handler.cc",
"atom/browser/net/http_protocol_handler.h", "atom/browser/net/http_protocol_handler.h",
"atom/browser/net/proxying_url_loader_factory.cc",
"atom/browser/net/proxying_url_loader_factory.h",
"atom/browser/net/js_asker.cc", "atom/browser/net/js_asker.cc",
"atom/browser/net/js_asker.h", "atom/browser/net/js_asker.h",
"atom/browser/net/network_context_service_factory.cc", "atom/browser/net/network_context_service_factory.cc",