/parser/html/nsHtml5Module.cpp

http://github.com/zpao/v8monkey · C++ · 169 lines · 116 code · 11 blank · 42 comment · 4 complexity · 0212af1483f12343d2a8d59f2b5b6033 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  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/
  8. *
  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.
  13. *
  14. * The Original Code is HTML Parser C++ Translator code.
  15. *
  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.
  20. *
  21. * Contributor(s):
  22. * Henri Sivonen <hsivonen@iki.fi>
  23. *
  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.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #include "nsHtml5AttributeName.h"
  38. #include "nsHtml5ElementName.h"
  39. #include "nsHtml5HtmlAttributes.h"
  40. #include "nsHtml5NamedCharacters.h"
  41. #include "nsHtml5Portability.h"
  42. #include "nsHtml5StackNode.h"
  43. #include "nsHtml5Tokenizer.h"
  44. #include "nsHtml5TreeBuilder.h"
  45. #include "nsHtml5UTF16Buffer.h"
  46. #include "nsHtml5Module.h"
  47. #include "nsIObserverService.h"
  48. #include "nsIServiceManager.h"
  49. #include "mozilla/Services.h"
  50. #include "mozilla/Preferences.h"
  51. using namespace mozilla;
  52. // static
  53. bool nsHtml5Module::sEnabled = true;
  54. bool nsHtml5Module::sOffMainThread = true;
  55. nsIThread* nsHtml5Module::sStreamParserThread = nsnull;
  56. nsIThread* nsHtml5Module::sMainThread = nsnull;
  57. // static
  58. void
  59. nsHtml5Module::InitializeStatics()
  60. {
  61. Preferences::AddBoolVarCache(&sOffMainThread, "html5.offmainthread");
  62. nsHtml5Atoms::AddRefAtoms();
  63. nsHtml5AttributeName::initializeStatics();
  64. nsHtml5ElementName::initializeStatics();
  65. nsHtml5HtmlAttributes::initializeStatics();
  66. nsHtml5NamedCharacters::initializeStatics();
  67. nsHtml5Portability::initializeStatics();
  68. nsHtml5StackNode::initializeStatics();
  69. nsHtml5Tokenizer::initializeStatics();
  70. nsHtml5TreeBuilder::initializeStatics();
  71. nsHtml5UTF16Buffer::initializeStatics();
  72. nsHtml5StreamParser::InitializeStatics();
  73. #ifdef DEBUG
  74. sNsHtml5ModuleInitialized = true;
  75. #endif
  76. }
  77. // static
  78. void
  79. nsHtml5Module::ReleaseStatics()
  80. {
  81. #ifdef DEBUG
  82. sNsHtml5ModuleInitialized = false;
  83. #endif
  84. nsHtml5AttributeName::releaseStatics();
  85. nsHtml5ElementName::releaseStatics();
  86. nsHtml5HtmlAttributes::releaseStatics();
  87. nsHtml5NamedCharacters::releaseStatics();
  88. nsHtml5Portability::releaseStatics();
  89. nsHtml5StackNode::releaseStatics();
  90. nsHtml5Tokenizer::releaseStatics();
  91. nsHtml5TreeBuilder::releaseStatics();
  92. nsHtml5UTF16Buffer::releaseStatics();
  93. NS_IF_RELEASE(sStreamParserThread);
  94. NS_IF_RELEASE(sMainThread);
  95. }
  96. // static
  97. already_AddRefed<nsIParser>
  98. nsHtml5Module::NewHtml5Parser()
  99. {
  100. NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
  101. nsIParser* rv = static_cast<nsIParser*> (new nsHtml5Parser());
  102. NS_ADDREF(rv);
  103. return rv;
  104. }
  105. // static
  106. nsresult
  107. nsHtml5Module::Initialize(nsIParser* aParser, nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, nsIChannel* aChannel)
  108. {
  109. NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
  110. nsHtml5Parser* parser = static_cast<nsHtml5Parser*> (aParser);
  111. return parser->Initialize(aDoc, aURI, aContainer, aChannel);
  112. }
  113. class nsHtml5ParserThreadTerminator : public nsIObserver
  114. {
  115. public:
  116. NS_DECL_ISUPPORTS
  117. nsHtml5ParserThreadTerminator(nsIThread* aThread)
  118. : mThread(aThread)
  119. {}
  120. NS_IMETHODIMP Observe(nsISupports *, const char *topic, const PRUnichar *)
  121. {
  122. NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"),
  123. "Unexpected topic");
  124. if (mThread) {
  125. mThread->Shutdown();
  126. mThread = nsnull;
  127. }
  128. return NS_OK;
  129. }
  130. private:
  131. nsCOMPtr<nsIThread> mThread;
  132. };
  133. NS_IMPL_ISUPPORTS1(nsHtml5ParserThreadTerminator, nsIObserver)
  134. // static
  135. nsIThread*
  136. nsHtml5Module::GetStreamParserThread()
  137. {
  138. if (sOffMainThread) {
  139. if (!sStreamParserThread) {
  140. NS_NewThread(&sStreamParserThread);
  141. NS_ASSERTION(sStreamParserThread, "Thread creation failed!");
  142. nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
  143. NS_ASSERTION(os, "do_GetService failed");
  144. os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread),
  145. "xpcom-shutdown-threads",
  146. false);
  147. }
  148. return sStreamParserThread;
  149. }
  150. if (!sMainThread) {
  151. NS_GetMainThread(&sMainThread);
  152. NS_ASSERTION(sMainThread, "Main thread getter failed");
  153. }
  154. return sMainThread;
  155. }
  156. #ifdef DEBUG
  157. bool nsHtml5Module::sNsHtml5ModuleInitialized = false;
  158. #endif