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"
|
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) {
|
|
|
|
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.
|
2020-04-01 07:38:40 +00:00
|
|
|
//
|
2021-04-07 01:46:23 +00:00
|
|
|
// 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.
|
2020-09-21 08:00:36 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|