tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

JSValidatorParent.cpp (3552B)


      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/. */
      6 
      7 #include "mozilla/dom/JSValidatorParent.h"
      8 
      9 #include "HttpBaseChannel.h"
     10 #include "mozilla/RefPtr.h"
     11 #include "mozilla/dom/JSOracleParent.h"
     12 #include "mozilla/dom/JSValidatorUtils.h"
     13 #include "mozilla/net/OpaqueResponseUtils.h"
     14 #include "nsCOMPtr.h"
     15 
     16 namespace mozilla::dom {
     17 /* static */
     18 already_AddRefed<JSValidatorParent> JSValidatorParent::Create() {
     19  RefPtr<JSValidatorParent> validator = new JSValidatorParent();
     20  JSOracleParent::WithJSOracle([validator](JSOracleParent* aParent) {
     21    MOZ_ASSERT_IF(aParent, aParent->CanSend());
     22    if (aParent) {
     23      MOZ_ALWAYS_TRUE(aParent->SendPJSValidatorConstructor(validator));
     24    }
     25  });
     26  return validator.forget();
     27 }
     28 
     29 void JSValidatorParent::IsOpaqueResponseAllowed(
     30    const std::function<void(Maybe<Shmem>, ValidatorResult)>& aCallback) {
     31  JSOracleParent::WithJSOracle([=, self = RefPtr{this}](const auto* aParent) {
     32    if (aParent) {
     33      MOZ_DIAGNOSTIC_ASSERT(self->CanSend());
     34      self->SendIsOpaqueResponseAllowed()->Then(
     35          GetMainThreadSerialEventTarget(), __func__,
     36          [aCallback](
     37              const IsOpaqueResponseAllowedPromise::ResolveOrRejectValue&
     38                  aResult) {
     39            if (aResult.IsResolve()) {
     40              auto [data, result] = aResult.ResolveValue();
     41              aCallback(std::move(data), result);
     42            } else {
     43              // For cases like the Utility Process crashes, the promise will be
     44              // rejected due to sending failures, and we'll block the request
     45              // since we can't validate it.
     46              aCallback(Nothing(), ValidatorResult::Failure);
     47            }
     48          });
     49    } else {
     50      aCallback(Nothing(), ValidatorResult::Failure);
     51    }
     52  });
     53 }
     54 
     55 void JSValidatorParent::OnDataAvailable(const nsACString& aData) {
     56  JSOracleParent::WithJSOracle(
     57      [self = RefPtr{this}, data = nsCString{aData}](const auto* aParent) {
     58        if (!aParent) {
     59          return;
     60        }
     61 
     62        if (self->CanSend()) {
     63          Shmem sharedData;
     64          nsresult rv =
     65              JSValidatorUtils::CopyCStringToShmem(self, data, sharedData);
     66          if (NS_FAILED(rv)) {
     67            return;
     68          }
     69          (void)self->SendOnDataAvailable(std::move(sharedData));
     70        }
     71      });
     72 }
     73 
     74 void JSValidatorParent::OnStopRequest(nsresult aResult, nsIRequest& aRequest) {
     75  JSOracleParent::WithJSOracle(
     76      [self = RefPtr{this}, aResult,
     77       request = nsCOMPtr{&aRequest}](const auto* aParent) {
     78        if (!aParent) {
     79          return;
     80        }
     81        if (self->CanSend() && request) {
     82          nsCOMPtr<net::HttpBaseChannel> httpBaseChannel =
     83              do_QueryInterface(request);
     84          MOZ_ASSERT(httpBaseChannel);
     85 
     86          nsAutoCString contentCharset;
     87          (void)httpBaseChannel->GetContentCharset(contentCharset);
     88 
     89          nsAutoString hintCharset;
     90          (void)httpBaseChannel->GetClassicScriptHintCharset(hintCharset);
     91 
     92          nsAutoString documentCharset;
     93          (void)httpBaseChannel->GetDocumentCharacterSet(documentCharset);
     94 
     95          (void)self->SendOnStopRequest(aResult, contentCharset, hintCharset,
     96                                        documentCharset);
     97        }
     98      });
     99 }
    100 }  // namespace mozilla::dom