Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sigurdur Asgeirsson | 51d9d24 | 2019-10-07 20:38:34 | [diff] [blame] | 5 | #ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ |
| 6 | #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 7 | |
Chris Hamilton | ff2b2ced | 2019-05-30 13:29:05 | [diff] [blame] | 8 | #include <cstdint> |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 9 | #include <memory> |
Patrick Monette | 6a213b67 | 2024-06-07 03:05:24 | [diff] [blame] | 10 | #include <unordered_set> |
Chris Hamilton | ff2b2ced | 2019-05-30 13:29:05 | [diff] [blame] | 11 | |
Chris Hamilton | f3beb68 | 2020-07-07 21:23:22 | [diff] [blame] | 12 | #include "base/dcheck_is_on.h" |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
Joe Mason | 7db74f5 | 2024-06-17 13:54:31 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
Joe Mason | de0c60f | 2024-04-16 16:51:49 | [diff] [blame] | 15 | #include "base/observer_list_types.h" |
Joe Mason | 48ac2fa | 2025-02-09 05:03:36 | [diff] [blame] | 16 | #include "base/scoped_observation_traits.h" |
Joe Mason | bbc660a | 2024-06-18 15:21:06 | [diff] [blame] | 17 | #include "base/sequence_checker.h" |
Patrick Monette | 6a213b67 | 2024-06-07 03:05:24 | [diff] [blame] | 18 | #include "components/performance_manager/public/graph/node_set_view.h" |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 19 | |
Chris Hamilton | f48eead | 2019-07-04 18:07:40 | [diff] [blame] | 20 | namespace ukm { |
| 21 | class UkmRecorder; |
| 22 | } // namespace ukm |
| 23 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 24 | namespace performance_manager { |
| 25 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 26 | class GraphOwned; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 27 | class GraphRegistered; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 28 | class FrameNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 29 | class FrameNodeObserver; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 30 | class NodeDataDescriberRegistry; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 31 | class PageNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 32 | class PageNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 33 | class ProcessNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 34 | class ProcessNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 35 | class SystemNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 36 | class SystemNodeObserver; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 37 | class WorkerNode; |
| 38 | class WorkerNodeObserver; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 39 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 40 | template <typename DerivedType> |
| 41 | class GraphRegisteredImpl; |
Sigurdur Asgeirsson | fab8e9e | 2020-04-07 20:22:57 | [diff] [blame] | 42 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 43 | // Represents a graph of the nodes representing a single browser. Maintains a |
| 44 | // set of nodes that can be retrieved in different ways, some indexed. Keeps |
| 45 | // a list of observers that are notified of node addition and removal. |
| 46 | class Graph { |
| 47 | public: |
Patrick Monette | 6a213b67 | 2024-06-07 03:05:24 | [diff] [blame] | 48 | using NodeSet = std::unordered_set<const Node*>; |
| 49 | template <class NodeViewPtr> |
| 50 | using NodeSetView = NodeSetView<NodeSet, NodeViewPtr>; |
Joe Mason | 8da0b8f0 | 2023-08-04 16:48:32 | [diff] [blame] | 51 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 52 | Graph(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 53 | |
| 54 | Graph(const Graph&) = delete; |
| 55 | Graph& operator=(const Graph&) = delete; |
| 56 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 57 | virtual ~Graph(); |
| 58 | |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 59 | // Adds an |observer| on the graph. It is safe for observers to stay |
| 60 | // registered on the graph at the time of its death. |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 61 | virtual void AddFrameNodeObserver(FrameNodeObserver* observer) = 0; |
| 62 | virtual void AddPageNodeObserver(PageNodeObserver* observer) = 0; |
| 63 | virtual void AddProcessNodeObserver(ProcessNodeObserver* observer) = 0; |
| 64 | virtual void AddSystemNodeObserver(SystemNodeObserver* observer) = 0; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 65 | virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 66 | |
| 67 | // Removes an |observer| from the graph. |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 68 | virtual void RemoveFrameNodeObserver(FrameNodeObserver* observer) = 0; |
| 69 | virtual void RemovePageNodeObserver(PageNodeObserver* observer) = 0; |
| 70 | virtual void RemoveProcessNodeObserver(ProcessNodeObserver* observer) = 0; |
| 71 | virtual void RemoveSystemNodeObserver(SystemNodeObserver* observer) = 0; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 72 | virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 73 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 74 | // For convenience, allows you to pass ownership of an object to the graph. |
| 75 | // Useful for attaching observers that will live with the graph until it dies. |
| 76 | // If you can name the object you can also take it back via "TakeFromGraph". |
Chris Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 77 | virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 78 | virtual std::unique_ptr<GraphOwned> TakeFromGraph( |
| 79 | GraphOwned* graph_owned) = 0; |
| 80 | |
Chris Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 81 | // Templated PassToGraph helper that also returns a pointer to the object, |
| 82 | // which makes it easy to use PassToGraph in constructors. |
| 83 | template <typename DerivedType> |
| 84 | DerivedType* PassToGraph(std::unique_ptr<DerivedType> graph_owned) { |
| 85 | DerivedType* object = graph_owned.get(); |
| 86 | PassToGraphImpl(std::move(graph_owned)); |
| 87 | return object; |
| 88 | } |
| 89 | |
Sigurdur Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 90 | // A TakeFromGraph helper for taking back the ownership of a GraphOwned |
| 91 | // subclass. |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 92 | template <typename DerivedType> |
Sigurdur Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 93 | std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) { |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 94 | return base::WrapUnique( |
| 95 | static_cast<DerivedType*>(TakeFromGraph(graph_owned).release())); |
| 96 | } |
| 97 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 98 | // Registers an object with this graph. It is expected that no more than one |
| 99 | // object of a given type is registered at a given moment, and that all |
| 100 | // registered objects are unregistered before graph tear-down. |
| 101 | virtual void RegisterObject(GraphRegistered* object) = 0; |
| 102 | |
| 103 | // Unregisters the provided |object|, which must previously have been |
| 104 | // registered with "RegisterObject". It is expected that all registered |
| 105 | // objects are unregistered before graph tear-down. |
| 106 | virtual void UnregisterObject(GraphRegistered* object) = 0; |
| 107 | |
| 108 | // Returns the registered object of the given type, nullptr if none has been |
| 109 | // registered. |
| 110 | template <typename DerivedType> |
| 111 | DerivedType* GetRegisteredObjectAs() { |
Chris Hamilton | f7effeb | 2020-05-19 21:43:12 | [diff] [blame] | 112 | // Be sure to access the TypeId provided by GraphRegisteredImpl, in case |
| 113 | // this class has other TypeId implementations. |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 114 | GraphRegistered* object = |
| 115 | GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId()); |
| 116 | return static_cast<DerivedType*>(object); |
| 117 | } |
| 118 | |
Joe Mason | 8da0b8f0 | 2023-08-04 16:48:32 | [diff] [blame] | 119 | // Returns the single system node. |
Sebastien Marchand | b56b8aa9a | 2021-06-15 14:46:27 | [diff] [blame] | 120 | virtual const SystemNode* GetSystemNode() const = 0; |
Joe Mason | 8da0b8f0 | 2023-08-04 16:48:32 | [diff] [blame] | 121 | |
Patrick Monette | b981853d | 2024-06-07 03:38:34 | [diff] [blame] | 122 | // Returns a collection of all known nodes of the given type. |
Patrick Monette | 6a213b67 | 2024-06-07 03:05:24 | [diff] [blame] | 123 | virtual NodeSetView<const ProcessNode*> GetAllProcessNodes() const = 0; |
| 124 | virtual NodeSetView<const FrameNode*> GetAllFrameNodes() const = 0; |
| 125 | virtual NodeSetView<const PageNode*> GetAllPageNodes() const = 0; |
| 126 | virtual NodeSetView<const WorkerNode*> GetAllWorkerNodes() const = 0; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 127 | |
Sebastien Marchand | b56b8aa9a | 2021-06-15 14:46:27 | [diff] [blame] | 128 | // Returns true if the graph only contains the default nodes. |
| 129 | virtual bool HasOnlySystemNode() const = 0; |
Chris Hamilton | 417d037 | 2019-11-08 17:13:22 | [diff] [blame] | 130 | |
Chris Hamilton | f48eead | 2019-07-04 18:07:40 | [diff] [blame] | 131 | // Returns the associated UKM recorder if it is defined. |
| 132 | virtual ukm::UkmRecorder* GetUkmRecorder() const = 0; |
| 133 | |
Sigurdur Asgeirsson | fab8e9e | 2020-04-07 20:22:57 | [diff] [blame] | 134 | // Returns the data describer registry. |
| 135 | virtual NodeDataDescriberRegistry* GetNodeDataDescriberRegistry() const = 0; |
| 136 | |
Chris Hamilton | ff2b2ced | 2019-05-30 13:29:05 | [diff] [blame] | 137 | // The following functions are implementation detail and should not need to be |
| 138 | // used by external clients. They provide the ability to safely downcast to |
| 139 | // the underlying implementation. |
| 140 | virtual uintptr_t GetImplType() const = 0; |
| 141 | virtual const void* GetImpl() const = 0; |
| 142 | |
Chris Hamilton | f3beb68 | 2020-07-07 21:23:22 | [diff] [blame] | 143 | // Allows code that is not explicitly aware of the Graph sequence to determine |
| 144 | // if they are in fact on the right sequence. Prefer to use the |
| 145 | // DCHECK_ON_GRAPH_SEQUENCE macro. |
| 146 | #if DCHECK_IS_ON() |
| 147 | virtual bool IsOnGraphSequence() const = 0; |
| 148 | #endif |
| 149 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 150 | private: |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 151 | // Retrieves the object with the given |type_id|, returning nullptr if none |
| 152 | // exists. Clients must use the GetRegisteredObjectAs wrapper instead. |
| 153 | virtual GraphRegistered* GetRegisteredObject(uintptr_t type_id) = 0; |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 154 | }; |
| 155 | |
Chris Hamilton | f3beb68 | 2020-07-07 21:23:22 | [diff] [blame] | 156 | #if DCHECK_IS_ON() |
| 157 | #define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(graph->IsOnGraphSequence()) |
| 158 | #else |
| 159 | // Compiles to a nop, and will eat ostream input. |
| 160 | #define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(true) |
| 161 | #endif |
| 162 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 163 | // Helper class for passing ownership of objects to a graph. |
| 164 | class GraphOwned { |
| 165 | public: |
| 166 | GraphOwned(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 167 | |
| 168 | GraphOwned(const GraphOwned&) = delete; |
| 169 | GraphOwned& operator=(const GraphOwned&) = delete; |
| 170 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 171 | virtual ~GraphOwned(); |
| 172 | |
| 173 | // Called when the object is passed into the graph. |
| 174 | virtual void OnPassedToGraph(Graph* graph) = 0; |
| 175 | |
| 176 | // Called when the object is removed from the graph, either via an explicit |
| 177 | // call to Graph::TakeFromGraph, or prior to the Graph being destroyed. |
| 178 | virtual void OnTakenFromGraph(Graph* graph) = 0; |
Joe Mason | be3fb0f0 | 2024-06-14 15:45:22 | [diff] [blame] | 179 | |
Joe Mason | 7db74f5 | 2024-06-17 13:54:31 | [diff] [blame] | 180 | // Returns a pointer to the owning Graph. The will return nullptr before |
| 181 | // OnPassedToGraph() and after OnTakenFromGraph(), and a valid pointer at all |
| 182 | // other times. |
Joe Mason | bbc660a | 2024-06-18 15:21:06 | [diff] [blame] | 183 | Graph* GetOwningGraph() const; |
Joe Mason | 7db74f5 | 2024-06-17 13:54:31 | [diff] [blame] | 184 | |
Joe Mason | be3fb0f0 | 2024-06-14 15:45:22 | [diff] [blame] | 185 | private: |
| 186 | // GraphImpl is allowed to call PassToGraphImpl and TakeFromGraphImpl. |
| 187 | friend class GraphImpl; |
| 188 | |
| 189 | // GraphOwnedAndRegistered overrides PassToGraphImpl and TakeFromGraphImpl. |
| 190 | template <typename SelfType> |
| 191 | friend class GraphOwnedAndRegistered; |
| 192 | |
| 193 | // Only friends can override these. The default implementations just call |
| 194 | // OnPassedToGraph() and OnTakenFromGraph(). Helper classes like |
| 195 | // GraphOwnedAndRegistered can override these to add actions, while their |
| 196 | // subclasses continue to override OnPassedToGraph and OnTakenFromGraph |
| 197 | // without having to remember to call the inherited methods. |
| 198 | virtual void PassToGraphImpl(Graph* graph); |
| 199 | virtual void TakeFromGraphImpl(Graph* graph); |
Joe Mason | 7db74f5 | 2024-06-17 13:54:31 | [diff] [blame] | 200 | |
Joe Mason | bbc660a | 2024-06-18 15:21:06 | [diff] [blame] | 201 | SEQUENCE_CHECKER(sequence_checker_); |
| 202 | |
Joe Mason | 7db74f5 | 2024-06-17 13:54:31 | [diff] [blame] | 203 | // Pointer back to the owning graph. |
Joe Mason | bbc660a | 2024-06-18 15:21:06 | [diff] [blame] | 204 | raw_ptr<Graph> graph_ GUARDED_BY_CONTEXT(sequence_checker_) = nullptr; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | // A default implementation of GraphOwned. |
| 208 | class GraphOwnedDefaultImpl : public GraphOwned { |
| 209 | public: |
| 210 | GraphOwnedDefaultImpl(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 211 | |
| 212 | GraphOwnedDefaultImpl(const GraphOwnedDefaultImpl&) = delete; |
| 213 | GraphOwnedDefaultImpl& operator=(const GraphOwnedDefaultImpl&) = delete; |
| 214 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 215 | ~GraphOwnedDefaultImpl() override; |
| 216 | |
| 217 | // GraphOwned implementation: |
| 218 | void OnPassedToGraph(Graph* graph) override {} |
| 219 | void OnTakenFromGraph(Graph* graph) override {} |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 220 | }; |
| 221 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 222 | } // namespace performance_manager |
| 223 | |
Joe Mason | 48ac2fa | 2025-02-09 05:03:36 | [diff] [blame] | 224 | namespace base { |
| 225 | |
| 226 | // Specialize ScopedObservation to invoke the correct add and remove methods for |
| 227 | // each node observer type. These must be in the same namespace as |
| 228 | // base::ScopedObservationTraits. |
| 229 | |
| 230 | template <> |
| 231 | struct ScopedObservationTraits<performance_manager::Graph, |
| 232 | performance_manager::FrameNodeObserver> { |
| 233 | static void AddObserver(performance_manager::Graph* graph, |
| 234 | performance_manager::FrameNodeObserver* observer) { |
| 235 | graph->AddFrameNodeObserver(observer); |
| 236 | } |
| 237 | static void RemoveObserver(performance_manager::Graph* graph, |
| 238 | performance_manager::FrameNodeObserver* observer) { |
| 239 | graph->RemoveFrameNodeObserver(observer); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | template <> |
| 244 | struct ScopedObservationTraits<performance_manager::Graph, |
| 245 | performance_manager::PageNodeObserver> { |
| 246 | static void AddObserver(performance_manager::Graph* graph, |
| 247 | performance_manager::PageNodeObserver* observer) { |
| 248 | graph->AddPageNodeObserver(observer); |
| 249 | } |
| 250 | static void RemoveObserver(performance_manager::Graph* graph, |
| 251 | performance_manager::PageNodeObserver* observer) { |
| 252 | graph->RemovePageNodeObserver(observer); |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | template <> |
| 257 | struct ScopedObservationTraits<performance_manager::Graph, |
| 258 | performance_manager::ProcessNodeObserver> { |
| 259 | static void AddObserver(performance_manager::Graph* graph, |
| 260 | performance_manager::ProcessNodeObserver* observer) { |
| 261 | graph->AddProcessNodeObserver(observer); |
| 262 | } |
| 263 | static void RemoveObserver( |
| 264 | performance_manager::Graph* graph, |
| 265 | performance_manager::ProcessNodeObserver* observer) { |
| 266 | graph->RemoveProcessNodeObserver(observer); |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | template <> |
| 271 | struct ScopedObservationTraits<performance_manager::Graph, |
| 272 | performance_manager::SystemNodeObserver> { |
| 273 | static void AddObserver(performance_manager::Graph* graph, |
| 274 | performance_manager::SystemNodeObserver* observer) { |
| 275 | graph->AddSystemNodeObserver(observer); |
| 276 | } |
| 277 | static void RemoveObserver( |
| 278 | performance_manager::Graph* graph, |
| 279 | performance_manager::SystemNodeObserver* observer) { |
| 280 | graph->RemoveSystemNodeObserver(observer); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | template <> |
| 285 | struct ScopedObservationTraits<performance_manager::Graph, |
| 286 | performance_manager::WorkerNodeObserver> { |
| 287 | static void AddObserver(performance_manager::Graph* graph, |
| 288 | performance_manager::WorkerNodeObserver* observer) { |
| 289 | graph->AddWorkerNodeObserver(observer); |
| 290 | } |
| 291 | static void RemoveObserver( |
| 292 | performance_manager::Graph* graph, |
| 293 | performance_manager::WorkerNodeObserver* observer) { |
| 294 | graph->RemoveWorkerNodeObserver(observer); |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | } // namespace base |
| 299 | |
Sigurdur Asgeirsson | 51d9d24 | 2019-10-07 20:38:34 | [diff] [blame] | 300 | #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ |