tor-browser

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

nsSocketProviderService.cpp (2551B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "nsString.h"
      7 #include "nsISocketProvider.h"
      8 #include "nsError.h"
      9 #include "nsNSSComponent.h"
     10 #include "nsSOCKSSocketProvider.h"
     11 #include "nsSocketProviderService.h"
     12 #include "nsSSLSocketProvider.h"
     13 #include "nsTLSSocketProvider.h"
     14 #include "nsUDPSocketProvider.h"
     15 #include "mozilla/ClearOnShutdown.h"
     16 #include "nsThreadUtils.h"
     17 #include "nsCRT.h"
     18 
     19 mozilla::StaticRefPtr<nsSocketProviderService>
     20    nsSocketProviderService::gSingleton;
     21 
     22 ////////////////////////////////////////////////////////////////////////////////
     23 
     24 already_AddRefed<nsISocketProviderService>
     25 nsSocketProviderService::GetOrCreate() {
     26  RefPtr<nsSocketProviderService> inst;
     27  if (gSingleton) {
     28    inst = gSingleton;
     29  } else {
     30    inst = new nsSocketProviderService();
     31    gSingleton = inst;
     32    if (NS_IsMainThread()) {
     33      mozilla::ClearOnShutdown(&gSingleton);
     34    } else {
     35      NS_DispatchToMainThread(NS_NewRunnableFunction(
     36          "net::nsSocketProviderService::GetOrCreate",
     37          []() -> void { mozilla::ClearOnShutdown(&gSingleton); }));
     38    }
     39  }
     40  return inst.forget();
     41 }
     42 
     43 NS_IMPL_ISUPPORTS(nsSocketProviderService, nsISocketProviderService)
     44 
     45 ////////////////////////////////////////////////////////////////////////////////
     46 
     47 NS_IMETHODIMP
     48 nsSocketProviderService::GetSocketProvider(const char* type,
     49                                           nsISocketProvider** result) {
     50  nsCOMPtr<nsISocketProvider> inst;
     51  if (!nsCRT::strcmp(type, "ssl") &&
     52      (XRE_IsParentProcess() || XRE_IsSocketProcess()) &&
     53      EnsureNSSInitializedChromeOrContent()) {
     54    inst = new nsSSLSocketProvider();
     55  } else if (!nsCRT::strcmp(type, "starttls") &&
     56             (XRE_IsParentProcess() || XRE_IsSocketProcess()) &&
     57             EnsureNSSInitializedChromeOrContent()) {
     58    inst = new nsTLSSocketProvider();
     59  } else if (!nsCRT::strcmp(type, "socks")) {
     60    inst = new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5);
     61  } else if (!nsCRT::strcmp(type, "socks4")) {
     62    inst = new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4);
     63  } else if (!nsCRT::strcmp(type, "udp")) {
     64    inst = new nsUDPSocketProvider();
     65  } else {
     66    return NS_ERROR_UNKNOWN_SOCKET_TYPE;
     67  }
     68  inst.forget(result);
     69  return NS_OK;
     70 }
     71 
     72 ////////////////////////////////////////////////////////////////////////////////