PageRenderTime 34ms CodeModel.GetById 17ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

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