File: v8_context_tracker_helpers.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (207 lines) | stat: -rw-r--r-- 7,896 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/v8_memory/v8_context_tracker_helpers.h"

#include "base/check.h"
#include "base/notreached.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/execution_context/execution_context.h"
#include "components/performance_manager/public/execution_context/execution_context_registry.h"
#include "components/performance_manager/public/mojom/v8_contexts.mojom.h"

namespace performance_manager {
namespace v8_memory {

namespace {

bool IsSynchronousIframeAttributionDataExpected(
    const execution_context::ExecutionContext* ec) {
  DCHECK(ec);
  auto* frame = ec->GetFrameNode();
  // We only expect iframe data for frames...
  if (!frame)
    return false;
  // ... that aren't main frames (have a parent) ...
  if (frame->IsMainFrame())
    return false;
  auto* parent = frame->GetParentFrameNode();
  DCHECK(parent);
  // ... where the parent is hosted in the same process ...
  if (frame->GetProcessNode() != parent->GetProcessNode())
    return false;
  // ... and where they are both in the same SiteInstanceGroup (implying they
  // are both in the same frame-tree and know directly of each other's
  // LocalFrame rather then communicating via a RemoteFrame and a
  // RenderFrameProxy).
  return frame->GetSiteInstanceGroupId() == parent->GetSiteInstanceGroupId();
}

}  // namespace

bool HasCrossProcessParent(const FrameNode* frame_node) {
  DCHECK(frame_node);
  if (frame_node->IsMainFrame())
    return false;
  const ProcessNode* process = frame_node->GetProcessNode();
  const ProcessNode* parent_process =
      frame_node->GetParentFrameNode()->GetProcessNode();
  return process != parent_process;
}

bool IsValidExtensionId(const std::string& s) {
  // Must be a 32-character string with lowercase letters between a and p,
  // inclusive.
  if (s.size() != 32u)
    return false;
  for (char c : s) {
    if (c < 'a' || c > 'p')
      return false;
  }
  return true;
}

bool IsWorkletToken(const blink::ExecutionContextToken& token) {
  return token.Is<blink::AnimationWorkletToken>() ||
         token.Is<blink::AudioWorkletToken>() ||
         token.Is<blink::LayoutWorkletToken>() ||
         token.Is<blink::PaintWorkletToken>();
}

bool IsWorkerToken(const blink::ExecutionContextToken& token) {
  return token.Is<blink::DedicatedWorkerToken>() ||
         token.Is<blink::ServiceWorkerToken>() ||
         token.Is<blink::SharedWorkerToken>();
}

const execution_context::ExecutionContext* GetExecutionContext(
    const blink::ExecutionContextToken& token,
    Graph* graph) {
  auto* registry =
      execution_context::ExecutionContextRegistry::GetFromGraph(graph);
  DCHECK(registry);
  return registry->GetExecutionContextByToken(token);
}

V8ContextDescriptionStatus ValidateV8ContextDescription(
    const mojom::V8ContextDescription& description) {
  switch (description.world_type) {
    case mojom::V8ContextWorldType::kMain: {
      if (description.world_name)
        return V8ContextDescriptionStatus::kUnexpectedWorldName;
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (!description.execution_context_token->Is<blink::LocalFrameToken>())
        return V8ContextDescriptionStatus::kMissingLocalFrameToken;
    } break;

    case mojom::V8ContextWorldType::kWorkerOrWorklet: {
      if (description.world_name)
        return V8ContextDescriptionStatus::kUnexpectedWorldName;
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (description.execution_context_token->Is<blink::LocalFrameToken>())
        return V8ContextDescriptionStatus::kUnexpectedLocalFrameToken;
    } break;

    case mojom::V8ContextWorldType::kShadowRealm: {
      if (description.world_name)
        return V8ContextDescriptionStatus::kUnexpectedWorldName;
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (!description.execution_context_token->Is<blink::ShadowRealmToken>())
        return V8ContextDescriptionStatus::kMissingShadowRealmToken;
    } break;

    case mojom::V8ContextWorldType::kExtension: {
      if (!description.world_name)
        return V8ContextDescriptionStatus::kMissingWorldName;
      if (!IsValidExtensionId(description.world_name.value()))
        return V8ContextDescriptionStatus::kInvalidExtensionWorldName;
      // Extensions can only inject into frames and workers, *not* worklets.
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (IsWorkletToken(description.execution_context_token.value()))
        return V8ContextDescriptionStatus::kUnexpectedWorkletToken;
    } break;

    case mojom::V8ContextWorldType::kIsolated: {
      // World names are optional in isolated worlds.
      // Only frame and workers can have isolated worlds, *not* worklets.
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (IsWorkletToken(description.execution_context_token.value()))
        return V8ContextDescriptionStatus::kUnexpectedWorkletToken;
    } break;

    case mojom::V8ContextWorldType::kInspector: {
      if (description.world_name)
        return V8ContextDescriptionStatus::kUnexpectedWorldName;
      // Devtools can only inject into frames and workers, *not* worklets.
      if (!description.execution_context_token)
        return V8ContextDescriptionStatus::kMissingExecutionContextToken;
      if (IsWorkletToken(description.execution_context_token.value()))
        return V8ContextDescriptionStatus::kUnexpectedWorkletToken;
    } break;

    case mojom::V8ContextWorldType::kRegExp: {
      // Regexp worlds have no additional data.
      if (description.world_name)
        return V8ContextDescriptionStatus::kUnexpectedWorldName;
      if (description.execution_context_token)
        return V8ContextDescriptionStatus::kUnexpectedExecutionContextToken;
    } break;
  }

  return V8ContextDescriptionStatus::kValid;
}

std::optional<bool> ExpectIframeAttributionDataForV8ContextDescription(
    const mojom::V8ContextDescription& description,
    Graph* graph) {
  switch (description.world_type) {
    case mojom::V8ContextWorldType::kMain: {
      // There's no guarantee that the actual ExecutionContext has yet been
      // created from our POV as there's a race between V8Context creation
      // notifications and node creations. But if it does exist, we sanity check
      // that we should in fact be receiving iframe data for this frame.
      if (auto* ec = GetExecutionContext(*description.execution_context_token,
                                         graph)) {
        return IsSynchronousIframeAttributionDataExpected(ec);
      }
      // Unable to be determined.
      return std::nullopt;
    }

    case mojom::V8ContextWorldType::kWorkerOrWorklet:
    case mojom::V8ContextWorldType::kShadowRealm:
    case mojom::V8ContextWorldType::kExtension:
    case mojom::V8ContextWorldType::kIsolated:
    case mojom::V8ContextWorldType::kInspector:
    case mojom::V8ContextWorldType::kRegExp:
      break;
  }

  return false;
}

void MarkedObjectCount::Mark() {
  DCHECK_LT(marked_count_, count_);
  ++marked_count_;
}

void MarkedObjectCount::Decrement(bool marked) {
  DCHECK_LT(0u, count_);
  if (marked) {
    DCHECK_LT(0u, marked_count_);
    --marked_count_;
  } else {
    DCHECK_LT(marked_count_, count_);
  }
  --count_;
}

}  // namespace v8_memory
}  // namespace performance_manager