web_worker_observer.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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() = default;
  34. void WebWorkerObserver::WorkerScriptReadyForEvaluation(
  35. v8::Local<v8::Context> worker_context) {
  36. v8::Context::Scope context_scope(worker_context);
  37. auto* isolate = worker_context->GetIsolate();
  38. v8::MicrotasksScope microtasks_scope(
  39. isolate, worker_context->GetMicrotaskQueue(),
  40. v8::MicrotasksScope::kDoNotRunMicrotasks);
  41. // Start the embed thread.
  42. node_bindings_->PrepareEmbedThread();
  43. // Setup node tracing controller.
  44. if (!node::tracing::TraceEventHelper::GetAgent())
  45. node::tracing::TraceEventHelper::SetAgent(node::CreateAgent());
  46. // Setup node environment for each window.
  47. v8::Maybe<bool> initialized = node::InitializeContext(worker_context);
  48. CHECK(!initialized.IsNothing() && initialized.FromJust());
  49. std::shared_ptr<node::Environment> env =
  50. node_bindings_->CreateEnvironment(worker_context, nullptr);
  51. // We need to use the Blink implementation of fetch in web workers
  52. // Node.js deletes the global fetch function when their fetch implementation
  53. // is disabled, so we need to save and re-add it after the Node.js environment
  54. // is loaded. See corresponding change in node/init.ts.
  55. v8::Local<v8::Object> global = worker_context->Global();
  56. std::vector<std::string> keys = {"fetch", "Response", "FormData",
  57. "Request", "Headers", "EventSource"};
  58. for (const auto& key : keys) {
  59. v8::MaybeLocal<v8::Value> value =
  60. global->Get(worker_context, gin::StringToV8(isolate, key.c_str()));
  61. if (!value.IsEmpty()) {
  62. std::string blink_key = "blink" + key;
  63. global
  64. ->Set(worker_context, gin::StringToV8(isolate, blink_key.c_str()),
  65. value.ToLocalChecked())
  66. .Check();
  67. }
  68. }
  69. // Add Electron extended APIs.
  70. electron_bindings_->BindTo(env->isolate(), env->process_object());
  71. // Load everything.
  72. node_bindings_->LoadEnvironment(env.get());
  73. // Make uv loop being wrapped by window context.
  74. node_bindings_->set_uv_env(env.get());
  75. // Give the node loop a run to make sure everything is ready.
  76. node_bindings_->StartPolling();
  77. // Keep the environment alive until we free it in ContextWillDestroy()
  78. environments_.insert(std::move(env));
  79. }
  80. void WebWorkerObserver::ContextWillDestroy(v8::Local<v8::Context> context) {
  81. node::Environment* env = node::Environment::GetCurrent(context);
  82. if (env) {
  83. v8::Context::Scope context_scope(env->context());
  84. gin_helper::EmitEvent(env->isolate(), env->process_object(), "exit");
  85. }
  86. // Destroying the node environment will also run the uv loop,
  87. // Node.js expects `kExplicit` microtasks policy and will run microtasks
  88. // checkpoints after every call into JavaScript. Since we use a different
  89. // policy in the renderer - switch to `kExplicit`
  90. v8::MicrotaskQueue* microtask_queue = context->GetMicrotaskQueue();
  91. auto old_policy = microtask_queue->microtasks_policy();
  92. DCHECK_EQ(microtask_queue->GetMicrotasksScopeDepth(), 0);
  93. microtask_queue->set_microtasks_policy(v8::MicrotasksPolicy::kExplicit);
  94. base::EraseIf(environments_,
  95. [env](auto const& item) { return item.get() == env; });
  96. microtask_queue->set_microtasks_policy(old_policy);
  97. // ElectronBindings is tracking node environments.
  98. electron_bindings_->EnvironmentDestroyed(env);
  99. if (lazy_tls->Get())
  100. lazy_tls->Set(nullptr);
  101. }
  102. } // namespace electron