Repository: electron/electron
File: shell/renderer/electron_renderer_client.cc (C++)

Code before:
    if (current)
      current->ContextWillDestroy(context);
  }
}

void ElectronRendererClient::SetUpWebAssemblyTrapHandler() {
  // Freezing flags after init conflicts with node in the renderer.
  // We do this here in order to avoid having to patch the ctor in
  // content/renderer/render_process_impl.cc.
  v8::V8::SetFlagsFromString("--no-freeze-flags-after-init");

// See CL:5372409 - copied from ShellContentRendererClient.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  // Mac and Windows use the default implementation (where the default v8 trap
  // handler gets set up).
  ContentRendererClient::SetUpWebAssemblyTrapHandler();
  return;
#else
  base::CommandLine* const command_line =
      base::CommandLine::ForCurrentProcess();
  const bool crash_reporter_enabled =
      command_line->HasSwitch(::switches::kEnableCrashReporter)
#if BUILDFLAG(IS_POSIX)
      || command_line->HasSwitch(::switches::kEnableCrashReporterForTesting)
#endif  // BUILDFLAG(IS_POSIX)
      ;

  if (crash_reporter_enabled) {
    // If either --enable-crash-reporter or --enable-crash-reporter-for-testing
    // is enabled it should take care of signal handling for us, use the default
    // implementation which doesn't register an additional handler.
    ContentRendererClient::SetUpWebAssemblyTrapHandler();
    return;
  }

  const bool use_v8_default_handler =
#if defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
      base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableInProcessStackTraces)
#else
      true
#endif  // defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
      ;

  if (use_v8_default_handler) {
    // There is no signal handler yet, but it's okay if v8 registers one.
    v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/true);
    return;
  }

#if defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)

Review comment (bug):
We always append `kEnableCrashReporter` https://github.com/electron/electron/blob/7112c592c9ce0f0e55ee23ca8415b2f14f62a56c/shell/browser/electron_browser_client.cc#L512-L521 on linux but the actual presence of crashpad is determined by an application calling `crashReporter.start()`. A better way to confirm this would be to check for the presence of valid socket to the crashpad handler process.

```suggestion
  const bool crash_reporter_enabled =
      crash_reporter::GetHandlerSocket(nullptr, nullptr);
```

Code after:
  }
}

void ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread(
    v8::Local<v8::Context> context) {
  auto* ec = blink::ExecutionContext::From(context);
  if (ec->IsServiceWorkerGlobalScope() || ec->IsSharedWorkerGlobalScope() ||
      ec->IsMainThreadWorkletGlobalScope())
    return;

  // TODO(loc): Note that this will not be correct for in-process child windows
  // with webPreferences that have a different value for nodeIntegrationInWorker
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kNodeIntegrationInWorker)) {
    auto* current = WebWorkerObserver::GetCurrent();
    if (current)
      current->ContextWillDestroy(context);
  }
}

void ElectronRendererClient::SetUpWebAssemblyTrapHandler() {
// See CL:5372409 - copied from ShellContentRendererClient.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  // Mac and Windows use the default implementation (where the default v8 trap
  // handler gets set up).
  ContentRendererClient::SetUpWebAssemblyTrapHandler();
  return;
#elif defined(ENABLE_WEB_ASSEMBLY_TRAP_HANDLER_LINUX)
  const bool crash_reporter_enabled =
      crash_reporter::GetHandlerSocket(nullptr, nullptr);

  if (crash_reporter_enabled) {
    // If either --enable-crash-reporter or --enable-crash-reporter-for-testing
    // is enabled it should take care of signal handling for us, use the default
    // implementation which doesn't register an additional handler.
    ContentRendererClient::SetUpWebAssemblyTrapHandler();
    return;
  }

  const bool use_v8_default_handler =
      base::CommandLine::ForCurrentProcess()->HasSwitch(
          ::switches::kDisableInProcessStackTraces);

  if (use_v8_default_handler) {
    // There is no signal handler yet, but it's okay if v8 registers one.
    v8::V8::EnableWebAssemblyTrapHandler(/*use_v8_signal_handler=*/true);
    return;
  }

  if (base::debug::SetStackDumpFirstChanceCallback(
          v8::TryHandleWebAssemblyTrapPosix)) {