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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for content script caching</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
// This file defines content scripts.
const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
add_task(async function test_contentscript_cache() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
content_scripts: [{
"matches": ["http://mochi.test/*/file_sample.html"],
"js": ["content_script.js"],
"run_at": "document_start",
}],
permissions: ["<all_urls>", "tabs"],
},
async background() {
// Force our extension instance to be initialized for the current content process.
await browser.tabs.insertCSS({code: ""});
browser.test.sendMessage("origin", location.origin);
},
files: {
"content_script.js": function() {
browser.test.sendMessage("content-script-loaded");
},
},
});
await extension.startup();
let origin = await extension.awaitMessage("origin");
let scriptUrl = `${origin}/content_script.js`;
const { ExtensionProcessScript } = SpecialPowers.ChromeUtils.import(
"resource://gre/modules/ExtensionProcessScript.jsm"
);
let ext = ExtensionProcessScript.getExtensionChild(extension.id);
ext.staticScripts.expiryTimeout = 3000;
is(ext.staticScripts.size, 0, "Should have no cached scripts");
let win = window.open(`${BASE}/file_sample.html`);
await extension.awaitMessage("content-script-loaded");
if (AppConstants.platform !== "android") {
is(ext.staticScripts.size, 1, "Should have one cached script");
ok(ext.staticScripts.has(scriptUrl), "Script cache should contain script URL");
}
let chromeScript, chromeScriptDone;
let { appinfo } = SpecialPowers.Services;
if (appinfo.processType === appinfo.PROCESS_TYPE_CONTENT) {
/* globals addMessageListener, assert */
chromeScript = SpecialPowers.loadChromeScript(() => {
/* eslint-env mozilla/chrome-script */
addMessageListener("check-script-cache", extensionId => {
const { ExtensionProcessScript } = ChromeUtils.import(
"resource://gre/modules/ExtensionProcessScript.jsm"
);
let ext = ExtensionProcessScript.getExtensionChild(extensionId);
if (ext && ext.staticScripts) {
assert.equal(ext.staticScripts.size, 0, "Should have no cached scripts in the parent process");
}
sendAsyncMessage("done");
});
});
chromeScript.sendAsyncMessage("check-script-cache", extension.id);
chromeScriptDone = chromeScript.promiseOneMessage("done");
}
SimpleTest.requestFlakyTimeout("Required to test expiry timeout");
await new Promise(resolve => setTimeout(resolve, 3000));
is(ext.staticScripts.size, 0, "Should have no cached scripts");
if (chromeScript) {
await chromeScriptDone;
chromeScript.destroy();
}
win.close();
win = window.open(`${BASE}/file_sample.html`);
await extension.awaitMessage("content-script-loaded");
is(ext.staticScripts.size, 1, "Should have one cached script");
ok(ext.staticScripts.has(scriptUrl));
SpecialPowers.Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
is(ext.staticScripts.size, 0, "Should have no cached scripts after heap-minimize");
win.close();
await extension.unload();
});
</script>
</body>
</html>
|