web_worker_observer.cc 4.6 KB

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