protocol_registry.cc 3.4 KB

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