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"
13 #include "HttpBaseChannel.h"
15 namespace mozilla::dom
{
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());
22 MOZ_ALWAYS_TRUE(aParent
->SendPJSValidatorConstructor(validator
));
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
) {
32 MOZ_DIAGNOSTIC_ASSERT(self
->CanSend());
33 self
->SendIsOpaqueResponseAllowed()->Then(
34 GetMainThreadSerialEventTarget(), __func__
,
36 const IsOpaqueResponseAllowedPromise::ResolveOrRejectValue
&
38 if (aResult
.IsResolve()) {
39 auto [data
, result
] = aResult
.ResolveValue();
40 aCallback(std::move(data
), result
);
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
);
49 aCallback(Nothing(), ValidatorResult::Failure
);
54 void JSValidatorParent::OnDataAvailable(const nsACString
& aData
) {
55 JSOracleParent::WithJSOracle(
56 [self
= RefPtr
{this}, data
= nsCString
{aData
}](const auto* aParent
) {
61 if (self
->CanSend()) {
64 JSValidatorUtils::CopyCStringToShmem(self
, data
, sharedData
);
68 Unused
<< self
->SendOnDataAvailable(std::move(sharedData
));
73 void JSValidatorParent::OnStopRequest(nsresult aResult
, nsIRequest
& aRequest
) {
74 JSOracleParent::WithJSOracle(
75 [self
= RefPtr
{this}, aResult
,
76 request
= nsCOMPtr
{&aRequest
}](const auto* aParent
) {
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
);
99 } // namespace mozilla::dom