File: worker_script_context_set.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,860 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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/worker_script_context_set.h"

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

#include "extensions/renderer/dispatcher.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/worker_thread_util.h"
#include "v8/include/v8-context.h"

namespace extensions {

namespace {

using ContextVector = std::vector<std::unique_ptr<ScriptContext>>;

// Returns an iterator to the ScriptContext associated with |v8_context| from
// |contexts|, or |contexts|->end() if not found.
ContextVector::iterator FindContext(ContextVector* contexts,
                                    v8::Local<v8::Context> v8_context) {
  auto context_matches =
      [&v8_context](const std::unique_ptr<ScriptContext>& context) {
        v8::HandleScope handle_scope(context->isolate());
        v8::Context::Scope context_scope(context->v8_context());
        return context->v8_context() == v8_context;
      };
  return std::ranges::find_if(*contexts, context_matches);
}

// Implement thread safety by storing each ScriptContext in TLS.
constinit thread_local ContextVector* contexts = nullptr;

}  // namespace

WorkerScriptContextSet::WorkerScriptContextSet() = default;

WorkerScriptContextSet::~WorkerScriptContextSet() = default;

void WorkerScriptContextSet::ForEach(
    const mojom::HostID& host_id,
    content::RenderFrame* render_frame,
    const base::RepeatingCallback<void(ScriptContext*)>& callback) {
  DCHECK(!render_frame);

  for (const std::unique_ptr<ScriptContext>& context : *contexts) {
    DCHECK(!context->GetRenderFrame());

    switch (host_id.type) {
      case mojom::HostID::HostType::kExtensions:
        // Note: If the type is kExtensions and host_id.id is empty, then the
        // call should affect all extensions. See comment in dispatcher.cc
        // UpdateAllBindings().
        if (host_id.id.empty() || context->GetExtensionID() == host_id.id) {
          ExecuteCallbackWithContext(context.get(), callback);
        }
        break;

      case mojom::HostID::HostType::kWebUi:
        DCHECK(host_id.id.empty());
        ExecuteCallbackWithContext(context.get(), callback);
        break;

      case mojom::HostID::HostType::kControlledFrameEmbedder:
        DCHECK(!host_id.id.empty());
        // Verify that host_id matches context->host_id.
        if (context->host_id().type == host_id.type ||
            context->host_id().id == host_id.id) {
          ExecuteCallbackWithContext(context.get(), callback);
        }
        break;
    }
  }
}

void WorkerScriptContextSet::ExecuteCallbackWithContext(
    ScriptContext* context,
    const base::RepeatingCallback<void(ScriptContext*)>& callback) {
  CHECK(context);
  callback.Run(context);
}

void WorkerScriptContextSet::Insert(std::unique_ptr<ScriptContext> context) {
  DCHECK(worker_thread_util::IsWorkerThread())
      << "Must be called on a worker thread";
  if (!contexts) {
    // First context added for this thread. Create a new set, then wait for
    // this thread's shutdown.
    contexts = new ContextVector();
    content::WorkerThread::AddObserver(this);
  }
  CHECK(FindContext(contexts, context->v8_context()) == contexts->end())
      << "Worker for " << context->url() << " is already in this set";
  contexts->push_back(std::move(context));
}

ScriptContext* WorkerScriptContextSet::GetContextByV8Context(
    v8::Local<v8::Context> v8_context) {
  DCHECK(worker_thread_util::IsWorkerThread())
      << "Must be called on a worker thread";
  if (!contexts)
    return nullptr;

  auto context_it = FindContext(contexts, v8_context);
  return context_it == contexts->end() ? nullptr : context_it->get();
}

void WorkerScriptContextSet::Remove(v8::Local<v8::Context> v8_context,
                                    const GURL& url) {
  DCHECK(worker_thread_util::IsWorkerThread())
      << "Must be called on a worker thread";
  if (!contexts) {
    // Thread has already been torn down, and |v8_context| removed. I'm not
    // sure this can actually happen (depends on in what order blink fires
    // events), but SW lifetime has bitten us before, so be cautious.
    return;
  }
  auto context_it = FindContext(contexts, v8_context);
  CHECK(context_it != contexts->end()) << "Worker for " << url
                                       << " is not in this set";
  DCHECK_EQ(url, (*context_it)->url());
  (*context_it)->Invalidate();
  contexts->erase(context_it);
}

void WorkerScriptContextSet::WillStopCurrentWorkerThread() {
  content::WorkerThread::RemoveObserver(this);
  DCHECK(contexts);
  for (const auto& context : *contexts)
    context->Invalidate();
  delete contexts;
  contexts = nullptr;
}

}  // namespace extensions