1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/parser/html/nsHtml5Module.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsHtml5AttributeName.h" 1.9 +#include "nsHtml5ElementName.h" 1.10 +#include "nsHtml5HtmlAttributes.h" 1.11 +#include "nsHtml5NamedCharacters.h" 1.12 +#include "nsHtml5Portability.h" 1.13 +#include "nsHtml5StackNode.h" 1.14 +#include "nsHtml5Tokenizer.h" 1.15 +#include "nsHtml5TreeBuilder.h" 1.16 +#include "nsHtml5UTF16Buffer.h" 1.17 +#include "nsHtml5Module.h" 1.18 +#include "nsIObserverService.h" 1.19 +#include "nsIServiceManager.h" 1.20 +#include "mozilla/Services.h" 1.21 +#include "mozilla/Preferences.h" 1.22 +#include "mozilla/Attributes.h" 1.23 + 1.24 +using namespace mozilla; 1.25 + 1.26 +// static 1.27 +bool nsHtml5Module::sOffMainThread = true; 1.28 +nsIThread* nsHtml5Module::sStreamParserThread = nullptr; 1.29 +nsIThread* nsHtml5Module::sMainThread = nullptr; 1.30 + 1.31 +// static 1.32 +void 1.33 +nsHtml5Module::InitializeStatics() 1.34 +{ 1.35 + Preferences::AddBoolVarCache(&sOffMainThread, "html5.offmainthread"); 1.36 + nsHtml5Atoms::AddRefAtoms(); 1.37 + nsHtml5AttributeName::initializeStatics(); 1.38 + nsHtml5ElementName::initializeStatics(); 1.39 + nsHtml5HtmlAttributes::initializeStatics(); 1.40 + nsHtml5NamedCharacters::initializeStatics(); 1.41 + nsHtml5Portability::initializeStatics(); 1.42 + nsHtml5StackNode::initializeStatics(); 1.43 + nsHtml5Tokenizer::initializeStatics(); 1.44 + nsHtml5TreeBuilder::initializeStatics(); 1.45 + nsHtml5UTF16Buffer::initializeStatics(); 1.46 + nsHtml5StreamParser::InitializeStatics(); 1.47 + nsHtml5TreeOpExecutor::InitializeStatics(); 1.48 +#ifdef DEBUG 1.49 + sNsHtml5ModuleInitialized = true; 1.50 +#endif 1.51 +} 1.52 + 1.53 +// static 1.54 +void 1.55 +nsHtml5Module::ReleaseStatics() 1.56 +{ 1.57 +#ifdef DEBUG 1.58 + sNsHtml5ModuleInitialized = false; 1.59 +#endif 1.60 + nsHtml5AttributeName::releaseStatics(); 1.61 + nsHtml5ElementName::releaseStatics(); 1.62 + nsHtml5HtmlAttributes::releaseStatics(); 1.63 + nsHtml5NamedCharacters::releaseStatics(); 1.64 + nsHtml5Portability::releaseStatics(); 1.65 + nsHtml5StackNode::releaseStatics(); 1.66 + nsHtml5Tokenizer::releaseStatics(); 1.67 + nsHtml5TreeBuilder::releaseStatics(); 1.68 + nsHtml5UTF16Buffer::releaseStatics(); 1.69 + NS_IF_RELEASE(sStreamParserThread); 1.70 + NS_IF_RELEASE(sMainThread); 1.71 +} 1.72 + 1.73 +// static 1.74 +already_AddRefed<nsIParser> 1.75 +nsHtml5Module::NewHtml5Parser() 1.76 +{ 1.77 + NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized."); 1.78 + nsCOMPtr<nsIParser> rv = new nsHtml5Parser(); 1.79 + return rv.forget(); 1.80 +} 1.81 + 1.82 +// static 1.83 +nsresult 1.84 +nsHtml5Module::Initialize(nsIParser* aParser, nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, nsIChannel* aChannel) 1.85 +{ 1.86 + NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized."); 1.87 + nsHtml5Parser* parser = static_cast<nsHtml5Parser*> (aParser); 1.88 + return parser->Initialize(aDoc, aURI, aContainer, aChannel); 1.89 +} 1.90 + 1.91 +class nsHtml5ParserThreadTerminator MOZ_FINAL : public nsIObserver 1.92 +{ 1.93 + public: 1.94 + NS_DECL_ISUPPORTS 1.95 + nsHtml5ParserThreadTerminator(nsIThread* aThread) 1.96 + : mThread(aThread) 1.97 + {} 1.98 + NS_IMETHODIMP Observe(nsISupports *, const char *topic, const char16_t *) 1.99 + { 1.100 + NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"), 1.101 + "Unexpected topic"); 1.102 + if (mThread) { 1.103 + mThread->Shutdown(); 1.104 + mThread = nullptr; 1.105 + } 1.106 + return NS_OK; 1.107 + } 1.108 + private: 1.109 + nsCOMPtr<nsIThread> mThread; 1.110 +}; 1.111 + 1.112 +NS_IMPL_ISUPPORTS(nsHtml5ParserThreadTerminator, nsIObserver) 1.113 + 1.114 +// static 1.115 +nsIThread* 1.116 +nsHtml5Module::GetStreamParserThread() 1.117 +{ 1.118 + if (sOffMainThread) { 1.119 + if (!sStreamParserThread) { 1.120 + NS_NewNamedThread("HTML5 Parser", &sStreamParserThread); 1.121 + NS_ASSERTION(sStreamParserThread, "Thread creation failed!"); 1.122 + nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); 1.123 + NS_ASSERTION(os, "do_GetService failed"); 1.124 + os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread), 1.125 + "xpcom-shutdown-threads", 1.126 + false); 1.127 + } 1.128 + return sStreamParserThread; 1.129 + } 1.130 + if (!sMainThread) { 1.131 + NS_GetMainThread(&sMainThread); 1.132 + NS_ASSERTION(sMainThread, "Main thread getter failed"); 1.133 + } 1.134 + return sMainThread; 1.135 +} 1.136 + 1.137 +#ifdef DEBUG 1.138 +bool nsHtml5Module::sNsHtml5ModuleInitialized = false; 1.139 +#endif