web_worker_observer.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/renderer/web_worker_observer.h"
  5. #include <utility>
  6. #include "base/no_destructor.h"
  7. #include "base/threading/thread_local.h"
  8. #include "shell/common/api/electron_bindings.h"
  9. #include "shell/common/gin_helper/event_emitter_caller.h"
  10. #include "shell/common/node_bindings.h"
  11. #include "shell/common/node_includes.h"
  12. namespace electron {
  13. namespace {
  14. static base::NoDestructor<base::ThreadLocalOwnedPointer<WebWorkerObserver>>
  15. lazy_tls;
  16. } // namespace
  17. // static
  18. WebWorkerObserver* WebWorkerObserver::GetCurrent() {
  19. return lazy_tls->Get();
  20. }
  21. // static
  22. WebWorkerObserver* WebWorkerObserver::Create() {
  23. auto obs = std::make_unique<WebWorkerObserver>();
  24. auto* obs_raw = obs.get();
  25. lazy_tls->Set(std::move(obs));
  26. return obs_raw;
  27. }
  28. WebWorkerObserver::WebWorkerObserver()
  29. : node_bindings_(
  30. NodeBindings::Create(NodeBindings::BrowserEnvironment::kWorker)),
  31. electron_bindings_(
  32. std::make_unique<ElectronBindings>(node_bindings_->uv_loop())) {}
  33. WebWorkerObserver::~WebWorkerObserver() {
  34. // Destroying the node environment will also run the uv loop,
  35. // Node.js expects `kExplicit` microtasks policy and will run microtasks
  36. // checkpoints after every call into JavaScript. Since we use a different
  37. // policy in the renderer - switch to `kExplicit`
  38. v8::MicrotaskQueue* microtask_queue =
  39. node_bindings_->uv_env()->context()->GetMicrotaskQueue();
  40. auto old_policy = microtask_queue->microtasks_policy();
  41. DCHECK_EQ(microtask_queue->GetMicrotasksScopeDepth(), 0);
  42. microtask_queue->set_microtasks_policy(v8::MicrotasksPolicy::kExplicit);
  43. node::FreeEnvironment(node_bindings_->uv_env());
  44. node::FreeIsolateData(node_bindings_->isolate_data());
  45. microtask_queue->set_microtasks_policy(old_policy);
  46. }
  47. void WebWorkerObserver::WorkerScriptReadyForEvaluation(
  48. v8::Local<v8::Context> worker_context) {
  49. v8::Context::Scope context_scope(worker_context);
  50. auto* isolate = worker_context->GetIsolate();
  51. v8::MicrotasksScope microtasks_scope(
  52. isolate, worker_context->GetMicrotaskQueue(),
  53. v8::MicrotasksScope::kDoNotRunMicrotasks);
  54. // Start the embed thread.
  55. node_bindings_->PrepareEmbedThread();
  56. // Setup node tracing controller.
  57. if (!node::tracing::TraceEventHelper::GetAgent())
  58. node::tracing::TraceEventHelper::SetAgent(node::CreateAgent());
  59. // Setup node environment for each window.
  60. v8::Maybe<bool> initialized = node::InitializeContext(worker_context);
  61. CHECK(!initialized.IsNothing() && initialized.FromJust());
  62. node::Environment* env =
  63. node_bindings_->CreateEnvironment(worker_context, nullptr);
  64. // Add Electron extended APIs.
  65. electron_bindings_->BindTo(env->isolate(), env->process_object());
  66. // Load everything.
  67. node_bindings_->LoadEnvironment(env);
  68. // Make uv loop being wrapped by window context.
  69. node_bindings_->set_uv_env(env);
  70. // Give the node loop a run to make sure everything is ready.
  71. node_bindings_->StartPolling();
  72. }
  73. void WebWorkerObserver::ContextWillDestroy(v8::Local<v8::Context> context) {
  74. node::Environment* env = node::Environment::GetCurrent(context);
  75. if (env)
  76. gin_helper::EmitEvent(env->isolate(), env->process_object(), "exit");
  77. if (lazy_tls->Get())
  78. lazy_tls->Set(nullptr);
  79. }
  80. } // namespace electron