graph.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_GRAPH_GRAPH_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_
  6. #include <cstdint>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/dcheck_is_on.h"
  10. #include "base/memory/ptr_util.h"
  11. namespace ukm {
  12. class UkmRecorder;
  13. } // namespace ukm
  14. namespace performance_manager {
  15. class GraphObserver;
  16. class GraphOwned;
  17. class GraphRegistered;
  18. class FrameNode;
  19. class FrameNodeObserver;
  20. class NodeDataDescriberRegistry;
  21. class PageNode;
  22. class PageNodeObserver;
  23. class ProcessNode;
  24. class ProcessNodeObserver;
  25. class SystemNode;
  26. class SystemNodeObserver;
  27. class WorkerNode;
  28. class WorkerNodeObserver;
  29. template <typename DerivedType>
  30. class GraphRegisteredImpl;
  31. // Represents a graph of the nodes representing a single browser. Maintains a
  32. // set of nodes that can be retrieved in different ways, some indexed. Keeps
  33. // a list of observers that are notified of node addition and removal.
  34. class Graph {
  35. public:
  36. using Observer = GraphObserver;
  37. Graph();
  38. Graph(const Graph&) = delete;
  39. Graph& operator=(const Graph&) = delete;
  40. virtual ~Graph();
  41. // Adds an |observer| on the graph. It is safe for observers to stay
  42. // registered on the graph at the time of its death.
  43. virtual void AddGraphObserver(GraphObserver* observer) = 0;
  44. virtual void AddFrameNodeObserver(FrameNodeObserver* observer) = 0;
  45. virtual void AddPageNodeObserver(PageNodeObserver* observer) = 0;
  46. virtual void AddProcessNodeObserver(ProcessNodeObserver* observer) = 0;
  47. virtual void AddSystemNodeObserver(SystemNodeObserver* observer) = 0;
  48. virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
  49. // Removes an |observer| from the graph.
  50. virtual void RemoveGraphObserver(GraphObserver* observer) = 0;
  51. virtual void RemoveFrameNodeObserver(FrameNodeObserver* observer) = 0;
  52. virtual void RemovePageNodeObserver(PageNodeObserver* observer) = 0;
  53. virtual void RemoveProcessNodeObserver(ProcessNodeObserver* observer) = 0;
  54. virtual void RemoveSystemNodeObserver(SystemNodeObserver* observer) = 0;
  55. virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
  56. // For convenience, allows you to pass ownership of an object to the graph.
  57. // Useful for attaching observers that will live with the graph until it dies.
  58. // If you can name the object you can also take it back via "TakeFromGraph".
  59. virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0;
  60. virtual std::unique_ptr<GraphOwned> TakeFromGraph(
  61. GraphOwned* graph_owned) = 0;
  62. // Templated PassToGraph helper that also returns a pointer to the object,
  63. // which makes it easy to use PassToGraph in constructors.
  64. template <typename DerivedType>
  65. DerivedType* PassToGraph(std::unique_ptr<DerivedType> graph_owned) {
  66. DerivedType* object = graph_owned.get();
  67. PassToGraphImpl(std::move(graph_owned));
  68. return object;
  69. }
  70. // A TakeFromGraph helper for taking back the ownership of a GraphOwned
  71. // subclass.
  72. template <typename DerivedType>
  73. std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) {
  74. return base::WrapUnique(
  75. static_cast<DerivedType*>(TakeFromGraph(graph_owned).release()));
  76. }
  77. // Registers an object with this graph. It is expected that no more than one
  78. // object of a given type is registered at a given moment, and that all
  79. // registered objects are unregistered before graph tear-down.
  80. virtual void RegisterObject(GraphRegistered* object) = 0;
  81. // Unregisters the provided |object|, which must previously have been
  82. // registered with "RegisterObject". It is expected that all registered
  83. // objects are unregistered before graph tear-down.
  84. virtual void UnregisterObject(GraphRegistered* object) = 0;
  85. // Returns the registered object of the given type, nullptr if none has been
  86. // registered.
  87. template <typename DerivedType>
  88. DerivedType* GetRegisteredObjectAs() {
  89. // Be sure to access the TypeId provided by GraphRegisteredImpl, in case
  90. // this class has other TypeId implementations.
  91. GraphRegistered* object =
  92. GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId());
  93. return static_cast<DerivedType*>(object);
  94. }
  95. // Returns a collection of all known nodes of the given type.
  96. virtual const SystemNode* GetSystemNode() const = 0;
  97. virtual std::vector<const ProcessNode*> GetAllProcessNodes() const = 0;
  98. virtual std::vector<const FrameNode*> GetAllFrameNodes() const = 0;
  99. virtual std::vector<const PageNode*> GetAllPageNodes() const = 0;
  100. virtual std::vector<const WorkerNode*> GetAllWorkerNodes() const = 0;
  101. // Returns true if the graph only contains the default nodes.
  102. virtual bool HasOnlySystemNode() const = 0;
  103. // Returns the associated UKM recorder if it is defined.
  104. virtual ukm::UkmRecorder* GetUkmRecorder() const = 0;
  105. // Returns the data describer registry.
  106. virtual NodeDataDescriberRegistry* GetNodeDataDescriberRegistry() const = 0;
  107. // The following functions are implementation detail and should not need to be
  108. // used by external clients. They provide the ability to safely downcast to
  109. // the underlying implementation.
  110. virtual uintptr_t GetImplType() const = 0;
  111. virtual const void* GetImpl() const = 0;
  112. // Allows code that is not explicitly aware of the Graph sequence to determine
  113. // if they are in fact on the right sequence. Prefer to use the
  114. // DCHECK_ON_GRAPH_SEQUENCE macro.
  115. #if DCHECK_IS_ON()
  116. virtual bool IsOnGraphSequence() const = 0;
  117. #endif
  118. private:
  119. // Retrieves the object with the given |type_id|, returning nullptr if none
  120. // exists. Clients must use the GetRegisteredObjectAs wrapper instead.
  121. virtual GraphRegistered* GetRegisteredObject(uintptr_t type_id) = 0;
  122. };
  123. #if DCHECK_IS_ON()
  124. #define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(graph->IsOnGraphSequence())
  125. #else
  126. // Compiles to a nop, and will eat ostream input.
  127. #define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(true)
  128. #endif
  129. // Observer interface for the graph.
  130. class GraphObserver {
  131. public:
  132. GraphObserver();
  133. GraphObserver(const GraphObserver&) = delete;
  134. GraphObserver& operator=(const GraphObserver&) = delete;
  135. virtual ~GraphObserver();
  136. // Called before the |graph| associated with this observer disappears. This
  137. // allows the observer to do any necessary cleanup work. Note that the
  138. // observer should remove itself from observing the graph using this
  139. // callback.
  140. // TODO(chrisha): Make this run before the destructor!
  141. // crbug.com/966840
  142. virtual void OnBeforeGraphDestroyed(Graph* graph) = 0;
  143. };
  144. // Helper class for passing ownership of objects to a graph.
  145. class GraphOwned {
  146. public:
  147. GraphOwned();
  148. GraphOwned(const GraphOwned&) = delete;
  149. GraphOwned& operator=(const GraphOwned&) = delete;
  150. virtual ~GraphOwned();
  151. // Called when the object is passed into the graph.
  152. virtual void OnPassedToGraph(Graph* graph) = 0;
  153. // Called when the object is removed from the graph, either via an explicit
  154. // call to Graph::TakeFromGraph, or prior to the Graph being destroyed.
  155. virtual void OnTakenFromGraph(Graph* graph) = 0;
  156. };
  157. // A default implementation of GraphOwned.
  158. class GraphOwnedDefaultImpl : public GraphOwned {
  159. public:
  160. GraphOwnedDefaultImpl();
  161. GraphOwnedDefaultImpl(const GraphOwnedDefaultImpl&) = delete;
  162. GraphOwnedDefaultImpl& operator=(const GraphOwnedDefaultImpl&) = delete;
  163. ~GraphOwnedDefaultImpl() override;
  164. // GraphOwned implementation:
  165. void OnPassedToGraph(Graph* graph) override {}
  166. void OnTakenFromGraph(Graph* graph) override {}
  167. };
  168. } // namespace performance_manager
  169. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_