tor-browser

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

nsNetModule.cpp (6900B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set sw=2 ts=8 et 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 #define ALLOW_LATE_HTTPLOG_H_INCLUDE 1
      8 #include "base/basictypes.h"
      9 
     10 #include "nsCOMPtr.h"
     11 #include "nsIClassInfoImpl.h"
     12 #include "mozilla/Components.h"
     13 #include "mozilla/ModuleUtils.h"
     14 #include "nscore.h"
     15 #include "nsSimpleURI.h"
     16 #include "nsLoadGroup.h"
     17 #include "nsMimeTypes.h"
     18 #include "nsDNSPrefetch.h"
     19 #include "nsXULAppAPI.h"
     20 #include "nsCategoryCache.h"
     21 #include "nsIContentSniffer.h"
     22 #include "nsStandardURL.h"
     23 #include "mozilla/net/BackgroundChannelRegistrar.h"
     24 #include "mozilla/net/NeckoChild.h"
     25 #ifdef MOZ_AUTH_EXTENSION
     26 #  include "nsAuthGSSAPI.h"
     27 #endif
     28 
     29 #include "nsNetCID.h"
     30 
     31 #if defined(XP_MACOSX) || defined(XP_WIN) || defined(XP_LINUX)
     32 #  define BUILD_NETWORK_INFO_SERVICE 1
     33 #endif
     34 
     35 using namespace mozilla;
     36 
     37 using ContentSnifferCache = nsCategoryCache<nsIContentSniffer>;
     38 ContentSnifferCache* gNetSniffers = nullptr;
     39 ContentSnifferCache* gDataSniffers = nullptr;
     40 ContentSnifferCache* gORBSniffers = nullptr;
     41 ContentSnifferCache* gNetAndORBSniffers = nullptr;
     42 
     43 #define static
     44 using nsLoadGroup = mozilla::net::nsLoadGroup;
     45 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsLoadGroup, Init)
     46 #undef static
     47 
     48 ///////////////////////////////////////////////////////////////////////////////
     49 // protocols
     50 ///////////////////////////////////////////////////////////////////////////////
     51 
     52 // http/https
     53 #include "nsHttpHandler.h"
     54 #include "Http2Compression.h"
     55 #undef LOG
     56 #undef LOG_ENABLED
     57 #include "nsHttpAuthManager.h"
     58 #include "nsHttpActivityDistributor.h"
     59 #include "ThrottleQueue.h"
     60 #undef LOG
     61 #undef LOG_ENABLED
     62 
     63 NS_IMPL_COMPONENT_FACTORY(net::nsHttpHandler) {
     64  return net::nsHttpHandler::GetInstance().downcast<nsIHttpProtocolHandler>();
     65 }
     66 
     67 NS_IMPL_COMPONENT_FACTORY(net::nsHttpsHandler) {
     68  auto handler = MakeRefPtr<net::nsHttpsHandler>();
     69 
     70  if (NS_FAILED(handler->Init())) {
     71    return nullptr;
     72  }
     73  return handler.forget().downcast<nsIHttpProtocolHandler>();
     74 }
     75 
     76 #include "WebSocketChannel.h"
     77 #include "WebSocketChannelChild.h"
     78 namespace mozilla::net {
     79 static BaseWebSocketChannel* WebSocketChannelConstructor(bool aSecure) {
     80  if (IsNeckoChild()) {
     81    return new WebSocketChannelChild(aSecure);
     82  }
     83 
     84  if (aSecure) {
     85    return new WebSocketSSLChannel;
     86  }
     87  return new WebSocketChannel;
     88 }
     89 
     90 #define WEB_SOCKET_HANDLER_CONSTRUCTOR(type, secure)          \
     91  nsresult type##Constructor(REFNSIID aIID, void** aResult) { \
     92    RefPtr<BaseWebSocketChannel> inst;                        \
     93                                                              \
     94    *aResult = nullptr;                                       \
     95    inst = WebSocketChannelConstructor(secure);               \
     96    return inst->QueryInterface(aIID, aResult);               \
     97  }
     98 
     99 WEB_SOCKET_HANDLER_CONSTRUCTOR(WebSocketChannel, false)
    100 WEB_SOCKET_HANDLER_CONSTRUCTOR(WebSocketSSLChannel, true)
    101 #undef WEB_SOCKET_HANDLER_CONSTRUCTOR
    102 }  // namespace mozilla::net
    103 
    104 ///////////////////////////////////////////////////////////////////////////////
    105 
    106 #include "nsStreamConverterService.h"
    107 #include "nsMultiMixedConv.h"
    108 #include "nsHTTPCompressConv.h"
    109 #include "mozTXTToHTMLConv.h"
    110 #include "nsUnknownDecoder.h"
    111 
    112 ///////////////////////////////////////////////////////////////////////////////
    113 
    114 #include "nsIndexedToHTML.h"
    115 
    116 nsresult NS_NewMultiMixedConv(nsMultiMixedConv** result);
    117 nsresult MOZ_NewTXTToHTMLConv(mozTXTToHTMLConv** result);
    118 nsresult NS_NewHTTPCompressConv(mozilla::net::nsHTTPCompressConv** result);
    119 nsresult NS_NewStreamConv(nsStreamConverterService** aStreamConv);
    120 
    121 nsresult CreateNewStreamConvServiceFactory(REFNSIID aIID, void** aResult) {
    122  if (!aResult) {
    123    return NS_ERROR_INVALID_POINTER;
    124  }
    125  RefPtr<nsStreamConverterService> inst;
    126  nsresult rv = NS_NewStreamConv(getter_AddRefs(inst));
    127  if (NS_FAILED(rv)) {
    128    *aResult = nullptr;
    129    return rv;
    130  }
    131  rv = inst->QueryInterface(aIID, aResult);
    132  if (NS_FAILED(rv)) {
    133    *aResult = nullptr;
    134  }
    135  return rv;
    136 }
    137 
    138 nsresult CreateNewMultiMixedConvFactory(REFNSIID aIID, void** aResult) {
    139  if (!aResult) {
    140    return NS_ERROR_INVALID_POINTER;
    141  }
    142  RefPtr<nsMultiMixedConv> inst;
    143  nsresult rv = NS_NewMultiMixedConv(getter_AddRefs(inst));
    144  if (NS_FAILED(rv)) {
    145    *aResult = nullptr;
    146    return rv;
    147  }
    148  rv = inst->QueryInterface(aIID, aResult);
    149  if (NS_FAILED(rv)) {
    150    *aResult = nullptr;
    151  }
    152  return rv;
    153 }
    154 
    155 nsresult CreateNewTXTToHTMLConvFactory(REFNSIID aIID, void** aResult) {
    156  if (!aResult) {
    157    return NS_ERROR_INVALID_POINTER;
    158  }
    159  RefPtr<mozTXTToHTMLConv> inst;
    160  nsresult rv = MOZ_NewTXTToHTMLConv(getter_AddRefs(inst));
    161  if (NS_FAILED(rv)) {
    162    *aResult = nullptr;
    163    return rv;
    164  }
    165  rv = inst->QueryInterface(aIID, aResult);
    166  if (NS_FAILED(rv)) {
    167    *aResult = nullptr;
    168  }
    169  return rv;
    170 }
    171 
    172 nsresult CreateNewHTTPCompressConvFactory(REFNSIID aIID, void** aResult) {
    173  if (!aResult) {
    174    return NS_ERROR_INVALID_POINTER;
    175  }
    176  RefPtr<mozilla::net::nsHTTPCompressConv> inst;
    177  nsresult rv = NS_NewHTTPCompressConv(getter_AddRefs(inst));
    178  if (NS_FAILED(rv)) {
    179    *aResult = nullptr;
    180    return rv;
    181  }
    182  rv = inst->QueryInterface(aIID, aResult);
    183  if (NS_FAILED(rv)) {
    184    *aResult = nullptr;
    185  }
    186  return rv;
    187 }
    188 
    189 nsresult CreateNewUnknownDecoderFactory(REFNSIID aIID, void** aResult) {
    190  if (!aResult) {
    191    return NS_ERROR_NULL_POINTER;
    192  }
    193  *aResult = nullptr;
    194 
    195  RefPtr<nsUnknownDecoder> inst = new nsUnknownDecoder();
    196  return inst->QueryInterface(aIID, aResult);
    197 }
    198 
    199 nsresult CreateNewBinaryDetectorFactory(REFNSIID aIID, void** aResult) {
    200  if (!aResult) {
    201    return NS_ERROR_NULL_POINTER;
    202  }
    203  *aResult = nullptr;
    204 
    205  RefPtr<nsBinaryDetector> inst = new nsBinaryDetector();
    206  return inst->QueryInterface(aIID, aResult);
    207 }
    208 
    209 ///////////////////////////////////////////////////////////////////////////////
    210 // Module implementation for the net library
    211 
    212 // Net module startup hook
    213 nsresult nsNetStartup() {
    214  mozilla::net::nsStandardURL::InitGlobalObjects();
    215  return NS_OK;
    216 }
    217 
    218 // Net module shutdown hook
    219 void nsNetShutdown() {
    220  // Release the url parser that the stdurl is holding.
    221  mozilla::net::nsStandardURL::ShutdownGlobalObjects();
    222 
    223  // Release global state used by the URL helper module.
    224  net_ShutdownURLHelper();
    225 
    226  // Release DNS service reference.
    227  nsDNSPrefetch::Shutdown();
    228 
    229  // Release the Websocket Admission Manager
    230  mozilla::net::WebSocketChannel::Shutdown();
    231 
    232  mozilla::net::Http2CompressionCleanup();
    233 
    234 #ifdef MOZ_AUTH_EXTENSION
    235  nsAuthGSSAPI::Shutdown();
    236 #endif
    237 
    238  delete gNetSniffers;
    239  gNetSniffers = nullptr;
    240  delete gDataSniffers;
    241  gDataSniffers = nullptr;
    242  delete gORBSniffers;
    243  gORBSniffers = nullptr;
    244  delete gNetAndORBSniffers;
    245  gNetAndORBSniffers = nullptr;
    246 }