File: protocol-handler-register.https.html

package info (click to toggle)
firefox-esr 140.6.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,552,424 kB
  • sloc: cpp: 7,430,808; javascript: 6,389,773; ansic: 3,712,263; python: 1,393,776; xml: 628,165; asm: 426,918; java: 184,004; sh: 65,744; makefile: 19,302; objc: 13,059; perl: 12,912; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,226; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (107 lines) | stat: -rw-r--r-- 4,600 bytes parent folder | download | duplicates (13)
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
<!DOCTYPE html>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="../resources/utils.js"></script>
<script src="resources/utils.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>

<script>
function getNextWindowMessageFromFrame(frame) {
  return new Promise(resolve => {
    window.addEventListener('message', event => {
      if (event.source === frame.contentWindow) {
        resolve(event.data);
      }
    });
  });
}

promise_setup(async () => {
  assertSpeculationRulesIsSupported()
  await test_driver.set_rph_registration_mode('autoAccept');
  await test_driver.bless('handler registration');
});

// The overall idea for this test is:
// 1. Register a protocol handler for a custom URI scheme to target 1 (initial).
// 2. Create a prerendered page that registers the same custom URI scheme to
// target 2 (modified). But the registration will defer until after activation.
// 3. Navigate an iframe to the custom URI scheme. It should go to target 1,
// because the deferred registration hasn't yet been applied.
// 4. Activate the prerendered page. This should run the deferred registration.
// 5. Navigate the iframe to the custom URI scheme again. It should go to
// target 2.
promise_test(async t => {
  const customUrlScheme = 'web+wptrphtest';
  function getProtocolHandlerUrlTemplate(id) {
    return new URL(
        `resources/protocol-handler.html?id=${id}&s=%s`, location.href).href;
  }

  const initialUrlTemplate = getProtocolHandlerUrlTemplate('initial');
  const modifiedUrlTemplate = getProtocolHandlerUrlTemplate('modified');

  // To start we register the initial protocol handler. When there is nothing
  // registered for a custom URI scheme, and we navigate to it, there's no
  // good event to tell us that the navigation failed. So instead we
  // have an initial handler so we can tell that the modified protocol handler
  // hasn't been registered yet.
  t.add_cleanup(() => {
    navigator.unregisterProtocolHandler(
        customUrlScheme, initialUrlTemplate);
  });
  navigator.registerProtocolHandler(customUrlScheme, initialUrlTemplate);

  // Create a prerender page that attempts to register an invalid scheme.
  const {exec, activate} = await create_prerendered_page(t);

  t.add_cleanup(() => {
    navigator.unregisterProtocolHandler(
      customUrlScheme, modifiedUrlTemplate);
  });
  // First register protocol handler during prerender
  const result = await exec(
      (customUrlScheme, modifiedUrlTemplate) => {
        try {
          navigator.registerProtocolHandler(
            customUrlScheme, modifiedUrlTemplate);
        } catch (registerProtocolHandlerException) {
          return 'registerProtocolHandler failed with \''
              + registerProtocolHandlerException.name + '\'';
        }
        return 'success';
  }, [customUrlScheme, modifiedUrlTemplate]);

  assert_equals(result, 'success',
    'registerProtocolHandler should succeed.');

  // Next create the iframe that we will navigate to the custom URI scheme so
  // we can validate that the prerenderer registration hasn't yet been applied.
  const frame = await with_iframe('about:blank');

  // Navigate to the custom URI scheme. Even though the prerendered page
  // registered a handler, registration shouldn't happen until after activation.
  // So this navigation should go to the initial handler.
  const beforeActivationMessagePromise = getNextWindowMessageFromFrame(frame);
  frame.src = `${customUrlScheme}:1`;
  assert_equals((await beforeActivationMessagePromise).id, 'initial',
    'Until activation, the initial handler should be registered.');

  // Activate the prerendered page. This should run the deferred registration.
  await activate();

  // Now we listen for messages and again navigate to the custom URI scheme
  // and by now the prerenderer registration should have applied and we should
  // get the modified handler's id.
  const afterActivationMessagePromise = getNextWindowMessageFromFrame(frame);
  frame.src = `${customUrlScheme}:2`;
  assert_equals((await afterActivationMessagePromise).id, 'modified',
    'After activation, the modified handler should be registered.');
}, 'prerendering page registerProtocolHandler call defers registration until activation.');

</script>