protocol_registry.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. bool ProtocolRegistry::RegisterProtocol(ProtocolType type,
  43. const std::string& scheme,
  44. const ProtocolHandler& handler) {
  45. return handlers_.try_emplace(scheme, type, handler).second;
  46. }
  47. bool ProtocolRegistry::UnregisterProtocol(const std::string& scheme) {
  48. return handlers_.erase(scheme) != 0;
  49. }
  50. bool ProtocolRegistry::IsProtocolRegistered(const std::string& scheme) {
  51. return base::Contains(handlers_, scheme);
  52. }
  53. bool ProtocolRegistry::InterceptProtocol(ProtocolType type,
  54. const std::string& scheme,
  55. const ProtocolHandler& handler) {
  56. return intercept_handlers_.try_emplace(scheme, type, handler).second;
  57. }
  58. bool ProtocolRegistry::UninterceptProtocol(const std::string& scheme) {
  59. return intercept_handlers_.erase(scheme) != 0;
  60. }
  61. bool ProtocolRegistry::IsProtocolIntercepted(const std::string& scheme) {
  62. return base::Contains(intercept_handlers_, scheme);
  63. }
  64. } // namespace electron