tor-browser

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

AltServiceChild.cpp (3194B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
      2 /* vim:set ts=4 sw=4 sts=4 et cin: */
      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 // HttpLog.h should generally be included first
      8 #include "HttpLog.h"
      9 
     10 #include "AltServiceChild.h"
     11 #include "mozilla/ClearOnShutdown.h"
     12 #include "mozilla/StaticPtr.h"
     13 #include "mozilla/net/SocketProcessChild.h"
     14 #include "nsHttpConnectionInfo.h"
     15 #include "nsProxyInfo.h"
     16 #include "nsThreadUtils.h"
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 StaticRefPtr<AltServiceChild> sAltServiceChild;
     22 
     23 AltServiceChild::AltServiceChild() {
     24  LOG(("AltServiceChild ctor [%p]\n", this));
     25 }
     26 
     27 AltServiceChild::~AltServiceChild() {
     28  LOG(("AltServiceChild dtor [%p]\n", this));
     29 }
     30 
     31 // static
     32 bool AltServiceChild::EnsureAltServiceChild() {
     33  MOZ_ASSERT(XRE_IsSocketProcess());
     34  MOZ_ASSERT(NS_IsMainThread());
     35 
     36  if (sAltServiceChild) {
     37    return true;
     38  }
     39 
     40  SocketProcessChild* socketChild = SocketProcessChild::GetSingleton();
     41  if (!socketChild || socketChild->IsShuttingDown()) {
     42    return false;
     43  }
     44 
     45  sAltServiceChild = new AltServiceChild();
     46  ClearOnShutdown(&sAltServiceChild);
     47 
     48  if (!socketChild->SendPAltServiceConstructor(sAltServiceChild)) {
     49    sAltServiceChild = nullptr;
     50    return false;
     51  }
     52 
     53  return true;
     54 }
     55 
     56 // static
     57 void AltServiceChild::ClearHostMapping(nsHttpConnectionInfo* aCi) {
     58  LOG(("AltServiceChild::ClearHostMapping ci=%s", aCi->HashKey().get()));
     59  MOZ_ASSERT(aCi);
     60 
     61  RefPtr<nsHttpConnectionInfo> ci = aCi->Clone();
     62  auto task = [ci{std::move(ci)}]() {
     63    if (!EnsureAltServiceChild()) {
     64      return;
     65    }
     66 
     67    if (!ci->GetOrigin().IsEmpty() && sAltServiceChild->CanSend()) {
     68      (void)sAltServiceChild->SendClearHostMapping(
     69          ci->GetOrigin(), ci->OriginPort(), ci->GetOriginAttributes());
     70    }
     71  };
     72 
     73  if (!NS_IsMainThread()) {
     74    MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(NS_NewRunnableFunction(
     75        "net::AltServiceChild::ClearHostMapping", task)));
     76    return;
     77  }
     78 
     79  task();
     80 }
     81 
     82 // static
     83 void AltServiceChild::ProcessHeader(
     84    const nsCString& aBuf, const nsCString& aOriginScheme,
     85    const nsCString& aOriginHost, int32_t aOriginPort,
     86    const nsCString& aUsername, bool aPrivateBrowsing,
     87    nsIInterfaceRequestor* aCallbacks, nsProxyInfo* aProxyInfo, uint32_t aCaps,
     88    const OriginAttributes& aOriginAttributes,
     89    nsHttpConnectionInfo* aConnInfo) {
     90  LOG(("AltServiceChild::ProcessHeader"));
     91  MOZ_ASSERT(NS_IsMainThread());
     92 
     93  if (!EnsureAltServiceChild()) {
     94    return;
     95  }
     96 
     97  if (!sAltServiceChild->CanSend()) {
     98    return;
     99  }
    100 
    101  nsTArray<ProxyInfoCloneArgs> proxyInfoArray;
    102  if (aProxyInfo) {
    103    nsProxyInfo::SerializeProxyInfo(aProxyInfo, proxyInfoArray);
    104  }
    105 
    106  HttpConnectionInfoCloneArgs infoArgs;
    107  nsHttpConnectionInfo::SerializeHttpConnectionInfo(aConnInfo, infoArgs);
    108 
    109  (void)sAltServiceChild->SendProcessHeader(
    110      aBuf, aOriginScheme, aOriginHost, aOriginPort, aUsername,
    111      aPrivateBrowsing, proxyInfoArray, aCaps, aOriginAttributes, infoArgs);
    112 }
    113 
    114 }  // namespace net
    115 }  // namespace mozilla