summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/local-url-inherit-controller-frame.html
blob: b1e554d2204d728bfec17fa24c3706bd9d66bdfe (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
<!DOCTYPE html>
<html>
<script>

const fetchURL = new URL('sample.js', window.location).href;

const frameControllerText =
`<script>
  let t = null;
  try {
    if (navigator.serviceWorker.controller) {
      t = navigator.serviceWorker.controller.scriptURL;
    }
  } catch (e) {
    t = e.message;
  } finally {
    parent.postMessage({ data: t }, '*');
  }
</` + `script>`;

const frameFetchText =
`<script>
  fetch('${fetchURL}', { mode: 'no-cors' }).then(response => {
    return response.text();
  }).then(text => {
    parent.postMessage({ data: text }, '*');
  }).catch(e => {
    parent.postMessage({ data: e.message }, '*');
  });
</` + `script>`;

const workerControllerText =
`let t = navigator.serviceWorker.controller
       ? navigator.serviceWorker.controller.scriptURL
       : null;
self.postMessage(t);`;

const workerFetchText =
`fetch('${fetchURL}', { mode: 'no-cors' }).then(response => {
  return response.text();
}).then(text => {
  self.postMessage(text);
}).catch(e => {
  self.postMessage(e.message);
});`

function getChildText(opts) {
  if (opts.child === 'iframe') {
    if (opts.check === 'controller') {
      return frameControllerText;
    }

    if (opts.check === 'fetch') {
      return frameFetchText;
    }

    throw('unexpected feature to check: ' + opts.check);
  }

  if (opts.child === 'worker') {
    if (opts.check === 'controller') {
      return workerControllerText;
    }

    if (opts.check === 'fetch') {
      return workerFetchText;
    }

    throw('unexpected feature to check: ' + opts.check);
  }

  throw('unexpected child type ' + opts.child);
}

function makeURL(opts) {
  let mimetype = opts.child === 'iframe' ? 'text/html'
                                         : 'text/javascript';

  if (opts.scheme === 'blob') {
    let blob = new Blob([getChildText(opts)], { type: mimetype });
    return URL.createObjectURL(blob);
  }

  if (opts.scheme === 'data') {
    return `data:${mimetype},${getChildText(opts)}`;
  }

  throw(`unexpected URL scheme ${opts.scheme}`);
}

function testWorkerChild(url) {
  let w = new Worker(url);
  return new Promise((resolve, reject) => {
    w.onmessage = resolve;
    w.onerror = evt => {
      reject(evt.message);
    }
  });
}

function testIframeChild(url) {
  let frame = document.createElement('iframe');
  frame.src = url;
  document.body.appendChild(frame);

  return new Promise(resolve => {
    addEventListener('message', evt => {
      resolve(evt.data);
    }, { once: true });
  });
}

function testURL(opts, url) {
  if (opts.child === 'worker') {
    return testWorkerChild(url);
  }

  if (opts.child === 'iframe') {
    return testIframeChild(url);
  }

  throw(`unexpected child type ${opts.child}`);
}

function checkChildController(opts) {
  let url = makeURL(opts);
  return testURL(opts, url);
}
</script>
</html>