protocol_registry.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "shell/browser/electron_browser_context.h"
  7. #include "shell/browser/net/asar/asar_url_loader.h"
  8. #include "shell/browser/protocol_registry.h"
  9. namespace electron {
  10. namespace {
  11. // Provide support for accessing asar archives in file:// protocol.
  12. class AsarURLLoaderFactory : public network::mojom::URLLoaderFactory {
  13. public:
  14. AsarURLLoaderFactory() {}
  15. private:
  16. // network::mojom::URLLoaderFactory:
  17. void CreateLoaderAndStart(
  18. mojo::PendingReceiver<network::mojom::URLLoader> loader,
  19. int32_t routing_id,
  20. int32_t request_id,
  21. uint32_t options,
  22. const network::ResourceRequest& request,
  23. mojo::PendingRemote<network::mojom::URLLoaderClient> client,
  24. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
  25. override {
  26. asar::CreateAsarURLLoader(request, std::move(loader), std::move(client),
  27. new net::HttpResponseHeaders(""));
  28. }
  29. void Clone(
  30. mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader) override {
  31. receivers_.Add(this, std::move(loader));
  32. }
  33. mojo::ReceiverSet<network::mojom::URLLoaderFactory> receivers_;
  34. };
  35. } // namespace
  36. // static
  37. ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
  38. content::BrowserContext* context) {
  39. return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
  40. }
  41. ProtocolRegistry::ProtocolRegistry() {}
  42. ProtocolRegistry::~ProtocolRegistry() = default;
  43. void ProtocolRegistry::RegisterURLLoaderFactories(
  44. URLLoaderFactoryType type,
  45. content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories) {
  46. // Override the default FileURLLoaderFactory to support asar archives.
  47. if (type == URLLoaderFactoryType::kNavigation) {
  48. // Always allow navigating to file:// URLs.
  49. //
  50. // Note that Chromium calls |emplace| to create the default file factory
  51. // after this call, so it won't override our asar factory.
  52. DCHECK(!base::Contains(*factories, url::kFileScheme));
  53. factories->emplace(url::kFileScheme,
  54. std::make_unique<AsarURLLoaderFactory>());
  55. } else if (type == URLLoaderFactoryType::kDocumentSubResource) {
  56. // Only support requesting file:// subresource URLs when Chromium does so,
  57. // it is usually supported under file:// or about:blank documents.
  58. auto file_factory = factories->find(url::kFileScheme);
  59. if (file_factory != factories->end())
  60. file_factory->second = std::make_unique<AsarURLLoaderFactory>();
  61. }
  62. for (const auto& it : handlers_) {
  63. factories->emplace(it.first, std::make_unique<ElectronURLLoaderFactory>(
  64. it.second.first, it.second.second));
  65. }
  66. }
  67. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  68. const std::string& scheme,
  69. const ProtocolHandler& handler) {
  70. return base::TryEmplace(handlers_, scheme, type, handler).second;
  71. }
  72. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  73. return handlers_.erase(scheme) != 0;
  74. }
  75. bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
  76. return base::Contains(handlers_, scheme);
  77. }
  78. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  79. const std::string& scheme,
  80. const ProtocolHandler& handler) {
  81. return base::TryEmplace(intercept_handlers_, scheme, type, handler).second;
  82. }
  83. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  84. return intercept_handlers_.erase(scheme) != 0;
  85. }
  86. bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
  87. return base::Contains(intercept_handlers_, scheme);
  88. }
  89. } // namespace electron