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"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/NullPrincipal.h"
18 StaticRefPtr
<ConsoleUtils
> gConsoleUtilsService
;
23 ConsoleUtils
* ConsoleUtils::GetOrCreate() {
24 if (!gConsoleUtilsService
) {
25 MOZ_ASSERT(NS_IsMainThread());
27 gConsoleUtilsService
= new ConsoleUtils();
28 ClearOnShutdown(&gConsoleUtilsService
);
31 return gConsoleUtilsService
;
34 ConsoleUtils::ConsoleUtils() = default;
35 ConsoleUtils::~ConsoleUtils() = default;
38 void ConsoleUtils::ReportForServiceWorkerScope(const nsAString
& aScope
,
39 const nsAString
& aMessage
,
40 const nsAString
& aFilename
,
42 uint32_t aColumnNumber
,
44 MOZ_ASSERT(NS_IsMainThread());
46 RefPtr
<ConsoleUtils
> service
= ConsoleUtils::GetOrCreate();
47 if (NS_WARN_IF(!service
)) {
51 service
->ReportForServiceWorkerScopeInternal(
52 aScope
, aMessage
, aFilename
, aLineNumber
, aColumnNumber
, aLevel
);
55 void ConsoleUtils::ReportForServiceWorkerScopeInternal(
56 const nsAString
& aScope
, const nsAString
& aMessage
,
57 const nsAString
& aFilename
, uint32_t aLineNumber
, uint32_t aColumnNumber
,
59 MOZ_ASSERT(NS_IsMainThread());
64 JSContext
* cx
= jsapi
.cx();
66 ConsoleCommon::ClearException
ce(cx
);
67 JS::Rooted
<JSObject
*> global(cx
, GetOrCreateSandbox(cx
));
68 if (NS_WARN_IF(!global
)) {
72 // The GetOrCreateSandbox call returns a proxy to the actual sandbox object.
73 // We don't need a proxy here.
74 global
= js::UncheckedUnwrap(global
);
76 JSAutoRealm
ar(cx
, global
);
78 RootedDictionary
<ConsoleEvent
> event(cx
);
80 event
.mID
.Construct();
81 event
.mID
.Value().SetAsString() = aScope
;
83 event
.mInnerID
.Construct();
84 event
.mInnerID
.Value().SetAsString() = NS_LITERAL_STRING("ServiceWorker");
88 event
.mLevel
= NS_LITERAL_STRING("log");
92 event
.mLevel
= NS_LITERAL_STRING("warn");
96 event
.mLevel
= NS_LITERAL_STRING("error");
100 event
.mFilename
= aFilename
;
101 event
.mLineNumber
= aLineNumber
;
102 event
.mColumnNumber
= aColumnNumber
;
103 event
.mTimeStamp
= JS_Now() / PR_USEC_PER_MSEC
;
105 JS::Rooted
<JS::Value
> messageValue(cx
);
106 if (!dom::ToJSValue(cx
, aMessage
, &messageValue
)) {
110 event
.mArguments
.Construct();
111 if (!event
.mArguments
.Value().AppendElement(messageValue
, fallible
)) {
115 nsCOMPtr
<nsIConsoleAPIStorage
> storage
=
116 do_GetService("@mozilla.org/consoleAPI-storage;1");
118 if (NS_WARN_IF(!storage
)) {
122 JS::Rooted
<JS::Value
> eventValue(cx
);
123 if (!ToJSValue(cx
, event
, &eventValue
)) {
127 // This is a legacy property.
128 JS::Rooted
<JSObject
*> eventObj(cx
, &eventValue
.toObject());
129 if (NS_WARN_IF(!JS_DefineProperty(cx
, eventObj
, "wrappedJSObject", eventObj
,
130 JSPROP_ENUMERATE
))) {
134 storage
->RecordEvent(NS_LITERAL_STRING("ServiceWorker"), aScope
, eventValue
);
137 JSObject
* ConsoleUtils::GetOrCreateSandbox(JSContext
* aCx
) {
138 AssertIsOnMainThread();
141 nsIXPConnect
* xpc
= nsContentUtils::XPConnect();
142 MOZ_ASSERT(xpc
, "This should never be null!");
144 RefPtr
<NullPrincipal
> nullPrincipal
=
145 NullPrincipal::CreateWithoutOriginAttributes();
147 JS::Rooted
<JSObject
*> sandbox(aCx
);
148 nsresult rv
= xpc
->CreateSandbox(aCx
, nullPrincipal
, sandbox
.address());
149 if (NS_WARN_IF(NS_FAILED(rv
))) {
153 mSandbox
= new JSObjectHolder(aCx
, sandbox
);
156 return mSandbox
->GetJSObject();
160 } // namespace mozilla