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/. */
7 #include "ConsoleUtils.h"
8 #include "ConsoleCommon.h"
9 #include "nsContentUtils.h"
10 #include "nsIConsoleAPIStorage.h"
11 #include "nsIXPConnect.h"
12 #include "nsServiceManagerUtils.h"
14 #include "mozilla/ClearOnShutdown.h"
15 #include "mozilla/NullPrincipal.h"
16 #include "mozilla/dom/ConsoleBinding.h"
17 #include "mozilla/dom/ConsoleInstanceBinding.h"
18 #include "mozilla/dom/RootedDictionary.h"
19 #include "mozilla/dom/ScriptSettings.h"
20 #include "js/PropertyAndElement.h" // JS_DefineProperty
22 namespace mozilla::dom
{
26 StaticRefPtr
<ConsoleUtils
> gConsoleUtilsService
;
31 ConsoleUtils
* ConsoleUtils::GetOrCreate() {
32 if (!gConsoleUtilsService
) {
33 MOZ_ASSERT(NS_IsMainThread());
35 gConsoleUtilsService
= new ConsoleUtils();
36 ClearOnShutdown(&gConsoleUtilsService
);
39 return gConsoleUtilsService
;
42 ConsoleUtils::ConsoleUtils() = default;
43 ConsoleUtils::~ConsoleUtils() = default;
46 void ConsoleUtils::ReportForServiceWorkerScope(const nsAString
& aScope
,
47 const nsAString
& aMessage
,
48 const nsACString
& aFilename
,
50 uint32_t aColumnNumber
,
52 MOZ_ASSERT(NS_IsMainThread());
54 RefPtr
<ConsoleUtils
> service
= ConsoleUtils::GetOrCreate();
55 if (NS_WARN_IF(!service
)) {
59 service
->ReportForServiceWorkerScopeInternal(
60 aScope
, aMessage
, aFilename
, aLineNumber
, aColumnNumber
, aLevel
);
63 void ConsoleUtils::ReportForServiceWorkerScopeInternal(
64 const nsAString
& aScope
, const nsAString
& aMessage
,
65 const nsACString
& aFilename
, uint32_t aLineNumber
, uint32_t aColumnNumber
,
67 MOZ_ASSERT(NS_IsMainThread());
72 JSContext
* cx
= jsapi
.cx();
74 ConsoleCommon::ClearException
ce(cx
);
75 JS::Rooted
<JSObject
*> global(cx
, GetOrCreateSandbox(cx
));
76 if (NS_WARN_IF(!global
)) {
80 // The GetOrCreateSandbox call returns a proxy to the actual sandbox object.
81 // We don't need a proxy here.
82 global
= js::UncheckedUnwrap(global
);
84 JSAutoRealm
ar(cx
, global
);
86 RootedDictionary
<ConsoleEvent
> event(cx
);
88 event
.mID
.Construct();
89 event
.mID
.Value().SetAsString() = aScope
;
91 event
.mInnerID
.Construct();
92 event
.mInnerID
.Value().SetAsString() = u
"ServiceWorker"_ns
;
96 event
.mLevel
= u
"log"_ns
;
100 event
.mLevel
= u
"warn"_ns
;
104 event
.mLevel
= u
"error"_ns
;
108 event
.mFilename
= aFilename
;
109 event
.mLineNumber
= aLineNumber
;
110 event
.mColumnNumber
= aColumnNumber
;
111 event
.mTimeStamp
= JS_Now() / PR_USEC_PER_MSEC
;
112 event
.mMicroSecondTimeStamp
= JS_Now();
114 JS::Rooted
<JS::Value
> messageValue(cx
);
115 if (!dom::ToJSValue(cx
, aMessage
, &messageValue
)) {
119 event
.mArguments
.Construct();
120 if (!event
.mArguments
.Value().AppendElement(messageValue
, fallible
)) {
124 nsCOMPtr
<nsIConsoleAPIStorage
> storage
=
125 do_GetService("@mozilla.org/consoleAPI-storage;1");
127 if (NS_WARN_IF(!storage
)) {
131 JS::Rooted
<JS::Value
> eventValue(cx
);
132 if (!ToJSValue(cx
, event
, &eventValue
)) {
136 // This is a legacy property.
137 JS::Rooted
<JSObject
*> eventObj(cx
, &eventValue
.toObject());
138 if (NS_WARN_IF(!JS_DefineProperty(cx
, eventObj
, "wrappedJSObject", eventObj
,
139 JSPROP_ENUMERATE
))) {
143 storage
->RecordEvent(u
"ServiceWorker"_ns
, eventValue
);
146 JSObject
* ConsoleUtils::GetOrCreateSandbox(JSContext
* aCx
) {
147 AssertIsOnMainThread();
150 nsIXPConnect
* xpc
= nsContentUtils::XPConnect();
151 MOZ_ASSERT(xpc
, "This should never be null!");
153 RefPtr
<NullPrincipal
> nullPrincipal
=
154 NullPrincipal::CreateWithoutOriginAttributes();
156 JS::Rooted
<JSObject
*> sandbox(aCx
);
157 nsresult rv
= xpc
->CreateSandbox(aCx
, nullPrincipal
, sandbox
.address());
158 if (NS_WARN_IF(NS_FAILED(rv
))) {
162 mSandbox
= new JSObjectHolder(aCx
, sandbox
);
165 return mSandbox
->GetJSObject();
168 } // namespace mozilla::dom