tor-browser

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

DNSUtils.cpp (2567B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=4 sw=2 sts=2 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 #include "DNSUtils.h"
      8 
      9 #include "nsContentUtils.h"
     10 #include "nsHttpHandler.h"
     11 #include "nsIHttpChannel.h"
     12 #include "nsIHttpChannelInternal.h"
     13 #include "nsIIOService.h"
     14 #include "mozilla/SyncRunnable.h"
     15 #include "TRRServiceChannel.h"
     16 #include "TRRLoadInfo.h"
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 static void InitHttpHandler() {
     22  nsresult rv;
     23  nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
     24  if (NS_FAILED(rv)) {
     25    return;
     26  }
     27 
     28  nsCOMPtr<nsIProtocolHandler> handler;
     29  rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
     30  if (NS_FAILED(rv)) {
     31    return;
     32  }
     33 }
     34 
     35 // static
     36 nsresult DNSUtils::CreateChannelHelper(nsIURI* aUri, nsIChannel** aResult) {
     37  *aResult = nullptr;
     38 
     39  if (NS_IsMainThread() && !XRE_IsSocketProcess()) {
     40    nsresult rv;
     41    nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
     42    NS_ENSURE_SUCCESS(rv, rv);
     43 
     44    return NS_NewChannel(
     45        aResult, aUri, nsContentUtils::GetSystemPrincipal(),
     46        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
     47        nsIContentPolicy::TYPE_OTHER,
     48        nullptr,  // nsICookieJarSettings
     49        nullptr,  // PerformanceStorage
     50        nullptr,  // aLoadGroup
     51        nullptr,  // aCallbacks
     52        nsIRequest::LOAD_NORMAL, ios);
     53  }
     54 
     55  // Unfortunately, we can only initialize gHttpHandler on main thread.
     56  if (!gHttpHandler) {
     57    nsCOMPtr<nsIEventTarget> main = GetMainThreadSerialEventTarget();
     58    if (main) {
     59      // Forward to the main thread synchronously.
     60      SyncRunnable::DispatchToThread(
     61          main, NS_NewRunnableFunction("InitHttpHandler",
     62                                       []() { InitHttpHandler(); }));
     63    }
     64  }
     65 
     66  if (!gHttpHandler) {
     67    return NS_ERROR_UNEXPECTED;
     68  }
     69 
     70  RefPtr<TRRLoadInfo> loadInfo =
     71      new TRRLoadInfo(aUri, nsIContentPolicy::TYPE_OTHER);
     72  return gHttpHandler->CreateTRRServiceChannel(aUri,
     73                                               nullptr,   // givenProxyInfo
     74                                               0,         // proxyResolveFlags
     75                                               nullptr,   // proxyURI
     76                                               loadInfo,  // aLoadInfo
     77                                               aResult);
     78 }
     79 
     80 }  // namespace net
     81 }  // namespace mozilla