From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- dom/ipc/JSValidatorParent.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 dom/ipc/JSValidatorParent.cpp (limited to 'dom/ipc/JSValidatorParent.cpp') diff --git a/dom/ipc/JSValidatorParent.cpp b/dom/ipc/JSValidatorParent.cpp new file mode 100644 index 0000000000..45b6d9de4c --- /dev/null +++ b/dom/ipc/JSValidatorParent.cpp @@ -0,0 +1,99 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/net/OpaqueResponseUtils.h" +#include "mozilla/dom/JSValidatorParent.h" +#include "mozilla/dom/JSValidatorUtils.h" +#include "mozilla/dom/JSOracleParent.h" +#include "mozilla/RefPtr.h" +#include "nsCOMPtr.h" +#include "HttpBaseChannel.h" + +namespace mozilla::dom { +/* static */ +already_AddRefed JSValidatorParent::Create() { + RefPtr validator = new JSValidatorParent(); + JSOracleParent::WithJSOracle([validator](JSOracleParent* aParent) { + MOZ_ASSERT_IF(aParent, aParent->CanSend()); + if (aParent) { + MOZ_ALWAYS_TRUE(aParent->SendPJSValidatorConstructor(validator)); + } + }); + return validator.forget(); +} + +void JSValidatorParent::IsOpaqueResponseAllowed( + const std::function, ValidatorResult)>& aCallback) { + JSOracleParent::WithJSOracle([=, self = RefPtr{this}](const auto* aParent) { + if (aParent) { + MOZ_DIAGNOSTIC_ASSERT(self->CanSend()); + self->SendIsOpaqueResponseAllowed()->Then( + GetMainThreadSerialEventTarget(), __func__, + [aCallback]( + const IsOpaqueResponseAllowedPromise::ResolveOrRejectValue& + aResult) { + if (aResult.IsResolve()) { + auto [data, result] = aResult.ResolveValue(); + aCallback(std::move(data), result); + } else { + // For cases like the Utility Process crashes, the promise will be + // rejected due to sending failures, and we'll block the request + // since we can't validate it. + aCallback(Nothing(), ValidatorResult::Failure); + } + }); + } else { + aCallback(Nothing(), ValidatorResult::Failure); + } + }); +} + +void JSValidatorParent::OnDataAvailable(const nsACString& aData) { + JSOracleParent::WithJSOracle( + [self = RefPtr{this}, data = nsCString{aData}](const auto* aParent) { + if (!aParent) { + return; + } + + if (self->CanSend()) { + Shmem sharedData; + nsresult rv = + JSValidatorUtils::CopyCStringToShmem(self, data, sharedData); + if (NS_FAILED(rv)) { + return; + } + Unused << self->SendOnDataAvailable(std::move(sharedData)); + } + }); +} + +void JSValidatorParent::OnStopRequest(nsresult aResult, nsIRequest& aRequest) { + JSOracleParent::WithJSOracle( + [self = RefPtr{this}, aResult, + request = nsCOMPtr{&aRequest}](const auto* aParent) { + if (!aParent) { + return; + } + if (self->CanSend() && request) { + nsCOMPtr httpBaseChannel = + do_QueryInterface(request); + MOZ_ASSERT(httpBaseChannel); + + nsAutoCString contentCharset; + Unused << httpBaseChannel->GetContentCharset(contentCharset); + + nsAutoString hintCharset; + Unused << httpBaseChannel->GetClassicScriptHintCharset(hintCharset); + + nsAutoString documentCharset; + Unused << httpBaseChannel->GetDocumentCharacterSet(documentCharset); + + Unused << self->SendOnStopRequest(aResult, contentCharset, + hintCharset, documentCharset); + } + }); +} +} // namespace mozilla::dom -- cgit v1.2.3