summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js
blob: aa24b36e8ba11bb07885a28090087e64300d876e (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
'use strict';

// Requires:
// - /common/dispatcher/dispatcher.js
// - /common/utils.js
// - /common/get-host-info.sub.js if automagic conversion of origin names to
// URLs is used.

/**
 * This provides a more friendly interface to remote contexts in dispatches.js.
 * The goal is to make it easy to write multi-window/-frame/-worker tests where
 * the logic is entirely in 1 test file and there is no need to check in any
 * other file (although it is often helpful to check in files of JS helper
 * functions that are shared across remote context).
 *
 * So for example, to test that history traversal works, we create a new window,
 * navigate it to a new document, go back and then go forward.
 *
 * @example
 * promise_test(async t => {
 *   const rcHelper = new RemoteContextHelper();
 *   const rc1 = await rcHelper.addWindow();
 *   const rc2 = await rc1.navigateToNew();
 *   assert_equals(await rc2.executeScript(() => 'here'), 'here', 'rc2 is live');
 *   rc2.historyBack();
 *   assert_equals(await rc1.executeScript(() => 'here'), 'here', 'rc1 is live');
 *   rc1.historyForward();
 *   assert_equals(await rc2.executeScript(() => 'here'), 'here', 'rc2 is live');
 * });
 *
 * Note on the correspondence between remote contexts and
 * `RemoteContextWrapper`s. A remote context is entirely determined by its URL.
 * So navigating away from one and then back again will result in a remote
 * context that can be controlled by the same `RemoteContextWrapper` instance
 * before and after navigation. Messages sent to a remote context while it is
 * destroyed or in BFCache will be queued and processed if that that URL is
 * navigated back to.
 *
 * Navigation:
 * This framework does not keep track of the history of the frame tree and so it
 * is up to the test script to keep track of what remote contexts are currently
 * active and to keep references to the corresponding `RemoteContextWrapper`s.
 *
 * Any action that leads to navigation in the remote context must be executed
 * using
 * @see RemoteContextWrapper.navigate.
 */

{
  const RESOURCES_PATH =
      '/html/browsers/browsing-the-web/remote-context-helper/resources';
  const WINDOW_EXECUTOR_PATH = `${RESOURCES_PATH}/executor-window.py`;
  const WORKER_EXECUTOR_PATH = `${RESOURCES_PATH}/executor-worker.js`;

  /**
   * Turns a string into an origin. If `origin` is null this will return the
   * current document's origin. If `origin` contains not '/', this will attempt
   * to use it as an index in `get_host_info()`. Otherwise returns the input
   * origin.
   * @private
   * @param {string|null} origin The input origin.
   * @return {string|null} The output origin.
   * @throws {RangeError} is `origin` cannot be found in
   *     `get_host_info()`.
   */
  function finalizeOrigin(origin) {
    if (!origin) {
      return location.origin;
    }
    if (!origin.includes('/')) {
      const origins = get_host_info();
      if (origin in origins) {
        return origins[origin];
      } else {
        throw new RangeError(
            `${origin} is not a key in the get_host_info() object`);
      }
    }
    return origin;
  }

  /**
   * @private
   * @param {string} url
   * @returns {string} Absolute url using `location` as the base.
   */
  function makeAbsolute(url) {
    return new URL(url, location).toString();
  }

  /**
   * Represents a configuration for a remote context executor.
   */
  class RemoteContextConfig {
    /**
     * @param {Object} [options]
     * @param {string} [options.origin] A URL or a key in `get_host_info()`.
     *                 @see finalizeOrigin for how origins are handled.
     * @param {string[]} [options.scripts]  A list of script URLs. The current
     *     document will be used as the base for relative URLs.
     * @param {[string, string][]} [options.headers]  A list of pairs of name
     *     and value. The executor will be served with these headers set.
     * @param {string} [options.startOn] If supplied, the executor will start
     *     when this event occurs, e.g. "pageshow",
     *     (@see window.addEventListener). This only makes sense for
     *     window-based executors, not worker-based.
     * @param {string} [options.status] If supplied, the executor will pass
     *     this value in the "status" parameter to the executor. The default
     *     executor will default to a status code of 200, if the parameter is
     *     not supplied.
     */
    constructor(
        {origin, scripts = [], headers = [], startOn, status} = {}) {
      this.origin = origin;
      this.scripts = scripts;
      this.headers = headers;
      this.startOn = startOn;
      this.status = status;
    }

    /**
     * If `config` is not already a `RemoteContextConfig`, one is constructed
     * using `config`.
     * @private
     * @param {object} [config]
     * @returns
     */
    static ensure(config) {
      if (!config) {
        return DEFAULT_CONTEXT_CONFIG;
      }
      return new RemoteContextConfig(config);
    }

    /**
     * Merges `this` with another `RemoteContextConfig` and to give a new
     * `RemoteContextConfig`. `origin` is replaced by the other if present,
     * `headers` and `scripts` are concatenated with `this`'s coming first.
     * @param {RemoteContextConfig} extraConfig
     * @returns {RemoteContextConfig}
     */
    merged(extraConfig) {
      let origin = this.origin;
      if (extraConfig.origin) {
        origin = extraConfig.origin;
      }
      let startOn = this.startOn;
      if (extraConfig.startOn) {
        startOn = extraConfig.startOn;
      }
      let status = this.status;
      if (extraConfig.status) {
        status = extraConfig.status;
      }
      const headers = this.headers.concat(extraConfig.headers);
      const scripts = this.scripts.concat(extraConfig.scripts);
      return new RemoteContextConfig({
        origin,
        headers,
        scripts,
        startOn,
        status
      });
    }
  }

  /**
   * The default `RemoteContextConfig` to use if none is supplied. It has no
   * origin, headers or scripts.
   * @constant {RemoteContextConfig}
   */
  const DEFAULT_CONTEXT_CONFIG = new RemoteContextConfig();

  /**
   * This class represents a configuration for creating remote contexts. This is
   * the entry-point
   * for creating remote contexts, providing @see addWindow .
   */
  class RemoteContextHelper {
    /**
     * @param {RemoteContextConfig|object} config The configuration
     *     for this remote context.
     */
    constructor(config) {
      this.config = RemoteContextConfig.ensure(config);
    }

    /**
     * Creates a new remote context and returns a `RemoteContextWrapper` giving
     * access to it.
     * @private
     * @param {Object} options
     * @param {(url: string) => Promise<undefined>} [options.executorCreator] A
     *     function that takes a URL and causes the browser to navigate some
     *     window to that URL, e.g. via an iframe or a new window. If this is
     *     not supplied, then the returned RemoteContextWrapper won't actually
     *     be communicating with something yet, and something will need to
     *     navigate to it using its `url` property, before communication can be
     *     established.
     * @param {RemoteContextConfig|object} [options.extraConfig] If supplied,
     *     extra configuration for this remote context to be merged with
     *     `this`'s existing config. If it's not a `RemoteContextConfig`, it
     *     will be used to construct a new one.
     * @returns {Promise<RemoteContextWrapper>}
     */
    async createContext({
      executorCreator,
      extraConfig,
      isWorker = false,
    }) {
      const config =
          this.config.merged(RemoteContextConfig.ensure(extraConfig));

      const origin = finalizeOrigin(config.origin);
      const url = new URL(
          isWorker ? WORKER_EXECUTOR_PATH : WINDOW_EXECUTOR_PATH, origin);

      // UUID is needed for executor.
      const uuid = token();
      url.searchParams.append('uuid', uuid);

      if (config.headers) {
        addHeaders(url, config.headers);
      }
      for (const script of config.scripts) {
        url.searchParams.append('script', makeAbsolute(script));
      }

      if (config.startOn) {
        url.searchParams.append('startOn', config.startOn);
      }

      if (config.status) {
        url.searchParams.append('status', config.status);
      }

      if (executorCreator) {
        await executorCreator(url.href);
      }

      return new RemoteContextWrapper(new RemoteContext(uuid), this, url.href);
    }

    /**
     * Creates a window with a remote context. @see createContext for
     * @param {RemoteContextConfig|object} [extraConfig] Will be
     *     merged with `this`'s config.
     * @param {Object} [options]
     * @param {string} [options.target] Passed to `window.open` as the
     *     2nd argument
     * @param {string} [options.features] Passed to `window.open` as the
     *     3rd argument
     * @returns {Promise<RemoteContextWrapper>}
     */
    addWindow(extraConfig, options) {
      return this.createContext({
        executorCreator: windowExecutorCreator(options),
        extraConfig,
      });
    }
  }
  // Export this class.
  self.RemoteContextHelper = RemoteContextHelper;

  /**
   * Attaches header to the URL. See
   * https://web-platform-tests.org/writing-tests/server-pipes.html#headers
   * @param {string} url the URL to which headers should be attached.
   * @param {[[string, string]]} headers a list of pairs of head-name,
   *     header-value.
   */
  function addHeaders(url, headers) {
    function escape(s) {
      return s.replace('(', '\\(').replace(')', '\\)');
    }
    const formattedHeaders = headers.map((header) => {
      return `header(${escape(header[0])}, ${escape(header[1])})`;
    });
    url.searchParams.append('pipe', formattedHeaders.join('|'));
  }

  function windowExecutorCreator({target = '_blank', features} = {}) {
    return url => {
      window.open(url, target, features);
    };
  }

  function elementExecutorCreator(
      remoteContextWrapper, elementName, attributes) {
    return url => {
      return remoteContextWrapper.executeScript((url, elementName, attributes) => {
        const el = document.createElement(elementName);
        for (const attribute in attributes) {
          el.setAttribute(attribute, attributes[attribute]);
        }
        el.src = url;
        document.body.appendChild(el);
      }, [url, elementName, attributes]);
    };
  }

  function iframeSrcdocExecutorCreator(remoteContextWrapper, attributes) {
    return async (url) => {
      // `url` points to the content needed to run an `Executor` in the frame.
      // So we download the content and pass it via the `srcdoc` attribute,
      // setting the iframe's `src` to `undefined`.
      attributes['srcdoc'] = await fetch(url).then(r => r.text());
      elementExecutorCreator(
          remoteContextWrapper, 'iframe', attributes)(undefined);
    };
  }

  function workerExecutorCreator() {
    return url => {
      new Worker(url);
    };
  }

  function navigateExecutorCreator(remoteContextWrapper) {
    return url => {
      return remoteContextWrapper.navigate((url) => {
        window.location = url;
      }, [url]);
    };
  }

  /**
   * This class represents a remote context running an executor (a
   * window/frame/worker that can receive commands). It is the interface for
   * scripts to control remote contexts.
   *
   * Instances are returned when new remote contexts are created (e.g.
   * `addFrame` or `navigateToNew`).
   */
  class RemoteContextWrapper {
    /**
     * This should only be constructed by `RemoteContextHelper`.
     * @private
     */
    constructor(context, helper, url) {
      this.context = context;
      this.helper = helper;
      this.url = url;
    }

    /**
     * Executes a script in the remote context.
     * @param {function} fn The script to execute.
     * @param {any[]} args An array of arguments to pass to the script.
     * @returns {Promise<any>} The return value of the script (after
     *     being serialized and deserialized).
     */
    async executeScript(fn, args) {
      return this.context.execute_script(fn, args);
    }

    /**
     * Adds a string of HTML to the executor's document.
     * @param {string} html
     * @returns {Promise<undefined>}
     */
    async addHTML(html) {
      return this.executeScript((htmlSource) => {
        document.body.insertAdjacentHTML('beforebegin', htmlSource);
      }, [html]);
    }

    /**
     * Adds scripts to the executor's document.
     * @param {string[]} urls A list of URLs. URLs are relative to the current
     *     document.
     * @returns {Promise<undefined>}
     */
    async addScripts(urls) {
      if (!urls) {
        return [];
      }
      return this.executeScript(urls => {
        return addScripts(urls);
      }, [urls.map(makeAbsolute)]);
    }

    /**
     * Adds an iframe with `src` attribute to the current document.
     * @param {RemoteContextConfig} [extraConfig]
     * @param {[string, string][]} [attributes] A list of pairs of strings
     *     of attribute name and value these will be set on the iframe element
     *     when added to the document.
     * @returns {Promise<RemoteContextWrapper>} The remote context.
     */
    addIframe(extraConfig, attributes = {}) {
      return this.helper.createContext({
        executorCreator: elementExecutorCreator(this, 'iframe', attributes),
        extraConfig,
      });
    }

    /**
     * Adds an iframe with `srcdoc` attribute to the current document
     * @param {RemoteContextConfig} [extraConfig]
     * @param {[string, string][]} [attributes] A list of pairs of strings
     *     of attribute name and value these will be set on the iframe element
     *     when added to the document.
     * @returns {Promise<RemoteContextWrapper>} The remote context.
     */
    addIframeSrcdoc(extraConfig, attributes = {}) {
      return this.helper.createContext({
        executorCreator: iframeSrcdocExecutorCreator(this, attributes),
        extraConfig,
      });
    }

    /**
     * Adds a dedicated worker to the current document.
     * @param {RemoteContextConfig} [extraConfig]
     * @returns {Promise<RemoteContextWrapper>} The remote context.
     */
    addWorker(extraConfig) {
      return this.helper.createContext({
        executorCreator: workerExecutorCreator(),
        extraConfig,
        isWorker: true,
      });
    }

    /**
     * Gets a `Headers` object containing the request headers that were used
     * when the browser requested this document.
     *
     * Currently, this only works for `RemoteContextHelper`s representing
     * windows, not workers.
     * @returns {Promise<Headers>}
     */
    async getRequestHeaders() {
      // This only works in window environments for now. We could make it work
      // for workers too; if you have a need, just share or duplicate the code
      // that's in executor-window.py. Anyway, we explicitly use `window` in
      // the script so that we get a clear error if you try using it on a
      // worker.

      // We need to serialize and deserialize the `Headers` object manually.
      const asNestedArrays = await this.executeScript(() => [...window.__requestHeaders]);
      return new Headers(asNestedArrays);
    }

    /**
     * Executes a script in the remote context that will perform a navigation.
     * To do this safely, we must suspend the executor and wait for that to
     * complete before executing. This ensures that all outstanding requests are
     * completed and no more can start. It also ensures that the executor will
     * restart if the page goes into BFCache or it was a same-document
     * navigation. It does not return a value.
     *
     * NOTE: We cannot monitor whether and what navigations are happening. The
     * logic has been made as robust as possible but is not fool-proof.
     *
     * Foolproof rule:
     * - The script must perform exactly one navigation.
     * - If that navigation is a same-document history traversal, you must
     * `await` the result of `waitUntilLocationIs`. (Same-document non-traversal
     * navigations do not need this extra step.)
     *
     * More complex rules:
     * - The script must perform a navigation. If it performs no navigation,
     *   the remote context will be left in the suspended state.
     * - If the script performs a direct same-document navigation, it is not
     * necessary to use this function but it will work as long as it is the only
     *   navigation performed.
     * - If the script performs a same-document history navigation, you must
     * `await` the result of `waitUntilLocationIs`.
     *
     * @param {function} fn The script to execute.
     * @param {any[]} args An array of arguments to pass to the script.
     * @returns {Promise<undefined>}
     */
    navigate(fn, args) {
      return this.executeScript((fnText, args) => {
        executeScriptToNavigate(fnText, args);
      }, [fn.toString(), args]);
    }

    /**
     * Navigates to the given URL, by executing a script in the remote
     * context that will perform navigation with the `location.href`
     * setter.
     *
     * Be aware that performing a cross-document navigation using this
     * method will cause this `RemoteContextWrapper` to become dormant,
     * since the remote context it points to is no longer active and
     * able to receive messages. You also won't be able to reliably
     * tell when the navigation finishes; the returned promise will
     * fulfill when the script finishes running, not when the navigation
     * is done. As such, this is most useful for testing things like
     * unload behavior (where it doesn't matter) or prerendering (where
     * there is already a `RemoteContextWrapper` for the destination).
     * For other cases, using `navigateToNew()` will likely be better.
     *
     * @param {string|URL} url The URL to navigate to.
     * @returns {Promise<undefined>}
     */
    navigateTo(url) {
      return this.navigate(url => {
        location.href = url;
      }, [url.toString()]);
    }

    /**
     * Navigates the context to a new document running an executor.
     * @param {RemoteContextConfig} [extraConfig]
     * @returns {Promise<RemoteContextWrapper>} The remote context.
     */
    async navigateToNew(extraConfig) {
      return this.helper.createContext({
        executorCreator: navigateExecutorCreator(this),
        extraConfig,
      });
    }

    //////////////////////////////////////
    // Navigation Helpers.
    //
    // It is up to the test script to know which remote context will be
    // navigated to and which `RemoteContextWrapper` should be used after
    // navigation.
    //
    // NOTE: For a same-document history navigation, the caller use `await` a
    // call to `waitUntilLocationIs` in order to know that the navigation has
    // completed. For convenience the method below can return the promise to
    // wait on, if passed the expected location.

    async waitUntilLocationIs(expectedLocation) {
      return this.executeScript(async (expectedLocation) => {
        if (location.href === expectedLocation) {
          return;
        }

        // Wait until the location updates to the expected one.
        await new Promise(resolve => {
          const listener = addEventListener('hashchange', (event) => {
            if (event.newURL === expectedLocation) {
              removeEventListener(listener);
              resolve();
            }
          });
        });
      }, [expectedLocation]);
    }

    /**
     * Performs a history traversal.
     * @param {integer} n How many steps to traverse. @see history.go
     * @param {string} [expectedLocation] If supplied will be passed to @see waitUntilLocationIs.
     * @returns {Promise<undefined>}
     */
    async historyGo(n, expectedLocation) {
      await this.navigate((n) => {
        history.go(n);
      }, [n]);
      if (expectedLocation) {
        await this.waitUntilLocationIs(expectedLocation);
      }
    }

    /**
     * Performs a history traversal back.
     * @param {string} [expectedLocation] If supplied will be passed to @see waitUntilLocationIs.
     * @returns {Promise<undefined>}
     */
    async historyBack(expectedLocation) {
      await this.navigate(() => {
        history.back();
      });
      if (expectedLocation) {
        await this.waitUntilLocationIs(expectedLocation);
      }
    }

    /**
     * Performs a history traversal back.
     * @param {string} [expectedLocation] If supplied will be passed to @see waitUntilLocationIs.
     * @returns {Promise<undefined>}
     */
    async historyForward(expectedLocation) {
      await this.navigate(() => {
        history.forward();
      });
      if (expectedLocation) {
        await this.waitUntilLocationIs(expectedLocation);
      }
    }
  }
}