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.
|
|
|
|
|
2021-04-12 04:59:36 +00:00
|
|
|
#include "shell/browser/protocol_registry.h"
|
|
|
|
|
2021-04-07 01:46:23 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2024-01-02 21:06:33 +00:00
|
|
|
#include "electron/fuses.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2021-04-12 04:59:36 +00:00
|
|
|
#include "shell/browser/net/asar/asar_url_loader_factory.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
// static
|
|
|
|
ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
|
|
|
|
content::BrowserContext* context) {
|
|
|
|
return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
|
|
|
|
}
|
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
ProtocolRegistry::ProtocolRegistry() = default;
|
2020-03-26 17:34:32 +00:00
|
|
|
|
|
|
|
ProtocolRegistry::~ProtocolRegistry() = default;
|
|
|
|
|
|
|
|
void ProtocolRegistry::RegisterURLLoaderFactories(
|
2021-04-07 01:46:23 +00:00
|
|
|
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories,
|
|
|
|
bool allow_file_access) {
|
2024-01-02 21:06:33 +00:00
|
|
|
if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
|
|
|
|
auto file_factory = factories->find(url::kFileScheme);
|
|
|
|
if (file_factory != factories->end()) {
|
|
|
|
// If Chromium already allows file access then replace the url factory to
|
|
|
|
// also loading asar files.
|
|
|
|
file_factory->second = AsarURLLoaderFactory::Create();
|
|
|
|
} else if (allow_file_access) {
|
|
|
|
// Otherwise only allow file access when it is explicitly allowed.
|
|
|
|
//
|
|
|
|
// Note that Chromium may call |emplace| to create the default file
|
|
|
|
// factory after this call, it won't override our asar factory, but if
|
|
|
|
// asar support breaks in future, please check if Chromium has changed the
|
|
|
|
// call.
|
|
|
|
factories->emplace(url::kFileScheme, 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 09:31:13 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory>
|
|
|
|
ProtocolRegistry::CreateNonNetworkNavigationURLLoaderFactory(
|
|
|
|
const std::string& scheme) {
|
|
|
|
if (scheme == url::kFileScheme) {
|
|
|
|
if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
|
|
|
|
return AsarURLLoaderFactory::Create();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto handler = handlers_.find(scheme);
|
|
|
|
if (handler != handlers_.end()) {
|
|
|
|
return ElectronURLLoaderFactory::Create(handler->second.first,
|
|
|
|
handler->second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:34:32 +00:00
|
|
|
bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler) {
|
2022-02-10 02:58:52 +00:00
|
|
|
return handlers_.try_emplace(scheme, type, handler).second;
|
2020-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
|
|
|
|
return handlers_.erase(scheme) != 0;
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:53:09 +00:00
|
|
|
const HandlersMap::mapped_type* ProtocolRegistry::FindRegistered(
|
|
|
|
const std::string& scheme) const {
|
|
|
|
const auto& map = handlers_;
|
|
|
|
const auto iter = map.find(scheme);
|
|
|
|
return iter != std::end(map) ? &iter->second : nullptr;
|
2020-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler) {
|
2022-02-10 02:58:52 +00:00
|
|
|
return intercept_handlers_.try_emplace(scheme, type, handler).second;
|
2020-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
|
|
|
|
return intercept_handlers_.erase(scheme) != 0;
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:53:09 +00:00
|
|
|
const HandlersMap::mapped_type* ProtocolRegistry::FindIntercepted(
|
|
|
|
const std::string& scheme) const {
|
|
|
|
const auto& map = intercept_handlers_;
|
|
|
|
const auto iter = map.find(scheme);
|
|
|
|
return iter != std::end(map) ? &iter->second : nullptr;
|
2020-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|