protocol_registry.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2020 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/protocol_registry.h"
  5. #include "electron/fuses.h"
  6. #include "shell/browser/electron_browser_context.h"
  7. #include "shell/browser/net/asar/asar_url_loader_factory.h"
  8. namespace electron {
  9. // static
  10. ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
  11. content::BrowserContext* context) {
  12. return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
  13. }
  14. ProtocolRegistry::ProtocolRegistry() = default;
  15. ProtocolRegistry::~ProtocolRegistry() = default;
  16. void ProtocolRegistry::RegisterURLLoaderFactories(
  17. content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories,
  18. bool allow_file_access) {
  19. if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
  20. auto file_factory = factories->find(url::kFileScheme);
  21. if (file_factory != factories->end()) {
  22. // If Chromium already allows file access then replace the url factory to
  23. // also loading asar files.
  24. file_factory->second = AsarURLLoaderFactory::Create();
  25. } else if (allow_file_access) {
  26. // Otherwise only allow file access when it is explicitly allowed.
  27. //
  28. // Note that Chromium may call |emplace| to create the default file
  29. // factory after this call, it won't override our asar factory, but if
  30. // asar support breaks in future, please check if Chromium has changed the
  31. // call.
  32. factories->emplace(url::kFileScheme, AsarURLLoaderFactory::Create());
  33. }
  34. }
  35. for (const auto& it : handlers_) {
  36. factories->emplace(it.first, ElectronURLLoaderFactory::Create(
  37. it.second.first, it.second.second));
  38. }
  39. }
  40. mojo::PendingRemote<network::mojom::URLLoaderFactory>
  41. ProtocolRegistry::CreateNonNetworkNavigationURLLoaderFactory(
  42. const std::string& scheme) {
  43. if (scheme == url::kFileScheme) {
  44. if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
  45. return AsarURLLoaderFactory::Create();
  46. }
  47. } else {
  48. auto handler = handlers_.find(scheme);
  49. if (handler != handlers_.end()) {
  50. return ElectronURLLoaderFactory::Create(handler->second.first,
  51. handler->second.second);
  52. }
  53. }
  54. return {};
  55. }
  56. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  57. const std::string& scheme,
  58. const ProtocolHandler& handler) {
  59. return handlers_.try_emplace(scheme, type, handler).second;
  60. }
  61. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  62. return handlers_.erase(scheme) != 0;
  63. }
  64. const HandlersMap::mapped_type* ProtocolRegistry::FindRegistered(
  65. const std::string_view scheme) const {
  66. const auto& map = handlers_;
  67. const auto iter = map.find(scheme);
  68. return iter != std::end(map) ? &iter->second : nullptr;
  69. }
  70. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  71. const std::string& scheme,
  72. const ProtocolHandler& handler) {
  73. return intercept_handlers_.try_emplace(scheme, type, handler).second;
  74. }
  75. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  76. return intercept_handlers_.erase(scheme) != 0;
  77. }
  78. const HandlersMap::mapped_type* ProtocolRegistry::FindIntercepted(
  79. const std::string_view scheme) const {
  80. const auto& map = intercept_handlers_;
  81. const auto iter = map.find(scheme);
  82. return iter != std::end(map) ? &iter->second : nullptr;
  83. }
  84. } // namespace electron