performance_manager_impl.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_IMPL_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/callback_helpers.h"
  12. #include "base/location.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/task/sequenced_task_runner.h"
  15. #include "base/task/task_runner_util.h"
  16. #include "components/performance_manager/graph/graph_impl.h"
  17. #include "components/performance_manager/public/graph/frame_node.h"
  18. #include "components/performance_manager/public/graph/page_node.h"
  19. #include "components/performance_manager/public/graph/worker_node.h"
  20. #include "components/performance_manager/public/performance_manager.h"
  21. #include "components/performance_manager/public/render_process_host_proxy.h"
  22. #include "components/performance_manager/public/web_contents_proxy.h"
  23. #include "content/public/browser/browsing_instance_id.h"
  24. #include "content/public/browser/site_instance.h"
  25. #include "content/public/common/process_type.h"
  26. #include "third_party/blink/public/common/tokens/tokens.h"
  27. class GURL;
  28. namespace performance_manager {
  29. class PageNodeImpl;
  30. // The performance manager is a rendezvous point for binding to performance
  31. // manager interfaces.
  32. class PerformanceManagerImpl : public PerformanceManager {
  33. public:
  34. using FrameNodeCreationCallback = base::OnceCallback<void(FrameNodeImpl*)>;
  35. PerformanceManagerImpl(const PerformanceManagerImpl&) = delete;
  36. PerformanceManagerImpl& operator=(const PerformanceManagerImpl&) = delete;
  37. ~PerformanceManagerImpl() override;
  38. // Posts a callback that will run on the PM sequence. Valid to call from any
  39. // sequence.
  40. //
  41. // Note: If called from the main thread, the |graph_callback| is guaranteed to
  42. // run if and only if "IsAvailable()" returns true.
  43. //
  44. // If called from any other sequence, there is no guarantee that the
  45. // callback will run. It will depend on if the PerformanceManager was
  46. // destroyed before the the task is scheduled.
  47. static void CallOnGraphImpl(const base::Location& from_here,
  48. base::OnceClosure callback);
  49. // Same as the above, but the callback is provided a pointer to the graph.
  50. using GraphImplCallback = base::OnceCallback<void(GraphImpl*)>;
  51. static void CallOnGraphImpl(const base::Location& from_here,
  52. GraphImplCallback graph_callback);
  53. // Posts a callback that will run on the PM sequence, and be provided a
  54. // pointer to the Graph. The return value is returned as an argument to the
  55. // reply callback. As opposed to CallOnGraphImpl(), this is valid to call from
  56. // the main thread only, and only if "IsAvailable" returns true.
  57. template <typename TaskReturnType>
  58. static void CallOnGraphAndReplyWithResult(
  59. const base::Location& from_here,
  60. base::OnceCallback<TaskReturnType(GraphImpl*)> task,
  61. base::OnceCallback<void(TaskReturnType)> reply);
  62. // Creates, initializes and registers an instance. Invokes |on_start| on the
  63. // PM sequence. Valid to call from the main thread only.
  64. static std::unique_ptr<PerformanceManagerImpl> Create(
  65. GraphImplCallback on_start);
  66. // Unregisters |instance| and arranges for its deletion on its sequence. Valid
  67. // to call from the main thread only.
  68. static void Destroy(std::unique_ptr<PerformanceManager> instance);
  69. // Creates a new node of the requested type and adds it to the graph.
  70. // May be called from any sequence. If a |creation_callback| is provided, it
  71. // will be run on the performance manager sequence immediately after adding
  72. // the node to the graph. This callback will not be executed if the node could
  73. // not be added to the graph.
  74. //
  75. // Note: If called from the main thread, the node is guaranteed to be added to
  76. // the graph if and only if "IsAvailable()" returns true.
  77. //
  78. // If called from any other sequence, there is no guarantee that the
  79. // node will be added to the graph. It will depend on if the
  80. // PerformanceManager was destroyed before the the task is scheduled.
  81. static std::unique_ptr<FrameNodeImpl> CreateFrameNode(
  82. ProcessNodeImpl* process_node,
  83. PageNodeImpl* page_node,
  84. FrameNodeImpl* parent_frame_node,
  85. int render_frame_id,
  86. const blink::LocalFrameToken& frame_token,
  87. content::BrowsingInstanceId browsing_instance_id,
  88. content::SiteInstanceId site_instance_id,
  89. FrameNodeCreationCallback creation_callback =
  90. FrameNodeCreationCallback());
  91. static std::unique_ptr<PageNodeImpl> CreatePageNode(
  92. const WebContentsProxy& contents_proxy,
  93. const std::string& browser_context_id,
  94. const GURL& visible_url,
  95. bool is_visible,
  96. bool is_audible,
  97. base::TimeTicks visibility_change_time,
  98. PageNode::PageState page_state);
  99. static std::unique_ptr<ProcessNodeImpl> CreateProcessNode(
  100. content::ProcessType process_type,
  101. RenderProcessHostProxy proxy);
  102. static std::unique_ptr<WorkerNodeImpl> CreateWorkerNode(
  103. const std::string& browser_context_id,
  104. WorkerNode::WorkerType worker_type,
  105. ProcessNodeImpl* process_node,
  106. const blink::WorkerToken& worker_token);
  107. // Destroys a node returned from the creation functions above. May be called
  108. // from any sequence.
  109. static void DeleteNode(std::unique_ptr<NodeBase> node);
  110. // Destroys multiples nodes in one single task. Equivalent to calling
  111. // DeleteNode() on all elements of the vector. This function takes care of
  112. // removing them from the graph in topological order and destroying them.
  113. // May be called from any sequence.
  114. static void BatchDeleteNodes(std::vector<std::unique_ptr<NodeBase>> nodes);
  115. // Indicates whether or not the caller is currently running on the PM task
  116. // runner.
  117. static bool OnPMTaskRunnerForTesting();
  118. // Allows testing code to know when tear down is complete. This can only be
  119. // called from the main thread, and the callback will also be invoked on the
  120. // main thread.
  121. static void SetOnDestroyedCallbackForTesting(base::OnceClosure callback);
  122. private:
  123. friend class PerformanceManager;
  124. PerformanceManagerImpl();
  125. // Returns the performance manager TaskRunner.
  126. static scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
  127. // Retrieves the currently registered instance. Can only be called from the PM
  128. // sequence.
  129. // Note: Only exists so that RunCallbackWithGraphAndReplyWithResult can be
  130. // implemented in the header file.
  131. static PerformanceManagerImpl* GetInstance();
  132. template <typename NodeType, typename... Args>
  133. static std::unique_ptr<NodeType> CreateNodeImpl(
  134. base::OnceCallback<void(NodeType*)> creation_callback,
  135. Args&&... constructor_args);
  136. // Helper functions that removes a node/vector of nodes from the graph on the
  137. // PM sequence and deletes them.
  138. //
  139. // Note that this function has similar semantics to
  140. // SequencedTaskRunner::DeleteSoon(). The node/vector of nodes is passed via a
  141. // regular pointer so that they are not deleted if the task is not executed.
  142. static void DeleteNodeImpl(NodeBase* node_ptr, GraphImpl* graph);
  143. static void BatchDeleteNodesImpl(
  144. std::vector<std::unique_ptr<NodeBase>>* nodes_ptr,
  145. GraphImpl* graph);
  146. void OnStartImpl(GraphImplCallback graph_callback);
  147. static void RunCallbackWithGraphImpl(GraphImplCallback graph_callback);
  148. static void RunCallbackWithGraph(GraphCallback graph_callback);
  149. template <typename TaskReturnType>
  150. static TaskReturnType RunCallbackWithGraphAndReplyWithResult(
  151. base::OnceCallback<TaskReturnType(GraphImpl*)> task);
  152. static void SetOnDestroyedCallbackImpl(base::OnceClosure callback);
  153. GraphImpl graph_ GUARDED_BY_CONTEXT(sequence_checker_);
  154. base::OnceClosure on_destroyed_callback_
  155. GUARDED_BY_CONTEXT(sequence_checker_);
  156. // If the PM is running on the UI sequence, this is its task runner.
  157. // Otherwise it uses a thread pool task runner defined in the .cc file.
  158. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  159. SEQUENCE_CHECKER(sequence_checker_);
  160. };
  161. // static
  162. template <typename TaskReturnType>
  163. void PerformanceManagerImpl::CallOnGraphAndReplyWithResult(
  164. const base::Location& from_here,
  165. base::OnceCallback<TaskReturnType(GraphImpl*)> task,
  166. base::OnceCallback<void(TaskReturnType)> reply) {
  167. base::PostTaskAndReplyWithResult(
  168. GetTaskRunner().get(), from_here,
  169. base::BindOnce(
  170. &PerformanceManagerImpl::RunCallbackWithGraphAndReplyWithResult<
  171. TaskReturnType>,
  172. std::move(task)),
  173. std::move(reply));
  174. }
  175. // static
  176. template <typename TaskReturnType>
  177. TaskReturnType PerformanceManagerImpl::RunCallbackWithGraphAndReplyWithResult(
  178. base::OnceCallback<TaskReturnType(GraphImpl*)> task) {
  179. return std::move(task).Run(&GetInstance()->graph_);
  180. }
  181. } // namespace performance_manager
  182. #endif // COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_IMPL_H_