summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/page/browser_createIsolatedWorld.js
blob: 41ed83d26449cd01fcad748a79d7cdfbeb85a37d (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test Page.createIsolatedWorld

const WORLD_NAME_1 = "testWorld1";
const WORLD_NAME_2 = "testWorld2";

const DESTROYED = "Runtime.executionContextDestroyed";
const CREATED = "Runtime.executionContextCreated";
const CLEARED = "Runtime.executionContextsCleared";

add_task(async function frameIdMissing({ client }) {
  const { Page } = client;

  await Assert.rejects(
    Page.createIsolatedWorld({
      worldName: WORLD_NAME_1,
      grantUniversalAccess: true,
    }),
    /frameId: string value expected/,
    `Fails with missing frameId`
  );
});

add_task(async function frameIdInvalidTypes({ client }) {
  const { Page } = client;

  for (const frameId of [null, true, 1, [], {}]) {
    await Assert.rejects(
      Page.createIsolatedWorld({
        frameId,
      }),
      /frameId: string value expected/,
      `Fails with invalid type: ${frameId}`
    );
  }
});

add_task(async function worldNameInvalidTypes({ client }) {
  const { Page } = client;

  await Page.enable();
  info("Page notifications are enabled");

  const loadEvent = Page.loadEventFired();
  const { frameId } = await Page.navigate({ url: PAGE_URL });
  await loadEvent;

  for (const worldName of [null, true, 1, [], {}]) {
    await Assert.rejects(
      Page.createIsolatedWorld({
        frameId,
        worldName,
      }),
      /worldName: string value expected/,
      `Fails with invalid type: ${worldName}`
    );
  }
});

add_task(async function noEventsWhenRuntimeDomainDisabled({ client }) {
  const { Page, Runtime } = client;

  await Page.enable();
  info("Page notifications are enabled");

  const history = recordEvents(Runtime, 0);
  const loadEvent = Page.loadEventFired();
  const { frameId } = await Page.navigate({ url: PAGE_URL });
  await loadEvent;

  let errorThrown = "";
  try {
    await Page.createIsolatedWorld({
      frameId,
      worldName: WORLD_NAME_1,
      grantUniversalAccess: true,
    });
    await assertEvents({ history, expectedEvents: [] });
  } catch (e) {
    errorThrown = e.message;
  }
  todo(
    errorThrown === "",
    "No contexts tracked internally without Runtime enabled (Bug 1623482)"
  );
});

add_task(async function noEventsAfterRuntimeDomainDisabled({ client }) {
  const { Page, Runtime } = client;

  await Page.enable();
  info("Page notifications are enabled");

  await enableRuntime(client);
  await Runtime.disable();
  info("Runtime notifications are disabled");

  const history = recordEvents(Runtime, 0);
  const loadEvent = Page.loadEventFired();
  const { frameId } = await Page.navigate({ url: PAGE_URL });
  await loadEvent;

  await Page.createIsolatedWorld({
    frameId,
    worldName: WORLD_NAME_2,
    grantUniversalAccess: true,
  });
  await assertEvents({ history, expectedEvents: [] });
});

add_task(async function contextCreatedAfterNavigation({ client }) {
  const { Page, Runtime } = client;

  await Page.enable();
  info("Page notifications are enabled");

  await enableRuntime(client);

  const history = recordEvents(Runtime, 3);
  const loadEvent = Page.loadEventFired();
  const { frameId } = await Page.navigate({ url: PAGE_URL });
  await loadEvent;

  const { executionContextId: isolatedId } = await Page.createIsolatedWorld({
    frameId,
    worldName: WORLD_NAME_1,
    grantUniversalAccess: true,
  });
  await assertEvents({
    history,
    expectedEvents: [
      DESTROYED, // default, about:blank
      CREATED, // default, PAGE_URL
      CREATED, // isolated, PAGE_URL
    ],
  });

  const contexts = history
    .findEvents(CREATED)
    .map(event => event.payload.context);
  const defaultContext = contexts[0];
  const isolatedContext = contexts[1];
  is(defaultContext.auxData.isDefault, true, "Default context is default");
  is(
    defaultContext.auxData.type,
    "default",
    "Default context has type 'default'"
  );
  is(defaultContext.origin, BASE_ORIGIN, "Default context has expected origin");
  checkIsolated(isolatedContext, isolatedId, WORLD_NAME_1, frameId);
  compareContexts(isolatedContext, defaultContext);
});

add_task(async function contextDestroyedForNavigation({ client }) {
  const { Page, Runtime } = client;

  const defaultContext = await enableRuntime(client);
  const isolatedContext = await createIsolatedContext(client, defaultContext);

  await Page.enable();

  const history = recordEvents(Runtime, 4, true);
  const frameNavigated = Page.frameNavigated();
  await Page.navigate({ url: PAGE_URL });
  await frameNavigated;

  await assertEvents({
    history,
    expectedEvents: [
      DESTROYED, // default, about:blank
      DESTROYED, // isolated, about:blank
      CLEARED,
      CREATED, // default, PAGE_URL
    ],
  });

  const destroyed = history
    .findEvents(DESTROYED)
    .map(event => event.payload.executionContextId);
  ok(destroyed.includes(isolatedContext.id), "Isolated context destroyed");
  ok(destroyed.includes(defaultContext.id), "Default context destroyed");

  const { context: newContext } = history.findEvent(CREATED).payload;
  is(newContext.auxData.isDefault, true, "The new context is a default one");
  ok(!!newContext.id, "The new context has an id");
  ok(
    ![defaultContext.id, isolatedContext.id].includes(newContext.id),
    "The new context has a new id"
  );
});

add_task(async function contextsForFramesetNavigation({ client }) {
  const { Page, Runtime } = client;

  await Page.enable();
  info("Page notifications are enabled");

  await enableRuntime(client);

  // check creation when navigating to a frameset
  const historyTo = recordEvents(Runtime, 5);
  const loadEventTo = Page.loadEventFired();
  const { frameId: frameIdTo } = await Page.navigate({
    url: FRAMESET_SINGLE_URL,
  });
  await loadEventTo;

  const { frameTree } = await Page.getFrameTree();
  const subFrame = frameTree.childFrames[0].frame;

  const { executionContextId: contextIdParent } =
    await Page.createIsolatedWorld({
      frameId: frameIdTo,
      worldName: WORLD_NAME_1,
      grantUniversalAccess: true,
    });
  const { executionContextId: contextIdSubFrame } =
    await Page.createIsolatedWorld({
      frameId: subFrame.id,
      worldName: WORLD_NAME_2,
      grantUniversalAccess: true,
    });

  await assertEvents({
    history: historyTo,
    expectedEvents: [
      DESTROYED, // default, about:blank
      CREATED, // default, FRAMESET_SINGLE_URL
      CREATED, // default, PAGE_URL
      CREATED, // isolated, FRAMESET_SINGLE_URL
      CREATED, // isolated, PAGE_URL
    ],
  });

  const contextsCreated = historyTo
    .findEvents(CREATED)
    .map(event => event.payload.context);
  const parentDefaultContextCreated = contextsCreated[0];
  const frameDefaultContextCreated = contextsCreated[1];
  const parentIsolatedContextCreated = contextsCreated[2];
  const frameIsolatedContextCreated = contextsCreated[3];

  checkIsolated(
    parentIsolatedContextCreated,
    contextIdParent,
    WORLD_NAME_1,
    frameIdTo
  );
  compareContexts(parentIsolatedContextCreated, parentDefaultContextCreated);

  checkIsolated(
    frameIsolatedContextCreated,
    contextIdSubFrame,
    WORLD_NAME_2,
    subFrame.id
  );
  compareContexts(frameIsolatedContextCreated, frameDefaultContextCreated);

  // check destroying when navigating away from a frameset
  const historyFrom = recordEvents(Runtime, 6);
  const loadEventFrom = Page.loadEventFired();
  await Page.navigate({ url: PAGE_URL });
  await loadEventFrom;

  await assertEvents({
    history: historyFrom,
    expectedEvents: [
      DESTROYED, // default, PAGE_URL
      DESTROYED, // isolated, PAGE_URL
      DESTROYED, // default, FRAMESET_SINGLE_URL
      DESTROYED, // isolated, FRAMESET_SINGLE_URL
      CREATED, // default, PAGE_URL
    ],
  });

  const contextsDestroyed = historyFrom
    .findEvents(DESTROYED)
    .map(event => event.payload.executionContextId);
  contextsCreated.forEach(context => {
    ok(
      contextsDestroyed.includes(context.id),
      `Context with id ${context.id} destroyed`
    );
  });

  const { context: newContext } = historyFrom.findEvent(CREATED).payload;
  is(newContext.auxData.isDefault, true, "The new context is a default one");
  ok(!!newContext.id, "The new context has an id");
  ok(
    ![parentDefaultContextCreated.id, frameDefaultContextCreated.id].includes(
      newContext.id
    ),
    "The new context has a new id"
  );
});

add_task(async function evaluateInIsolatedAndDefault({ client }) {
  const { Runtime } = client;

  const defaultContext = await enableRuntime(client);
  const isolatedContext = await createIsolatedContext(client, defaultContext);

  const { result: objDefault } = await Runtime.evaluate({
    contextId: defaultContext.id,
    expression: "({ foo: 1 })",
  });
  const { result: objIsolated } = await Runtime.evaluate({
    contextId: isolatedContext.id,
    expression: "({ foo: 10 })",
  });
  const { result: result1 } = await Runtime.callFunctionOn({
    executionContextId: isolatedContext.id,
    functionDeclaration: "arg => ++arg.foo",
    arguments: [{ objectId: objIsolated.objectId }],
  });
  is(result1.value, 11, "Isolated context incremented the expected value");

  await Assert.rejects(
    Runtime.callFunctionOn({
      executionContextId: isolatedContext.id,
      functionDeclaration: "arg => ++arg.foo",
      arguments: [{ objectId: objDefault.objectId }],
    }),
    /Could not find object with given id/,
    "Contexts do not share objects"
  );
});

add_task(async function contextEvaluationIsIsolated({ client }) {
  const { Runtime } = client;

  // If a document makes changes to standard global object, an isolated
  // world should not be affected
  await loadURL(toDataURL("<script>window.Node = null</script>"));

  const defaultContext = await enableRuntime(client);
  const isolatedContext = await createIsolatedContext(client, defaultContext);

  const { result: result1 } = await Runtime.callFunctionOn({
    executionContextId: defaultContext.id,
    functionDeclaration: "arg => window.Node",
  });
  const { result: result2 } = await Runtime.callFunctionOn({
    executionContextId: isolatedContext.id,
    functionDeclaration: "arg => window.Node",
  });
  is(result1.value, null, "Default context sees content changes to global");
  todo_isnot(
    result2.value,
    null,
    "Isolated context is not affected by changes to global, Bug 1601421"
  );
});

function checkIsolated(context, expectedId, expectedName, expectedFrameId) {
  is(
    expectedId,
    context.id,
    "createIsolatedWorld returns id of isolated context"
  );
  is(
    context.auxData.frameId,
    expectedFrameId,
    "Isolated context has expected frameId"
  );
  is(context.auxData.isDefault, false, "Isolated context is not default");
  is(context.auxData.type, "isolated", "Isolated context has type 'isolated'");
  is(context.name, expectedName, "Isolated context is named as requested");
  ok(!!context.origin, "Isolated context has an origin");
}

function compareContexts(isolatedContext, defaultContext) {
  isnot(
    defaultContext.name,
    isolatedContext.name,
    "The contexts have different names"
  );
  isnot(
    defaultContext.id,
    isolatedContext.id,
    "The contexts have different ids"
  );
  is(
    defaultContext.origin,
    isolatedContext.origin,
    "The contexts have same origin"
  );
  is(
    defaultContext.auxData.frameId,
    isolatedContext.auxData.frameId,
    "The contexts have same frameId"
  );
}

async function createIsolatedContext(
  client,
  defaultContext,
  worldName = WORLD_NAME_1
) {
  const { Page, Runtime } = client;

  const frameId = defaultContext.auxData.frameId;

  const isolatedContextCreated = Runtime.executionContextCreated();
  const { executionContextId: isolatedId } = await Page.createIsolatedWorld({
    frameId,
    worldName,
    grantUniversalAccess: true,
  });
  const { context: isolatedContext } = await isolatedContextCreated;
  info("Isolated world created");

  checkIsolated(isolatedContext, isolatedId, worldName, frameId);
  compareContexts(isolatedContext, defaultContext);

  return isolatedContext;
}

function recordEvents(Runtime, total, cleared = false) {
  const history = new RecordEvents(total);

  history.addRecorder({
    event: Runtime.executionContextDestroyed,
    eventName: DESTROYED,
    messageFn: payload => {
      return `Received ${DESTROYED} for id ${payload.executionContextId}`;
    },
  });
  history.addRecorder({
    event: Runtime.executionContextCreated,
    eventName: CREATED,
    messageFn: payload => {
      return (
        `Received ${CREATED} for id ${payload.context.id}` +
        ` type: ${payload.context.auxData.type}` +
        ` name: ${payload.context.name}` +
        ` origin: ${payload.context.origin}`
      );
    },
  });
  if (cleared) {
    history.addRecorder({
      event: Runtime.executionContextsCleared,
      eventName: CLEARED,
    });
  }

  return history;
}

async function assertEvents(options = {}) {
  const { history, expectedEvents, timeout = 1000 } = options;
  const events = await history.record(timeout);
  const eventNames = events.map(item => item.eventName);
  info(`Expected events: ${expectedEvents}`);
  info(`Received events: ${eventNames}`);
  is(
    events.length,
    expectedEvents.length,
    "Received expected number of Runtime context events"
  );
  Assert.deepEqual(
    eventNames.sort(),
    expectedEvents.sort(),
    "Received expected Runtime context events"
  );
}