1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { console, ConsoleAPI } = ChromeUtils.importESModule(
"resource://gre/modules/Console.sys.mjs"
);
const {
ConsoleAPIListener,
} = require("resource://devtools/server/actors/webconsole/listeners/console-api.js");
var seenMessages = 0;
var seenTypes = 0;
var onConsoleAPICall = function (message) {
if (message.consoleID && message.consoleID == "addon/foo") {
Assert.equal(message.level, "warn");
Assert.equal(message.arguments[0], "Warning from foo");
seenTypes |= 1;
} else if (message.addonId == "bar") {
Assert.equal(message.level, "error");
Assert.equal(message.arguments[0], "Error from bar");
seenTypes |= 2;
} else {
Assert.equal(message.level, "log");
Assert.equal(message.arguments[0], "Hello from default console");
seenTypes |= 4;
}
seenMessages++;
};
let policy;
registerCleanupFunction(() => {
policy.active = false;
});
function createFakeAddonWindow({ addonId } = {}) {
const uuidGen = Services.uuid;
const uuid = uuidGen.generateUUID().number.slice(1, -1);
if (policy) {
policy.active = false;
}
/* globals MatchPatternSet, WebExtensionPolicy */
policy = new WebExtensionPolicy({
id: addonId,
mozExtensionHostname: uuid,
baseURL: "file:///",
allowedOrigins: new MatchPatternSet([]),
localizeCallback() {},
});
policy.active = true;
const baseURI = Services.io.newURI(`moz-extension://${uuid}/`);
const principal = Services.scriptSecurityManager.createContentPrincipal(
baseURI,
{}
);
const chromeWebNav = Services.appShell.createWindowlessBrowser(true);
const { docShell } = chromeWebNav;
docShell.createAboutBlankContentViewer(principal, principal);
const addonWindow = docShell.contentViewer.DOMDocument.defaultView;
return { addonWindow, chromeWebNav };
}
/**
* Tests that the consoleID property of the ConsoleAPI options gets passed
* through to console messages.
*/
function run_test() {
// console1 Test Console.sys.mjs messages tagged by the Addon SDK
// are still filtered correctly.
const console1 = new ConsoleAPI({
consoleID: "addon/foo",
});
// console2 - WebExtension page's console messages tagged
// by 'originAttributes.addonId' are filtered correctly.
const { addonWindow, chromeWebNav } = createFakeAddonWindow({
addonId: "bar",
});
const console2 = addonWindow.console;
// console - Plain console object (messages are tagged with window ids
// and originAttributes, but the addonId will be empty).
console.log("Hello from default console");
console1.warn("Warning from foo");
console2.error("Error from bar");
let listener = new ConsoleAPIListener(null, onConsoleAPICall);
listener.init();
let messages = listener.getCachedMessages();
seenTypes = 0;
seenMessages = 0;
messages.forEach(onConsoleAPICall);
Assert.equal(seenMessages, 3);
Assert.equal(seenTypes, 7);
seenTypes = 0;
seenMessages = 0;
console.log("Hello from default console");
console1.warn("Warning from foo");
console2.error("Error from bar");
Assert.equal(seenMessages, 3);
Assert.equal(seenTypes, 7);
listener.destroy();
listener = new ConsoleAPIListener(null, onConsoleAPICall, { addonId: "foo" });
listener.init();
messages = listener.getCachedMessages();
seenTypes = 0;
seenMessages = 0;
messages.forEach(onConsoleAPICall);
Assert.equal(seenMessages, 2);
Assert.equal(seenTypes, 1);
seenTypes = 0;
seenMessages = 0;
console.log("Hello from default console");
console1.warn("Warning from foo");
console2.error("Error from bar");
Assert.equal(seenMessages, 1);
Assert.equal(seenTypes, 1);
listener.destroy();
listener = new ConsoleAPIListener(null, onConsoleAPICall, { addonId: "bar" });
listener.init();
messages = listener.getCachedMessages();
seenTypes = 0;
seenMessages = 0;
messages.forEach(onConsoleAPICall);
Assert.equal(seenMessages, 3);
Assert.equal(seenTypes, 2);
seenTypes = 0;
seenMessages = 0;
console.log("Hello from default console");
console1.warn("Warning from foo");
console2.error("Error from bar");
Assert.equal(seenMessages, 1);
Assert.equal(seenTypes, 2);
listener.destroy();
// Close the addon window's chromeWebNav.
chromeWebNav.close();
}
|