Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/parser/html/nsHtml5Module.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
4
5
#include "nsHtml5Module.h"
6
#include "mozilla/Attributes.h"
7
#include "mozilla/Preferences.h"
8
#include "mozilla/Services.h"
9
#include "mozilla/StaticPrefs.h"
10
#include "nsHtml5AttributeName.h"
11
#include "nsHtml5ElementName.h"
12
#include "nsHtml5HtmlAttributes.h"
13
#include "nsHtml5NamedCharacters.h"
14
#include "nsHtml5Portability.h"
15
#include "nsHtml5StackNode.h"
16
#include "nsHtml5Tokenizer.h"
17
#include "nsHtml5TreeBuilder.h"
18
#include "nsHtml5UTF16Buffer.h"
19
#include "nsIObserverService.h"
20
#include "nsIServiceManager.h"
21
22
using namespace mozilla;
23
24
// static
25
nsIThread* nsHtml5Module::sStreamParserThread = nullptr;
26
nsIThread* nsHtml5Module::sMainThread = nullptr;
27
28
// static
29
void
30
nsHtml5Module::InitializeStatics()
31
3
{
32
3
  nsHtml5AttributeName::initializeStatics();
33
3
  nsHtml5ElementName::initializeStatics();
34
3
  nsHtml5HtmlAttributes::initializeStatics();
35
3
  nsHtml5NamedCharacters::initializeStatics();
36
3
  nsHtml5Portability::initializeStatics();
37
3
  nsHtml5StackNode::initializeStatics();
38
3
  nsHtml5Tokenizer::initializeStatics();
39
3
  nsHtml5TreeBuilder::initializeStatics();
40
3
  nsHtml5UTF16Buffer::initializeStatics();
41
#ifdef DEBUG
42
  sNsHtml5ModuleInitialized = true;
43
#endif
44
}
45
46
// static
47
void
48
nsHtml5Module::ReleaseStatics()
49
0
{
50
#ifdef DEBUG
51
  sNsHtml5ModuleInitialized = false;
52
#endif
53
  nsHtml5AttributeName::releaseStatics();
54
0
  nsHtml5ElementName::releaseStatics();
55
0
  nsHtml5HtmlAttributes::releaseStatics();
56
0
  nsHtml5NamedCharacters::releaseStatics();
57
0
  nsHtml5Portability::releaseStatics();
58
0
  nsHtml5StackNode::releaseStatics();
59
0
  nsHtml5Tokenizer::releaseStatics();
60
0
  nsHtml5TreeBuilder::releaseStatics();
61
0
  nsHtml5UTF16Buffer::releaseStatics();
62
0
  NS_IF_RELEASE(sStreamParserThread);
63
0
  NS_IF_RELEASE(sMainThread);
64
0
}
65
66
// static
67
already_AddRefed<nsIParser>
68
nsHtml5Module::NewHtml5Parser()
69
0
{
70
0
  MOZ_ASSERT(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
71
0
  nsCOMPtr<nsIParser> rv = new nsHtml5Parser();
72
0
  return rv.forget();
73
0
}
74
75
// static
76
nsresult
77
nsHtml5Module::Initialize(nsIParser* aParser,
78
                          nsIDocument* aDoc,
79
                          nsIURI* aURI,
80
                          nsISupports* aContainer,
81
                          nsIChannel* aChannel)
82
0
{
83
0
  MOZ_ASSERT(sNsHtml5ModuleInitialized, "nsHtml5Module not initialized.");
84
0
  nsHtml5Parser* parser = static_cast<nsHtml5Parser*>(aParser);
85
0
  return parser->Initialize(aDoc, aURI, aContainer, aChannel);
86
0
}
87
88
class nsHtml5ParserThreadTerminator final : public nsIObserver
89
{
90
public:
91
  NS_DECL_ISUPPORTS
92
  explicit nsHtml5ParserThreadTerminator(nsIThread* aThread)
93
    : mThread(aThread)
94
0
  {
95
0
  }
96
  NS_IMETHOD Observe(nsISupports*, const char* topic, const char16_t*) override
97
0
  {
98
0
    NS_ASSERTION(!strcmp(topic, "xpcom-shutdown-threads"), "Unexpected topic");
99
0
    if (mThread) {
100
0
      mThread->Shutdown();
101
0
      mThread = nullptr;
102
0
    }
103
0
    return NS_OK;
104
0
  }
105
106
private:
107
0
  ~nsHtml5ParserThreadTerminator() {}
108
109
  nsCOMPtr<nsIThread> mThread;
110
};
111
112
NS_IMPL_ISUPPORTS(nsHtml5ParserThreadTerminator, nsIObserver)
113
114
// static
115
nsIThread*
116
nsHtml5Module::GetStreamParserThread()
117
0
{
118
0
  if (StaticPrefs::html5_offmainthread()) {
119
0
    if (!sStreamParserThread) {
120
0
      NS_NewNamedThread("HTML5 Parser", &sStreamParserThread);
121
0
      NS_ASSERTION(sStreamParserThread, "Thread creation failed!");
122
0
      nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
123
0
      NS_ASSERTION(os, "do_GetService failed");
124
0
      os->AddObserver(new nsHtml5ParserThreadTerminator(sStreamParserThread),
125
0
                      "xpcom-shutdown-threads",
126
0
                      false);
127
0
    }
128
0
    return sStreamParserThread;
129
0
  }
130
0
  if (!sMainThread) {
131
0
    NS_GetMainThread(&sMainThread);
132
0
    NS_ASSERTION(sMainThread, "Main thread getter failed");
133
0
  }
134
0
  return sMainThread;
135
0
}
136
137
#ifdef DEBUG
138
bool nsHtml5Module::sNsHtml5ModuleInitialized = false;
139
#endif