tor-browser

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

ProxyConfigLookup.cpp (3412B)


      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 "ProxyConfigLookup.h"
      8 #include "ProxyConfigLookupChild.h"
      9 #include "mozilla/Components.h"
     10 #include "nsContentUtils.h"
     11 #include "nsICancelable.h"
     12 #include "nsIProtocolProxyService.h"
     13 #include "nsIProtocolProxyService2.h"
     14 #include "nsNetUtil.h"
     15 #include "nsThreadUtils.h"
     16 #include "nsIChannel.h"
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 // static
     22 nsresult ProxyConfigLookup::Create(
     23    std::function<void(nsIProxyInfo*, nsresult)>&& aCallback, nsIURI* aURI,
     24    uint32_t aProxyResolveFlags, nsICancelable** aLookupCancellable) {
     25  MOZ_ASSERT(NS_IsMainThread());
     26 
     27  RefPtr<ProxyConfigLookup> lookUp =
     28      new ProxyConfigLookup(std::move(aCallback), aURI, aProxyResolveFlags);
     29  return lookUp->DoProxyResolve(aLookupCancellable);
     30 }
     31 
     32 ProxyConfigLookup::ProxyConfigLookup(
     33    std::function<void(nsIProxyInfo*, nsresult)>&& aCallback, nsIURI* aURI,
     34    uint32_t aProxyResolveFlags)
     35    : mCallback(std::move(aCallback)),
     36      mURI(aURI),
     37      mProxyResolveFlags(aProxyResolveFlags) {}
     38 
     39 ProxyConfigLookup::~ProxyConfigLookup() = default;
     40 
     41 nsresult ProxyConfigLookup::DoProxyResolve(nsICancelable** aLookupCancellable) {
     42  if (!XRE_IsParentProcess()) {
     43    RefPtr<ProxyConfigLookup> self = this;
     44    bool result = ProxyConfigLookupChild::Create(
     45        mURI, mProxyResolveFlags,
     46        [self](nsIProxyInfo* aProxyinfo, nsresult aResult) {
     47          self->OnProxyAvailable(nullptr, nullptr, aProxyinfo, aResult);
     48        });
     49    return result ? NS_OK : NS_ERROR_FAILURE;
     50  }
     51 
     52  nsresult rv;
     53  nsCOMPtr<nsIChannel> channel;
     54  rv = NS_NewChannel(getter_AddRefs(channel), mURI,
     55                     nsContentUtils::GetSystemPrincipal(),
     56                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
     57                     nsIContentPolicy::TYPE_OTHER);
     58  if (NS_FAILED(rv)) {
     59    return rv;
     60  }
     61 
     62  nsCOMPtr<nsIProtocolProxyService> pps;
     63  pps = mozilla::components::ProtocolProxy::Service(&rv);
     64  if (NS_FAILED(rv)) {
     65    return rv;
     66  }
     67 
     68  // using the nsIProtocolProxyService2 allows a minor performance
     69  // optimization, but if an add-on has only provided the original interface
     70  // then it is ok to use that version.
     71  nsCOMPtr<nsICancelable> proxyRequest;
     72  nsCOMPtr<nsIProtocolProxyService2> pps2 = do_QueryInterface(pps);
     73  if (pps2) {
     74    rv = pps2->AsyncResolve2(channel, mProxyResolveFlags, this, nullptr,
     75                             getter_AddRefs(proxyRequest));
     76  } else {
     77    rv = pps->AsyncResolve(channel, mProxyResolveFlags, this, nullptr,
     78                           getter_AddRefs(proxyRequest));
     79  }
     80 
     81  if (aLookupCancellable) {
     82    proxyRequest.forget(aLookupCancellable);
     83  }
     84 
     85  return rv;
     86 }
     87 
     88 NS_IMETHODIMP ProxyConfigLookup::OnProxyAvailable(nsICancelable* aRequest,
     89                                                  nsIChannel* aChannel,
     90                                                  nsIProxyInfo* aProxyinfo,
     91                                                  nsresult aResult) {
     92  mCallback(aProxyinfo, aResult);
     93  return NS_OK;
     94 }
     95 
     96 NS_IMPL_ISUPPORTS(ProxyConfigLookup, nsIProtocolProxyCallback)
     97 
     98 }  // namespace net
     99 }  // namespace mozilla