electron_extension_loader.h 3.6 KB

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