Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / ipc / JSValidatorParent.cpp
blob45b6d9de4c0f4ff763a760911ebde2e63597d8b1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/net/OpaqueResponseUtils.h"
8 #include "mozilla/dom/JSValidatorParent.h"
9 #include "mozilla/dom/JSValidatorUtils.h"
10 #include "mozilla/dom/JSOracleParent.h"
11 #include "mozilla/RefPtr.h"
12 #include "nsCOMPtr.h"
13 #include "HttpBaseChannel.h"
15 namespace mozilla::dom {
16 /* static */
17 already_AddRefed<JSValidatorParent> JSValidatorParent::Create() {
18 RefPtr<JSValidatorParent> validator = new JSValidatorParent();
19 JSOracleParent::WithJSOracle([validator](JSOracleParent* aParent) {
20 MOZ_ASSERT_IF(aParent, aParent->CanSend());
21 if (aParent) {
22 MOZ_ALWAYS_TRUE(aParent->SendPJSValidatorConstructor(validator));
24 });
25 return validator.forget();
28 void JSValidatorParent::IsOpaqueResponseAllowed(
29 const std::function<void(Maybe<Shmem>, ValidatorResult)>& aCallback) {
30 JSOracleParent::WithJSOracle([=, self = RefPtr{this}](const auto* aParent) {
31 if (aParent) {
32 MOZ_DIAGNOSTIC_ASSERT(self->CanSend());
33 self->SendIsOpaqueResponseAllowed()->Then(
34 GetMainThreadSerialEventTarget(), __func__,
35 [aCallback](
36 const IsOpaqueResponseAllowedPromise::ResolveOrRejectValue&
37 aResult) {
38 if (aResult.IsResolve()) {
39 auto [data, result] = aResult.ResolveValue();
40 aCallback(std::move(data), result);
41 } else {
42 // For cases like the Utility Process crashes, the promise will be
43 // rejected due to sending failures, and we'll block the request
44 // since we can't validate it.
45 aCallback(Nothing(), ValidatorResult::Failure);
47 });
48 } else {
49 aCallback(Nothing(), ValidatorResult::Failure);
51 });
54 void JSValidatorParent::OnDataAvailable(const nsACString& aData) {
55 JSOracleParent::WithJSOracle(
56 [self = RefPtr{this}, data = nsCString{aData}](const auto* aParent) {
57 if (!aParent) {
58 return;
61 if (self->CanSend()) {
62 Shmem sharedData;
63 nsresult rv =
64 JSValidatorUtils::CopyCStringToShmem(self, data, sharedData);
65 if (NS_FAILED(rv)) {
66 return;
68 Unused << self->SendOnDataAvailable(std::move(sharedData));
70 });
73 void JSValidatorParent::OnStopRequest(nsresult aResult, nsIRequest& aRequest) {
74 JSOracleParent::WithJSOracle(
75 [self = RefPtr{this}, aResult,
76 request = nsCOMPtr{&aRequest}](const auto* aParent) {
77 if (!aParent) {
78 return;
80 if (self->CanSend() && request) {
81 nsCOMPtr<net::HttpBaseChannel> httpBaseChannel =
82 do_QueryInterface(request);
83 MOZ_ASSERT(httpBaseChannel);
85 nsAutoCString contentCharset;
86 Unused << httpBaseChannel->GetContentCharset(contentCharset);
88 nsAutoString hintCharset;
89 Unused << httpBaseChannel->GetClassicScriptHintCharset(hintCharset);
91 nsAutoString documentCharset;
92 Unused << httpBaseChannel->GetDocumentCharacterSet(documentCharset);
94 Unused << self->SendOnStopRequest(aResult, contentCharset,
95 hintCharset, documentCharset);
97 });
99 } // namespace mozilla::dom