protocol_registry.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "shell/browser/electron_browser_context.h"
  8. #include "shell/browser/net/asar/asar_url_loader_factory.h"
  9. namespace electron {
  10. // static
  11. ProtocolRegistry* ProtocolRegistry::FromBrowserContext(
  12. content::BrowserContext* context) {
  13. return static_cast<ElectronBrowserContext*>(context)->protocol_registry();
  14. }
  15. ProtocolRegistry::ProtocolRegistry() = default;
  16. ProtocolRegistry::~ProtocolRegistry() = default;
  17. void ProtocolRegistry::RegisterURLLoaderFactories(
  18. content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories,
  19. bool allow_file_access) {
  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 factory
  29. // after this call, it won't override our asar factory, but if asar support
  30. // breaks in future, please check if Chromium has changed the call.
  31. factories->emplace(url::kFileScheme, AsarURLLoaderFactory::Create());
  32. }
  33. for (const auto& it : handlers_) {
  34. factories->emplace(it.first, ElectronURLLoaderFactory::Create(
  35. it.second.first, it.second.second));
  36. }
  37. }
  38. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  39. const std::string& scheme,
  40. const ProtocolHandler& handler) {
  41. return handlers_.try_emplace(scheme, type, handler).second;
  42. }
  43. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  44. return handlers_.erase(scheme) != 0;
  45. }
  46. bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
  47. return base::Contains(handlers_, scheme);
  48. }
  49. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  50. const std::string& scheme,
  51. const ProtocolHandler& handler) {
  52. return intercept_handlers_.try_emplace(scheme, type, handler).second;
  53. }
  54. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  55. return intercept_handlers_.erase(scheme) != 0;
  56. }
  57. bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
  58. return base::Contains(intercept_handlers_, scheme);
  59. }
  60. } // namespace electron