extension_host_registry.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2021 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_HOST_REGISTRY_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_HOST_REGISTRY_H_
  6. #include <unordered_set>
  7. #include <vector>
  8. #include "base/observer_list.h"
  9. #include "components/keyed_service/core/keyed_service.h"
  10. #include "extensions/common/extension_id.h"
  11. class BrowserContextKeyedServiceFactory;
  12. namespace content {
  13. class BrowserContext;
  14. class RenderFrameHost;
  15. }
  16. namespace extensions {
  17. class ExtensionHost;
  18. // A class responsible for tracking ExtensionHosts and notifying observers of
  19. // relevant changes.
  20. // See also ProcessManager, which is responsible for more of the construction
  21. // lifetime management of these hosts.
  22. class ExtensionHostRegistry : public KeyedService {
  23. public:
  24. class Observer : public base::CheckedObserver {
  25. public:
  26. // Called when the RenderProcessHost for an ExtensionHost is ready.
  27. // In practice, this corresponds to "shortly after" the first render frame
  28. // is created in the host.
  29. // The `browser_context` is the context associated with that host (which
  30. // might be an incognito version of
  31. // ExtensionHostRegistry::browser_context_).
  32. virtual void OnExtensionHostRenderProcessReady(
  33. content::BrowserContext* browser_context,
  34. ExtensionHost* host) {}
  35. // Called when an ExtensionHost is destroyed. The `browser_context` is
  36. // the context associated with that host (which might be an incognito
  37. // version of ExtensionHostRegistry::browser_context_).
  38. virtual void OnExtensionHostDestroyed(
  39. content::BrowserContext* browser_context,
  40. ExtensionHost* host) {}
  41. // Called when an ExtensionHost completes its first load. The
  42. // `browser_context` is the context associated with that host (which might
  43. // be an incognito version of ExtensionHostRegistry::browser_context_).
  44. // Note: If you only need to observe a single ExtensionHost (that's already
  45. // created), prefer overriding
  46. // ExtensionHostObserver::OnExtensionHostDidStopFirstLoad().
  47. virtual void OnExtensionHostCompletedFirstLoad(
  48. content::BrowserContext* browser_context,
  49. ExtensionHost* host) {}
  50. // Called when a document element is first available in an ExtensionHost.
  51. // `browser_context` is the context associated with that host (which might
  52. // be an incognito version of ExtensionHostRegistry::browser_context_).
  53. // TODO(devlin): Do we really need both first load completed and document
  54. // element available notifications? This matches previous implementations,
  55. // but I'm not sure the distinction is relevant.
  56. virtual void OnExtensionHostDocumentElementAvailable(
  57. content::BrowserContext* browser_context,
  58. ExtensionHost* extension_host) {}
  59. // Called when an ExtensionHost's render process is terminated. Note that
  60. // this may be called multiple times for a single process termination, since
  61. // there may be multiple ExtensionHosts in the same process.
  62. // `browser_context` is the context associated with that host (which might
  63. // be an incognito version of ExtensionHostRegistry::browser_context_).
  64. virtual void OnExtensionHostRenderProcessGone(
  65. content::BrowserContext* browser_context,
  66. ExtensionHost* extension_host) {}
  67. };
  68. ExtensionHostRegistry();
  69. ExtensionHostRegistry(const ExtensionHostRegistry&) = delete;
  70. ExtensionHostRegistry& operator=(const ExtensionHostRegistry&) = delete;
  71. ~ExtensionHostRegistry() override;
  72. // Retrieves the ExtensionHostRegistry for a given `browser_context`.
  73. // NOTE: ExtensionHostRegistry is shared between on- and off-the-record
  74. // contexts. See also the comment
  75. // ExtensionHostRegistryFactory::GetBrowserContextToUse().
  76. static ExtensionHostRegistry* Get(content::BrowserContext* browser_context);
  77. // Retrieves the factory instance for the ExtensionHostRegistry.
  78. static BrowserContextKeyedServiceFactory* GetFactory();
  79. // Called when a new ExtensionHost is created, and starts tracking the host
  80. // in `extension_hosts_`.
  81. void ExtensionHostCreated(ExtensionHost* extension_host);
  82. // Called when an ExtensionHost's corresponding renderer process is ready, and
  83. // and notifies observers.
  84. void ExtensionHostRenderProcessReady(ExtensionHost* extension_host);
  85. // Called when an ExtensionHost completes its first load.
  86. void ExtensionHostCompletedFirstLoad(ExtensionHost* extension_host);
  87. // Called when an ExtensionHost has created a document element for its first
  88. // time.
  89. void ExtensionHostDocumentElementAvailable(ExtensionHost* extension_host);
  90. // Called when an ExtensionHost's render process is terminated.
  91. void ExtensionHostRenderProcessGone(ExtensionHost* extension_host);
  92. // Called when an ExtensionHost is destroyed. Stops tracking the host and
  93. // notifies observers.
  94. void ExtensionHostDestroyed(ExtensionHost* extension_host);
  95. // Returns the collection of ExtensionHosts associated with the specified
  96. // `extension_id`.
  97. // If performance ever becomes a consideration here, we can update the
  98. // storage in the registry to be an unordered_map split apart by extension.
  99. std::vector<ExtensionHost*> GetHostsForExtension(
  100. const ExtensionId& extension_id);
  101. // Returns the ExtensionHost for the given `render_frame_host`, if one exists.
  102. // `render_frame_host` must be the primary main frame host; we do this to
  103. // avoid returning an ExtensionHost for a non-extension frame within an
  104. // extension document.
  105. ExtensionHost* GetExtensionHostForPrimaryMainFrame(
  106. content::RenderFrameHost* render_frame_host);
  107. void AddObserver(Observer* observer);
  108. void RemoveObserver(Observer* observer);
  109. private:
  110. // The active set of ExtensionHosts.
  111. std::unordered_set<ExtensionHost*> extension_hosts_;
  112. base::ObserverList<Observer> observers_;
  113. };
  114. } // namespace extensions
  115. #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_REGISTRY_H_