File: v8_context_tracker_internal.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 (457 lines) | stat: -rw-r--r-- 15,508 bytes parent folder | download | duplicates (4)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 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_internal.h"

#include <utility>

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

namespace performance_manager {
namespace v8_memory {
namespace internal {

////////////////////////////////////////////////////////////////////////////////
// ExecutionContextData implementation:

ExecutionContextData::ExecutionContextData(
    ProcessData* process_data,
    const blink::ExecutionContextToken& token,
    mojom::IframeAttributionDataPtr iframe_attribution_data)
    : ExecutionContextState(token, std::move(iframe_attribution_data)),
      process_data_(process_data) {}

ExecutionContextData::~ExecutionContextData() {
  DCHECK(!IsTracked());
  DCHECK(ShouldDestroy());
  DCHECK_EQ(0u, main_nondetached_v8_context_count_);
}

bool ExecutionContextData::IsTracked() const {
  return previous() || next();
}

bool ExecutionContextData::ShouldDestroy() const {
  return v8_context_count_ == 0 && !remote_frame_data_;
}

void ExecutionContextData::SetRemoteFrameData(
    base::PassKey<RemoteFrameData>,
    RemoteFrameData* remote_frame_data) {
  DCHECK(!remote_frame_data_);
  DCHECK(remote_frame_data);
  remote_frame_data_ = remote_frame_data;
}

bool ExecutionContextData::ClearRemoteFrameData(
    base::PassKey<RemoteFrameData>) {
  DCHECK(remote_frame_data_);
  remote_frame_data_ = nullptr;
  return ShouldDestroy();
}

void ExecutionContextData::IncrementV8ContextCount(
    base::PassKey<V8ContextData>) {
  ++v8_context_count_;
}

bool ExecutionContextData::DecrementV8ContextCount(
    base::PassKey<V8ContextData>) {
  DCHECK_LT(0u, v8_context_count_);
  DCHECK_LT(main_nondetached_v8_context_count_, v8_context_count_);
  --v8_context_count_;
  return ShouldDestroy();
}

bool ExecutionContextData::MarkDestroyed(base::PassKey<ProcessData>) {
  if (destroyed)
    return false;
  destroyed = true;
  return true;
}

bool ExecutionContextData::MarkMainV8ContextCreated(
    base::PassKey<V8ContextTrackerDataStore>) {
  if (main_nondetached_v8_context_count_ >= v8_context_count_)
    return false;
  if (main_nondetached_v8_context_count_ >= 1)
    return false;
  ++main_nondetached_v8_context_count_;
  return true;
}

void ExecutionContextData::MarkMainV8ContextDetached(
    base::PassKey<V8ContextData>) {
  DCHECK_LT(0u, main_nondetached_v8_context_count_);
  --main_nondetached_v8_context_count_;
}

////////////////////////////////////////////////////////////////////////////////
// RemoteFrameData implementation:

RemoteFrameData::RemoteFrameData(ProcessData* process_data,
                                 const blink::RemoteFrameToken& token,
                                 ExecutionContextData* execution_context_data)
    : process_data_(process_data),
      token_(token),
      execution_context_data_(execution_context_data) {
  DCHECK(process_data);
  DCHECK(execution_context_data);
  execution_context_data->SetRemoteFrameData(PassKey(), this);
}

RemoteFrameData::~RemoteFrameData() {
  DCHECK(!IsTracked());

  // If this is the last reference keeping alive a tracked ExecutionContextData,
  // then clean it up as well. Untracked ExecutionContextDatas will go out of
  // scope on their own.
  if (execution_context_data_->ClearRemoteFrameData(PassKey()) &&
      execution_context_data_->IsTracked()) {
    // Reset `execution_context_data_` to nullptr because it will be destroyed
    // using the token below.
    blink::ExecutionContextToken token = execution_context_data_->GetToken();
    execution_context_data_ = nullptr;
    process_data_->data_store()->Destroy(token);
  }
}

bool RemoteFrameData::IsTracked() const {
  return previous() || next();
}

////////////////////////////////////////////////////////////////////////////////
// V8ContextData implementation:

V8ContextData::V8ContextData(ProcessData* process_data,
                             const mojom::V8ContextDescription& description,
                             ExecutionContextData* execution_context_data)
    : V8ContextState(description, execution_context_data),
      process_data_(process_data) {
  DCHECK(process_data);
  DCHECK_EQ(static_cast<bool>(execution_context_data),
            static_cast<bool>(description.execution_context_token));
  if (execution_context_data) {
    DCHECK_EQ(execution_context_data->GetToken(),
              *description.execution_context_token);

    // These must be same process.
    DCHECK_EQ(process_data, execution_context_data->process_data());
    execution_context_data->IncrementV8ContextCount(PassKey());
  }
}

V8ContextData::~V8ContextData() {
  DCHECK(!IsTracked());

  // Mark as detached if necessary so that main world counts are appropriately
  // updated even during tear down code paths.
  MarkDetachedImpl();

  // If this is the last reference keeping alive a tracked ExecutionContextData,
  // then clean it up as well. Untracked ExecutionContextDatas will go out of
  // scope on their own.
  auto* execution_context_data = GetExecutionContextData();
  if (execution_context_data &&
      execution_context_data->DecrementV8ContextCount(PassKey()) &&
      execution_context_data->IsTracked()) {
    // Reset the execution_context_state to nullptr because it will be
    // destroyed using the token below.
    blink::ExecutionContextToken token = execution_context_data->GetToken();
    execution_context_state = nullptr;
    process_data_->data_store()->Destroy(token);
  }
}

bool V8ContextData::IsTracked() const {
  return previous() || next();
}

ExecutionContextData* V8ContextData::GetExecutionContextData() const {
  return static_cast<ExecutionContextData*>(execution_context_state);
}

void V8ContextData::SetWasTracked(base::PassKey<V8ContextTrackerDataStore>) {
  DCHECK(!was_tracked_);
  was_tracked_ = true;
}

bool V8ContextData::MarkDetached(base::PassKey<ProcessData>) {
  return MarkDetachedImpl();
}

bool V8ContextData::IsMainV8Context() const {
  auto* ec_data = GetExecutionContextData();
  if (!ec_data)
    return false;
  // ExecutionContexts hosting worklets have no main world (there can be many
  // worklets sharing an ExecutionContext).
  if (IsWorkletToken(ec_data->GetToken()))
    return false;

  // We've already checked sane combinations of ExecutionContextToken types and
  // world types in ValidateV8ContextDescription, so don't need to be overly
  // thorough here.

  // Only main frames and workers can be "main" contexts.
  auto world_type = description.world_type;
  return world_type == mojom::V8ContextWorldType::kMain ||
         world_type == mojom::V8ContextWorldType::kWorkerOrWorklet;
}

bool V8ContextData::MarkDetachedImpl() {
  if (detached)
    return false;
  detached = true;
  // The EC is notified of the main V8 context only when it is passed to the
  // data store (at which point |was_tracked_| is set to true). Only do the
  // symmetric operation if the first occurred.
  if (IsMainV8Context() && was_tracked_) {
    if (auto* ec_data = GetExecutionContextData())
      ec_data->MarkMainV8ContextDetached(PassKey());
  }
  return true;
}

////////////////////////////////////////////////////////////////////////////////
// ProcessData implementation:

ProcessData::ProcessData(const ProcessNodeImpl* process_node)
    : data_store_(GetDataStore(process_node)) {}

ProcessData::~ProcessData() {
  DCHECK(execution_context_datas_.empty());
  DCHECK(remote_frame_datas_.empty());
  DCHECK(v8_context_datas_.empty());
}

void ProcessData::TearDown() {
  // First, remove any RemoteFrameData references owned by this ProcessData
  // that are keeping alive ExecutionContextDatas in other ProcessDatas. This
  // can cause ExecutionContextDatas to be torn down.
  while (!remote_frame_datas_.empty()) {
    auto* node = remote_frame_datas_.head();
    data_store_->Destroy(node->value()->GetToken());
  }

  // Drain the list of V8ContextTokens. This will also indirectly clean up and
  // ExecutionContextDatas that are only being kept alive by V8ContextData
  // references.
  while (!v8_context_datas_.empty()) {
    auto* node = v8_context_datas_.head();
    data_store_->Destroy(node->value()->GetToken());
  }

  // Any ExecutionContextDatas still alive are only being kept alive because of
  // RemoteFrameData references from another ProcessData. Clean those up.
  while (!execution_context_datas_.empty()) {
    auto* node = execution_context_datas_.head();
    auto* ec_data = node->value();
    auto* rfd = ec_data->remote_frame_data();
    DCHECK(rfd);
    DCHECK_EQ(0u, ec_data->v8_context_count());
    data_store_->Destroy(rfd->GetToken());
  }

  // We now expect everything to have been cleaned up.
  DCHECK(execution_context_datas_.empty());
  DCHECK(remote_frame_datas_.empty());
  DCHECK(v8_context_datas_.empty());
}

void ProcessData::Add(base::PassKey<V8ContextTrackerDataStore>,
                      ExecutionContextData* ec_data) {
  DCHECK(ec_data);
  DCHECK_EQ(this, ec_data->process_data());
  DCHECK(!ec_data->ShouldDestroy());
  DCHECK(!ec_data->IsTracked());
  execution_context_datas_.Append(ec_data);
  counts_.IncrementExecutionContextDataCount();
}

void ProcessData::Add(base::PassKey<V8ContextTrackerDataStore>,
                      RemoteFrameData* rf_data) {
  DCHECK(rf_data);
  DCHECK_EQ(this, rf_data->process_data());
  DCHECK(!rf_data->IsTracked());
  remote_frame_datas_.Append(rf_data);
}

void ProcessData::Add(base::PassKey<V8ContextTrackerDataStore>,
                      V8ContextData* v8_data) {
  DCHECK(v8_data);
  DCHECK_EQ(this, v8_data->process_data());
  DCHECK(!v8_data->IsTracked());
  v8_context_datas_.Append(v8_data);
  counts_.IncrementV8ContextDataCount();
}

void ProcessData::Remove(base::PassKey<V8ContextTrackerDataStore>,
                         ExecutionContextData* ec_data) {
  DCHECK(ec_data);
  DCHECK_EQ(this, ec_data->process_data());
  DCHECK(ec_data->IsTracked());
  DCHECK(ec_data->ShouldDestroy());
  counts_.DecrementExecutionContextDataCount(ec_data->destroyed);
  ec_data->RemoveFromList();
}

void ProcessData::Remove(base::PassKey<V8ContextTrackerDataStore>,
                         RemoteFrameData* rf_data) {
  DCHECK(rf_data);
  DCHECK_EQ(this, rf_data->process_data());
  DCHECK(rf_data->IsTracked());
  rf_data->RemoveFromList();
}

void ProcessData::Remove(base::PassKey<V8ContextTrackerDataStore>,
                         V8ContextData* v8_data) {
  DCHECK(v8_data);
  DCHECK_EQ(this, v8_data->process_data());
  DCHECK(v8_data->IsTracked());
  counts_.DecrementV8ContextDataCount(v8_data->detached);
  v8_data->RemoveFromList();
}

bool ProcessData::MarkDestroyed(base::PassKey<V8ContextTrackerDataStore>,
                                ExecutionContextData* ec_data) {
  bool result = ec_data->MarkDestroyed(PassKey());
  if (result)
    counts_.MarkExecutionContextDataDestroyed();
  return result;
}

bool ProcessData::MarkDetached(base::PassKey<V8ContextTrackerDataStore>,
                               V8ContextData* v8_data) {
  bool result = v8_data->MarkDetached(PassKey());
  if (result)
    counts_.MarkV8ContextDataDetached();
  return result;
}

////////////////////////////////////////////////////////////////////////////////
// V8ContextTrackerDataStore implementation:

V8ContextTrackerDataStore::V8ContextTrackerDataStore() = default;

V8ContextTrackerDataStore::~V8ContextTrackerDataStore() {
  DCHECK(global_execution_context_datas_.empty());
  DCHECK(global_remote_frame_datas_.empty());
  DCHECK(global_v8_context_datas_.empty());
}

void V8ContextTrackerDataStore::Pass(
    std::unique_ptr<ExecutionContextData> ec_data) {
  DCHECK(ec_data.get());
  ec_data->process_data()->Add(PassKey(), ec_data.get());
  auto result = global_execution_context_datas_.insert(std::move(ec_data));
  DCHECK(result.second);
}

void V8ContextTrackerDataStore::Pass(std::unique_ptr<RemoteFrameData> rf_data) {
  DCHECK(rf_data.get());
  rf_data->process_data()->Add(PassKey(), rf_data.get());
  auto result = global_remote_frame_datas_.insert(std::move(rf_data));
  DCHECK(result.second);
}

bool V8ContextTrackerDataStore::Pass(std::unique_ptr<V8ContextData> v8_data) {
  DCHECK(v8_data.get());
  auto* ec_data = v8_data->GetExecutionContextData();
  if (ec_data && v8_data->IsMainV8Context()) {
    if (!ec_data->MarkMainV8ContextCreated(PassKey()))
      return false;
  }
  v8_data->process_data()->Add(PassKey(), v8_data.get());
  v8_data->SetWasTracked(PassKey());
  auto result = global_v8_context_datas_.insert(std::move(v8_data));
  DCHECK(result.second);
  return true;
}

ExecutionContextData* V8ContextTrackerDataStore::Get(
    const blink::ExecutionContextToken& token) {
  auto it = global_execution_context_datas_.find(token);
  if (it == global_execution_context_datas_.end())
    return nullptr;
  return it->get();
}

RemoteFrameData* V8ContextTrackerDataStore::Get(
    const blink::RemoteFrameToken& token) {
  auto it = global_remote_frame_datas_.find(token);
  if (it == global_remote_frame_datas_.end())
    return nullptr;
  return it->get();
}

V8ContextData* V8ContextTrackerDataStore::Get(
    const blink::V8ContextToken& token) {
  auto it = global_v8_context_datas_.find(token);
  if (it == global_v8_context_datas_.end())
    return nullptr;
  return it->get();
}

void V8ContextTrackerDataStore::MarkDestroyed(ExecutionContextData* ec_data) {
  DCHECK(ec_data);
  if (ec_data->process_data()->MarkDestroyed(PassKey(), ec_data)) {
    DCHECK_LT(destroyed_execution_context_count_,
              global_execution_context_datas_.size());
    ++destroyed_execution_context_count_;
  }
}

bool V8ContextTrackerDataStore::MarkDetached(V8ContextData* v8_data) {
  DCHECK(v8_data);
  if (v8_data->process_data()->MarkDetached(PassKey(), v8_data)) {
    DCHECK_LT(detached_v8_context_count_, global_v8_context_datas_.size());
    ++detached_v8_context_count_;
    return true;
  }
  return false;
}

void V8ContextTrackerDataStore::Destroy(
    const blink::ExecutionContextToken& token) {
  auto it = global_execution_context_datas_.find(token);
  CHECK(it != global_execution_context_datas_.end());
  auto* ec_data = it->get();
  if (ec_data->destroyed) {
    DCHECK_LT(0u, destroyed_execution_context_count_);
    --destroyed_execution_context_count_;
  } else {
    DCHECK_LT(destroyed_execution_context_count_,
              global_execution_context_datas_.size());
  }
  ec_data->process_data()->Remove(PassKey(), ec_data);
  global_execution_context_datas_.erase(it);
}

void V8ContextTrackerDataStore::Destroy(const blink::RemoteFrameToken& token) {
  auto it = global_remote_frame_datas_.find(token);
  CHECK(it != global_remote_frame_datas_.end());
  auto* rf_data = it->get();
  rf_data->process_data()->Remove(PassKey(), rf_data);
  global_remote_frame_datas_.erase(it);
}

void V8ContextTrackerDataStore::Destroy(const blink::V8ContextToken& token) {
  auto it = global_v8_context_datas_.find(token);
  CHECK(it != global_v8_context_datas_.end());
  auto* v8_data = it->get();
  if (v8_data->detached) {
    DCHECK_LT(0u, detached_v8_context_count_);
    --detached_v8_context_count_;
  } else {
    DCHECK_LT(detached_v8_context_count_, global_v8_context_datas_.size());
  }
  v8_data->process_data()->Remove(PassKey(), v8_data);
  global_v8_context_datas_.erase(it);
}

}  // namespace internal
}  // namespace v8_memory
}  // namespace performance_manager