Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / parser / html / nsHtml5Module.cpp
blob366a63e54fb9d754ffb51a4aa4fcb9e8bd7d46be
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsHtml5Module.h"
6 #include "mozilla/AlreadyAddRefed.h"
7 #include "mozilla/Attributes.h"
8 #include "mozilla/Preferences.h"
9 #include "mozilla/Services.h"
10 #include "mozilla/StaticPrefs_html5.h"
11 #include "nsCOMPtr.h"
12 #include "nsHtml5AttributeName.h"
13 #include "nsHtml5ElementName.h"
14 #include "nsHtml5HtmlAttributes.h"
15 #include "nsHtml5NamedCharacters.h"
16 #include "nsHtml5Portability.h"
17 #include "nsHtml5StackNode.h"
18 #include "nsHtml5Tokenizer.h"
19 #include "nsHtml5TreeBuilder.h"
20 #include "nsHtml5UTF16Buffer.h"
21 #include "nsIObserverService.h"
23 using namespace mozilla;
25 // static
26 nsIThread* nsHtml5Module::sStreamParserThread = nullptr;
28 class nsHtml5ParserThreadTerminator final : public nsIObserver {
29 public:
30 NS_DECL_ISUPPORTS
31 explicit nsHtml5ParserThreadTerminator(nsIThread* aThread)
32 : mThread(aThread) {}
33 NS_IMETHOD Observe(nsISupports*, const char* topic,
34 const char16_t*) override {
35 NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"), "Unexpected topic");
36 mThread->Shutdown();
37 mThread = nullptr;
38 NS_IF_RELEASE(nsHtml5Module::sStreamParserThread);
39 nsHtml5Module::sStreamParserThread = nullptr;
40 return NS_OK;
43 private:
44 ~nsHtml5ParserThreadTerminator() = default;
46 nsCOMPtr<nsIThread> mThread;
49 NS_IMPL_ISUPPORTS(nsHtml5ParserThreadTerminator, nsIObserver)
51 // static
52 void nsHtml5Module::InitializeStatics() {
53 nsHtml5AttributeName::initializeStatics();
54 nsHtml5ElementName::initializeStatics();
55 nsHtml5HtmlAttributes::initializeStatics();
56 nsHtml5NamedCharacters::initializeStatics();
57 nsHtml5Portability::initializeStatics();
58 nsHtml5StackNode::initializeStatics();
59 nsHtml5Tokenizer::initializeStatics();
60 nsHtml5TreeBuilder::initializeStatics();
61 nsHtml5UTF16Buffer::initializeStatics();
63 NS_NewNamedThread("HTML5 Parser", &sStreamParserThread);
64 if (sStreamParserThread) {
65 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
66 if (os) {
67 os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread),
68 "xpcom-shutdown-threads", false);
69 } else {
70 MOZ_ASSERT(false,
71 "How come we failed to create get the observer service?");
73 } else {
74 MOZ_ASSERT(false, "How come we failed to create the parser thread?");
77 #ifdef DEBUG
78 sNsHtml5ModuleInitialized = true;
79 #endif
82 // static
83 void nsHtml5Module::ReleaseStatics() {
84 #ifdef DEBUG
85 sNsHtml5ModuleInitialized = false;
86 #endif
87 nsHtml5AttributeName::releaseStatics();
88 nsHtml5ElementName::releaseStatics();
89 nsHtml5HtmlAttributes::releaseStatics();
90 nsHtml5NamedCharacters::releaseStatics();
91 nsHtml5Portability::releaseStatics();
92 nsHtml5StackNode::releaseStatics();
93 nsHtml5Tokenizer::releaseStatics();
94 nsHtml5TreeBuilder::releaseStatics();
95 nsHtml5UTF16Buffer::releaseStatics();
96 NS_IF_RELEASE(sStreamParserThread);
99 // static
100 already_AddRefed<nsHtml5Parser> nsHtml5Module::NewHtml5Parser() {
101 MOZ_ASSERT(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
102 RefPtr<nsHtml5Parser> rv = new nsHtml5Parser();
103 return rv.forget();
106 // static
107 already_AddRefed<nsISerialEventTarget>
108 nsHtml5Module::GetStreamParserEventTarget() {
109 MOZ_ASSERT(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
110 if (sStreamParserThread) {
111 nsCOMPtr<nsISerialEventTarget> target = sStreamParserThread;
112 return target.forget();
114 nsCOMPtr<nsIThread> mainThread;
115 NS_GetMainThread(getter_AddRefs(mainThread));
116 MOZ_RELEASE_ASSERT(mainThread); // Unrecoverable situation
117 nsCOMPtr<nsISerialEventTarget> target = mainThread;
118 return target.forget();
121 #ifdef DEBUG
122 bool nsHtml5Module::sNsHtml5ModuleInitialized = false;
123 #endif