blob: b48e1a47b0a517049109b34b7e5f7e1761a706f4 [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 Hamilton3e598872019-05-06 16:15:1313#include "base/macros.h"
Chris Hamilton8f5839732019-06-27 23:13:0814#include "base/memory/ptr_util.h"
Chris Hamilton3e598872019-05-06 16:15:1315
Chris Hamiltonf48eead2019-07-04 18:07:4016namespace ukm {
17class UkmRecorder;
18} // namespace ukm
19
Chris Hamilton3e598872019-05-06 16:15:1320namespace performance_manager {
21
Chris Hamiltonc8f596e12019-06-03 15:24:5822class GraphObserver;
Chris Hamilton8f5839732019-06-27 23:13:0823class GraphOwned;
Chris Hamilton1b06b3292020-05-06 19:58:0624class GraphRegistered;
Chris Hamilton4a717262019-07-03 15:57:3325class FrameNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5826class FrameNodeObserver;
Chris Hamilton1b06b3292020-05-06 19:58:0627class NodeDataDescriberRegistry;
Chris Hamilton4a717262019-07-03 15:57:3328class PageNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5829class PageNodeObserver;
Chris Hamilton4a717262019-07-03 15:57:3330class ProcessNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5831class ProcessNodeObserver;
Chris Hamilton4a717262019-07-03 15:57:3332class SystemNode;
Chris Hamiltonc8f596e12019-06-03 15:24:5833class SystemNodeObserver;
Patrick Monette514a5432019-08-12 22:00:4734class WorkerNode;
35class WorkerNodeObserver;
Chris Hamiltonc8f596e12019-06-03 15:24:5836
Chris Hamilton1b06b3292020-05-06 19:58:0637template <typename DerivedType>
38class GraphRegisteredImpl;
Sigurdur Asgeirssonfab8e9e2020-04-07 20:22:5739
Chris Hamilton3e598872019-05-06 16:15:1340// Represents a graph of the nodes representing a single browser. Maintains a
41// set of nodes that can be retrieved in different ways, some indexed. Keeps
42// a list of observers that are notified of node addition and removal.
43class Graph {
44 public:
Chris Hamiltonc8f596e12019-06-03 15:24:5845 using Observer = GraphObserver;
46
Chris Hamilton3e598872019-05-06 16:15:1347 Graph();
48 virtual ~Graph();
49
Chris Hamiltonc8f596e12019-06-03 15:24:5850 // Adds an |observer| on the graph. It is safe for observers to stay
51 // registered on the graph at the time of its death.
52 virtual void AddGraphObserver(GraphObserver* observer) = 0;
53 virtual void AddFrameNodeObserver(FrameNodeObserver* observer) = 0;
54 virtual void AddPageNodeObserver(PageNodeObserver* observer) = 0;
55 virtual void AddProcessNodeObserver(ProcessNodeObserver* observer) = 0;
56 virtual void AddSystemNodeObserver(SystemNodeObserver* observer) = 0;
Patrick Monette514a5432019-08-12 22:00:4757 virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:5858
59 // Removes an |observer| from the graph.
60 virtual void RemoveGraphObserver(GraphObserver* observer) = 0;
61 virtual void RemoveFrameNodeObserver(FrameNodeObserver* observer) = 0;
62 virtual void RemovePageNodeObserver(PageNodeObserver* observer) = 0;
63 virtual void RemoveProcessNodeObserver(ProcessNodeObserver* observer) = 0;
64 virtual void RemoveSystemNodeObserver(SystemNodeObserver* observer) = 0;
Patrick Monette514a5432019-08-12 22:00:4765 virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:5866
Chris Hamilton8f5839732019-06-27 23:13:0867 // For convenience, allows you to pass ownership of an object to the graph.
68 // Useful for attaching observers that will live with the graph until it dies.
69 // If you can name the object you can also take it back via "TakeFromGraph".
Chris Hamilton48cf6f02020-09-30 15:43:2170 virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0;
Chris Hamilton8f5839732019-06-27 23:13:0871 virtual std::unique_ptr<GraphOwned> TakeFromGraph(
72 GraphOwned* graph_owned) = 0;
73
Chris Hamilton48cf6f02020-09-30 15:43:2174 // Templated PassToGraph helper that also returns a pointer to the object,
75 // which makes it easy to use PassToGraph in constructors.
76 template <typename DerivedType>
77 DerivedType* PassToGraph(std::unique_ptr<DerivedType> graph_owned) {
78 DerivedType* object = graph_owned.get();
79 PassToGraphImpl(std::move(graph_owned));
80 return object;
81 }
82
Sigurdur Asgeirssond2ee573e2019-08-28 15:20:3883 // A TakeFromGraph helper for taking back the ownership of a GraphOwned
84 // subclass.
Chris Hamilton8f5839732019-06-27 23:13:0885 template <typename DerivedType>
Sigurdur Asgeirssond2ee573e2019-08-28 15:20:3886 std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) {
Chris Hamilton8f5839732019-06-27 23:13:0887 return base::WrapUnique(
88 static_cast<DerivedType*>(TakeFromGraph(graph_owned).release()));
89 }
90
Chris Hamilton1b06b3292020-05-06 19:58:0691 // Registers an object with this graph. It is expected that no more than one
92 // object of a given type is registered at a given moment, and that all
93 // registered objects are unregistered before graph tear-down.
94 virtual void RegisterObject(GraphRegistered* object) = 0;
95
96 // Unregisters the provided |object|, which must previously have been
97 // registered with "RegisterObject". It is expected that all registered
98 // objects are unregistered before graph tear-down.
99 virtual void UnregisterObject(GraphRegistered* object) = 0;
100
101 // Returns the registered object of the given type, nullptr if none has been
102 // registered.
103 template <typename DerivedType>
104 DerivedType* GetRegisteredObjectAs() {
Chris Hamiltonf7effeb2020-05-19 21:43:12105 // Be sure to access the TypeId provided by GraphRegisteredImpl, in case
106 // this class has other TypeId implementations.
Chris Hamilton1b06b3292020-05-06 19:58:06107 GraphRegistered* object =
108 GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId());
109 return static_cast<DerivedType*>(object);
110 }
111
Chris Hamilton4a717262019-07-03 15:57:33112 // Returns a collection of all known nodes of the given type.
113 virtual const SystemNode* FindOrCreateSystemNode() = 0;
Patrick Monette514a5432019-08-12 22:00:47114 virtual std::vector<const ProcessNode*> GetAllProcessNodes() const = 0;
Chris Hamilton4a717262019-07-03 15:57:33115 virtual std::vector<const FrameNode*> GetAllFrameNodes() const = 0;
116 virtual std::vector<const PageNode*> GetAllPageNodes() const = 0;
Patrick Monette514a5432019-08-12 22:00:47117 virtual std::vector<const WorkerNode*> GetAllWorkerNodes() const = 0;
Chris Hamilton4a717262019-07-03 15:57:33118
Chris Hamilton417d0372019-11-08 17:13:22119 // Returns true if the graph is currently empty.
120 virtual bool IsEmpty() const = 0;
121
Chris Hamiltonf48eead2019-07-04 18:07:40122 // Returns the associated UKM recorder if it is defined.
123 virtual ukm::UkmRecorder* GetUkmRecorder() const = 0;
124
Sigurdur Asgeirssonfab8e9e2020-04-07 20:22:57125 // Returns the data describer registry.
126 virtual NodeDataDescriberRegistry* GetNodeDataDescriberRegistry() const = 0;
127
Chris Hamiltonff2b2ced2019-05-30 13:29:05128 // The following functions are implementation detail and should not need to be
129 // used by external clients. They provide the ability to safely downcast to
130 // the underlying implementation.
131 virtual uintptr_t GetImplType() const = 0;
132 virtual const void* GetImpl() const = 0;
133
Chris Hamiltonf3beb682020-07-07 21:23:22134 // Allows code that is not explicitly aware of the Graph sequence to determine
135 // if they are in fact on the right sequence. Prefer to use the
136 // DCHECK_ON_GRAPH_SEQUENCE macro.
137#if DCHECK_IS_ON()
138 virtual bool IsOnGraphSequence() const = 0;
139#endif
140
Chris Hamilton3e598872019-05-06 16:15:13141 private:
Chris Hamilton1b06b3292020-05-06 19:58:06142 // Retrieves the object with the given |type_id|, returning nullptr if none
143 // exists. Clients must use the GetRegisteredObjectAs wrapper instead.
144 virtual GraphRegistered* GetRegisteredObject(uintptr_t type_id) = 0;
145
Chris Hamilton3e598872019-05-06 16:15:13146 DISALLOW_COPY_AND_ASSIGN(Graph);
147};
148
Chris Hamiltonf3beb682020-07-07 21:23:22149#if DCHECK_IS_ON()
150#define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(graph->IsOnGraphSequence())
151#else
152// Compiles to a nop, and will eat ostream input.
153#define DCHECK_ON_GRAPH_SEQUENCE(graph) DCHECK(true)
154#endif
155
Chris Hamiltonc8f596e12019-06-03 15:24:58156// Observer interface for the graph.
157class GraphObserver {
158 public:
159 GraphObserver();
160 virtual ~GraphObserver();
161
162 // Called before the |graph| associated with this observer disappears. This
Chris Hamilton8f5839732019-06-27 23:13:08163 // allows the observer to do any necessary cleanup work. Note that the
164 // observer should remove itself from observing the graph using this
165 // callback.
166 // TODO(chrisha): Make this run before the destructor!
Chris Hamiltonc8f596e12019-06-03 15:24:58167 // crbug.com/966840
Chris Hamilton8f5839732019-06-27 23:13:08168 virtual void OnBeforeGraphDestroyed(Graph* graph) = 0;
Chris Hamiltonc8f596e12019-06-03 15:24:58169
170 private:
171 DISALLOW_COPY_AND_ASSIGN(GraphObserver);
172};
173
Chris Hamilton8f5839732019-06-27 23:13:08174// Helper class for passing ownership of objects to a graph.
175class GraphOwned {
176 public:
177 GraphOwned();
178 virtual ~GraphOwned();
179
180 // Called when the object is passed into the graph.
181 virtual void OnPassedToGraph(Graph* graph) = 0;
182
183 // Called when the object is removed from the graph, either via an explicit
184 // call to Graph::TakeFromGraph, or prior to the Graph being destroyed.
185 virtual void OnTakenFromGraph(Graph* graph) = 0;
186
187 private:
188 DISALLOW_COPY_AND_ASSIGN(GraphOwned);
189};
190
191// A default implementation of GraphOwned.
192class GraphOwnedDefaultImpl : public GraphOwned {
193 public:
194 GraphOwnedDefaultImpl();
195 ~GraphOwnedDefaultImpl() override;
196
197 // GraphOwned implementation:
198 void OnPassedToGraph(Graph* graph) override {}
199 void OnTakenFromGraph(Graph* graph) override {}
200
201 private:
202 DISALLOW_COPY_AND_ASSIGN(GraphOwnedDefaultImpl);
203};
204
Chris Hamilton3e598872019-05-06 16:15:13205} // namespace performance_manager
206
Sigurdur Asgeirsson51d9d242019-10-07 20:38:34207#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_