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
|
<!DOCTYPE html>
<title>Service Worker: 3P iframe for partitioned service workers</title>
<script src="./test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="./partitioned-utils.js"></script>
<body>
<script>
// 1p mode will respond to requests for its current controller and
// postMessage when its controller changes.
async function onLoad1pMode(){
self.addEventListener('message', evt => {
if(!evt.data)
return;
if (evt.data.type === "get-controller") {
window.parent.postMessage({controller: navigator.serviceWorker.controller});
}
});
navigator.serviceWorker.addEventListener('controllerchange', evt => {
window.parent.postMessage({status: "success", context: "1p"}, '*');
});
}
// 3p mode will tell its SW to claim and then postMessage its results
// automatically.
async function onLoad3pMode() {
reg = await setupServiceWorker();
if(navigator.serviceWorker.controller != null){
//This iframe is already under control of a service worker, testing for
// a controller change will timeout. Return a failure.
window.parent.postMessage({status: "failure", context: "3p"}, '*');
return;
}
// Once this client is claimed, let the test know.
navigator.serviceWorker.addEventListener('controllerchange', evt => {
window.parent.postMessage({status: "success", context: "3p"}, '*');
});
// Trigger the SW to claim.
reg.active.postMessage({type: "claim"});
}
const request_url = new URL(window.location.href);
var url_search = request_url.search.substr(1);
if(url_search == "1p-mode") {
self.addEventListener('load', onLoad1pMode);
}
else if(url_search == "3p-mode") {
self.addEventListener('load', onLoad3pMode);
}
// Else do nothing.
</script>
</body>
|