v8_context_tracker_helpers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_V8_CONTEXT_TRACKER_HELPERS_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_CONTEXT_TRACKER_HELPERS_H_
  6. #include <string>
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. #include "third_party/blink/public/common/tokens/tokens.h"
  9. namespace performance_manager {
  10. class FrameNode;
  11. class Graph;
  12. namespace execution_context {
  13. class ExecutionContext;
  14. } // namespace execution_context
  15. namespace mojom {
  16. class V8ContextDescription;
  17. } // namespace mojom
  18. namespace v8_memory {
  19. // Helper function to convert a WorkerToken to an ExecutionContext token.
  20. // TODO(crbug.com/1126285): There should be automatic type conversion for this
  21. // added to MultiToken<>.
  22. [[nodiscard]] blink::ExecutionContextToken ToExecutionContextToken(
  23. const blink::WorkerToken& token);
  24. // Determines if the provided frame has a cross-process parent frame.
  25. [[nodiscard]] bool HasCrossProcessParent(const FrameNode* frame_node);
  26. // Determines if a string is a valid extension ID.
  27. // TODO(crbug.com/1096617): The extension ID should be strongly typed, with
  28. // built-in validation, mojo type-mapping, etc. Ideally this would be done
  29. // directly in extensions/common/extension_id.h.
  30. [[nodiscard]] bool IsValidExtensionId(const std::string& s);
  31. // Returns true if an ExecutionContextToken corresponds to a worklet.
  32. [[nodiscard]] bool IsWorkletToken(const blink::ExecutionContextToken& token);
  33. // Returns true if an ExecutionContextToken corresponds to a worker.
  34. [[nodiscard]] bool IsWorkerToken(const blink::ExecutionContextToken& token);
  35. // Looks up the execution context corresponding to the given token. Note that
  36. // the ExecutionContextRegistry must be installed on the graph.
  37. [[nodiscard]] const execution_context::ExecutionContext* GetExecutionContext(
  38. const blink::ExecutionContextToken& token,
  39. Graph* graph);
  40. // Return type for V8ContextDescription validation.
  41. enum class V8ContextDescriptionStatus {
  42. kValid,
  43. // World name errors.
  44. kMissingWorldName,
  45. kUnexpectedWorldName,
  46. kInvalidExtensionWorldName,
  47. // ExecutionContextToken errors.
  48. kMissingExecutionContextToken,
  49. kUnexpectedExecutionContextToken,
  50. kMissingLocalFrameToken,
  51. kUnexpectedLocalFrameToken,
  52. kUnexpectedWorkletToken,
  53. };
  54. // Validates the given V8ContextDescription.
  55. [[nodiscard]] V8ContextDescriptionStatus ValidateV8ContextDescription(
  56. const mojom::V8ContextDescription& description);
  57. // Determines whether or not IframeAttributionData is expected to accompany the
  58. // provided V8ContextDescription. This is not always able to be determined, in
  59. // which case absl::nullopt will be returned. It is assumed that the
  60. // |description| has previously been validated.
  61. [[nodiscard]] absl::optional<bool>
  62. ExpectIframeAttributionDataForV8ContextDescription(
  63. const mojom::V8ContextDescription& description,
  64. Graph* graph);
  65. // Small helper class for maintaining a count of objects that are optionally
  66. // "marked".
  67. class MarkedObjectCount {
  68. public:
  69. MarkedObjectCount() = default;
  70. MarkedObjectCount(const MarkedObjectCount&) = delete;
  71. MarkedObjectCount& operator=(const MarkedObjectCount&) = delete;
  72. ~MarkedObjectCount() = default;
  73. size_t count() const { return count_; }
  74. size_t marked_count() const { return marked_count_; }
  75. void Increment() { ++count_; }
  76. void Mark();
  77. void Decrement(bool marked);
  78. private:
  79. size_t marked_count_ = 0;
  80. size_t count_ = 0;
  81. };
  82. // Helper class for maintaining a pair of context counts for both
  83. // ExecutionContexts and V8Contexts.
  84. class ContextCounts {
  85. public:
  86. ContextCounts() = default;
  87. ContextCounts(const ContextCounts&) = delete;
  88. ContextCounts& operator=(const ContextCounts&) = delete;
  89. ~ContextCounts() = default;
  90. size_t GetExecutionContextDataCount() const { return ec_count_.count(); }
  91. size_t GetDestroyedExecutionContextDataCount() const {
  92. return ec_count_.marked_count();
  93. }
  94. void IncrementExecutionContextDataCount() { ec_count_.Increment(); }
  95. void MarkExecutionContextDataDestroyed() { ec_count_.Mark(); }
  96. void DecrementExecutionContextDataCount(bool destroyed) {
  97. ec_count_.Decrement(destroyed);
  98. }
  99. size_t GetV8ContextDataCount() const { return v8_count_.count(); }
  100. size_t GetDetachedV8ContextDataCount() const {
  101. return v8_count_.marked_count();
  102. }
  103. void IncrementV8ContextDataCount() { v8_count_.Increment(); }
  104. void MarkV8ContextDataDetached() { v8_count_.Mark(); }
  105. void DecrementV8ContextDataCount(bool detached) {
  106. v8_count_.Decrement(detached);
  107. }
  108. private:
  109. MarkedObjectCount ec_count_;
  110. MarkedObjectCount v8_count_;
  111. };
  112. } // namespace v8_memory
  113. } // namespace performance_manager
  114. #endif // COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_CONTEXT_TRACKER_HELPERS_H_