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

Code before:
template <typename... Args>
void WebRequest::HandleSimpleEvent(SimpleEvent event,
                                   extensions::WebRequestInfo* request_info,
                                   Args... args) {
  const auto iter = simple_listeners_.find(event);
  if (iter == std::end(simple_listeners_))
    return;

  const auto& info = iter->second;
  if (!info.filter.MatchesRequest(request_info))
    return;

  v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  v8::HandleScope handle_scope(isolate);
  gin_helper::Dictionary details(isolate, v8::Object::New(isolate));
  FillDetails(&details, request_info, args...);
  info.listener.Run(gin::ConvertToV8(isolate, details));
}

void WebRequest::OnLoginAuthResult(
    uint64_t id,
    net::AuthCredentials* credentials,
    const std::optional<net::AuthCredentials>& maybe_creds) {
  auto iter = blocked_requests_.find(id);
  if (iter == blocked_requests_.end())
    return;

  AuthRequiredResponse action =
      AuthRequiredResponse::AUTH_REQUIRED_RESPONSE_NO_ACTION;
  if (maybe_creds.has_value()) {
    *credentials = maybe_creds.value();
    action = AuthRequiredResponse::AUTH_REQUIRED_RESPONSE_SET_AUTH;
  }

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(iter->second.auth_callback), action));
  blocked_requests_.erase(iter);
}

// static
gin_helper::Handle<WebRequest> WebRequest::FromOrCreate(
    v8::Isolate* isolate,
    content::BrowserContext* browser_context) {
  gin_helper::Handle<WebRequest> handle = From(isolate, browser_context);
  if (handle.IsEmpty()) {
    // Make sure the |Session| object has the |webRequest| property created.
    v8::Local<v8::Value> web_request =
        Session::CreateFrom(
            isolate, static_cast<ElectronBrowserContext*>(browser_context))
            ->WebRequest(isolate);
    gin::ConvertFromV8(isolate, web_request, &handle);

Review comment (security):
Should we have `NOTREACHED` check in this case when the auth callback from `LoginHandler` got called but the request is no longer available ?

Code after:
  FillDetails(&details, request_info, args...);
  info.listener.Run(gin::ConvertToV8(isolate, details));
}

void WebRequest::OnLoginAuthResult(
    uint64_t id,
    net::AuthCredentials* credentials,
    const std::optional<net::AuthCredentials>& maybe_creds) {
  auto iter = blocked_requests_.find(id);
  if (iter == blocked_requests_.end())
    NOTREACHED();

  AuthRequiredResponse action =
      AuthRequiredResponse::AUTH_REQUIRED_RESPONSE_NO_ACTION;
  if (maybe_creds.has_value()) {
    *credentials = maybe_creds.value();
    action = AuthRequiredResponse::AUTH_REQUIRED_RESPONSE_SET_AUTH;
  }

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(iter->second.auth_callback), action));
  blocked_requests_.erase(iter);
}

// static
gin_helper::Handle<WebRequest> WebRequest::FromOrCreate(
    v8::Isolate* isolate,
    content::BrowserContext* browser_context) {
  v8::Local<v8::Value> web_request =
      Session::FromOrCreate(isolate, browser_context)->WebRequest(isolate);
  gin_helper::Handle<WebRequest> handle;
  gin::ConvertFromV8(isolate, web_request, &handle);
  DCHECK(!handle.IsEmpty());
  return handle;
}

// static
gin_helper::Handle<WebRequest> WebRequest::Create(
    base::PassKey<Session> passkey,
    v8::Isolate* isolate) {
  return gin_helper::CreateHandle(isolate, new WebRequest{std::move(passkey)});
}

}  // namespace electron::api