Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / ipc / MMPrinter.cpp
blobbe2911341ae40597a846345ca820c82ef11ed848
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 "MMPrinter.h"
9 #include "jsapi.h"
10 #include "nsJSUtils.h"
11 #include "Logging.h"
12 #include "mozilla/Bootstrap.h"
13 #include "mozilla/dom/ipc/StructuredCloneData.h"
14 #include "mozilla/dom/ScriptSettings.h"
15 #include "mozilla/ErrorResult.h"
16 #include "mozilla/RandomNum.h"
17 #include "nsFrameMessageManager.h"
18 #include "prenv.h"
20 namespace mozilla::dom {
22 LazyLogModule MMPrinter::sMMLog("MessageManager");
24 // You can use
25 // https://gist.github.com/tomrittervg/adb8688426a9a5340da96004e2c8af79 to parse
26 // the output of the logs into logs more friendly to reading.
28 /* static */
29 void MMPrinter::PrintImpl(char const* aLocation, const nsAString& aMsg,
30 ClonedMessageData const& aData) {
31 NS_ConvertUTF16toUTF8 charMsg(aMsg);
34 * The topic will be skipped if the topic name appears anywhere as a substring
35 * of the filter.
37 * Example:
38 * MOZ_LOG_MESSAGEMANAGER_SKIP="foobar|extension"
39 * Will match the topics 'foobar', 'foo', 'bar', and 'ten' (even though
40 * you may not have intended to match the latter three) and it will not match
41 * the topics 'extensionresult' or 'Foo'.
43 char* mmSkipLog = PR_GetEnv("MOZ_LOG_MESSAGEMANAGER_SKIP");
45 if (mmSkipLog && strstr(mmSkipLog, charMsg.get())) {
46 return;
49 uint64_t msg_id = RandomUint64OrDie();
51 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Debug,
52 ("%" PRIu64 " %s Message: %s in process type: %s", msg_id, aLocation,
53 charMsg.get(), XRE_GetProcessTypeString()));
55 if (!MOZ_LOG_TEST(sMMLog, LogLevel::Verbose)) {
56 return;
59 ErrorResult rv;
61 AutoJSAPI jsapi;
62 // We're using this context to deserialize, stringify, and print a message
63 // manager message here. Since the messages are always sent from and to system
64 // scopes, we need to do this in a system scope, or attempting to deserialize
65 // certain privileged objects will fail.
66 MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
67 JSContext* cx = jsapi.cx();
69 ipc::StructuredCloneData data;
70 ipc::UnpackClonedMessageData(aData, data);
72 /* Read original StructuredCloneData. */
73 JS::Rooted<JS::Value> scdContent(cx);
74 data.Read(cx, &scdContent, rv);
75 if (rv.Failed()) {
76 // In testing, the only reason this would fail was if there was no data in
77 // the message; so it seems like this is safe-ish.
78 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Verbose,
79 ("%" PRIu64 " (No Data)", msg_id));
80 rv.SuppressException();
81 return;
84 JS::Rooted<JSString*> unevalObj(cx, JS_ValueToSource(cx, scdContent));
85 nsAutoJSString srcString;
86 if (!srcString.init(cx, unevalObj)) return;
88 MOZ_LOG(MMPrinter::sMMLog, LogLevel::Verbose,
89 ("%" PRIu64 " %s", msg_id, NS_ConvertUTF16toUTF8(srcString).get()));
92 } // namespace mozilla::dom