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:27