performance_manager_registry_impl.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2019 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 COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_
  6. #include <memory>
  7. #include "base/containers/flat_map.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/observer_list.h"
  10. #include "base/sequence_checker.h"
  11. #include "components/performance_manager/embedder/performance_manager_registry.h"
  12. #include "components/performance_manager/owned_objects.h"
  13. #include "components/performance_manager/performance_manager_tab_helper.h"
  14. #include "components/performance_manager/process_node_source.h"
  15. #include "components/performance_manager/public/performance_manager_owned.h"
  16. #include "components/performance_manager/public/performance_manager_registered.h"
  17. #include "components/performance_manager/registered_objects.h"
  18. #include "components/performance_manager/render_process_user_data.h"
  19. #include "components/performance_manager/tab_helper_frame_node_source.h"
  20. #include "content/public/browser/render_process_host_creation_observer.h"
  21. namespace content {
  22. class RenderProcessHost;
  23. class WebContents;
  24. } // namespace content
  25. namespace performance_manager {
  26. class PerformanceManagerMainThreadMechanism;
  27. class PerformanceManagerMainThreadObserver;
  28. class ServiceWorkerContextAdapter;
  29. class WorkerWatcher;
  30. class PerformanceManagerRegistryImpl
  31. : public content::RenderProcessHostCreationObserver,
  32. public PerformanceManagerRegistry,
  33. public PerformanceManagerTabHelper::DestructionObserver,
  34. public RenderProcessUserData::DestructionObserver {
  35. public:
  36. PerformanceManagerRegistryImpl();
  37. ~PerformanceManagerRegistryImpl() override;
  38. PerformanceManagerRegistryImpl(const PerformanceManagerRegistryImpl&) =
  39. delete;
  40. void operator=(const PerformanceManagerRegistryImpl&) = delete;
  41. // Returns the only instance of PerformanceManagerRegistryImpl living in this
  42. // process, or nullptr if there is none.
  43. static PerformanceManagerRegistryImpl* GetInstance();
  44. // Adds / removes an observer that is notified when a PageNode is created on
  45. // the main thread. Forwarded to from the public PerformanceManager interface.
  46. void AddObserver(PerformanceManagerMainThreadObserver* observer);
  47. void RemoveObserver(PerformanceManagerMainThreadObserver* observer);
  48. // Adds / removes main thread mechanisms. Forwarded to from the public
  49. // PerformanceManager interface.
  50. void AddMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  51. void RemoveMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  52. bool HasMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  53. // PM owned objects. Forwarded to from the public PerformanceManager
  54. // interface. See performance_manager.h for details.
  55. void PassToPM(std::unique_ptr<PerformanceManagerOwned> pm_owned);
  56. std::unique_ptr<PerformanceManagerOwned> TakeFromPM(
  57. PerformanceManagerOwned* pm_owned);
  58. // PM registered objects. Forwarded to from the public PerformanceManager
  59. // interface. See performance_manager.h for details.
  60. void RegisterObject(PerformanceManagerRegistered* pm_object);
  61. void UnregisterObject(PerformanceManagerRegistered* object);
  62. PerformanceManagerRegistered* GetRegisteredObject(uintptr_t type_id);
  63. // PerformanceManagerRegistry:
  64. void CreatePageNodeForWebContents(
  65. content::WebContents* web_contents) override;
  66. void SetPageType(content::WebContents* web_contents, PageType type) override;
  67. Throttles CreateThrottlesForNavigation(
  68. content::NavigationHandle* handle) override;
  69. void NotifyBrowserContextAdded(
  70. content::BrowserContext* browser_context) override;
  71. void NotifyBrowserContextRemoved(
  72. content::BrowserContext* browser_context) override;
  73. void CreateProcessNodeAndExposeInterfacesToRendererProcess(
  74. service_manager::BinderRegistry* registry,
  75. content::RenderProcessHost* render_process_host) override;
  76. void ExposeInterfacesToRenderFrame(
  77. mojo::BinderMapWithContext<content::RenderFrameHost*>* map) override;
  78. void TearDown() override;
  79. // PerformanceManagerTabHelper::DestructionObserver:
  80. void OnPerformanceManagerTabHelperDestroying(
  81. content::WebContents* web_contents) override;
  82. // RenderProcessUserData::DestructionObserver:
  83. void OnRenderProcessUserDataDestroying(
  84. content::RenderProcessHost* render_process_host) override;
  85. // This is exposed so that the tab helper can call it as well, as in some
  86. // testing configurations we otherwise miss RPH creation notifications that
  87. // usually arrive when interfaces are exposed to the renderer.
  88. void EnsureProcessNodeForRenderProcessHost(
  89. content::RenderProcessHost* render_process_host);
  90. size_t GetOwnedCountForTesting() const {
  91. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  92. return pm_owned_.size();
  93. }
  94. size_t GetRegisteredCountForTesting() const {
  95. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  96. return pm_registered_.size();
  97. }
  98. private:
  99. SEQUENCE_CHECKER(sequence_checker_);
  100. // content::RenderProcessHostCreationObserver:
  101. void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
  102. // Tracks WebContents and RenderProcessHost for which we have created user
  103. // data. Used to destroy all user data when the registry is destroyed.
  104. base::flat_set<content::WebContents*> web_contents_
  105. GUARDED_BY_CONTEXT(sequence_checker_);
  106. base::flat_set<content::RenderProcessHost*> render_process_hosts_
  107. GUARDED_BY_CONTEXT(sequence_checker_);
  108. // Maps each browser context to its ServiceWorkerContextAdapter.
  109. base::flat_map<content::BrowserContext*,
  110. std::unique_ptr<ServiceWorkerContextAdapter>>
  111. service_worker_context_adapters_ GUARDED_BY_CONTEXT(sequence_checker_);
  112. // Maps each browser context to its worker watcher.
  113. base::flat_map<content::BrowserContext*, std::unique_ptr<WorkerWatcher>>
  114. worker_watchers_ GUARDED_BY_CONTEXT(sequence_checker_);
  115. // Used by WorkerWatchers to access existing process nodes and frame
  116. // nodes.
  117. performance_manager::ProcessNodeSource process_node_source_
  118. GUARDED_BY_CONTEXT(sequence_checker_);
  119. performance_manager::TabHelperFrameNodeSource frame_node_source_
  120. GUARDED_BY_CONTEXT(sequence_checker_);
  121. base::ObserverList<PerformanceManagerMainThreadObserver> observers_
  122. GUARDED_BY_CONTEXT(sequence_checker_);
  123. base::ObserverList<PerformanceManagerMainThreadMechanism> mechanisms_
  124. GUARDED_BY_CONTEXT(sequence_checker_);
  125. // Objects owned by the PM.
  126. OwnedObjects<PerformanceManagerOwned,
  127. /* CallbackArgType = */ void,
  128. &PerformanceManagerOwned::OnPassedToPM,
  129. &PerformanceManagerOwned::OnTakenFromPM>
  130. pm_owned_ GUARDED_BY_CONTEXT(sequence_checker_);
  131. // Storage for PerformanceManagerRegistered objects.
  132. RegisteredObjects<PerformanceManagerRegistered> pm_registered_
  133. GUARDED_BY_CONTEXT(sequence_checker_);
  134. };
  135. } // namespace performance_manager
  136. #endif // COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_