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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
"use strict";
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
const { XPCShellContentUtils } = ChromeUtils.importESModule(
"resource://testing-common/XPCShellContentUtils.sys.mjs"
);
const PROCESS_COUNT_PREF = "dom.ipc.processCount";
const remote = AppConstants.platform !== "android";
XPCShellContentUtils.init(this);
let contentPage;
async function readBlob(key, sharedData = Services.cpmm.sharedData) {
const { ExtensionUtils } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionUtils.sys.mjs"
);
let reader = new FileReader();
reader.readAsText(sharedData.get(key));
await ExtensionUtils.promiseEvent(reader, "loadend");
return reader.result;
}
function getKey(key, sharedData = Services.cpmm.sharedData) {
return sharedData.get(key);
}
function hasKey(key, sharedData = Services.cpmm.sharedData) {
return sharedData.has(key);
}
function getContents(sharedMap = Services.cpmm.sharedData) {
return {
keys: Array.from(sharedMap.keys()),
values: Array.from(sharedMap.values()),
entries: Array.from(sharedMap.entries()),
getValues: Array.from(sharedMap.keys(), key => sharedMap.get(key)),
};
}
function checkMap(contents, expected) {
expected = Array.from(expected);
equal(contents.keys.length, expected.length, "Got correct number of keys");
equal(
contents.values.length,
expected.length,
"Got correct number of values"
);
equal(
contents.entries.length,
expected.length,
"Got correct number of entries"
);
for (let [i, [key, val]] of contents.entries.entries()) {
equal(key, contents.keys[i], `keys()[${i}] matches entries()[${i}]`);
deepEqual(
val,
contents.values[i],
`values()[${i}] matches entries()[${i}]`
);
}
expected.sort(([a], [b]) => a.localeCompare(b));
contents.entries.sort(([a], [b]) => a.localeCompare(b));
for (let [i, [key, val]] of contents.entries.entries()) {
equal(
key,
expected[i][0],
`expected[${i}].key matches entries()[${i}].key`
);
deepEqual(
val,
expected[i][1],
`expected[${i}].value matches entries()[${i}].value`
);
}
}
function checkParentMap(expected) {
info("Checking parent map");
checkMap(getContents(Services.ppmm.sharedData), expected);
}
async function checkContentMaps(expected, parentOnly = false) {
info("Checking in-process content map");
checkMap(getContents(Services.cpmm.sharedData), expected);
if (!parentOnly) {
info("Checking out-of-process content map");
let contents = await contentPage.spawn([], getContents);
checkMap(contents, expected);
}
}
async function loadContentPage() {
let page = await XPCShellContentUtils.loadContentPage("data:text/html,", {
remote,
});
registerCleanupFunction(() => page.close());
return page;
}
add_setup(async function () {
// Start with one content process so that we can increase the number
// later and test the behavior of a fresh content process.
Services.prefs.setIntPref(PROCESS_COUNT_PREF, 1);
contentPage = await loadContentPage();
});
add_task(async function test_sharedMap() {
let { sharedData } = Services.ppmm;
info("Check that parent and child maps are both initially empty");
checkParentMap([]);
await checkContentMaps([]);
let expected = [
["foo-a", { foo: "a" }],
["foo-b", { foo: "b" }],
["bar-c", null],
["bar-d", 42],
];
function setKey(key, val) {
sharedData.set(key, val);
expected = expected.filter(([k]) => k != key);
expected.push([key, val]);
}
function deleteKey(key) {
sharedData.delete(key);
expected = expected.filter(([k]) => k != key);
}
for (let [key, val] of expected) {
sharedData.set(key, val);
}
info(
"Add some entries, test that they are initially only available in the parent"
);
checkParentMap(expected);
await checkContentMaps([]);
info("Flush. Check that changes are visible in both parent and children");
sharedData.flush();
checkParentMap(expected);
await checkContentMaps(expected);
info(
"Add another entry. Check that it is initially only available in the parent"
);
let oldExpected = Array.from(expected);
setKey("baz-a", { meh: "meh" });
// When we do several checks in a row, we can't check the values in
// the content process, since the async checks may allow the idle
// flush task to run, and update it before we're ready.
checkParentMap(expected);
checkContentMaps(oldExpected, true);
info(
"Add another entry. Check that both new entries are only available in the parent"
);
setKey("baz-a", { meh: 12 });
checkParentMap(expected);
checkContentMaps(oldExpected, true);
info(
"Delete an entry. Check that all changes are only visible in the parent"
);
deleteKey("foo-b");
checkParentMap(expected);
checkContentMaps(oldExpected, true);
info(
"Flush. Check that all entries are available in both parent and children"
);
sharedData.flush();
checkParentMap(expected);
await checkContentMaps(expected);
info("Test that entries are automatically flushed on idle:");
info(
"Add a new entry. Check that it is initially only available in the parent"
);
// Test the idle flush task.
oldExpected = Array.from(expected);
setKey("thing", "stuff");
checkParentMap(expected);
checkContentMaps(oldExpected, true);
info(
"Wait for an idle timeout. Check that changes are now visible in all children"
);
await new Promise(resolve => ChromeUtils.idleDispatch(resolve));
checkParentMap(expected);
await checkContentMaps(expected);
// Test that has() rebuilds map after a flush.
sharedData.set("grick", true);
sharedData.flush();
equal(
await contentPage.spawn(["grick"], hasKey),
true,
"has() should see key after flush"
);
sharedData.set("grack", true);
sharedData.flush();
equal(
await contentPage.spawn(["gruck"], hasKey),
false,
"has() should return false for nonexistent key"
);
});
add_task(async function test_blobs() {
let { sharedData } = Services.ppmm;
let text = [
"The quick brown fox jumps over the lazy dog",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
];
let blobs = text.map(str => new Blob([str]));
let data = { foo: { bar: "baz" } };
sharedData.set("blob0", blobs[0]);
sharedData.set("blob1", blobs[1]);
sharedData.set("data", data);
equal(
await readBlob("blob0", sharedData),
text[0],
"Expected text for blob0 in parent ppmm"
);
sharedData.flush();
equal(
await readBlob("blob0", sharedData),
text[0],
"Expected text for blob0 in parent ppmm"
);
equal(
await readBlob("blob1", sharedData),
text[1],
"Expected text for blob1 in parent ppmm"
);
equal(
await readBlob("blob0"),
text[0],
"Expected text for blob0 in parent cpmm"
);
equal(
await readBlob("blob1"),
text[1],
"Expected text for blob1 in parent cpmm"
);
equal(
await contentPage.spawn(["blob0"], readBlob),
text[0],
"Expected text for blob0 in child 1 cpmm"
);
equal(
await contentPage.spawn(["blob1"], readBlob),
text[1],
"Expected text for blob1 in child 1 cpmm"
);
// Start a second child process
Services.prefs.setIntPref(PROCESS_COUNT_PREF, 2);
let page2 = await loadContentPage();
equal(
await page2.spawn(["blob0"], readBlob),
text[0],
"Expected text for blob0 in child 2 cpmm"
);
equal(
await page2.spawn(["blob1"], readBlob),
text[1],
"Expected text for blob1 in child 2 cpmm"
);
sharedData.set("blob0", blobs[2]);
equal(
await readBlob("blob0", sharedData),
text[2],
"Expected text for blob0 in parent ppmm"
);
sharedData.flush();
equal(
await readBlob("blob0", sharedData),
text[2],
"Expected text for blob0 in parent ppmm"
);
equal(
await readBlob("blob1", sharedData),
text[1],
"Expected text for blob1 in parent ppmm"
);
equal(
await readBlob("blob0"),
text[2],
"Expected text for blob0 in parent cpmm"
);
equal(
await readBlob("blob1"),
text[1],
"Expected text for blob1 in parent cpmm"
);
equal(
await contentPage.spawn(["blob0"], readBlob),
text[2],
"Expected text for blob0 in child 1 cpmm"
);
equal(
await contentPage.spawn(["blob1"], readBlob),
text[1],
"Expected text for blob1 in child 1 cpmm"
);
equal(
await page2.spawn(["blob0"], readBlob),
text[2],
"Expected text for blob0 in child 2 cpmm"
);
equal(
await page2.spawn(["blob1"], readBlob),
text[1],
"Expected text for blob1 in child 2 cpmm"
);
deepEqual(
await page2.spawn(["data"], getKey),
data,
"Expected data for data key in child 2 cpmm"
);
});
|