performance_manager.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_PUBLIC_PERFORMANCE_MANAGER_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_PERFORMANCE_MANAGER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "components/performance_manager/public/render_process_host_id.h"
  13. namespace content {
  14. class RenderFrameHost;
  15. class RenderProcessHost;
  16. class WebContents;
  17. }
  18. namespace performance_manager {
  19. class FrameNode;
  20. class Graph;
  21. class GraphOwned;
  22. class PageNode;
  23. class ProcessNode;
  24. class PerformanceManagerMainThreadMechanism;
  25. class PerformanceManagerMainThreadObserver;
  26. class PerformanceManagerOwned;
  27. class PerformanceManagerRegistered;
  28. template <typename DerivedType>
  29. class PerformanceManagerRegisteredImpl;
  30. // The performance manager is a rendezvous point for communicating with the
  31. // performance manager graph on its dedicated sequence.
  32. class PerformanceManager {
  33. public:
  34. virtual ~PerformanceManager();
  35. PerformanceManager(const PerformanceManager&) = delete;
  36. PerformanceManager& operator=(const PerformanceManager&) = delete;
  37. // Returns true if the performance manager is initialized. Valid to call from
  38. // the main thread only.
  39. static bool IsAvailable();
  40. // Posts a callback that will run on the PM sequence. Valid to call from any
  41. // sequence.
  42. //
  43. // Note: If called from the main thread, the |callback| is guaranteed to run
  44. // if and only if "IsAvailable()" returns true.
  45. //
  46. // If called from any other sequence, there is no guarantee that the
  47. // callback will run. It will depend on if the PerformanceManager was
  48. // destroyed before the the task is scheduled.
  49. static void CallOnGraph(const base::Location& from_here,
  50. base::OnceClosure callback);
  51. // Same as the above, but the callback is provided a pointer to the graph.
  52. using GraphCallback = base::OnceCallback<void(Graph*)>;
  53. static void CallOnGraph(const base::Location& from_here,
  54. GraphCallback callback);
  55. // Passes a GraphOwned object into the Graph on the PM sequence. Must only be
  56. // called if "IsAvailable()" returns true. Valid to call from the main thread
  57. // only.
  58. static void PassToGraph(const base::Location& from_here,
  59. std::unique_ptr<GraphOwned> graph_owned);
  60. // Returns a WeakPtr to the *primary* PageNode associated with a given
  61. // WebContents, or a null WeakPtr if there's no PageNode for this WebContents.
  62. // Valid to call from the main thread only, the returned WeakPtr should only
  63. // be dereferenced on the PM sequence (e.g. it can be used in a
  64. // CallOnGraph callback).
  65. // NOTE: Consider using GetPageNodeForRenderFrameHost if you are in the
  66. // context of a specific RenderFrameHost.
  67. static base::WeakPtr<PageNode> GetPrimaryPageNodeForWebContents(
  68. content::WebContents* wc);
  69. // Returns a WeakPtr to the PageNode associated with a given RenderFrameHost,
  70. // or nullptr if no such page node exists. Valid to call from the main thread
  71. // only, the returned WeakPtr should only be dereferenced on the PM sequence
  72. // (e.g. it can be used in a CallOnGraph callback). This is equivalent to
  73. // calling `GetFrameNodeForRenderFrameHost()` and subsequently calling
  74. // `FrameNode::GetPageNode()`.
  75. static base::WeakPtr<PageNode> GetPageNodeForRenderFrameHost(
  76. content::RenderFrameHost* rfh);
  77. // Returns a WeakPtr to the FrameNode associated with a given
  78. // RenderFrameHost, or a null WeakPtr if there's no FrameNode for this RFH.
  79. // (There is a brief window after the RFH is created before the FrameNode is
  80. // added.) Valid to call from the main thread only, the returned WeakPtr
  81. // should only be dereferenced on the PM sequence (e.g. it can be used in a
  82. // CallOnGraph callback).
  83. static base::WeakPtr<FrameNode> GetFrameNodeForRenderFrameHost(
  84. content::RenderFrameHost* rfh);
  85. // Returns a WeakPtr to the ProcessNode associated with a given
  86. // RenderProcessHost, or a null WeakPtr if there's no ProcessNode for this
  87. // RPH. (There is a brief window after the RPH is created before the
  88. // ProcessNode is added.) Valid to call from the main thread only, the
  89. // returned WeakPtr should only be dereferenced on the PM sequence (e.g. it
  90. // can be used in a CallOnGraph callback).
  91. static base::WeakPtr<ProcessNode> GetProcessNodeForRenderProcessHost(
  92. content::RenderProcessHost* rph);
  93. // Returns a WeakPtr to the ProcessNode associated with a given
  94. // RenderProcessHostId (which must be valid), or a null WeakPtr if there's no
  95. // ProcessNode for this ID. (There may be no RenderProcessHost for this ID,
  96. // or it may be during a brief window after the RPH is created but before the
  97. // ProcessNode is added.) Valid to call from the main thread only, the
  98. // returned WeakPtr should only be dereferenced on the PM sequence (e.g. it
  99. // can be used in a CallOnGraph callback).
  100. static base::WeakPtr<ProcessNode> GetProcessNodeForRenderProcessHostId(
  101. RenderProcessHostId id);
  102. // Adds / removes an observer that is notified of PerformanceManager events
  103. // that happen on the main thread. Can only be called on the main thread.
  104. static void AddObserver(PerformanceManagerMainThreadObserver* observer);
  105. static void RemoveObserver(PerformanceManagerMainThreadObserver* observer);
  106. // Adds / removes a mechanism that need to be called synchronously on the main
  107. // thread (ie, to apply NavigationThrottles).
  108. static void AddMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  109. static void RemoveMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  110. static bool HasMechanism(PerformanceManagerMainThreadMechanism* mechanism);
  111. // For convenience, allows you to pass ownership of an object that lives on
  112. // the main thread to the performance manager. Useful for attaching observers
  113. // or mechanisms that will live with the PM until it dies. If you can name the
  114. // object you can also take it back via "TakeFromPM". The objects will be
  115. // torn down gracefully (and their "OnTakenFromPM" functions invoked) as the
  116. // PM itself is torn down.
  117. static void PassToPM(std::unique_ptr<PerformanceManagerOwned> pm_owned);
  118. static std::unique_ptr<PerformanceManagerOwned> TakeFromPM(
  119. PerformanceManagerOwned* pm_owned);
  120. // A TakeFromPM helper for taking back the ownership of a
  121. // PerformanceManagerOwned object via its DerivedType.
  122. template <typename DerivedType>
  123. static std::unique_ptr<DerivedType> TakeFromPMAs(DerivedType* pm_owned) {
  124. return base::WrapUnique(
  125. static_cast<DerivedType*>(TakeFromPM(pm_owned).release()));
  126. }
  127. // Registers an object with the PM. It is expected that no more than one
  128. // object of a given type is registered at a given moment, and that all
  129. // registered objects are unregistered before PM tear-down. This allows the
  130. // PM to act as a rendez-vous point for objects that live on the main thread.
  131. // Combined with PerformanceManagerOwned this offers an alternative to
  132. // using singletons, and brings clear ownerships and lifetime semantics.
  133. static void RegisterObject(PerformanceManagerRegistered* pm_object);
  134. // Unregisters the provided |object|, which must previously have been
  135. // registered with "RegisterObject". It is expected that all registered
  136. // objects are unregistered before graph tear-down.
  137. static void UnregisterObject(PerformanceManagerRegistered* object);
  138. // Returns the registered object of the given type, nullptr if none has been
  139. // registered.
  140. template <typename DerivedType>
  141. static DerivedType* GetRegisteredObjectAs() {
  142. // Be sure to access the TypeId provided by PerformanceManagerRegisteredImpl
  143. // in case this class has other TypeId implementations.
  144. PerformanceManagerRegistered* object = GetRegisteredObject(
  145. PerformanceManagerRegisteredImpl<DerivedType>::TypeId());
  146. return static_cast<DerivedType*>(object);
  147. }
  148. // Returns the performance manager graph task runner. This is safe to call
  149. // from any thread at any time between the creation of the thread pool and its
  150. // destruction.
  151. //
  152. // NOTE: Tasks posted to this sequence from any thread but the UI thread, or
  153. // on the UI thread after IsAvailable() returns false, cannot safely access
  154. // the graph, graphowned objects or other performance manager related objects.
  155. // In practice it's preferable to use CallOnGraph() whenever possible.
  156. static scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
  157. protected:
  158. PerformanceManager();
  159. // Retrieves the object with the given |type_id|, returning nullptr if none
  160. // exists. Clients must use the GetRegisteredObjectAs wrapper instead.
  161. static PerformanceManagerRegistered* GetRegisteredObject(uintptr_t type_id);
  162. };
  163. } // namespace performance_manager
  164. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_PERFORMANCE_MANAGER_H_