electron/shell/renderer/web_worker_observer.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
3 KiB
C++
Raw Normal View History

// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/renderer/web_worker_observer.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"
#include "shell/common/api/electron_bindings.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
namespace electron {
namespace {
2018-04-18 01:55:30 +00:00
static base::LazyInstance<
base::ThreadLocalPointer<WebWorkerObserver>>::DestructorAtExit lazy_tls =
LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
WebWorkerObserver* WebWorkerObserver::GetCurrent() {
WebWorkerObserver* self = lazy_tls.Pointer()->Get();
return self ? self : new WebWorkerObserver;
}
WebWorkerObserver::WebWorkerObserver()
: node_bindings_(
NodeBindings::Create(NodeBindings::BrowserEnvironment::kWorker)),
electron_bindings_(
std::make_unique<ElectronBindings>(node_bindings_->uv_loop())) {
lazy_tls.Pointer()->Set(this);
}
WebWorkerObserver::~WebWorkerObserver() {
lazy_tls.Pointer()->Set(nullptr);
// Destroying the node environment will also run the uv loop,
// Node.js expects `kExplicit` microtasks policy and will run microtasks
// checkpoints after every call into JavaScript. Since we use a different
// policy in the renderer - switch to `kExplicit`
v8::Isolate* isolate = node_bindings_->uv_env()->isolate();
DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(isolate), 0);
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
2017-03-10 08:33:27 +00:00
node::FreeEnvironment(node_bindings_->uv_env());
node::FreeIsolateData(node_bindings_->isolate_data());
}
void WebWorkerObserver::WorkerScriptReadyForEvaluation(
v8::Local<v8::Context> worker_context) {
v8::Context::Scope context_scope(worker_context);
chore: bump chromium to 92.0.4505.0 (master) (#29058) * chore: bump chromium in DEPS to 92.0.4500.2 * resolve conflicts * update patches * chore: cherry-pick 82434206f306 from chromium (#29060) * fix patch * chore: bump chromium in DEPS to 92.0.4501.0 * chore: bump chromium in DEPS to 92.0.4502.0 * chore: bump chromium in DEPS to 92.0.4503.0 * chore: update patches * 2869869: [Code Health] Refactor ListValue::Insert in gpu compositor https://chromium-review.googlesource.com/c/chromium/src/+/2869869 * 2877924: Separate InkDropHost from InkDropHostView https://chromium-review.googlesource.com/c/chromium/src/+/2877924 * chore: bump chromium in DEPS to 92.0.4504.0 * update patches * Fixup for Separate InkDropHost from InkDropHostView https://chromium-review.googlesource.com/c/chromium/src/+/2877924 * 2873469: Compute hashes of .pak files during the build, and check it at runtime. https://chromium-review.googlesource.com/c/chromium/src/+/2873469 * 2874397: Remove flag to disable microtasks scope consistency checks https://chromium-review.googlesource.com/c/v8/v8/+/2874397 * 2881471: Remove unneeded trace_event.h includes in headers. https://chromium-review.googlesource.com/c/chromium/src/+/2881471 * 2844717: [Keyboard Tooltip] Rename RWHV*::SetTooltipText to UpdateTooltipUnderCursor https://chromium-review.googlesource.com/c/chromium/src/+/2844717 * chore: bump chromium in DEPS to 92.0.4505.0 * chore: update patches * 2883887: Retire ScopedObserver in /chrome/browser/predictors. https://chromium-review.googlesource.com/c/chromium/src/+/2883887 * 2883694: Retire ScopedObserver in /chrome/browser. https://chromium-review.googlesource.com/c/chromium/src/+/2883694 * fixup after merge * fixup: Remove flag to disable microtasks scope consistency checks * Temporarily disable setcallhandler-test.js nan test This test should be renabled once https://github.com/electron/electron/pull/29028 lands * Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope * chore: bump chromium in DEPS to 92.0.4506.0 * update patches * Revert "update patches" This reverts commit 333ec0d4c205bd3cbee28d2bc3d068871dbb900a. * Revert "chore: bump chromium in DEPS to 92.0.4506.0" This reverts commit 2bd52f8cd89b173c8b15a61d74fa7539cdbf574b. * Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope * Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope Co-authored-by: Jeremy Rose <nornagon@nornagon.net> Co-authored-by: Jeremy Rose <jeremya@chromium.org> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
2021-05-14 01:21:36 +00:00
auto* isolate = worker_context->GetIsolate();
v8::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
2017-03-07 10:59:48 +00:00
2017-03-10 06:22:25 +00:00
// Start the embed thread.
node_bindings_->PrepareEmbedThread();
// Setup node tracing controller.
if (!node::tracing::TraceEventHelper::GetAgent())
node::tracing::TraceEventHelper::SetAgent(node::CreateAgent());
// Setup node environment for each window.
bool initialized = node::InitializeContext(worker_context);
CHECK(initialized);
node::Environment* env =
node_bindings_->CreateEnvironment(worker_context, nullptr);
// Add Electron extended APIs.
electron_bindings_->BindTo(env->isolate(), env->process_object());
// Load everything.
node_bindings_->LoadEnvironment(env);
// Make uv loop being wrapped by window context.
node_bindings_->set_uv_env(env);
// Give the node loop a run to make sure everything is ready.
node_bindings_->StartPolling();
}
void WebWorkerObserver::ContextWillDestroy(v8::Local<v8::Context> context) {
node::Environment* env = node::Environment::GetCurrent(context);
if (env)
gin_helper::EmitEvent(env->isolate(), env->process_object(), "exit");
delete this;
}
} // namespace electron