parser/html/nsHtml5Module.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "nsHtml5AttributeName.h"
     6 #include "nsHtml5ElementName.h"
     7 #include "nsHtml5HtmlAttributes.h"
     8 #include "nsHtml5NamedCharacters.h"
     9 #include "nsHtml5Portability.h"
    10 #include "nsHtml5StackNode.h"
    11 #include "nsHtml5Tokenizer.h"
    12 #include "nsHtml5TreeBuilder.h"
    13 #include "nsHtml5UTF16Buffer.h"
    14 #include "nsHtml5Module.h"
    15 #include "nsIObserverService.h"
    16 #include "nsIServiceManager.h"
    17 #include "mozilla/Services.h"
    18 #include "mozilla/Preferences.h"
    19 #include "mozilla/Attributes.h"
    21 using namespace mozilla;
    23 // static
    24 bool nsHtml5Module::sOffMainThread = true;
    25 nsIThread* nsHtml5Module::sStreamParserThread = nullptr;
    26 nsIThread* nsHtml5Module::sMainThread = nullptr;
    28 // static
    29 void
    30 nsHtml5Module::InitializeStatics()
    31 {
    32   Preferences::AddBoolVarCache(&sOffMainThread, "html5.offmainthread");
    33   nsHtml5Atoms::AddRefAtoms();
    34   nsHtml5AttributeName::initializeStatics();
    35   nsHtml5ElementName::initializeStatics();
    36   nsHtml5HtmlAttributes::initializeStatics();
    37   nsHtml5NamedCharacters::initializeStatics();
    38   nsHtml5Portability::initializeStatics();
    39   nsHtml5StackNode::initializeStatics();
    40   nsHtml5Tokenizer::initializeStatics();
    41   nsHtml5TreeBuilder::initializeStatics();
    42   nsHtml5UTF16Buffer::initializeStatics();
    43   nsHtml5StreamParser::InitializeStatics();
    44   nsHtml5TreeOpExecutor::InitializeStatics();
    45 #ifdef DEBUG
    46   sNsHtml5ModuleInitialized = true;
    47 #endif
    48 }
    50 // static
    51 void
    52 nsHtml5Module::ReleaseStatics()
    53 {
    54 #ifdef DEBUG
    55   sNsHtml5ModuleInitialized = false;
    56 #endif
    57   nsHtml5AttributeName::releaseStatics();
    58   nsHtml5ElementName::releaseStatics();
    59   nsHtml5HtmlAttributes::releaseStatics();
    60   nsHtml5NamedCharacters::releaseStatics();
    61   nsHtml5Portability::releaseStatics();
    62   nsHtml5StackNode::releaseStatics();
    63   nsHtml5Tokenizer::releaseStatics();
    64   nsHtml5TreeBuilder::releaseStatics();
    65   nsHtml5UTF16Buffer::releaseStatics();
    66   NS_IF_RELEASE(sStreamParserThread);
    67   NS_IF_RELEASE(sMainThread);
    68 }
    70 // static
    71 already_AddRefed<nsIParser>
    72 nsHtml5Module::NewHtml5Parser()
    73 {
    74   NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
    75   nsCOMPtr<nsIParser> rv = new nsHtml5Parser();
    76   return rv.forget();
    77 }
    79 // static
    80 nsresult
    81 nsHtml5Module::Initialize(nsIParser* aParser, nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, nsIChannel* aChannel)
    82 {
    83   NS_ABORT_IF_FALSE(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
    84   nsHtml5Parser* parser = static_cast<nsHtml5Parser*> (aParser);
    85   return parser->Initialize(aDoc, aURI, aContainer, aChannel);
    86 }
    88 class nsHtml5ParserThreadTerminator MOZ_FINAL : public nsIObserver
    89 {
    90   public:
    91     NS_DECL_ISUPPORTS
    92     nsHtml5ParserThreadTerminator(nsIThread* aThread)
    93       : mThread(aThread)
    94     {}
    95     NS_IMETHODIMP Observe(nsISupports *, const char *topic, const char16_t *)
    96     {
    97       NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"), 
    98                    "Unexpected topic");
    99       if (mThread) {
   100         mThread->Shutdown();
   101         mThread = nullptr;
   102       }
   103       return NS_OK;
   104     }
   105   private:
   106     nsCOMPtr<nsIThread> mThread;
   107 };
   109 NS_IMPL_ISUPPORTS(nsHtml5ParserThreadTerminator, nsIObserver)
   111 // static 
   112 nsIThread*
   113 nsHtml5Module::GetStreamParserThread()
   114 {
   115   if (sOffMainThread) {
   116     if (!sStreamParserThread) {
   117       NS_NewNamedThread("HTML5 Parser", &sStreamParserThread);
   118       NS_ASSERTION(sStreamParserThread, "Thread creation failed!");
   119       nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   120       NS_ASSERTION(os, "do_GetService failed");
   121       os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread), 
   122                       "xpcom-shutdown-threads",
   123                       false);
   124     }
   125     return sStreamParserThread;
   126   }
   127   if (!sMainThread) {
   128     NS_GetMainThread(&sMainThread);
   129     NS_ASSERTION(sMainThread, "Main thread getter failed");
   130   }
   131   return sMainThread;
   132 }
   134 #ifdef DEBUG
   135 bool nsHtml5Module::sNsHtml5ModuleInitialized = false;
   136 #endif

mercurial