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
14 * The Original Code is HTML Parser C++ Translator code.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
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"
54 PRBool
nsHtml5Module::sEnabled
= PR_FALSE
;
55 PRBool
nsHtml5Module::sOffMainThread
= PR_TRUE
;
56 nsIThread
* nsHtml5Module::sStreamParserThread
= nsnull
;
57 nsIThread
* nsHtml5Module::sMainThread
= nsnull
;
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();
77 sNsHtml5ModuleInitialized
= PR_TRUE
;
83 nsHtml5Module::ReleaseStatics()
86 sNsHtml5ModuleInitialized
= PR_FALSE
;
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
);
102 already_AddRefed
<nsIParser
>
103 nsHtml5Module::NewHtml5Parser()
105 NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized
, "nsHtml5Module not initialized.");
106 nsIParser
* rv
= static_cast<nsIParser
*> (new nsHtml5Parser());
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
124 nsHtml5ParserThreadTerminator(nsIThread
* aThread
)
127 NS_IMETHODIMP
Observe(nsISupports
*, const char *topic
, const PRUnichar
*)
129 NS_ASSERTION(!strcmp(topic
, "xpcom-shutdown-threads"),
138 nsCOMPtr
<nsIThread
> mThread
;
141 NS_IMPL_ISUPPORTS1(nsHtml5ParserThreadTerminator
, nsIObserver
)
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",
157 return sStreamParserThread
;
160 NS_GetMainThread(&sMainThread
);
161 NS_ASSERTION(sMainThread
, "Main thread getter failed");
167 PRBool
nsHtml5Module::sNsHtml5ModuleInitialized
= PR_FALSE
;