2020-03-26 17:34:32 +00:00
|
|
|
// Copyright (c) 2020 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <memory>
|
2020-04-01 07:38:40 +00:00
|
|
|
#include <utility>
|
2020-03-26 17:34:32 +00:00
|
|
|
|
2021-03-04 17:27:05 +00:00
|
|
|
#include "services/network/public/cpp/self_deleting_url_loader_factory.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2020-04-01 07:38:40 +00:00
|
|
|
#include "shell/browser/net/asar/asar_url_loader.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
#include "shell/browser/protocol_registry.h"
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2020-04-01 07:38:40 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Provide support for accessing asar archives in file:// protocol.
|
2021-03-04 17:27:05 +00:00
|
|
|
class AsarURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
2020-04-01 07:38:40 +00:00
|
|
|
public:
|
2020-09-21 08:00:36 +00:00
|
|
|
static mojo::PendingRemote<network::mojom::URLLoaderFactory> Create() {
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote;
|
|
|
|
|
|
|
|
// The AsarURLLoaderFactory will delete itself when there are no more
|
2021-03-04 17:27:05 +00:00
|
|
|
// receivers - see the SelfDeletingURLLoaderFactory::OnDisconnect method.
|
2020-09-21 08:00:36 +00:00
|
|
|
new AsarURLLoaderFactory(pending_remote.InitWithNewPipeAndPassReceiver());
|
|
|
|
|
|
|
|
return pending_remote;
|
|
|
|
}
|
2020-04-01 07:38:40 +00:00
|
|
|
|
|
|
|
private:
|
2020-09-21 08:00:36 +00:00
|
|
|
AsarURLLoaderFactory(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver)
|
2021-03-04 17:27:05 +00:00
|
|
|
: network::SelfDeletingURLLoaderFactory(std::move(factory_receiver)) {}
|
2020-09-21 08:00:36 +00:00
|
|
|
~AsarURLLoaderFactory() override = default;
|
|
|
|
|
2020-04-01 07:38:40 +00:00
|
|
|
// network::mojom::URLLoaderFactory:
|
|
|
|
void CreateLoaderAndStart(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
|
|
int32_t routing_id,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
|
|
|
|
override {
|
|
|
|
asar::CreateAsarURLLoader(request, std::move(loader), std::move(client),
|
|
|
|
new net::HttpResponseHeaders(""));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-26 17:34:32 +00:00
|
|
|
// static
|
|
|
|
ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
|
|
|
|
content::BrowserContext* context) {
|
|
|
|
return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtocolRegistry::ProtocolRegistry() {}
|
|
|
|
|
|
|
|
ProtocolRegistry::~ProtocolRegistry() = default;
|
|
|
|
|
|
|
|
void ProtocolRegistry::RegisterURLLoaderFactories(
|
2020-04-01 07:38:40 +00:00
|
|
|
URLLoaderFactoryType type,
|
2020-03-26 17:34:32 +00:00
|
|
|
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories) {
|
2020-04-01 07:38:40 +00:00
|
|
|
// Override the default FileURLLoaderFactory to support asar archives.
|
|
|
|
if (type == URLLoaderFactoryType::kNavigation) {
|
|
|
|
// Always allow navigating to file:// URLs.
|
|
|
|
//
|
|
|
|
// Note that Chromium calls |emplace| to create the default file factory
|
|
|
|
// after this call, so it won't override our asar factory.
|
|
|
|
DCHECK(!base::Contains(*factories, url::kFileScheme));
|
2020-09-21 08:00:36 +00:00
|
|
|
factories->emplace(url::kFileScheme, AsarURLLoaderFactory::Create());
|
2020-04-01 07:38:40 +00:00
|
|
|
} else if (type == URLLoaderFactoryType::kDocumentSubResource) {
|
|
|
|
// Only support requesting file:// subresource URLs when Chromium does so,
|
|
|
|
// it is usually supported under file:// or about:blank documents.
|
|
|
|
auto file_factory = factories->find(url::kFileScheme);
|
|
|
|
if (file_factory != factories->end())
|
2020-09-21 08:00:36 +00:00
|
|
|
file_factory->second = AsarURLLoaderFactory::Create();
|
2020-04-01 07:38:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:34:32 +00:00
|
|
|
for (const auto& it : handlers_) {
|
2020-09-21 08:00:36 +00:00
|
|
|
factories->emplace(it.first, ElectronURLLoaderFactory::Create(
|
2020-03-26 17:34:32 +00:00
|
|
|
it.second.first, it.second.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler) {
|
|
|
|
return base::TryEmplace(handlers_, scheme, type, handler).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
|
|
|
|
return handlers_.erase(scheme) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
|
|
|
|
return base::Contains(handlers_, scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler) {
|
|
|
|
return base::TryEmplace(intercept_handlers_, scheme, type, handler).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
|
|
|
|
return intercept_handlers_.erase(scheme) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
|
|
|
|
return base::Contains(intercept_handlers_, scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|