Bug 788829 - Call SetSizeConstraints even if a popup is not open. r=enndeakin
[gecko.git] / parser / html / nsHtml5Module.cpp
blob64b1072c1e571f4f094c9bd6012b2296e9e127fa
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 "nsHtml5AttributeName.h"
6 #include "nsHtml5ElementName.h"
7 #include "nsHtml5HtmlAttributes.h"
8 #include "nsHtml5NamedCharacters.h"
9 #include "nsHtml5Portability.h"
10 #include "nsHtml5StackNode.h"
11 #include "nsHtml5Tokenizer.h"
12 #include "nsHtml5TreeBuilder.h"
13 #include "nsHtml5UTF16Buffer.h"
14 #include "nsHtml5Module.h"
15 #include "nsIObserverService.h"
16 #include "nsIServiceManager.h"
17 #include "mozilla/Services.h"
18 #include "mozilla/Preferences.h"
19 #include "mozilla/Attributes.h"
21 using namespace mozilla;
23 // static
24 bool nsHtml5Module::sOffMainThread = true;
25 nsIThread* nsHtml5Module::sStreamParserThread = nullptr;
26 nsIThread* nsHtml5Module::sMainThread = nullptr;
28 // static
29 void
30 nsHtml5Module::InitializeStatics()
32 Preferences::AddBoolVarCache(&sOffMainThread, "html5.offmainthread");
33 nsHtml5Atoms::AddRefAtoms();
34 nsHtml5AttributeName::initializeStatics();
35 nsHtml5ElementName::initializeStatics();
36 nsHtml5HtmlAttributes::initializeStatics();
37 nsHtml5NamedCharacters::initializeStatics();
38 nsHtml5Portability::initializeStatics();
39 nsHtml5StackNode::initializeStatics();
40 nsHtml5Tokenizer::initializeStatics();
41 nsHtml5TreeBuilder::initializeStatics();
42 nsHtml5UTF16Buffer::initializeStatics();
43 nsHtml5StreamParser::InitializeStatics();
44 nsHtml5TreeOpExecutor::InitializeStatics();
45 #ifdef DEBUG
46 sNsHtml5ModuleInitialized = true;
47 #endif
50 // static
51 void
52 nsHtml5Module::ReleaseStatics()
54 #ifdef DEBUG
55 sNsHtml5ModuleInitialized = false;
56 #endif
57 nsHtml5AttributeName::releaseStatics();
58 nsHtml5ElementName::releaseStatics();
59 nsHtml5HtmlAttributes::releaseStatics();
60 nsHtml5NamedCharacters::releaseStatics();
61 nsHtml5Portability::releaseStatics();
62 nsHtml5StackNode::releaseStatics();
63 nsHtml5Tokenizer::releaseStatics();
64 nsHtml5TreeBuilder::releaseStatics();
65 nsHtml5UTF16Buffer::releaseStatics();
66 NS_IF_RELEASE(sStreamParserThread);
67 NS_IF_RELEASE(sMainThread);
70 // static
71 already_AddRefed<nsIParser>
72 nsHtml5Module::NewHtml5Parser()
74 NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
75 nsIParser* rv = static_cast<nsIParser*> (new nsHtml5Parser());
76 NS_ADDREF(rv);
77 return rv;
80 // static
81 nsresult
82 nsHtml5Module::Initialize(nsIParser* aParser, nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, nsIChannel* aChannel)
84 NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
85 nsHtml5Parser* parser = static_cast<nsHtml5Parser*> (aParser);
86 return parser->Initialize(aDoc, aURI, aContainer, aChannel);
89 class nsHtml5ParserThreadTerminator MOZ_FINAL : public nsIObserver
91 public:
92 NS_DECL_ISUPPORTS
93 nsHtml5ParserThreadTerminator(nsIThread* aThread)
94 : mThread(aThread)
96 NS_IMETHODIMP Observe(nsISupports *, const char *topic, const PRUnichar *)
98 NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"),
99 "Unexpected topic");
100 if (mThread) {
101 mThread->Shutdown();
102 mThread = nullptr;
104 return NS_OK;
106 private:
107 nsCOMPtr<nsIThread> mThread;
110 NS_IMPL_ISUPPORTS1(nsHtml5ParserThreadTerminator, nsIObserver)
112 // static
113 nsIThread*
114 nsHtml5Module::GetStreamParserThread()
116 if (sOffMainThread) {
117 if (!sStreamParserThread) {
118 NS_NewNamedThread("HTML5 Parser", &sStreamParserThread);
119 NS_ASSERTION(sStreamParserThread, "Thread creation failed!");
120 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
121 NS_ASSERTION(os, "do_GetService failed");
122 os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread),
123 "xpcom-shutdown-threads",
124 false);
126 return sStreamParserThread;
128 if (!sMainThread) {
129 NS_GetMainThread(&sMainThread);
130 NS_ASSERTION(sMainThread, "Main thread getter failed");
132 return sMainThread;
135 #ifdef DEBUG
136 bool nsHtml5Module::sNsHtml5ModuleInitialized = false;
137 #endif