browser_ConsoleAPI_originAttributes.js (2880B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService( 5 Ci.nsIConsoleAPIStorage 6 ); 7 8 const { WebExtensionPolicy } = Cu.getGlobalForObject( 9 ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs") 10 ); 11 12 const FAKE_ADDON_ID = "test-webext-addon@mozilla.org"; 13 const EXPECTED_CONSOLE_ID = `addon/${FAKE_ADDON_ID}`; 14 const EXPECTED_CONSOLE_MESSAGE_CONTENT = "fake-webext-addon-test-log-message"; 15 16 const ConsoleObserver = { 17 init() { 18 ConsoleAPIStorage.addLogEventListener( 19 this.observe, 20 Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal) 21 ); 22 }, 23 24 uninit() { 25 ConsoleAPIStorage.removeLogEventListener(this.observe); 26 }, 27 28 observe(aSubject) { 29 let consoleAPIMessage = aSubject.wrappedJSObject; 30 31 is( 32 consoleAPIMessage.arguments[0], 33 EXPECTED_CONSOLE_MESSAGE_CONTENT, 34 "the consoleAPIMessage contains the expected message" 35 ); 36 37 is( 38 consoleAPIMessage.addonId, 39 FAKE_ADDON_ID, 40 "the consoleAPImessage originAttributes contains the expected addonId" 41 ); 42 43 let cachedMessages = ConsoleAPIStorage.getEvents().filter(msg => { 44 return msg.addonId == FAKE_ADDON_ID; 45 }); 46 47 is( 48 cachedMessages.length, 49 1, 50 "found the expected cached console messages from the addon" 51 ); 52 is( 53 cachedMessages[0] && cachedMessages[0].addonId, 54 FAKE_ADDON_ID, 55 "the cached message originAttributes contains the expected addonId" 56 ); 57 58 finish(); 59 }, 60 }; 61 62 function test() { 63 ConsoleObserver.init(); 64 65 waitForExplicitFinish(); 66 67 let uuidGenerator = Services.uuid; 68 let uuid = uuidGenerator.generateUUID().number; 69 uuid = uuid.slice(1, -1); // Strip { and } off the UUID. 70 71 const url = `moz-extension://${uuid}/`; 72 let policy = new WebExtensionPolicy({ 73 id: FAKE_ADDON_ID, 74 mozExtensionHostname: uuid, 75 baseURL: "file:///", 76 allowedOrigins: new MatchPatternSet([]), 77 localizeCallback() {}, 78 }); 79 policy.active = true; 80 81 let baseURI = Services.io.newURI(url); 82 let principal = Services.scriptSecurityManager.createContentPrincipal( 83 baseURI, 84 {} 85 ); 86 87 let chromeWebNav = Services.appShell.createWindowlessBrowser(true); 88 let docShell = chromeWebNav.docShell; 89 docShell.createAboutBlankDocumentViewer(principal, principal); 90 91 info("fake webextension docShell created"); 92 93 registerCleanupFunction(function () { 94 policy.active = false; 95 if (chromeWebNav) { 96 chromeWebNav.close(); 97 chromeWebNav = null; 98 } 99 ConsoleObserver.uninit(); 100 }); 101 102 let window = docShell.docViewer.DOMDocument.defaultView; 103 window.console.log(EXPECTED_CONSOLE_MESSAGE_CONTENT); 104 chromeWebNav.close(); 105 chromeWebNav = null; 106 107 info("fake webextension page logged a console api message"); 108 }