protocol_registry.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <memory>
  5. #include <utility>
  6. #include "content/public/browser/non_network_url_loader_factory_base.h" // nogncheck
  7. #include "content/public/browser/web_contents.h"
  8. #include "shell/browser/electron_browser_context.h"
  9. #include "shell/browser/net/asar/asar_url_loader.h"
  10. #include "shell/browser/protocol_registry.h"
  11. namespace electron {
  12. namespace {
  13. // Provide support for accessing asar archives in file:// protocol.
  14. class AsarURLLoaderFactory : public content::NonNetworkURLLoaderFactoryBase {
  15. public:
  16. static mojo::PendingRemote<network::mojom::URLLoaderFactory> Create() {
  17. mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote;
  18. // The AsarURLLoaderFactory will delete itself when there are no more
  19. // receivers - see the NonNetworkURLLoaderFactoryBase::OnDisconnect method.
  20. new AsarURLLoaderFactory(pending_remote.InitWithNewPipeAndPassReceiver());
  21. return pending_remote;
  22. }
  23. private:
  24. AsarURLLoaderFactory(
  25. mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver)
  26. : content::NonNetworkURLLoaderFactoryBase(std::move(factory_receiver)) {}
  27. ~AsarURLLoaderFactory() override = default;
  28. // network::mojom::URLLoaderFactory:
  29. void CreateLoaderAndStart(
  30. mojo::PendingReceiver<network::mojom::URLLoader> loader,
  31. int32_t routing_id,
  32. int32_t request_id,
  33. uint32_t options,
  34. const network::ResourceRequest& request,
  35. mojo::PendingRemote<network::mojom::URLLoaderClient> client,
  36. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
  37. override {
  38. asar::CreateAsarURLLoader(request, std::move(loader), std::move(client),
  39. new net::HttpResponseHeaders(""));
  40. }
  41. };
  42. } // namespace
  43. // static
  44. ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
  45. content::BrowserContext* context) {
  46. return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
  47. }
  48. ProtocolRegistry::ProtocolRegistry() {}
  49. ProtocolRegistry::~ProtocolRegistry() = default;
  50. void ProtocolRegistry::RegisterURLLoaderFactories(
  51. content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories,
  52. bool allow_file_access) {
  53. auto file_factory = factories->find(url::kFileScheme);
  54. if (file_factory != factories->end()) {
  55. // If Chromium already allows file access then replace the url factory to
  56. // also loading asar files.
  57. file_factory->second = AsarURLLoaderFactory::Create();
  58. } else if (allow_file_access) {
  59. // Otherwise only allow file access when it is explicitly allowed.
  60. //
  61. // Note that Chromium may call |emplace| to create the default file factory
  62. // after this call, it won't override our asar factory, but if asar support
  63. // breaks in future, please check if Chromium has changed the call.
  64. factories->emplace(url::kFileScheme, AsarURLLoaderFactory::Create());
  65. }
  66. for (const auto& it : handlers_) {
  67. factories->emplace(it.first, ElectronURLLoaderFactory::Create(
  68. it.second.first, it.second.second));
  69. }
  70. }
  71. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  72. const std::string& scheme,
  73. const ProtocolHandler& handler) {
  74. return base::TryEmplace(handlers_, scheme, type, handler).second;
  75. }
  76. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  77. return handlers_.erase(scheme) != 0;
  78. }
  79. bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
  80. return base::Contains(handlers_, scheme);
  81. }
  82. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  83. const std::string& scheme,
  84. const ProtocolHandler& handler) {
  85. return base::TryEmplace(intercept_handlers_, scheme, type, handler).second;
  86. }
  87. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  88. return intercept_handlers_.erase(scheme) != 0;
  89. }
  90. bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
  91. return base::Contains(intercept_handlers_, scheme);
  92. }
  93. } // namespace electron