Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/console/ConsoleInstance.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/dom/ConsoleInstance.h"
8
#include "mozilla/dom/ConsoleBinding.h"
9
#include "ConsoleCommon.h"
10
#include "ConsoleUtils.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ConsoleInstance, mConsole)
16
17
NS_IMPL_CYCLE_COLLECTING_ADDREF(ConsoleInstance)
18
NS_IMPL_CYCLE_COLLECTING_RELEASE(ConsoleInstance)
19
20
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ConsoleInstance)
21
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
22
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23
0
NS_INTERFACE_MAP_END
24
25
namespace {
26
27
ConsoleLogLevel
28
PrefToValue(const nsAString& aPref)
29
0
{
30
0
  if (!NS_IsMainThread()) {
31
0
    NS_WARNING("Console.maxLogLevelPref is not supported on workers!");
32
0
    return ConsoleLogLevel::All;
33
0
  }
34
0
35
0
  NS_ConvertUTF16toUTF8 pref(aPref);
36
0
  nsAutoCString value;
37
0
  nsresult rv = Preferences::GetCString(pref.get(), value);
38
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
39
0
    nsString message;
40
0
    message.AssignLiteral("Console.maxLogLevelPref used with a non-existing pref: ");
41
0
    message.Append(aPref);
42
0
43
0
    nsContentUtils::LogSimpleConsoleError(message, "chrome", false);
44
0
    return ConsoleLogLevel::All;
45
0
  }
46
0
47
0
  int index = FindEnumStringIndexImpl(value.get(), value.Length(),
48
0
                                      ConsoleLogLevelValues::strings);
49
0
  if (NS_WARN_IF(index < 0)) {
50
0
    nsString message;
51
0
    message.AssignLiteral("Invalid Console.maxLogLevelPref value: ");
52
0
    message.Append(NS_ConvertUTF8toUTF16(value));
53
0
54
0
    nsContentUtils::LogSimpleConsoleError(message, "chrome", false);
55
0
    return ConsoleLogLevel::All;
56
0
  }
57
0
58
0
  MOZ_ASSERT(index < (int)ConsoleLogLevel::EndGuard_);
59
0
  return static_cast<ConsoleLogLevel>(index);
60
0
}
61
62
ConsoleUtils::Level
63
WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel)
64
0
{
65
0
  switch (aLevel) {
66
0
    case ConsoleLevel::Log:
67
0
      return ConsoleUtils::eLog;
68
0
    case ConsoleLevel::Warning:
69
0
      return ConsoleUtils::eWarning;
70
0
    case ConsoleLevel::Error:
71
0
      return ConsoleUtils::eError;
72
0
    default:
73
0
      break;
74
0
  }
75
0
76
0
  return ConsoleUtils::eLog;
77
0
}
78
79
} // anonymous
80
81
ConsoleInstance::ConsoleInstance(JSContext* aCx,
82
                                 const ConsoleInstanceOptions& aOptions)
83
  : mConsole(new Console(aCx, nullptr, 0, 0))
84
0
{
85
0
  mConsole->mConsoleID = aOptions.mConsoleID;
86
0
  mConsole->mPassedInnerID = aOptions.mInnerID;
87
0
88
0
  if (aOptions.mDump.WasPassed()) {
89
0
    mConsole->mDumpFunction = &aOptions.mDump.Value();
90
0
  }
91
0
92
0
  mConsole->mPrefix = aOptions.mPrefix;
93
0
94
0
  // Let's inform that this is a custom instance.
95
0
  mConsole->mChromeInstance = true;
96
0
97
0
  if (aOptions.mMaxLogLevel.WasPassed()) {
98
0
    mConsole->mMaxLogLevel = aOptions.mMaxLogLevel.Value();
99
0
  }
100
0
101
0
  if (!aOptions.mMaxLogLevelPref.IsEmpty()) {
102
0
    mConsole->mMaxLogLevel =
103
0
      PrefToValue(aOptions.mMaxLogLevelPref);
104
0
  }
105
0
}
106
107
ConsoleInstance::~ConsoleInstance()
108
0
{}
109
110
JSObject*
111
ConsoleInstance::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
112
0
{
113
0
  return ConsoleInstance_Binding::Wrap(aCx, this, aGivenProto);
114
0
}
115
116
#define METHOD(name, string)                                               \
117
  void                                                                     \
118
  ConsoleInstance::name(JSContext* aCx, const Sequence<JS::Value>& aData)  \
