blob: 8bc22f91f55621fc185c58fd03a09f686817d3ed [file] [log] [blame]
Chris Hamilton3e598872019-05-06 16:15:131// 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
Sigurdur Asgeirsson51d9d242019-10-07 20:38:345#ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_
6#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_
Chris Hamilton3e598872019-05-06 16:15:137
Chris Hamiltonff2b2ced2019-05-30 13:29:058#include <cstdint>
Chris Hamilton8f5839732019-06-27 23:13:089#include <memory>
Raphael Kubo da Costaacf525a2019-07-04 17:39:3610#include <vector>
Chris Hamiltonff2b2ced2019-05-30 13:29:0511
Chris Hamiltonf3beb682020-07-07 21:23:2212#include "base/dcheck_is_on.h"
Chris Hamilton8f5839732019-06-27 23:13:0813#include "base/memory/ptr_util.h"
Chris Hamilton3e598872019-05-06 16:15:1314
Chris Hamiltonf48eead2019-07-04 18:07:4015namespace ukm {
16class UkmRecorder;
17} // namespace ukm
18
Chris Hamilton3e598872019-05-06 16:15:1319namespace performance_manager {
20
Chris Hamiltonc8f596e12019-06-03 15:24:5821class GraphObserver;
Chris Hamilton8f5839732019-06-27 23:13:0822class GraphOwned;
Chris Hamilton1b06b3292020-05-06 19:58:0623class GraphRegistered;
Chris Hamilton4a717262019-07-03 15:57:3324class FrameNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5825class FrameNodeObserver;
Chris Hamilton1b06b3292020-05-06 19:58:0626class NodeDataDescriberRegistry;
Chris Hamilton4a717262019-07-03 15:57:3327class PageNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5828class PageNodeObserver;
Chris Hamilton4a717262019-07-03 15:57:3329class ProcessNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5830class ProcessNodeObserver;
Chris Hamilton4a717262019-07-03 15:57:3331class SystemNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5832class SystemNodeObserver;
Patrick Monette514a5432019-08-12 22:00:4733class WorkerNode;
34class WorkerNodeObserver;
Chris Hamiltonc8f596e12019-06-03 15:24:5835
Chris Hamilton1b06b3292020-05-06 19:58:0636template <typename DerivedType>
37class GraphRegisteredImpl;
Sigurdur Asgeirssonfab8e9e2020-04-07 20:22:5738
Chris Hamilton3e598872019-05-06 16:15:1339// Represents a graph of the nodes representing a single browser. Maintains a
40// set of nodes that can be retrieved in different ways, some indexed. Keeps
41// a list of observers that are notified of node addition and removal.
42class Graph {
43 public:
Chris Hamiltonc8f596e12019-06-03 15:24:5844 using Observer = GraphObserver;
45
Chris Hamilton3e598872019-05-06 16:15:1346 Graph();
Peter Boström09c01822021-09-20 22:43:2747
48 Graph(const Graph&) = delete;
49 Graph& operator=(const Graph&) = delete;
50
Chris Hamilton3e598872019-05-06 16:15:1351 virtual ~Graph();
52
Chris Hamiltonc8f596e12019-06-03 15:24:5853 // Adds an |observer| on the graph. It is safe for observers to stay
54 // registered on the graph at the time of its death.
55 virtual void AddGraphObserver(GraphObserver* observer) = 0;
56 virtual void AddFrameNodeObserver(FrameNodeObserver* observer) = 0;
57 virtual void AddPageNodeObserver(PageNodeObserver* observer) = 0;
58 virtual void AddProcessNodeObserver(ProcessNodeObserver* observer) = 0;
59 virtual void AddSystemNodeObserver(SystemNodeObserver* observer) = 0;
Patrick Monette514a5432019-08-12 22:00:4760 virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:5861
62 // Removes an |observer| from the graph.
63 virtual void RemoveGraphObserver(GraphObserver* observer) = 0;
64 virtual void RemoveFrameNodeObserver(FrameNodeObserver* observer) = 0;
65 virtual void RemovePageNodeObserver(PageNodeObserver* observer) = 0;
66 virtual void RemoveProcessNodeObserver(ProcessNodeObserver* observer) = 0;
67 virtual void RemoveSystemNodeObserver(SystemNodeObserver* observer) = 0;
Patrick Monette514a5432019-08-12 22:00:4768 virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:5869
Chris Hamilton8f5839732019-06-27 23:13:0870 // For convenience, allows you to pass ownership of an object to the graph.
71 // Useful for attaching observers that will live with the graph until it dies.
72 // If you can name the object you can also take it back via "TakeFromGraph".
Chris Hamilton48cf6f02020-09-30 15:43:2173 virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0;
Chris Hamilton8f5839732019-06-27 23:13:0874 virtual std::unique_ptr<GraphOwned> TakeFromGraph(
75 GraphOwned* graph_owned) = 0;
76
Chris Hamilton48cf6f02020-09-30 15:43:2177 // Templated PassToGraph helper that also returns a pointer to the object,
78 // which makes it easy to use PassToGraph in constructors.
79 template <typename DerivedType>
80 DerivedType* PassToGraph(std::unique_ptr<DerivedType> graph_owned) {
81 DerivedType* object = graph_owned.get();
82 PassToGraphImpl(std::move(graph_owned));
83 return object;
84 }
85
Sigurdur Asgeirssond2ee573e2019-08-28 15:20:3886 // A TakeFromGraph helper for taking back the ownership of a GraphOwned
87 // subclass.
Chris Hamilton8f5839732019-06-27 23:13:0888 template <typename DerivedType>
Sigurdur Asgeirssond2ee573e2019-08-28 15:20:3889 std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) {
Chris Hamilton8f5839732019-06-27 23:13:0890 return base::WrapUnique(
91 static_cast<DerivedType*>(TakeFromGraph(graph_owned).release()));
92 }
93
Chris Hamilton1b06b3292020-05-06 19:58:0694 // Registers an object with this graph. It is expected that no more than one
95 // object of a given type is registered at a given moment, and that all
96 // registered objects are unregistered before graph tear-down.
97 virtual void RegisterObject(GraphRegistered* object) = 0;
98
99 // Unregisters the provided |object|, which must previously have been
100 // registered with "RegisterObject". It is expected that all registered
101 // objects are unregistered before graph tear-down.
102 virtual void UnregisterObject(GraphRegistered* object) = 0;
103
104 // Returns the registered object of the given type, nullptr if none has been
105 // registered.
106 template <typename DerivedType>
107 DerivedType* GetRegisteredObjectAs() {
Chris Hamiltonf7effeb2020-05-19 21:43:12108 // Be sure to access the TypeId provided by GraphRegisteredImpl, in case
109 // this class has other TypeId implementations.
Chris Hamilton1b06b3292020-05-06 19:58:06110 GraphRegistered* object =
111 GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId());
112 return static_cast<DerivedType*>(object);
113 }
114
Chris Hamilton4a717262019-07-03 15:57:33115 // Returns a collection of all known nodes of the given type.
Sebastien Marchandb56b8aa9a2021-06-15 14:46:27116 virtual const SystemNode* GetSystemNode() const = 0;
Patrick Monette514a5432019-08-12 22:00:47117 virtual std::vector<const ProcessNode*> GetAllProcessNodes() const = 0;
Chris Hamilton4a717262019-07-03 15:57:33118 virtual std::vector<const FrameNode*> GetAllFrameNodes() const = 0;
119 virtual std::vector<const PageNode*> GetAllPageNodes() const = 0;
Patrick Monette514a5432019-08-12 22:00:47120 virtual std::vector<const WorkerNode*> GetAllWorkerNodes() const = 0;
Chris Hamilton4a717262019-07-03 15:57:33121
Sebastien Marchandb56b8aa9a2021-06-15 14:46:27122 // Returns true if the graph only contains the default nodes.
123 virtual bool HasOnlySystemNode() const = 0;
Chris Hamilton417d0372019-11-08 17:13:22124
Chris Hamiltonf48eead2019-07-04 18:07:40125 // Returns the associated UKM recorder if it is defined.
126 virtual ukm::UkmRecorder* GetUkmRecorder() const = 0;
127
Sigurdur Asgeirssonfab8e9e2020-04-07 20:22:57128 // Returns the data describer registry.
129 virtual NodeDataDescriberRegistry* GetNodeDataDescriberRegistry() const = 0;
130
Chris Hamiltonff2b2ced2019-05-30 13:29:05131 // The following functions are implementation detail and should not need to be
132 // used by external clients. They provide the ability to safely downcast to
133 // the underlying implementation.
134 virtual uintptr_t GetImplType() const = 0;
135 virtual const void* GetImpl() const = 0;
136
Chris Hamiltonf3beb682020-07-07 21:23:22137 // Allows code that is not explicitly aware of the Graph sequence to determine
138 // if they are in fact on the right sequence. Prefer to use the
139 // DCHECK_ON_GRAPH_SEQUENCE macro.
140#if DCHECK_IS_ON()
141 virtual bool IsOnGraphSequence() const = 0;
142#endif
143
Chris Hamilton3e598872019-05-06 16:15:13144 private:
Chris Hamilton1b06b3292020-05-06 19:58:06145 // Retrieves the object with the given |type_id|, returning nullptr if none
146 // exists. Clients must use the GetRegisteredObjectAs wrapper instead.
147 virtual GraphRegistered* GetRegisteredObject(uintptr_t type_id) = 0;
Chris Hamilton3e598872019-05-06 16:15:13148};
149
Chris Hamiltonf3beb682020-07-07 21:23:22150#if DCHECK_IS_ON()
151#define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(graph->IsOnGraphSequence())
152#else
153// Compiles to a nop, and will eat ostream input.
154#define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(true)
155#endif
156
Chris Hamiltonc8f596e12019-06-03 15:24:58157// Observer interface for the graph.
158class GraphObserver {
159 public:
160 GraphObserver();
Peter Boström09c01822021-09-20 22:43:27161
162 GraphObserver(const GraphObserver&) = delete;
163 GraphObserver& operator=(const GraphObserver&) = delete;
164
Chris Hamiltonc8f596e12019-06-03 15:24:58165 virtual ~GraphObserver();
166
167 // Called before the |graph| associated with this observer disappears. This
Chris Hamilton8f5839732019-06-27 23:13:08168 // allows the observer to do any necessary cleanup work. Note that the
169 // observer should remove itself from observing the graph using this
170 // callback.
171 // TODO(chrisha): Make this run before the destructor!
Chris Hamiltonc8f596e12019-06-03 15:24:58172 // crbug.com/966840
Chris Hamilton8f5839732019-06-27 23:13:08173 virtual void OnBeforeGraphDestroyed(Graph* graph) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:58174};
175
Chris Hamilton8f5839732019-06-27 23:13:08176// Helper class for passing ownership of objects to a graph.
177class GraphOwned {
178 public:
179 GraphOwned();
Peter Boström09c01822021-09-20 22:43:27180
181 GraphOwned(const GraphOwned&) = delete;
182 GraphOwned& operator=(const GraphOwned&) = delete;
183
Chris Hamilton8f5839732019-06-27 23:13:08184 virtual ~GraphOwned();
185
186 // Called when the object is passed into the graph.
187 virtual void OnPassedToGraph(Graph* graph) = 0;
188
189 // Called when the object is removed from the graph, either via an explicit
190 // call to Graph::TakeFromGraph, or prior to the Graph being destroyed.
191 virtual void OnTakenFromGraph(Graph* graph) = 0;
Chris Hamilton8f5839732019-06-27 23:13:08192};
193
194// A default implementation of GraphOwned.
195class GraphOwnedDefaultImpl : public GraphOwned {
196 public:
197 GraphOwnedDefaultImpl();
Peter Boström09c01822021-09-20 22:43:27198
199 GraphOwnedDefaultImpl(const GraphOwnedDefaultImpl&) = delete;
200 GraphOwnedDefaultImpl& operator=(const GraphOwnedDefaultImpl&) = delete;
201
Chris Hamilton8f5839732019-06-27 23:13:08202 ~GraphOwnedDefaultImpl() override;
203
204 // GraphOwned implementation:
205 void OnPassedToGraph(Graph* graph) override {}
206 void OnTakenFromGraph(Graph* graph) override {}
Chris Hamilton8f5839732019-06-27 23:13:08207};
208
Chris Hamilton3e598872019-05-06 16:15:13209} // namespace performance_manager
210
Sigurdur Asgeirsson51d9d242019-10-07 20:38:34211#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_