Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 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 | |
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> |
Raphael Kubo da Costa | acf525a | 2019-07-04 17:39:36 | [diff] [blame] | 10 | #include <vector> |
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 | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 13 | #include "base/macros.h" |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 15 | |
Chris Hamilton | f48eead | 2019-07-04 18:07:40 | [diff] [blame] | 16 | namespace ukm { |
| 17 | class UkmRecorder; |
| 18 | } // namespace ukm |
| 19 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 20 | namespace performance_manager { |
| 21 | |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 22 | class GraphObserver; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 23 | class GraphOwned; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 24 | class GraphRegistered; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 25 | class FrameNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 26 | class FrameNodeObserver; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 27 | class NodeDataDescriberRegistry; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 28 | class PageNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 29 | class PageNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 30 | class ProcessNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 31 | class ProcessNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 32 | class SystemNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 33 | class SystemNodeObserver; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 34 | class WorkerNode; |
| 35 | class WorkerNodeObserver; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 36 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 37 | template <typename DerivedType> |
| 38 | class GraphRegisteredImpl; |
Sigurdur Asgeirsson | fab8e9e | 2020-04-07 20:22:57 | [diff] [blame] | 39 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 40 | // 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. |
| 43 | class Graph { |
| 44 | public: |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 45 | using Observer = GraphObserver; |
| 46 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 47 | Graph(); |
| 48 | virtual ~Graph(); |
| 49 | |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 50 | // 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 Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 57 | virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 58 | |
| 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 Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 65 | virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 66 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 67 | // 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 Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 70 | virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 71 | virtual std::unique_ptr<GraphOwned> TakeFromGraph( |
| 72 | GraphOwned* graph_owned) = 0; |
| 73 | |
Chris Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 74 | // 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 Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 83 | // A TakeFromGraph helper for taking back the ownership of a GraphOwned |
| 84 | // subclass. |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 85 | template <typename DerivedType> |
Sigurdur Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 86 | std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) { |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 87 | return base::WrapUnique( |
| 88 | static_cast<DerivedType*>(TakeFromGraph(graph_owned).release())); |
| 89 | } |
| 90 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 91 | // 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 Hamilton | f7effeb | 2020-05-19 21:43:12 | [diff] [blame] | 105 | // Be sure to access the TypeId provided by GraphRegisteredImpl, in case |
| 106 | // this class has other TypeId implementations. |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 107 | GraphRegistered* object = |
| 108 | GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId()); |
| 109 | return static_cast<DerivedType*>(object); |
| 110 | } |
| 111 | |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 112 | // Returns a collection of all known nodes of the given type. |
| 113 | virtual const SystemNode* FindOrCreateSystemNode() = 0; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 114 | virtual std::vector<const ProcessNode*> GetAllProcessNodes() const = 0; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 115 | virtual std::vector<const FrameNode*> GetAllFrameNodes() const = 0; |
| 116 | virtual std::vector<const PageNode*> GetAllPageNodes() const = 0; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 117 | virtual std::vector<const WorkerNode*> GetAllWorkerNodes() const = 0; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 118 | |
Chris Hamilton | 417d037 | 2019-11-08 17:13:22 | [diff] [blame] | 119 | // Returns true if the graph is currently empty. |
| 120 | virtual bool IsEmpty() const = 0; |
| 121 | |
Chris Hamilton | f48eead | 2019-07-04 18:07:40 | [diff] [blame] | 122 | // Returns the associated UKM recorder if it is defined. |
| 123 | virtual ukm::UkmRecorder* GetUkmRecorder() const = 0; |
| 124 | |
Sigurdur Asgeirsson | fab8e9e | 2020-04-07 20:22:57 | [diff] [blame] | 125 | // Returns the data describer registry. |
| 126 | virtual NodeDataDescriberRegistry* GetNodeDataDescriberRegistry() const = 0; |
| 127 | |
Chris Hamilton | ff2b2ced | 2019-05-30 13:29:05 | [diff] [blame] | 128 | // 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 Hamilton | f3beb68 | 2020-07-07 21:23:22 | [diff] [blame] | 134 | // 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 Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 141 | private: |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 142 | // 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 Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(Graph); |
| 147 | }; |
| 148 | |
Chris Hamilton | f3beb68 | 2020-07-07 21:23:22 | [diff] [blame] | 149 | #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 Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 156 | // Observer interface for the graph. |
| 157 | class GraphObserver { |
| 158 | public: |
| 159 | GraphObserver(); |
| 160 | virtual ~GraphObserver(); |
| 161 | |
| 162 | // Called before the |graph| associated with this observer disappears. This |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 163 | // 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 Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 167 | // crbug.com/966840 |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 168 | virtual void OnBeforeGraphDestroyed(Graph* graph) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 169 | |
| 170 | private: |
| 171 | DISALLOW_COPY_AND_ASSIGN(GraphObserver); |
| 172 | }; |
| 173 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 174 | // Helper class for passing ownership of objects to a graph. |
| 175 | class 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. |
| 192 | class 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 Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 205 | } // namespace performance_manager |
| 206 | |
Sigurdur Asgeirsson | 51d9d24 | 2019-10-07 20:38:34 | [diff] [blame] | 207 | #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_H_ |