protocol_registry.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <memory>
  6. #include <utility>
  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_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() {}
  17. ProtocolRegistry::~ProtocolRegistry() = default;
  18. void ProtocolRegistry::RegisterURLLoaderFactories(
  19. content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories,
  20. bool allow_file_access) {
  21. auto file_factory = factories->find(url::kFileScheme);
  22. if (file_factory != factories->end()) {
  23. // If Chromium already allows file access then replace the url factory to
  24. // also loading asar files.
  25. file_factory->second = AsarURLLoaderFactory::Create();
  26. } else if (allow_file_access) {
  27. // Otherwise only allow file access when it is explicitly allowed.
  28. //
  29. // Note that Chromium may call |emplace| to create the default file factory
  30. // after this call, it won't override our asar factory, but if asar support
  31. // breaks in future, please check if Chromium has changed the call.
  32. factories->emplace(url::kFileScheme, AsarURLLoaderFactory::Create());
  33. }
  34. for (const auto& it : handlers_) {
  35. factories->emplace(it.first, ElectronURLLoaderFactory::Create(
  36. it.second.first, it.second.second));
  37. }
  38. }
  39. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  40. const std::string& scheme,
  41. const ProtocolHandler& handler) {
  42. return base::TryEmplace(handlers_, scheme, type, handler).second;
  43. }
  44. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  45. return handlers_.erase(scheme) != 0;
  46. }
  47. bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
  48. return base::Contains(handlers_, scheme);
  49. }
  50. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  51. const std::string& scheme,
  52. const ProtocolHandler& handler) {
  53. return base::TryEmplace(intercept_handlers_, scheme, type, handler).second;
  54. }
  55. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  56. return intercept_handlers_.erase(scheme) != 0;
  57. }
  58. bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
  59. return base::Contains(intercept_handlers_, scheme);
  60. }
  61. } // namespace electron