extension_registry_observer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2014 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_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_
  6. #include "extensions/browser/uninstall_reason.h"
  7. namespace content {
  8. class BrowserContext;
  9. }
  10. namespace extensions {
  11. class Extension;
  12. class ExtensionRegistry;
  13. enum class UnloadedExtensionReason;
  14. // Observer for ExtensionRegistry. Exists in a separate header file to reduce
  15. // the include file burden for typical clients of ExtensionRegistry.
  16. //
  17. // There are separate event categories for loading (the OnExtensionLoaded,
  18. // OnExtensionReady and OnExtensionUnloaded events) and installing (the
  19. // OnExtensionWillBeInstalled, OnExtensionInstalled and OnExtensionUninstalled)
  20. // extensions.
  21. //
  22. // For example, comparing OnExtensionLoaded and OnExtensionInstalled,
  23. // OnExtensionLoaded is called whenever an extension is added to the "enabled"
  24. // set of the extension registry. This includes:
  25. //
  26. // - Extensions being loaded at Chrome startup.
  27. // - Extensions being reloaded:
  28. // * as part of an update.
  29. // * from a crash.
  30. // * from a disabled state (if the user toggled disabled -> enabled).
  31. // * as part of internal bookkeeping (we reload extensions on file access
  32. // being granted, for instance).
  33. // * if the extension requested it (chrome.runtime.reload()).
  34. // * probably others.
  35. // - New extensions being loaded for the first time (as part of installation).
  36. //
  37. // OnExtensionInstalled is called when a *new* extension is added, *or* when an
  38. // extension is updated to a *new* version. It is not called for existing
  39. // extensions being loaded at startup, etc. In a common run of Chrome, you
  40. // probably won't get many "OnInstalled" events.
  41. //
  42. // As a general rule, most sites should observe OnExtensionLoaded, because they
  43. // want to see "what are the enabled extensions".
  44. class ExtensionRegistryObserver {
  45. public:
  46. virtual ~ExtensionRegistryObserver() {}
  47. // Called after an extension is loaded. The extension will exclusively exist
  48. // in the enabled_extensions set of ExtensionRegistry.
  49. virtual void OnExtensionLoaded(
  50. content::BrowserContext* browser_context,
  51. const Extension* extension) {}
  52. // Called after an extension is loaded and all necessary browser state is
  53. // initialized to support the start of the extension's child process.
  54. virtual void OnExtensionReady(content::BrowserContext* browser_context,
  55. const Extension* extension) {}
  56. // Called after an extension is unloaded. The extension no longer exists in
  57. // the set |ExtensionRegistry::enabled_extensions()|, but it can still be a
  58. // member of one of the other sets, like disabled, blocklisted or terminated.
  59. virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
  60. const Extension* extension,
  61. UnloadedExtensionReason reason) {}
  62. // Called when |extension| is about to be installed. |is_update| is true if
  63. // the installation is the result of it updating, in which case |old_name| is
  64. // the name of the extension's previous version.
  65. // The ExtensionRegistry will not be tracking |extension| at the time this
  66. // event is fired, but will be immediately afterwards (note: not necessarily
  67. // enabled; it might be installed in the disabled or even blocklisted sets,
  68. // for example).
  69. // Note that it's much more common to care about extensions being loaded
  70. // (OnExtensionLoaded).
  71. //
  72. // TODO(tmdiep): We should stash the state of the previous extension version
  73. // somewhere and have observers retrieve it. |is_update|, and |old_name| can
  74. // be removed when this is done.
  75. virtual void OnExtensionWillBeInstalled(
  76. content::BrowserContext* browser_context,
  77. const Extension* extension,
  78. bool is_update,
  79. const std::string& old_name) {}
  80. // Called when the installation of |extension| is complete. At this point the
  81. // extension is tracked in one of the ExtensionRegistry sets, but is not
  82. // necessarily enabled.
  83. virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
  84. const Extension* extension,
  85. bool is_update) {}
  86. // Called after an extension is uninstalled. The extension no longer exists in
  87. // any of the ExtensionRegistry sets (enabled, disabled, etc.).
  88. virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
  89. const Extension* extension,
  90. UninstallReason reason) {}
  91. // Called after the uninstallation of an extension is denied.
  92. virtual void OnExtensionUninstallationDenied(
  93. content::BrowserContext* browser_context,
  94. const Extension* extension) {}
  95. // Notifies observers that the observed object is going away.
  96. virtual void OnShutdown(ExtensionRegistry* registry) {}
  97. };
  98. } // namespace extensions
  99. #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_