graph_impl_operations.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef COMPONENTS_PERFORMANCE_MANAGER_GRAPH_GRAPH_IMPL_OPERATIONS_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_GRAPH_GRAPH_IMPL_OPERATIONS_H_
  6. #include "base/containers/flat_set.h"
  7. #include "components/performance_manager/graph/frame_node_impl.h"
  8. #include "components/performance_manager/graph/page_node_impl.h"
  9. namespace performance_manager {
  10. class ProcessNodeImpl;
  11. // A collection of utilities for performing common queries and traversals on a
  12. // graph.
  13. struct GraphImplOperations {
  14. // Returns the collection of page nodes that are associated with the given
  15. // |process|. A page is associated with a process if the page's frame tree
  16. // contains 1 or more frames hosted in the given |process|.
  17. static base::flat_set<PageNodeImpl*> GetAssociatedPageNodes(
  18. const ProcessNodeImpl* process);
  19. // Returns the collection of process nodes associated with the given |page|.
  20. // A |process| is associated with a page if the page's frame tree contains 1
  21. // or more frames hosted in that |process|.
  22. static base::flat_set<ProcessNodeImpl*> GetAssociatedProcessNodes(
  23. const PageNodeImpl* page);
  24. // Returns the collection of frame nodes associated with a page. This is
  25. // returned in level order, with main frames first (level 0), main frame
  26. // children next (level 1), all the way down to the deepest leaf frames.
  27. static std::vector<FrameNodeImpl*> GetFrameNodes(const PageNodeImpl* page);
  28. // Traverse the frame tree of a |page| in the given order, invoking the
  29. // provided |callable| for each frame node in the tree. The |callable| has to
  30. // provide a "bool operator()(FrameNodeImpl*)". If the visitor returns false
  31. // then the iteration is halted. Returns true if all calls to the visitor
  32. // returned true, false otherwise.
  33. template <typename Callable>
  34. static bool VisitFrameTreePreOrder(const PageNodeImpl* page,
  35. Callable callable);
  36. template <typename Callable>
  37. static bool VisitFrameTreePostOrder(const PageNodeImpl* page,
  38. Callable callable);
  39. // Returns true if the given |frame| is in the frame tree associated with the
  40. // given |page|.
  41. static bool HasFrame(const PageNodeImpl* page, FrameNodeImpl* frame);
  42. };
  43. // Implementation details for VisitFrameTree*.
  44. namespace internal {
  45. template <typename Callable>
  46. bool VisitFrameAndChildren(FrameNodeImpl* frame,
  47. Callable callable,
  48. bool pre_order) {
  49. if (pre_order && !callable(frame))
  50. return false;
  51. for (auto* child : frame->child_frame_nodes()) {
  52. if (!VisitFrameAndChildren(child, callable, pre_order))
  53. return false;
  54. }
  55. if (!pre_order && !callable(frame))
  56. return false;
  57. return true;
  58. }
  59. template <typename Callable>
  60. bool VisitFrameTree(const PageNodeImpl* page,
  61. Callable callable,
  62. bool pre_order) {
  63. for (auto* main_frame_node : page->main_frame_nodes()) {
  64. if (!VisitFrameAndChildren(main_frame_node, callable, pre_order))
  65. return false;
  66. }
  67. return true;
  68. }
  69. } // namespace internal
  70. // static
  71. template <typename Callable>
  72. bool GraphImplOperations::VisitFrameTreePreOrder(const PageNodeImpl* page,
  73. Callable callable) {
  74. return internal::VisitFrameTree(page, callable, true);
  75. }
  76. // static
  77. template <typename Callable>
  78. bool GraphImplOperations::VisitFrameTreePostOrder(const PageNodeImpl* page,
  79. Callable callable) {
  80. return internal::VisitFrameTree(page, callable, false);
  81. }
  82. } // namespace performance_manager
  83. #endif // COMPONENTS_PERFORMANCE_MANAGER_GRAPH_GRAPH_IMPL_OPERATIONS_H_