tor-browser

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

nsLayoutModule.cpp (8515B)


      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 "nsLayoutModule.h"
      8 
      9 #include "ThirdPartyUtil.h"
     10 #include "XPCModule.h"
     11 #include "base/basictypes.h"
     12 #include "gfxPlatform.h"
     13 #include "mozilla/Components.h"
     14 #include "mozilla/ModuleUtils.h"
     15 #include "mozilla/dom/quota/QuotaManagerService.h"
     16 #include "mozilla/gfx/gfxVars.h"
     17 #include "mozilla/ipc/IOThread.h"
     18 #include "nsContentDLF.h"
     19 #include "nsContentPolicyUtils.h"
     20 #include "nsDOMCID.h"
     21 #include "nsDataDocumentContentPolicy.h"
     22 #include "nsFocusManager.h"
     23 #include "nsFrameMessageManager.h"
     24 #include "nsHTMLContentSerializer.h"
     25 #include "nsHTMLParts.h"
     26 #include "nsIContentSerializer.h"
     27 #include "nsIDocumentViewer.h"
     28 #include "nsIEventListenerService.h"
     29 #include "nsImageModule.h"
     30 #include "nsLayoutStatics.h"
     31 #include "nsNoDataProtocolContentPolicy.h"
     32 #include "nsPlainTextSerializer.h"
     33 #include "nsXHTMLContentSerializer.h"
     34 #include "nsXMLContentSerializer.h"
     35 
     36 // view stuff
     37 #include "mozilla/dom/LocalStorageCommon.h"
     38 #include "mozilla/dom/LocalStorageManager.h"
     39 #include "mozilla/dom/LocalStorageManager2.h"
     40 #include "mozilla/dom/SessionStorageManager.h"
     41 #include "nsContentCreatorFunctions.h"
     42 
     43 #ifdef MOZ_WEBSPEECH
     44 #  include "mozilla/dom/OnlineSpeechRecognitionService.h"
     45 #  include "mozilla/dom/nsSynthVoiceRegistry.h"
     46 #endif
     47 
     48 #include "mozilla/dom/PushNotifier.h"
     49 using mozilla::dom::PushNotifier;
     50 #define PUSHNOTIFIER_CID \
     51  {0x2fc2d3e3, 0x020f, 0x404e, {0xb0, 0x6a, 0x6e, 0xcf, 0x3e, 0xa2, 0x33, 0x4a}}
     52 
     53 #include "nsNetCID.h"
     54 #include "nsScriptSecurityManager.h"
     55 #if defined(MOZ_WIDGET_ANDROID)
     56 #  include "nsHapticFeedback.h"
     57 #endif
     58 #include "nsParserUtils.h"
     59 
     60 class nsIDocumentLoaderFactory;
     61 
     62 #define PRODUCT_NAME "Gecko"
     63 
     64 #include "inDeepTreeWalker.h"
     65 
     66 static void Shutdown();
     67 
     68 #include "mozilla/dom/nsContentSecurityManager.h"
     69 
     70 using namespace mozilla;
     71 using namespace mozilla::dom;
     72 using namespace mozilla::net;
     73 
     74 //-----------------------------------------------------------------------------
     75 
     76 static bool gInitialized = false;
     77 
     78 // Perform our one-time intialization for this module
     79 
     80 void nsLayoutModuleInitialize() {
     81  if (gInitialized) {
     82    MOZ_CRASH("Recursive layout module initialization");
     83  }
     84 
     85  static_assert(sizeof(uintptr_t) == sizeof(void*),
     86                "Eeek! You'll need to adjust the size of uintptr_t to the "
     87                "size of a pointer on your platform.");
     88 
     89  gInitialized = true;
     90 
     91  mozilla::ipc::IOThread::Startup();
     92 
     93  if (NS_FAILED(xpcModuleCtor())) {
     94    MOZ_CRASH("xpcModuleCtor failed");
     95  }
     96 
     97  if (NS_FAILED(nsLayoutStatics::Initialize())) {
     98    Shutdown();
     99    MOZ_CRASH("nsLayoutStatics::Initialize failed");
    100  }
    101 }
    102 
    103 // Shutdown this module, releasing all of the module resources
    104 
    105 // static
    106 void Shutdown() {
    107  MOZ_ASSERT(gInitialized, "module not initialized");
    108  if (!gInitialized) {
    109    return;
    110  }
    111 
    112  gInitialized = false;
    113 
    114  nsLayoutStatics::Release();
    115 }
    116 
    117 already_AddRefed<nsIDocumentViewer> NS_NewDocumentViewer();
    118 nsresult NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult);
    119 nsresult NS_NewContentPolicy(nsIContentPolicy** aResult);
    120 
    121 nsresult NS_NewEventListenerService(nsIEventListenerService** aResult);
    122 nsresult NS_NewGlobalMessageManager(nsISupports** aResult);
    123 nsresult NS_NewParentProcessMessageManager(nsISupports** aResult);
    124 nsresult NS_NewChildProcessMessageManager(nsISupports** aResult);
    125 
    126 #define MAKE_CTOR(ctor_, iface_, func_)           \
    127  nsresult ctor_(REFNSIID aIID, void** aResult) { \
    128    *aResult = nullptr;                           \
    129    iface_* inst;                                 \
    130    nsresult rv = func_(&inst);                   \
    131    if (NS_SUCCEEDED(rv)) {                       \
    132      rv = inst->QueryInterface(aIID, aResult);   \
    133      NS_RELEASE(inst);                           \
    134    }                                             \
    135    return rv;                                    \
    136  }
    137 
    138 #define MAKE_GENERIC_CTOR(iface_, func_)             \
    139  NS_IMPL_COMPONENT_FACTORY(iface_) {                \
    140    nsCOMPtr<iface_> inst;                           \
    141    if (NS_SUCCEEDED(func_(getter_AddRefs(inst)))) { \
    142      return inst.forget();                          \
    143    }                                                \
    144    return nullptr;                                  \
    145  }
    146 
    147 #define MAKE_GENERIC_CTOR2(iface_, func_) \
    148  NS_IMPL_COMPONENT_FACTORY(iface_) { return func_(); }
    149 
    150 MAKE_GENERIC_CTOR2(nsIDocumentViewer, NS_NewDocumentViewer)
    151 
    152 MAKE_CTOR(CreateXMLContentSerializer, nsIContentSerializer,
    153          NS_NewXMLContentSerializer)
    154 MAKE_CTOR(CreateHTMLContentSerializer, nsIContentSerializer,
    155          NS_NewHTMLContentSerializer)
    156 MAKE_CTOR(CreateXHTMLContentSerializer, nsIContentSerializer,
    157          NS_NewXHTMLContentSerializer)
    158 MAKE_CTOR(CreatePlainTextSerializer, nsIContentSerializer,
    159          NS_NewPlainTextSerializer)
    160 MAKE_CTOR(CreateContentPolicy, nsIContentPolicy, NS_NewContentPolicy)
    161 
    162 MAKE_GENERIC_CTOR(nsIDocumentLoaderFactory, NS_NewContentDocumentLoaderFactory)
    163 MAKE_GENERIC_CTOR(nsIEventListenerService, NS_NewEventListenerService)
    164 MAKE_CTOR(CreateGlobalMessageManager, nsISupports, NS_NewGlobalMessageManager)
    165 MAKE_CTOR(CreateParentMessageManager, nsISupports,
    166          NS_NewParentProcessMessageManager)
    167 MAKE_CTOR(CreateChildMessageManager, nsISupports,
    168          NS_NewChildProcessMessageManager)
    169 
    170 MAKE_GENERIC_CTOR(nsIFocusManager, NS_NewFocusManager)
    171 
    172 // views are not refcounted, so this is the same as
    173 // NS_GENERIC_FACTORY_CONSTRUCTOR without the NS_ADDREF/NS_RELEASE
    174 #define NS_GENERIC_FACTORY_CONSTRUCTOR_NOREFS(_InstanceClass)                  \
    175  static nsresult _InstanceClass##Constructor(REFNSIID aIID, void** aResult) { \
    176    nsresult rv;                                                               \
    177                                                                               \
    178    *aResult = nullptr;                                                        \
    179                                                                               \
    180    _InstanceClass* inst = new _InstanceClass();                               \
    181    if (nullptr == inst) {                                                     \
    182      rv = NS_ERROR_OUT_OF_MEMORY;                                             \
    183      return rv;                                                               \
    184    }                                                                          \
    185    rv = inst->QueryInterface(aIID, aResult);                                  \
    186                                                                               \
    187    return rv;                                                                 \
    188  }
    189 
    190 #ifdef ACCESSIBILITY
    191 #  include "xpcAccessibilityService.h"
    192 
    193 MAKE_GENERIC_CTOR(nsIAccessibilityService, NS_GetAccessibilityService)
    194 #endif
    195 
    196 nsresult Construct_nsIScriptSecurityManager(REFNSIID aIID, void** aResult) {
    197  if (!aResult) {
    198    return NS_ERROR_NULL_POINTER;
    199  }
    200  *aResult = nullptr;
    201  nsScriptSecurityManager* obj =
    202      nsScriptSecurityManager::GetScriptSecurityManager();
    203  if (!obj) {
    204    return NS_ERROR_OUT_OF_MEMORY;
    205  }
    206  if (NS_FAILED(obj->QueryInterface(aIID, aResult))) {
    207    return NS_ERROR_FAILURE;
    208  }
    209  return NS_OK;
    210 }
    211 
    212 nsresult LocalStorageManagerConstructor(REFNSIID aIID, void** aResult) {
    213  if (NextGenLocalStorageEnabled()) {
    214    auto manager = MakeRefPtr<LocalStorageManager2>();
    215    return manager->QueryInterface(aIID, aResult);
    216  }
    217 
    218  auto manager = MakeRefPtr<LocalStorageManager>();
    219  return manager->QueryInterface(aIID, aResult);
    220 }
    221 
    222 nsresult SessionStorageManagerConstructor(REFNSIID aIID, void** aResult) {
    223  auto manager = MakeRefPtr<SessionStorageManager>(nullptr);
    224  return manager->QueryInterface(aIID, aResult);
    225 }
    226 
    227 void nsLayoutModuleDtor() {
    228  if (XRE_GetProcessType() == GeckoProcessType_GPU ||
    229      XRE_GetProcessType() == GeckoProcessType_VR ||
    230      XRE_GetProcessType() == GeckoProcessType_RDD) {
    231    return;
    232  }
    233 
    234  Shutdown();
    235  nsContentUtils::XPCOMShutdown();
    236 
    237  // Layout depends heavily on gfx and imagelib, so we want to make sure that
    238  // these modules are shut down after all the layout cleanup runs.
    239  mozilla::image::ShutdownModule();
    240  gfxPlatform::Shutdown();
    241  gfx::gfxVars::Shutdown();
    242 
    243  nsScriptSecurityManager::Shutdown();
    244  xpcModuleDtor();
    245 }