Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / parser / html / nsHtml5Module.cpp
blobb6325009392cd8644f3328a3107196871d0a057e
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is HTML Parser C++ Translator code.
16 * The Initial Developer of the Original Code is
17 * Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Henri Sivonen <hsivonen@iki.fi>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsContentUtils.h"
39 #include "nsHtml5AttributeName.h"
40 #include "nsHtml5ElementName.h"
41 #include "nsHtml5HtmlAttributes.h"
42 #include "nsHtml5NamedCharacters.h"
43 #include "nsHtml5Portability.h"
44 #include "nsHtml5StackNode.h"
45 #include "nsHtml5Tokenizer.h"
46 #include "nsHtml5TreeBuilder.h"
47 #include "nsHtml5UTF16Buffer.h"
48 #include "nsHtml5Module.h"
49 #include "nsIObserverService.h"
50 #include "nsIServiceManager.h"
51 #include "mozilla/Services.h"
53 // static
54 PRBool nsHtml5Module::sEnabled = PR_FALSE;
55 PRBool nsHtml5Module::sOffMainThread = PR_TRUE;
56 nsIThread* nsHtml5Module::sStreamParserThread = nsnull;
57 nsIThread* nsHtml5Module::sMainThread = nsnull;
59 // static
60 void
61 nsHtml5Module::InitializeStatics()
63 nsContentUtils::AddBoolPrefVarCache("html5.enable", &sEnabled);
64 nsContentUtils::AddBoolPrefVarCache("html5.offmainthread", &sOffMainThread);
65 nsHtml5Atoms::AddRefAtoms();
66 nsHtml5AttributeName::initializeStatics();
67 nsHtml5ElementName::initializeStatics();
68 nsHtml5HtmlAttributes::initializeStatics();
69 nsHtml5NamedCharacters::initializeStatics();
70 nsHtml5Portability::initializeStatics();
71 nsHtml5StackNode::initializeStatics();
72 nsHtml5Tokenizer::initializeStatics();
73 nsHtml5TreeBuilder::initializeStatics();
74 nsHtml5UTF16Buffer::initializeStatics();
75 nsHtml5StreamParser::InitializeStatics();
76 #ifdef DEBUG
77 sNsHtml5ModuleInitialized = PR_TRUE;
78 #endif
81 // static
82 void
83 nsHtml5Module::ReleaseStatics()
85 #ifdef DEBUG
86 sNsHtml5ModuleInitialized = PR_FALSE;
87 #endif
88 nsHtml5AttributeName::releaseStatics();
89 nsHtml5ElementName::releaseStatics();
90 nsHtml5HtmlAttributes::releaseStatics();
91 nsHtml5NamedCharacters::releaseStatics();
92 nsHtml5Portability::releaseStatics();
93 nsHtml5StackNode::releaseStatics();
94 nsHtml5Tokenizer::releaseStatics();
95 nsHtml5TreeBuilder::releaseStatics();
96 nsHtml5UTF16Buffer::releaseStatics();
97 NS_IF_RELEASE(sStreamParserThread);
98 NS_IF_RELEASE(sMainThread);
101 // static
102 already_AddRefed<nsIParser>
103 nsHtml5Module::NewHtml5Parser()
105 NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
106 nsIParser* rv = static_cast<nsIParser*> (new nsHtml5Parser());
107 NS_ADDREF(rv);
108 return rv;
111 // static
112 nsresult
113 nsHtml5Module::Initialize(nsIParser* aParser, nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, nsIChannel* aChannel)
115 NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
116 nsHtml5Parser* parser = static_cast<nsHtml5Parser*> (aParser);
117 return parser->Initialize(aDoc, aURI, aContainer, aChannel);
120 class nsHtml5ParserThreadTerminator : public nsIObserver
122 public:
123 NS_DECL_ISUPPORTS
124 nsHtml5ParserThreadTerminator(nsIThread* aThread)
125 : mThread(aThread)
127 NS_IMETHODIMP Observe(nsISupports *, const char *topic, const PRUnichar *)
129 NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"),
130 "Unexpected topic");
131 if (mThread) {
132 mThread->Shutdown();
133 mThread = nsnull;
135 return NS_OK;
137 private:
138 nsCOMPtr<nsIThread> mThread;
141 NS_IMPL_ISUPPORTS1(nsHtml5ParserThreadTerminator, nsIObserver)
143 // static
144 nsIThread*
145 nsHtml5Module::GetStreamParserThread()
147 if (sOffMainThread) {
148 if (!sStreamParserThread) {
149 NS_NewThread(&sStreamParserThread);
150 NS_ASSERTION(sStreamParserThread, "Thread creation failed!");
151 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
152 NS_ASSERTION(os, "do_GetService failed");
153 os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread),
154 "xpcom-shutdown-threads",
155 PR_FALSE);
157 return sStreamParserThread;
159 if (!sMainThread) {
160 NS_GetMainThread(&sMainThread);
161 NS_ASSERTION(sMainThread, "Main thread getter failed");
163 return sMainThread;
166 #ifdef DEBUG
167 PRBool nsHtml5Module::sNsHtml5ModuleInitialized = PR_FALSE;
168 #endif