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 | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 14 | |
Chris Hamilton | f48eead | 2019-07-04 18:07:40 | [diff] [blame] | 15 | namespace ukm { |
| 16 | class UkmRecorder; |
| 17 | } // namespace ukm |
| 18 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 19 | namespace performance_manager { |
| 20 | |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 21 | class GraphObserver; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 22 | class GraphOwned; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 23 | class GraphRegistered; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 24 | class FrameNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 25 | class FrameNodeObserver; |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 26 | class NodeDataDescriberRegistry; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 27 | class PageNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 28 | class PageNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 29 | class ProcessNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 30 | class ProcessNodeObserver; |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 31 | class SystemNode; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 32 | class SystemNodeObserver; |
Patrick Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 33 | class WorkerNode; |
| 34 | class WorkerNodeObserver; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 35 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 36 | template <typename DerivedType> |
| 37 | class GraphRegisteredImpl; |
Sigurdur Asgeirsson | fab8e9e | 2020-04-07 20:22:57 | [diff] [blame] | 38 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 39 | // 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. |
| 42 | class Graph { |
| 43 | public: |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 44 | using Observer = GraphObserver; |
| 45 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 46 | Graph(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 47 | |
| 48 | Graph(const Graph&) = delete; |
| 49 | Graph& operator=(const Graph&) = delete; |
| 50 | |
Chris Hamilton | 3e59887 | 2019-05-06 16:15:13 | [diff] [blame] | 51 | virtual ~Graph(); |
| 52 | |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 53 | // 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 Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 60 | virtual void AddWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 61 | |
| 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 Monette | 514a543 | 2019-08-12 22:00:47 | [diff] [blame] | 68 | virtual void RemoveWorkerNodeObserver(WorkerNodeObserver* observer) = 0; |
Chris Hamilton | c8f596e1 | 2019-06-03 15:24:58 | [diff] [blame] | 69 | |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 70 | // 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 Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 73 | virtual void PassToGraphImpl(std::unique_ptr<GraphOwned> graph_owned) = 0; |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 74 | virtual std::unique_ptr<GraphOwned> TakeFromGraph( |
| 75 | GraphOwned* graph_owned) = 0; |
| 76 | |
Chris Hamilton | 48cf6f0 | 2020-09-30 15:43:21 | [diff] [blame] | 77 | // 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 Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 86 | // A TakeFromGraph helper for taking back the ownership of a GraphOwned |
| 87 | // subclass. |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 88 | template <typename DerivedType> |
Sigurdur Asgeirsson | d2ee573e | 2019-08-28 15:20:38 | [diff] [blame] | 89 | std::unique_ptr<DerivedType> TakeFromGraphAs(DerivedType* graph_owned) { |
Chris Hamilton | 8f583973 | 2019-06-27 23:13:08 | [diff] [blame] | 90 | return base::WrapUnique( |
| 91 | static_cast<DerivedType*>(TakeFromGraph(graph_owned).release())); |
| 92 | } |
| 93 | |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 94 | // 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 Hamilton | f7effeb | 2020-05-19 21:43:12 | [diff] [blame] | 108 | // Be sure to access the TypeId provided by GraphRegisteredImpl, in case |
| 109 | // this class has other TypeId implementations. |
Chris Hamilton | 1b06b329 | 2020-05-06 19:58:06 | [diff] [blame] | 110 | GraphRegistered* object = |
| 111 | GetRegisteredObject(GraphRegisteredImpl<DerivedType>::TypeId()); |
| 112 | return static_cast<DerivedType*>(object); |
| 113 | } |
| 114 | |
Chris Hamilton | 4a71726 | 2019-07-03 15:57:33 | [diff] [blame] | 115 | // Returns a collection of all known nodes of the given type. |
Sebastien Marchand | b56b8aa9a | 2021-06-15 14:46:27 | [diff] [blame] |
|