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 
5 #ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_OPERATIONS_H_
6 #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_OPERATIONS_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/containers/flat_set.h"
10 
11 namespace performance_manager {
12 
13 class FrameNode;
14 class PageNode;
15 class ProcessNode;
16 
17 // A collection of utilities for performing common queries and traversals on a
18 // graph.
19 struct GraphOperations {
20   using FrameNodeVisitor = base::RepeatingCallback<bool(const FrameNode*)>;
21 
22   // Returns the collection of page nodes that are associated with the given
23   // |process|. A page is associated with a process if the page's frame tree
24   // contains 1 or more frames hosted in the given |process|.
25   static base::flat_set<const PageNode*> GetAssociatedPageNodes(
26       const ProcessNode* process);
27 
28   // Returns the collection of process nodes associated with the given |page|.
29   // A |process| is associated with a page if the page's frame tree contains 1
30   // or more frames hosted in that |process|.
31   static base::flat_set<const ProcessNode*> GetAssociatedProcessNodes(
32       const PageNode* page);
33 
34   // Returns the collection of frame nodes associated with a page. This is
35   // returned in level order, with main frames first (level 0), main frame
36   // children next (level 1), all the way down to the deepest leaf frames.
37   static std::vector<const FrameNode*> GetFrameNodes(const PageNode* page);
38 
39   // Traverse the frame tree of a |page| in the given order, invoking the
40   // provided |callable| for each frame node in the tree. If the visitor returns
41   // false then then the iteration is halted. Returns true if all calls to the
42   // visitor returned true, false otherwise.
43   static bool VisitFrameTreePreOrder(const PageNode* page,
44                                      const FrameNodeVisitor& visitor);
45   static bool VisitFrameTreePostOrder(const PageNode* page,
46                                       const FrameNodeVisitor& visitor);
47 
48   // Returns true if the given |frame| is in the frame tree associated with the
49   // given |page|.
50   static bool HasFrame(const PageNode* page, const FrameNode* frame);
51 };
52 
53 }  // namespace performance_manager
54 
55 #endif  // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_GRAPH_GRAPH_OPERATIONS_H_
56