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 "base/basictypes.h"
8 
9 #include "nsLayoutStatics.h"
10 #include "nscore.h"
11 
12 #include "DateTimeFormat.h"
13 #include "MediaManager.h"
14 #include "mozilla/dom/ServiceWorkerRegistrar.h"
15 #include "nsAttrValue.h"
16 #include "nsColorNames.h"
17 #include "nsComputedDOMStyle.h"
18 #include "nsContentDLF.h"
19 #include "nsContentUtils.h"
20 #include "nsCSSAnonBoxes.h"
21 #include "mozilla/css/ErrorReporter.h"
22 #include "nsCSSProps.h"
23 #include "nsCSSPseudoElements.h"
24 #include "nsCSSRendering.h"
25 #include "nsGenericHTMLFrameElement.h"
26 #include "mozilla/dom/Attr.h"
27 #include "mozilla/dom/PopupBlocker.h"
28 #include "nsIFrame.h"
29 #include "nsFrameState.h"
30 #include "nsGlobalWindow.h"
31 #include "nsGkAtoms.h"
32 #include "nsImageFrame.h"
33 #include "mozilla/GlobalStyleSheetCache.h"
34 #include "nsRegion.h"
35 #include "nsRepeatService.h"
36 #include "nsFloatManager.h"
37 #include "nsSprocketLayout.h"
38 #include "nsStackLayout.h"
39 #include "nsTextControlFrame.h"
40 #include "txMozillaXSLTProcessor.h"
41 #include "nsTreeSanitizer.h"
42 #include "nsCellMap.h"
43 #include "nsTextFrame.h"
44 #include "nsCCUncollectableMarker.h"
45 #include "nsTextFragment.h"
46 #include "nsCORSListenerProxy.h"
47 #include "nsHtml5Module.h"
48 #include "nsHTMLTags.h"
49 #include "nsFocusManager.h"
50 #include "nsListControlFrame.h"
51 #include "mozilla/dom/HTMLDNSPrefetch.h"
52 #include "mozilla/dom/HTMLInputElement.h"
53 #include "mozilla/dom/SVGElementFactory.h"
54 #include "nsLanguageAtomService.h"
55 #include "nsMathMLAtoms.h"
56 #include "nsMathMLOperators.h"
57 #include "Navigator.h"
58 #include "StorageObserver.h"
59 #include "CacheObserver.h"
60 #include "DisplayItemClip.h"
61 #include "HitTestInfo.h"
62 #include "ActiveLayerTracker.h"
63 #include "FrameLayerBuilder.h"
64 #include "AnimationCommon.h"
65 #include "LayerAnimationInfo.h"
66 
67 #include "AudioChannelService.h"
68 #include "mozilla/dom/PromiseDebugging.h"
69 #include "mozilla/dom/nsMixedContentBlocker.h"
70 
71 #ifdef MOZ_XUL
72 #  include "nsXULPopupManager.h"
73 #  include "nsXULContentUtils.h"
74 #  include "nsXULPrototypeCache.h"
75 #  include "nsXULTooltipListener.h"
76 
77 #endif
78 
79 #include "mozilla/dom/UIDirectionManager.h"
80 
81 #include "CubebUtils.h"
82 #include "WebAudioUtils.h"
83 
84 #include "nsError.h"
85 
86 #include "nsJSEnvironment.h"
87 #include "nsContentSink.h"
88 #include "nsFrameMessageManager.h"
89 #include "nsDOMMutationObserver.h"
90 #include "nsHyphenationManager.h"
91 #include "nsWindowMemoryReporter.h"
92 #include "mozilla/dom/ContentParent.h"
93 #include "mozilla/ProcessPriorityManager.h"
94 #include "mozilla/PermissionManager.h"
95 #include "mozilla/dom/CustomElementRegistry.h"
96 #include "mozilla/EventDispatcher.h"
97 #include "mozilla/IMEStateManager.h"
98 #include "mozilla/dom/HTMLVideoElement.h"
99 #include "ThirdPartyUtil.h"
100 #include "TouchManager.h"
101 #include "DecoderDoctorLogger.h"
102 #include "MediaDecoder.h"
103 #include "mozilla/ClearSiteData.h"
104 #include "mozilla/EditorController.h"
105 #include "mozilla/Fuzzyfox.h"
106 #include "mozilla/HTMLEditorController.h"
107 #include "mozilla/ServoBindings.h"
108 #include "mozilla/StaticPresData.h"
109 #include "mozilla/dom/AbstractRange.h"
110 #include "mozilla/dom/Document.h"
111 #include "mozilla/dom/WebIDLGlobalNameHash.h"
112 #include "mozilla/dom/U2FTokenManager.h"
113 #ifdef OS_WIN
114 #  include "mozilla/dom/WinWebAuthnManager.h"
115 #endif
116 #include "mozilla/dom/PointerEventHandler.h"
117 #include "mozilla/dom/RemoteWorkerService.h"
118 #include "mozilla/dom/BlobURLProtocolHandler.h"
119 #include "mozilla/dom/ReportingHeader.h"
120 #include "mozilla/dom/BrowserParent.h"
121 #include "mozilla/dom/quota/ActorsParent.h"
122 #include "mozilla/dom/localstorage/ActorsParent.h"
123 #include "mozilla/net/UrlClassifierFeatureFactory.h"
124 #include "mozilla/RemoteLazyInputStreamStorage.h"
125 #include "nsLayoutUtils.h"
126 #include "nsThreadManager.h"
127 #include "mozilla/css/ImageLoader.h"
128 #include "gfxUserFontSet.h"
129 #include "RestoreTabContentObserver.h"
130 
131 using namespace mozilla;
132 using namespace mozilla::net;
133 using namespace mozilla::dom;
134 using namespace mozilla::dom::ipc;
135 using namespace mozilla::dom::quota;
136 
137 nsrefcnt nsLayoutStatics::sLayoutStaticRefcnt = 0;
138 
Initialize()139 nsresult nsLayoutStatics::Initialize() {
140   NS_ASSERTION(sLayoutStaticRefcnt == 0, "nsLayoutStatics isn't zero!");
141 
142   sLayoutStaticRefcnt = 1;
143   NS_LOG_ADDREF(&sLayoutStaticRefcnt, sLayoutStaticRefcnt, "nsLayoutStatics",
144                 1);
145 
146   nsresult rv;
147 
148   ContentParent::StartUp();
149 
150   nsCSSProps::AddRefTable();
151   nsColorNames::AddRefTable();
152 
153 #ifdef DEBUG
154   nsCSSPseudoElements::AssertAtoms();
155   nsCSSAnonBoxes::AssertAtoms();
156   DebugVerifyFrameStateBits();
157 #endif
158 
159   StartupJSEnvironment();
160 
161   nsGlobalWindowInner::Init();
162   nsGlobalWindowOuter::Init();
163 
164   rv = nsContentUtils::Init();
165   if (NS_FAILED(rv)) {
166     NS_ERROR("Could not initialize nsContentUtils");
167     return rv;
168   }
169 
170   nsAttrValue::Init();
171 
172   rv = nsTextFragment::Init();
173   if (NS_FAILED(rv)) {
174     NS_ERROR("Could not initialize nsTextFragment");
175     return rv;
176   }
177 
178   nsCellMap::Init();
179 
180   StaticPresData::Init();
181   nsCSSRendering::Init();
182   css::ImageLoader::Init();
183 
184   rv = HTMLDNSPrefetch::Initialize();
185   if (NS_FAILED(rv)) {
186     NS_ERROR("Could not initialize HTML DNS prefetch");
187     return rv;
188   }
189 
190   nsMathMLOperators::AddRefTable();
191 
192 #ifdef DEBUG
193   nsIFrame::DisplayReflowStartup();
194 #endif
195   Attr::Initialize();
196 
197   PopupBlocker::Initialize();
198 
199   rv = txMozillaXSLTProcessor::Startup();
200   if (NS_FAILED(rv)) {
201     NS_ERROR("Could not initialize txMozillaXSLTProcessor");
202     return rv;
203   }
204 
205   rv = StorageObserver::Init();
206   if (NS_FAILED(rv)) {
207     NS_ERROR("Could not initialize StorageObserver");
208     return rv;
209   }
210 
211   rv = nsCCUncollectableMarker::Init();
212   if (NS_FAILED(rv)) {
213     NS_ERROR("Could not initialize nsCCUncollectableMarker");
214     return rv;
215   }
216 
217 #ifdef MOZ_XUL
218   rv = nsXULPopupManager::Init();
219   if (NS_FAILED(rv)) {
220     NS_ERROR("Could not initialize nsXULPopupManager");
221     return rv;
222   }
223 #endif
224 
225   rv = nsFocusManager::Init();
226   if (NS_FAILED(rv)) {
227     NS_ERROR("Could not initialize nsFocusManager");
228     return rv;
229   }
230 
231   DecoderDoctorLogger::Init();
232   MediaManager::StartupInit();
233   CubebUtils::InitLibrary();
234 
235   nsHtml5Module::InitializeStatics();
236   nsLayoutUtils::Initialize();
237   PointerEventHandler::InitializeStatics();
238   TouchManager::InitializeStatics();
239 
240   nsWindowMemoryReporter::Init();
241 
242   SVGElementFactory::Init();
243 
244   ProcessPriorityManager::Init();
245 
246   UIDirectionManager::Initialize();
247 
248   CacheObserver::Init();
249 
250   IMEStateManager::Init();
251 
252   ServiceWorkerRegistrar::Initialize();
253 
254   MediaDecoder::InitStatics();
255 
256   PromiseDebugging::Init();
257 
258   if (XRE_IsParentProcess() || XRE_IsContentProcess()) {
259     InitializeServo();
260   }
261 
262   // This must be initialized on the main-thread.
263   mozilla::RemoteLazyInputStreamStorage::Initialize();
264 
265   mozilla::dom::U2FTokenManager::Initialize();
266 
267 #ifdef OS_WIN
268   mozilla::dom::WinWebAuthnManager::Initialize();
269 #endif
270 
271   if (XRE_IsParentProcess()) {
272     // On content process we initialize these components when PContentChild is
273     // fully initialized.
274     mozilla::dom::RemoteWorkerService::Initialize();
275   }
276 
277   mozilla::Fuzzyfox::Start();
278 
279   ClearSiteData::Initialize();
280 
281   // Reporting API.
282   ReportingHeader::Initialize();
283 
284   if (XRE_IsParentProcess()) {
285     InitializeQuotaManager();
286     InitializeLocalStorage();
287   }
288 
289   ThirdPartyUtil::Startup();
290 
291   RestoreTabContentObserver::Initialize();
292 
293   return NS_OK;
294 }
295 
Shutdown()296 void nsLayoutStatics::Shutdown() {
297   // Don't need to shutdown nsWindowMemoryReporter, that will be done by the
298   // memory reporter manager.
299 
300   if (XRE_IsParentProcess() || XRE_IsContentProcess()) {
301     ShutdownServo();
302   }
303 
304   mozilla::dom::AbstractRange::Shutdown();
305   Document::Shutdown();
306   nsMessageManagerScriptExecutor::Shutdown();
307   nsFocusManager::Shutdown();
308 #ifdef MOZ_XUL
309   nsXULPopupManager::Shutdown();
310 #endif
311   UIDirectionManager::Shutdown();
312   StorageObserver::Shutdown();
313   txMozillaXSLTProcessor::Shutdown();
314   Attr::Shutdown();
315   PopupBlocker::Shutdown();
316   IMEStateManager::Shutdown();
317   EditorController::Shutdown();
318   HTMLEditorController::Shutdown();
319   HTMLDNSPrefetch::Shutdown();
320   nsCSSRendering::Shutdown();
321   StaticPresData::Shutdown();
322   nsLanguageAtomService::Shutdown();
323 #ifdef DEBUG
324   nsIFrame::DisplayReflowShutdown();
325 #endif
326   nsCellMap::Shutdown();
327   ActiveLayerTracker::Shutdown();
328 
329   // Release all of our atoms
330   nsColorNames::ReleaseTable();
331   nsCSSProps::ReleaseTable();
332   nsRepeatService::Shutdown();
333   nsStackLayout::Shutdown();
334 
335 #ifdef MOZ_XUL
336   nsXULContentUtils::Finish();
337   nsXULPrototypeCache::ReleaseGlobals();
338   nsSprocketLayout::Shutdown();
339 #endif
340 
341   SVGElementFactory::Shutdown();
342   nsMathMLOperators::ReleaseTable();
343 
344   nsFloatManager::Shutdown();
345   nsImageFrame::ReleaseGlobals();
346 
347   mozilla::css::ErrorReporter::ReleaseGlobals();
348 
349   nsTextFragment::Shutdown();
350 
351   nsAttrValue::Shutdown();
352   nsContentUtils::Shutdown();
353   nsMixedContentBlocker::Shutdown();
354   GlobalStyleSheetCache::Shutdown();
355 
356   ShutdownJSEnvironment();
357   nsGlobalWindowInner::ShutDown();
358   nsGlobalWindowOuter::ShutDown();
359   nsListControlFrame::Shutdown();
360   FrameLayerBuilder::Shutdown();
361 
362   CubebUtils::ShutdownLibrary();
363   WebAudioUtils::Shutdown();
364 
365   nsCORSListenerProxy::Shutdown();
366 
367   PointerEventHandler::ReleaseStatics();
368 
369   TouchManager::ReleaseStatics();
370 
371   nsTreeSanitizer::ReleaseStatics();
372 
373   nsHtml5Module::ReleaseStatics();
374 
375   mozilla::EventDispatcher::Shutdown();
376 
377   HTMLInputElement::DestroyUploadLastDir();
378 
379   nsLayoutUtils::Shutdown();
380 
381   nsHyphenationManager::Shutdown();
382   nsDOMMutationObserver::Shutdown();
383 
384   DateTimeFormat::Shutdown();
385 
386   ContentParent::ShutDown();
387 
388   DisplayItemClip::Shutdown();
389   HitTestInfo::Shutdown();
390 
391   CacheObserver::Shutdown();
392 
393   PromiseDebugging::Shutdown();
394 
395   BlobURLProtocolHandler::RemoveDataEntries();
396 
397   css::ImageLoader::Shutdown();
398 
399   mozilla::net::UrlClassifierFeatureFactory::Shutdown();
400 
401   RestoreTabContentObserver::Shutdown();
402 }
403