|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { ExtensionPermissions } = ChromeUtils.import( |
| 4 | + "resource://gre/modules/ExtensionPermissions.jsm" |
| 5 | +); |
| 6 | + |
| 7 | +add_setup(async () => { |
| 8 | + await SpecialPowers.pushPrefEnv({ |
| 9 | + set: [["extensions.manifestV3.enabled", true]], |
| 10 | + }); |
| 11 | +}); |
| 12 | + |
| 13 | +function makeRunAtScript(runAt) { |
| 14 | + return ` |
| 15 | + window.order ??= []; |
| 16 | + window.order.push("${runAt}"); |
| 17 | + browser.test.sendMessage("injected", "order@" + window.order.join()); |
| 18 | + `; |
| 19 | +} |
| 20 | + |
| 21 | +async function makeExtension(id, manifest_version, granted) { |
| 22 | + info(`Loading extension ` + JSON.stringify({ id, granted })); |
| 23 | + |
| 24 | + let manifest = { |
| 25 | + manifest_version, |
| 26 | + browser_specific_settings: { gecko: { id } }, |
| 27 | + permissions: ["activeTab", "scripting"], |
| 28 | + content_scripts: [ |
| 29 | + { |
| 30 | + matches: ["*://*/*"], |
| 31 | + js: ["static.js"], |
| 32 | + }, |
| 33 | + ], |
| 34 | + }; |
| 35 | + |
| 36 | + if (manifest_version === 3) { |
| 37 | + manifest.action = {}; |
| 38 | + } else { |
| 39 | + manifest.browser_action = {}; |
| 40 | + } |
| 41 | + |
| 42 | + let ext = ExtensionTestUtils.loadExtension({ |
| 43 | + manifest, |
| 44 | + useAddonManager: "temporary", |
| 45 | + |
| 46 | + background() { |
| 47 | + browser.test.onMessage.addListener(async (msg, script) => { |
| 48 | + if (msg === "dynamic-script") { |
| 49 | + await browser.scripting.registerContentScripts([script]); |
| 50 | + browser.test.sendMessage("dynamic-script-done"); |
| 51 | + } else if (msg === "injected-flush?") { |
| 52 | + browser.test.sendMessage("injected", "flush"); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + let action = browser.action || browser.browserAction; |
| 57 | + |
| 58 | + action.onClicked.addListener(tab => { |
| 59 | + browser.scripting.executeScript({ |
| 60 | + target: { tabId: tab.id }, |
| 61 | + func: () => { |
| 62 | + // This needs to run after all scripts, to confirm the correct |
| 63 | + // number of scripts was injected. These two paths are inherently |
| 64 | + // independant, and since there's a variable number of content |
| 65 | + // scripts, there's no easy/better way to do it than a delay. |
| 66 | + requestAnimationFrame(() => |
| 67 | + requestAnimationFrame(() => { |
| 68 | + let id = browser.runtime.id.split("@")[0]; |
| 69 | + browser.test.sendMessage( |
| 70 | + "scriptCount", |
| 71 | + document.body.dataset[id] |
| 72 | + ); |
| 73 | + }) |
| 74 | + ); |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + }, |
| 79 | + |
| 80 | + files: { |
| 81 | + "static.js"() { |
| 82 | + // Need to use DOM attributes (or the dataset), because two different |
| 83 | + // content script sandboxes (from top frame and the same-origin iframe) |
| 84 | + // get different wrappers, they don't see each other's top expandos. |
| 85 | + |
| 86 | + // Need to avoid using the @ character (from the extension id) |
| 87 | + // because it's not allowed as part of the DOM attribute name. |
| 88 | + |
| 89 | + let id = browser.runtime.id.split("@")[0]; |
| 90 | + top.document.body.dataset[id] = (top.document.body.dataset[id] | 0) + 1; |
| 91 | + |
| 92 | + browser.test.log( |
| 93 | + `Static content script from ${id} running on ${location.href}.` |
| 94 | + ); |
| 95 | + |
| 96 | + browser.test.sendMessage("injected", "static@" + location.host); |
| 97 | + }, |
| 98 | + "dynamic.js"() { |
| 99 | + let id = browser.runtime.id.split("@")[0]; |
| 100 | + top.document.body.dataset[id] = (top.document.body.dataset[id] | 0) + 1; |
| 101 | + |
| 102 | + browser.test.log( |
| 103 | + `Dynamic content script from ${id} running on ${location.href}.` |
| 104 | + ); |
| 105 | + |
| 106 | + let frame = window === top ? "top" : "frame"; |
| 107 | + browser.test.sendMessage( |
| 108 | + "injected", |
| 109 | + `dynamic-${frame}@${location.host}` |
| 110 | + ); |
| 111 | + }, |
| 112 | + "document_start.js": makeRunAtScript("document_start"), |
| 113 | + "document_end.js": makeRunAtScript("document_end"), |
| 114 | + "document_idle.js": makeRunAtScript("document_idle"), |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + if (granted) { |
| 119 | + info("Granting initial permissions."); |
| 120 | + await ExtensionPermissions.add(id, { permissions: [], origins: granted }); |
| 121 | + } |
| 122 | + |
| 123 | + await ext.startup(); |
| 124 | + return ext; |
| 125 | +} |
| 126 | + |
| 127 | +async function testActiveScript(extension, expectCount, expectHosts) { |
| 128 | + info(`Testing ${extension.id} on ${gBrowser.currentURI.spec}.`); |
| 129 | + |
| 130 | + await clickBrowserAction(extension); |
| 131 | + |
| 132 | + let received = []; |
| 133 | + for (let host of expectHosts) { |
| 134 | + info(`Waiting for a script to run in a ${host} frame.`); |
| 135 | + received.push(await extension.awaitMessage("injected")); |
| 136 | + } |
| 137 | + |
| 138 | + extension.sendMessage("injected-flush?"); |
| 139 | + info("Waiting for the flush message between test runs."); |
| 140 | + let flush = await extension.awaitMessage("injected"); |
| 141 | + is(flush, "flush", "Messages properly flushed."); |
| 142 | + |
| 143 | + is(received.sort().join(), expectHosts.join(), "All messages received."); |
| 144 | + |
| 145 | + info(`Awaiting the counter from the activeTab content script.`); |
| 146 | + let scriptCount = await extension.awaitMessage("scriptCount"); |
| 147 | + is(scriptCount | 0, expectCount, "Expected number of scripts running"); |
| 148 | +} |
| 149 | + |
| 150 | +add_task(async function test_action_activeScript() { |
| 151 | + // Static MV2 extension content scripts are not affected. |
| 152 | + let ext0 = await makeExtension("ext0@test", 2, ["*://example.com/*"]); |
| 153 | + |
| 154 | + let ext1 = await makeExtension("ext1@test", 3); |
| 155 | + let ext2 = await makeExtension("ext2@test", 3, ["*://example.com/*"]); |
| 156 | + let ext3 = await makeExtension("ext3@test", 3, ["*://mochi.test/*"]); |
| 157 | + |
| 158 | + // Test run_at script ordering. |
| 159 | + let ext4 = await makeExtension("ext4@test", 3); |
| 160 | + |
| 161 | + await BrowserTestUtils.withNewTab("about:blank", async () => { |
| 162 | + info("No content scripts run on top level about:blank."); |
| 163 | + await testActiveScript(ext0, 0, []); |
| 164 | + await testActiveScript(ext1, 0, []); |
| 165 | + await testActiveScript(ext2, 0, []); |
| 166 | + await testActiveScript(ext3, 0, []); |
| 167 | + await testActiveScript(ext4, 0, []); |
| 168 | + }); |
| 169 | + |
| 170 | + let dynamicScript = { |
| 171 | + id: "script", |
| 172 | + js: ["dynamic.js"], |
| 173 | + matches: ["<all_urls>"], |
| 174 | + allFrames: true, |
| 175 | + persistAcrossSessions: false, |
| 176 | + }; |
| 177 | + |
| 178 | + // MV2 extensions don't support activeScript. This dynamic script won't run |
| 179 | + // when action button is clicked, but will run on example.com automatically. |
| 180 | + ext0.sendMessage("dynamic-script", dynamicScript); |
| 181 | + await ext0.awaitMessage("dynamic-script-done"); |
| 182 | + |
| 183 | + // Only ext3 will have a dynamic script, matching <all_urls> with allFrames. |
| 184 | + ext3.sendMessage("dynamic-script", dynamicScript); |
| 185 | + await ext3.awaitMessage("dynamic-script-done"); |
| 186 | + |
| 187 | + let url = |
| 188 | + "https://example.com/browser/browser/components/extensions/test/browser/file_with_xorigin_frame.html"; |
| 189 | + |
| 190 | + await BrowserTestUtils.withNewTab(url, async browser => { |
| 191 | + info("ext0 is MV2, static content script should run automatically."); |
| 192 | + info("ext0 has example.com permission, dynamic scripts should also run."); |
| 193 | + let received = [ |
| 194 | + await ext0.awaitMessage("injected"), |
| 195 | + await ext0.awaitMessage("injected"), |
| 196 | + await ext0.awaitMessage("injected"), |
| 197 | + ]; |
| 198 | + is( |
| 199 | + received.sort().join(), |
| 200 | + "dynamic-frame@example.com,dynamic-top@example.com,static@example.com", |
| 201 | + "All messages received" |
| 202 | + ); |
| 203 | + |
| 204 | + info("Clicking ext0 button should not run content script again."); |
| 205 | + await testActiveScript(ext0, 3, []); |
| 206 | + |
| 207 | + info("ext2 has host permission, content script should run automatically."); |
| 208 | + let static2 = await ext2.awaitMessage("injected"); |
| 209 | + is(static2, "static@example.com", "Script ran automatically"); |
| 210 | + |
| 211 | + info("Clicking ext2 button should not run content script again."); |
| 212 | + await testActiveScript(ext2, 1, []); |
| 213 | + |
| 214 | + await testActiveScript(ext1, 1, ["static@example.com"]); |
| 215 | + |
| 216 | + await testActiveScript(ext3, 3, [ |
| 217 | + "dynamic-frame@example.com", |
| 218 | + "dynamic-top@example.com", |
| 219 | + "static@example.com", |
| 220 | + ]); |
| 221 | + |
| 222 | + await testActiveScript(ext4, 1, ["static@example.com"]); |
| 223 | + |
| 224 | + // Navigate same-origin iframe to another page, activeScripts shouldn't run. |
| 225 | + let bc = browser.browsingContext.children[0].children[0]; |
| 226 | + SpecialPowers.spawn(bc, [], () => { |
| 227 | + content.location.href = "file_dummy.html"; |
| 228 | + }); |
| 229 | + // But dynamic script from ext0 should run automatically again. |
| 230 | + let dynamic0 = await ext0.awaitMessage("injected"); |
| 231 | + is(dynamic0, "dynamic-frame@example.com", "Script ran automatically"); |
| 232 | + |
| 233 | + info("Clicking all buttons again should not activeScripts."); |
| 234 | + await testActiveScript(ext0, 4, []); |
| 235 | + await testActiveScript(ext1, 1, []); |
| 236 | + await testActiveScript(ext2, 1, []); |
| 237 | + // Except ext3 dynamic allFrames script runs in the new navigated page. |
| 238 | + await testActiveScript(ext3, 4, ["dynamic-frame@example.com"]); |
| 239 | + await testActiveScript(ext4, 1, []); |
| 240 | + }); |
| 241 | + |
| 242 | + // Register run_at content scripts in reverse order. |
| 243 | + for (let runAt of ["document_idle", "document_end", "document_start"]) { |
| 244 | + ext4.sendMessage("dynamic-script", { |
| 245 | + id: runAt, |
| 246 | + runAt: runAt, |
| 247 | + js: [`${runAt}.js`], |
| 248 | + matches: ["http://mochi.test/*"], |
| 249 | + persistAcrossSessions: false, |
| 250 | + }); |
| 251 | + await ext4.awaitMessage("dynamic-script-done"); |
| 252 | + } |
| 253 | + |
| 254 | + await BrowserTestUtils.withNewTab("http://mochi.test:8888/", async () => { |
| 255 | + info("ext0 is MV2, static content script should run automatically."); |
| 256 | + let static0 = await ext0.awaitMessage("injected"); |
| 257 | + is(static0, "static@mochi.test:8888", "Script ran automatically."); |
| 258 | + |
| 259 | + info("Clicking ext0 button should not run content script again."); |
| 260 | + await testActiveScript(ext0, 1, []); |
| 261 | + |
| 262 | + info("ext3 has host permission, content script should run automatically."); |
| 263 | + let received3 = [ |
| 264 | + await ext3.awaitMessage("injected"), |
| 265 | + await ext3.awaitMessage("injected"), |
| 266 | + ]; |
| 267 | + is( |
| 268 | + received3.sort().join(), |
| 269 | + "dynamic-top@mochi.test:8888,static@mochi.test:8888", |
| 270 | + "All messages received." |
| 271 | + ); |
| 272 | + |
| 273 | + info("Clicking ext3 button should not run content script again."); |
| 274 | + await testActiveScript(ext3, 2, []); |
| 275 | + |
| 276 | + await testActiveScript(ext1, 1, ["static@mochi.test:8888"]); |
| 277 | + await testActiveScript(ext2, 1, ["static@mochi.test:8888"]); |
| 278 | + |
| 279 | + // Expect run_at content scripts to run in the correct order. |
| 280 | + await testActiveScript(ext4, 1, [ |
| 281 | + "order@document_start", |
| 282 | + "order@document_start,document_end", |
| 283 | + "order@document_start,document_end,document_idle", |
| 284 | + "static@mochi.test:8888", |
| 285 | + ]); |
| 286 | + |
| 287 | + info("Clicking all buttons again should not run content scripts."); |
| 288 | + await testActiveScript(ext0, 1, []); |
| 289 | + await testActiveScript(ext1, 1, []); |
| 290 | + await testActiveScript(ext2, 1, []); |
| 291 | + await testActiveScript(ext3, 2, []); |
| 292 | + await testActiveScript(ext4, 1, []); |
| 293 | + }); |
| 294 | + |
| 295 | + await ext0.unload(); |
| 296 | + await ext1.unload(); |
| 297 | + await ext2.unload(); |
| 298 | + await ext3.unload(); |
| 299 | + await ext4.unload(); |
| 300 | +}); |
0 commit comments