web_memory_aggregator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 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_V8_MEMORY_WEB_MEMORY_AGGREGATOR_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_WEB_MEMORY_AGGREGATOR_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/sequence_checker.h"
  9. #include "components/performance_manager/public/mojom/web_memory.mojom.h"
  10. #include "content/public/browser/browsing_instance_id.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "url/origin.h"
  13. namespace performance_manager {
  14. class FrameNode;
  15. class ProcessNode;
  16. class WorkerNode;
  17. namespace v8_memory {
  18. class AggregationPointVisitor;
  19. // Traverses the graph of execution contexts to find the results of the last
  20. // memory measurement and aggregates them according to the rules defined in the
  21. // performance.measureUserAgentSpecificMemory spec.
  22. // (See public/v8_memory/web_memory.h for the link and spec version.)
  23. class WebMemoryAggregator {
  24. public:
  25. // Constructs an aggregator for the results of a memory request from
  26. // |requesting_node|. This expects the caller to check if |requesting_node|
  27. // is allowed to measure memory according to the spec.
  28. //
  29. // The aggregation is performed by calling AggregateMemoryResult. The graph
  30. // traversal will not start directly from |requesting_node|, but from the
  31. // top frame nodes.
  32. explicit WebMemoryAggregator(const FrameNode* requesting_node);
  33. ~WebMemoryAggregator();
  34. WebMemoryAggregator(const WebMemoryAggregator& other) = delete;
  35. WebMemoryAggregator& operator=(const WebMemoryAggregator& other) = delete;
  36. // Performs the aggregation.
  37. mojom::WebMemoryMeasurementPtr AggregateMeasureMemoryResult();
  38. private:
  39. friend class AggregationPointVisitor;
  40. friend class WebMemoryAggregatorTest;
  41. // FrameNodeVisitor that recursively adds |frame_node| and its children to
  42. // the aggregation using |ap_visitor|. Always returns true to continue
  43. // traversal.
  44. bool VisitFrame(AggregationPointVisitor* ap_visitor,
  45. const FrameNode* frame_node);
  46. // WorkerNodeVisitor that recursively adds |worker_node| and its children to
  47. // the aggregation using |ap_visitor|. Always returns true to continue
  48. // traversal.
  49. bool VisitWorker(AggregationPointVisitor* ap_visitor,
  50. const WorkerNode* worker_node);
  51. // Creates a new breakdown entry with the given |scope| and |url|, and adds it
  52. // to the list in |measurement|. Returns a pointer to the newly created entry.
  53. static mojom::WebMemoryBreakdownEntry* CreateBreakdownEntry(
  54. mojom::WebMemoryAttribution::Scope scope,
  55. absl::optional<std::string> url,
  56. mojom::WebMemoryMeasurement* measurement);
  57. // Sets the id and src attributes of |breakdown| using those stored in the
  58. // V8ContextTracker for the given |frame_node|.
  59. static void SetBreakdownAttributionFromFrame(
  60. const FrameNode* frame_node,
  61. mojom::WebMemoryBreakdownEntry* breakdown);
  62. // Copies the id and src attributes from |from| to |to|.
  63. static void CopyBreakdownAttribution(
  64. const mojom::WebMemoryBreakdownEntry* from,
  65. mojom::WebMemoryBreakdownEntry* to);
  66. // The origin of the node that requests memory measurement.
  67. const url::Origin requesting_origin_;
  68. // The process node of the requesting frame.
  69. const raw_ptr<const ProcessNode> requesting_process_node_;
  70. // The process node of the main frame.
  71. const raw_ptr<const ProcessNode> main_process_node_;
  72. // The browsing instance id of the requesting frame.
  73. const content::BrowsingInstanceId browsing_instance_id_;
  74. SEQUENCE_CHECKER(sequence_checker_);
  75. };
  76. } // namespace v8_memory
  77. } // namespace performance_manager
  78. #endif // COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_WEB_MEMORY_AGGREGATOR_H_