Bug 1610775 [wpt PR 21336] - Update urllib3 to 1.25.8, a=testonly
[gecko.git] / dom / console / ConsoleUtils.cpp
blob7cc5ebb8208ea55c06b681da40b82ae676fac06d
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"
13 namespace mozilla {
14 namespace dom {
16 namespace {
18 StaticRefPtr<ConsoleUtils> gConsoleUtilsService;
22 /* static */
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;
37 /* static */
38 void ConsoleUtils::ReportForServiceWorkerScope(const nsAString& aScope,
39 const nsAString& aMessage,
40 const nsAString& aFilename,
41 uint32_t aLineNumber,
42 uint32_t aColumnNumber,
43 Level aLevel) {
44 MOZ_ASSERT(NS_IsMainThread());
46 RefPtr<ConsoleUtils> service = ConsoleUtils::GetOrCreate();
47 if (NS_WARN_IF(!service)) {
48 return;
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,
58 Level aLevel) {
59 MOZ_ASSERT(NS_IsMainThread());
61 AutoJSAPI jsapi;
62 jsapi.Init();
64 JSContext* cx = jsapi.cx();
66 ConsoleCommon::ClearException ce(cx);
67 JS::Rooted<JSObject*> global(cx, GetOrCreateSandbox(cx));
68 if (NS_WARN_IF(!global)) {
69 return;
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");
86 switch (aLevel) {
87 case eLog:
88 event.mLevel = NS_LITERAL_STRING("log");
89 break;
91 case eWarning:
92 event.mLevel = NS_LITERAL_STRING("warn");
93 break;
95 case eError:
96 event.mLevel = NS_LITERAL_STRING("error");
97 break;
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)) {
107 return;
110 event.mArguments.Construct();
111 if (!event.mArguments.Value().AppendElement(messageValue, fallible)) {
112 return;
115 nsCOMPtr<nsIConsoleAPIStorage> storage =
116 do_GetService("@mozilla.org/consoleAPI-storage;1");
118 if (NS_WARN_IF(!storage)) {
119 return;
122 JS::Rooted<JS::Value> eventValue(cx);
123 if (!ToJSValue(cx, event, &eventValue)) {
124 return;
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))) {
131 return;
134 storage->RecordEvent(NS_LITERAL_STRING("ServiceWorker"), aScope, eventValue);
137 JSObject* ConsoleUtils::GetOrCreateSandbox(JSContext* aCx) {
138 AssertIsOnMainThread();
140 if (!mSandbox) {
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))) {
150 return nullptr;
153 mSandbox = new JSObjectHolder(aCx, sandbox);
156 return mSandbox->GetJSObject();
159 } // namespace dom
160 } // namespace mozilla