shell_extension_loader.h 3.2 KB

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