|
18 | 18 | #include "shell/common/node_bindings.h" |
19 | 19 | #include "shell/common/node_includes.h" |
20 | 20 | #include "shell/common/node_util.h" |
21 | | -#include "shell/common/options_switches.h" |
| 21 | +#include "shell/common/v8_util.h" |
22 | 22 | #include "shell/renderer/electron_render_frame_observer.h" |
23 | 23 | #include "shell/renderer/web_worker_observer.h" |
24 | 24 | #include "third_party/blink/public/common/web_preferences/web_preferences.h" |
25 | 25 | #include "third_party/blink/public/web/web_document.h" |
26 | 26 | #include "third_party/blink/public/web/web_local_frame.h" |
27 | 27 | #include "third_party/blink/renderer/core/execution_context/execution_context.h" // nogncheck |
28 | 28 | #include "third_party/blink/renderer/core/frame/web_local_frame_impl.h" // nogncheck |
| 29 | +#include "third_party/blink/renderer/core/workers/worker_global_scope.h" // nogncheck |
| 30 | +#include "third_party/blink/renderer/core/workers/worker_settings.h" // nogncheck |
29 | 31 |
|
30 | 32 | #if BUILDFLAG(IS_LINUX) && (defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64)) |
31 | 33 | #define ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX |
@@ -207,44 +209,54 @@ void ElectronRendererClient::WillReleaseScriptContext( |
207 | 209 | electron_bindings_->EnvironmentDestroyed(env); |
208 | 210 | } |
209 | 211 |
|
210 | | -void ElectronRendererClient::WorkerScriptReadyForEvaluationOnWorkerThread( |
211 | | - v8::Local<v8::Context> context) { |
| 212 | +namespace { |
| 213 | + |
| 214 | +bool WorkerHasNodeIntegration(blink::ExecutionContext* ec) { |
212 | 215 | // We do not create a Node.js environment in service or shared workers |
213 | 216 | // owing to an inability to customize sandbox policies in these workers |
214 | 217 | // given that they're run out-of-process. |
215 | 218 | // Also avoid creating a Node.js environment for worklet global scope |
216 | 219 | // created on the main thread. |
217 | | - auto* ec = blink::ExecutionContext::From(context); |
218 | 220 | if (ec->IsServiceWorkerGlobalScope() || ec->IsSharedWorkerGlobalScope() || |
219 | 221 | ec->IsMainThreadWorkletGlobalScope()) |
| 222 | + return false; |
| 223 | + |
| 224 | + auto* wgs = blink::DynamicTo<blink::WorkerGlobalScope>(ec); |
| 225 | + if (!wgs) |
| 226 | + return false; |
| 227 | + |
| 228 | + // Read the nodeIntegrationInWorker preference from the worker's settings, |
| 229 | + // which were copied from the initiating frame's WebPreferences at worker |
| 230 | + // creation time. This ensures that in-process child windows with different |
| 231 | + // webPreferences get the correct per-frame value rather than a process-wide |
| 232 | + // value. |
| 233 | + auto* worker_settings = wgs->GetWorkerSettings(); |
| 234 | + return worker_settings && worker_settings->NodeIntegrationInWorker(); |
| 235 | +} |
| 236 | + |
| 237 | +} // namespace |
| 238 | + |
| 239 | +void ElectronRendererClient::WorkerScriptReadyForEvaluationOnWorkerThread( |
| 240 | + v8::Local<v8::Context> context) { |
| 241 | + auto* ec = blink::ExecutionContext::From(context); |
| 242 | + if (!WorkerHasNodeIntegration(ec)) |
220 | 243 | return; |
221 | 244 |
|
222 | | - // This won't be correct for in-process child windows with webPreferences |
223 | | - // that have a different value for nodeIntegrationInWorker |
224 | | - if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
225 | | - switches::kNodeIntegrationInWorker)) { |
226 | | - auto* current = WebWorkerObserver::GetCurrent(); |
227 | | - if (current) |
228 | | - return; |
229 | | - WebWorkerObserver::Create()->WorkerScriptReadyForEvaluation(context); |
230 | | - } |
| 245 | + auto* current = WebWorkerObserver::GetCurrent(); |
| 246 | + if (current) |
| 247 | + return; |
| 248 | + WebWorkerObserver::Create()->WorkerScriptReadyForEvaluation(context); |
231 | 249 | } |
232 | 250 |
|
233 | 251 | void ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread( |
234 | 252 | v8::Local<v8::Context> context) { |
235 | 253 | auto* ec = blink::ExecutionContext::From(context); |
236 | | - if (ec->IsServiceWorkerGlobalScope() || ec->IsSharedWorkerGlobalScope() || |
237 | | - ec->IsMainThreadWorkletGlobalScope()) |
| 254 | + if (!WorkerHasNodeIntegration(ec)) |
238 | 255 | return; |
239 | 256 |
|
240 | | - // TODO(loc): Note that this will not be correct for in-process child windows |
241 | | - // with webPreferences that have a different value for nodeIntegrationInWorker |
242 | | - if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
243 | | - switches::kNodeIntegrationInWorker)) { |
244 | | - auto* current = WebWorkerObserver::GetCurrent(); |
245 | | - if (current) |
246 | | - current->ContextWillDestroy(context); |
247 | | - } |
| 257 | + auto* current = WebWorkerObserver::GetCurrent(); |
| 258 | + if (current) |
| 259 | + current->ContextWillDestroy(context); |
248 | 260 | } |
249 | 261 |
|
250 | 262 | void ElectronRendererClient::SetUpWebAssemblyTrapHandler() { |
|
0 commit comments