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"
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
;
26 nsIThread
* nsHtml5Module::sStreamParserThread
= nullptr;
28 class nsHtml5ParserThreadTerminator final
: public nsIObserver
{
31 explicit nsHtml5ParserThreadTerminator(nsIThread
* aThread
)
33 NS_IMETHOD
Observe(nsISupports
*, const char* topic
,
34 const char16_t
*) override
{
35 NS_ASSERTION(!strcmp(topic
, "xpcom-shutdown-threads"), "Unexpected topic");
38 NS_IF_RELEASE(nsHtml5Module::sStreamParserThread
);
39 nsHtml5Module::sStreamParserThread
= nullptr;
44 ~nsHtml5ParserThreadTerminator() = default;
46 nsCOMPtr
<nsIThread
> mThread
;
49 NS_IMPL_ISUPPORTS(nsHtml5ParserThreadTerminator
, nsIObserver
)
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();
67 os
->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread
),
68 "xpcom-shutdown-threads", false);
71 "How come we failed to create get the observer service?");
74 MOZ_ASSERT(false, "How come we failed to create the parser thread?");
78 sNsHtml5ModuleInitialized
= true;
83 void nsHtml5Module::ReleaseStatics() {
85 sNsHtml5ModuleInitialized
= false;
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
);
100 already_AddRefed
<nsHtml5Parser
> nsHtml5Module::NewHtml5Parser() {
101 MOZ_ASSERT(sNsHtml5ModuleInitialized
, "nsHtml5Module not initialized.");
102 RefPtr
<nsHtml5Parser
> rv
= new nsHtml5Parser();
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();
122 bool nsHtml5Module::sNsHtml5ModuleInitialized
= false;