Repository: electron/electron
File: shell/browser/api/electron_api_utility_process.cc (C++)

Code before:
    url_loader_network_observer_.emplace();
    loader_params->url_loader_network_observer =
        url_loader_network_observer_->Bind();
  }

  network::mojom::NetworkContext* network_context =
      g_browser_process->system_network_context_manager()->GetContext();
  network_context->CreateURLLoaderFactory(
      url_loader_factory.InitWithNewPipeAndPassReceiver(),
      std::move(loader_params));
  params->url_loader_factory = std::move(url_loader_factory);
  mojo::PendingRemote<network::mojom::HostResolver> host_resolver;
  network_context->CreateHostResolver(
      {}, host_resolver.InitWithNewPipeAndPassReceiver());
  params->host_resolver = std::move(host_resolver);
  params->use_network_observer_from_url_loader_factory =
      create_network_observer;

  node_service_remote_->Initialize(std::move(params),
                                   receiver_.BindNewPipeAndPassRemote());

  // Subscribe to Network Service process gone notifications.
  network_service_gone_subscription_ =
      content::RegisterNetworkServiceProcessGoneHandler(
          base::BindRepeating(
              &UtilityProcessWrapper::OnNetworkServiceProcessGone,
              weak_factory_.GetWeakPtr()));
}

UtilityProcessWrapper::~UtilityProcessWrapper() {
  content::ServiceProcessHost::RemoveObserver(this);
}

void UtilityProcessWrapper::OnServiceProcessLaunch(
    const base::Process& process) {
  DCHECK(node_service_remote_.is_connected());
  pid_ = process.Pid();
  GetAllUtilityProcessWrappers().AddWithID(this, pid_);
  if (stdout_read_fd_ != -1)
    EmitWithoutEvent("stdout", stdout_read_fd_);
  if (stderr_read_fd_ != -1)
    EmitWithoutEvent("stderr", stderr_read_fd_);
  if (url_loader_network_observer_.has_value()) {
    url_loader_network_observer_->set_process_id(pid_);
  }
  EmitWithoutEvent("spawn");
}

void UtilityProcessWrapper::HandleTermination(uint64_t exit_code) {
  // HandleTermination is called from multiple callsites,
  // we need to ensure we only process it for the first callsite.

Review comment (refactor):
Can this instead be `&UtilityProcessWrapper::CreateAndSendURLLoaderFactory` ? Can it run in the same sequence or we need the indirection via `UtilityProcessWrapper::OnNetworkServiceProcessGone` ?

Code after:
}

UtilityProcessWrapper::~UtilityProcessWrapper() {
  content::ServiceProcessHost::RemoveObserver(this);
}

void UtilityProcessWrapper::OnServiceProcessLaunch(
    const base::Process& process) {
  DCHECK(node_service_remote_.is_connected());
  pid_ = process.Pid();
  GetAllUtilityProcessWrappers().AddWithID(this, pid_);
  if (stdout_read_fd_ != -1)
    EmitWithoutEvent("stdout", stdout_read_fd_);
  if (stderr_read_fd_ != -1)
    EmitWithoutEvent("stderr", stderr_read_fd_);
  if (url_loader_network_observer_.has_value()) {
    url_loader_network_observer_->set_process_id(pid_);
  }
  EmitWithoutEvent("spawn");
}

void UtilityProcessWrapper::HandleTermination(uint64_t exit_code) {
  // HandleTermination is called from multiple callsites,
  // we need to ensure we only process it for the first callsite.
  if (terminated_)
    return;
  terminated_ = true;

  if (pid_ != base::kNullProcessId)
    GetAllUtilityProcessWrappers().Remove(pid_);

  pid_ = base::kNullProcessId;
  CloseConnectorPort();
  if (killed_) {
#if BUILDFLAG(IS_POSIX)
    // UtilityProcessWrapper::Kill relies on base::Process::Terminate
    // to gracefully shutdown the process which is performed by sending
    // SIGTERM signal. When listening for exit events via ServiceProcessHost
    // observers, the exit code on posix is obtained via
    // BrowserChildProcessHostImpl::GetTerminationInfo which inturn relies
    // on waitpid to extract the exit signal. If the process is unavailable,
    // then the exit_code will be set to 0, otherwise we get the signal that
    // was sent during the base::Process::Terminate call. For a user, this is
    // still a graceful shutdown case so lets' convert the exit code to the
    // expected value.
    if (exit_code == SIGTERM || exit_code == SIGKILL) {
      exit_code = 0;
    }
#endif
  }
  EmitWithoutEvent("exit", exit_code);