119
0
  {                                                                        \
120
0
    mConsole->MethodInternal(aCx, Console::Method##name,                   \
121
0
                             NS_LITERAL_STRING(string), aData);            \
122
0
  }
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Log(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Info(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Warn(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Error(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Exception(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Debug(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Table(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Trace(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Dir(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Dirxml(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::Group(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
Unexecuted instantiation: mozilla::dom::ConsoleInstance::GroupCollapsed(JSContext*, mozilla::dom::Sequence<JS::Value> const&)
123
124
METHOD(Log, "log")
125
METHOD(Info, "info")
126
METHOD(Warn, "warn")
127
METHOD(Error, "error")
128
METHOD(Exception, "exception")
129
METHOD(Debug, "debug")
130
METHOD(Table, "table")
131
METHOD(Trace, "trace")
132
METHOD(Dir, "dir");
133
METHOD(Dirxml, "dirxml");
134
METHOD(Group, "group")
135
METHOD(GroupCollapsed, "groupCollapsed")
136
137
#undef METHOD
138
139
void
140
ConsoleInstance::GroupEnd(JSContext* aCx)
141
0
{
142
0
  const Sequence<JS::Value> data;
143
0
  mConsole->MethodInternal(aCx, Console::MethodGroupEnd,
144
0
                           NS_LITERAL_STRING("groupEnd"), data);
145
0
}
146
147
void
148
ConsoleInstance::Time(JSContext* aCx, const nsAString& aLabel)
149
0
{
150
0
  mConsole->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
151
0
                                 Console::MethodTime,
152
0
                                 NS_LITERAL_STRING("time"));
153
0
}
154
155
void
156
ConsoleInstance::TimeLog(JSContext* aCx, const nsAString& aLabel,
157
                         const Sequence<JS::Value>& aData)
158
0
{
159
0
  mConsole->StringMethodInternal(aCx, aLabel, aData, Console::MethodTimeLog,
160
0
                                 NS_LITERAL_STRING("timeLog"));
161
0
}
162
163
void
164
ConsoleInstance::TimeEnd(JSContext* aCx, const nsAString& aLabel)
165
0
{
166
0
  mConsole->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
167
0
                                 Console::MethodTimeEnd,
168
0
                                 NS_LITERAL_STRING("timeEnd"));
169
0
}
170
171
void
172
ConsoleInstance::TimeStamp(JSContext* aCx, const JS::Handle<JS::Value> aData)
173
0
{
174
0
  ConsoleCommon::ClearException ce(aCx);
175
0
176
0
  Sequence<JS::Value> data;
177
0
  SequenceRooter<JS::Value> rooter(aCx, &data);
178
0
179
0
  if (aData.isString() && !data.AppendElement(aData, fallible)) {
180
0
    return;
181
0
  }
182
0
183
0
  mConsole->MethodInternal(aCx, Console::MethodTimeStamp,
184
0
                           NS_LITERAL_STRING("timeStamp"), data);
185
0
}
186
187
void
188
ConsoleInstance::Profile(JSContext* aCx, const Sequence<JS::Value>& aData)
189
0
{
190
0
  mConsole->ProfileMethodInternal(aCx, Console::MethodProfile,
191
0
                                  NS_LITERAL_STRING("profile"), aData);
192
0
}
193
194
void
195
ConsoleInstance::ProfileEnd(JSContext* aCx, const Sequence<JS::Value>& aData)
196
0
{
197
0
  mConsole->ProfileMethodInternal(aCx, Console::MethodProfileEnd,
198
0
                                  NS_LITERAL_STRING("profileEnd"), aData);
199
0
}
200
201
void
202
ConsoleInstance::Assert(JSContext* aCx, bool aCondition,
203
                        const Sequence<JS::Value>& aData)
204
0
{
205
0
  if (!aCondition) {
206
0
    mConsole->MethodInternal(aCx, Console::MethodAssert,
207
0
                             NS_LITERAL_STRING("assert"), aData);
208
0
  }
209
0
}
210
211
void
212
ConsoleInstance::Count(JSContext* aCx, const nsAString& aLabel)
213
0
{
214
0
  mConsole->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
215
0
                                 Console::MethodCount,
216
0
                                 NS_LITERAL_STRING("count"));
217
0
}
218
219
void
220
ConsoleInstance::CountReset(JSContext* aCx, const nsAString& aLabel)
221
0
{
222
0
  mConsole->StringMethodInternal(aCx, aLabel, Sequence<JS::Value>(),
223
0
                                 Console::MethodCountReset,
224
0
                                 NS_LITERAL_STRING("countReset"));
225
0
}
226
227
void
228
ConsoleInstance::Clear(JSContext* aCx)
229
0
{
230
0
  const Sequence<JS::Value> data;
231
0
  mConsole->MethodInternal(aCx, Console::MethodClear,
232
0
                           NS_LITERAL_STRING("clear"), data);
233
0
}
234
235
void
236
ConsoleInstance::ReportForServiceWorkerScope(const nsAString& aScope,
237
                                             const nsAString& aMessage,
238
                                             const nsAString& aFilename,
239
                                             uint32_t aLineNumber,
240
                                             uint32_t aColumnNumber,
241
                                             ConsoleLevel aLevel)
242
0
{
243
0
  if (!NS_IsMainThread()) {
244
0
    return;
245
0
  }
246
0
247
0
  ConsoleUtils::ReportForServiceWorkerScope(aScope, aMessage, aFilename,
248
0
                                            aLineNumber, aColumnNumber,
249
0
                                            WebIDLevelToConsoleUtilsLevel(aLevel));
250
0
}
251
252
} // namespace dom
253
} // namespace mozilla