electron_extension_loader.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_EXTENSIONS_ELECTRON_EXTENSION_LOADER_H_
  5. #define ELECTRON_SHELL_BROWSER_EXTENSIONS_ELECTRON_EXTENSION_LOADER_H_
  6. #include <string>
  7. #include <utility>
  8. #include "base/functional/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "extensions/browser/extension_registrar.h"
  12. #include "extensions/common/extension_id.h"
  13. namespace base {
  14. class FilePath;
  15. }
  16. namespace content {
  17. class BrowserContext;
  18. }
  19. namespace extensions {
  20. class Extension;
  21. // Handles extension loading and reloading using ExtensionRegistrar.
  22. class ElectronExtensionLoader : public ExtensionRegistrar::Delegate {
  23. public:
  24. explicit ElectronExtensionLoader(content::BrowserContext* browser_context);
  25. ~ElectronExtensionLoader() override;
  26. // disable copy
  27. ElectronExtensionLoader(const ElectronExtensionLoader&) = delete;
  28. ElectronExtensionLoader& operator=(const ElectronExtensionLoader&) = delete;
  29. // Loads an unpacked extension from a directory synchronously. Returns the
  30. // extension on success, or nullptr otherwise.
  31. void LoadExtension(const base::FilePath& extension_dir,
  32. int load_flags,
  33. base::OnceCallback<void(const Extension* extension,
  34. const std::string&)> cb);
  35. // Starts reloading the extension. A keep-alive is maintained until the
  36. // reload succeeds/fails. If the extension is an app, it will be launched upon
  37. // reloading.
  38. // This may invalidate references to the old Extension object, so it takes the
  39. // ID by value.
  40. void ReloadExtension(const ExtensionId& extension_id);
  41. void UnloadExtension(const ExtensionId& extension_id,
  42. extensions::UnloadedExtensionReason reason);
  43. ExtensionRegistrar* registrar() { return &extension_registrar_; }
  44. private:
  45. // If the extension loaded successfully, enables it. If it's an app, launches
  46. // it. If the load failed, updates ShellKeepAliveRequester.
  47. void FinishExtensionReload(
  48. const ExtensionId& old_extension_id,
  49. std::pair<scoped_refptr<const Extension>, std::string> result);
  50. void FinishExtensionLoad(
  51. base::OnceCallback<void(const Extension*, const std::string&)> cb,
  52. std::pair<scoped_refptr<const Extension>, std::string> result);
  53. // ExtensionRegistrar::Delegate:
  54. void PreAddExtension(const Extension* extension,
  55. const Extension* old_extension) override;
  56. void PostActivateExtension(scoped_refptr<const Extension> extension) override;
  57. void PostDeactivateExtension(
  58. scoped_refptr<const Extension> extension) override;
  59. void LoadExtensionForReload(
  60. const ExtensionId& extension_id,
  61. const base::FilePath& path,
  62. ExtensionRegistrar::LoadErrorBehavior load_error_behavior) override;
  63. bool CanEnableExtension(const Extension* extension) override;
  64. bool CanDisableExtension(const Extension* extension) override;
  65. bool ShouldBlockExtension(const Extension* extension) override;
  66. raw_ptr<content::BrowserContext> browser_context_; // Not owned.
  67. // Registers and unregisters extensions.
  68. ExtensionRegistrar extension_registrar_;
  69. // Holds keep-alives for relaunching apps.
  70. // ShellKeepAliveRequester keep_alive_requester_;
  71. // Indicates that we posted the (asynchronous) task to start reloading.
  72. // Used by ReloadExtension() to check whether ExtensionRegistrar calls
  73. // LoadExtensionForReload().
  74. bool did_schedule_reload_ = false;
  75. base::WeakPtrFactory<ElectronExtensionLoader> weak_factory_{this};
  76. };
  77. } // namespace extensions
  78. #endif // ELECTRON_SHELL_BROWSER_EXTENSIONS_ELECTRON_EXTENSION_LOADER_H_