Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / fetch-mixed-content-iframe.html
blob9313ef4ec002ab2f8eef4b474e02286121a29628
1 <!DOCTYPE html>
2 <script src="../../resources/get-host-info.js"></script>
3 <script src="test-helpers.js?pipe=sub"></script>
4 <script>
5 var params = get_query_params(location.href);
6 var SCOPE = 'fetch-mixed-content-iframe-inscope-to-' + params['target'] + '.html';
7 var URL = 'fetch-rewrite-worker.js';
8 var host_info = get_host_info();
10 window.addEventListener('message', on_message, false);
12 navigator.serviceWorker.getRegistration(SCOPE)
13 .then(function(registration) {
14 if (registration)
15 return registration.unregister();
17 .then(function() {
18 return navigator.serviceWorker.register(URL, {scope: SCOPE});
20 .then(function(registration) {
21 return new Promise(function(resolve) {
22 registration.addEventListener('updatefound', function() {
23 resolve(registration.installing);
24 });
25 });
27 .then(function(worker) {
28 worker.addEventListener('statechange', on_state_change);
30 .catch(function(reason) {
31 window.parent.postMessage({results: 'FAILURE: ' + reason.message},
32 host_info['HTTP_ORIGIN']);
33 });
35 function on_state_change(event) {
36 if (event.target.state != 'activated')
37 return;
38 var frame = document.createElement('iframe');
39 frame.src = SCOPE;
40 document.body.appendChild(frame);
43 function on_message(e) {
44 navigator.serviceWorker.getRegistration(SCOPE)
45 .then(function(registration) {
46 if (registration)
47 return registration.unregister();
49 .then(function() {
50 window.parent.postMessage(e.data, host_info['HTTP_ORIGIN']);
52 .catch(function(reason) {
53 window.parent.postMessage({results: 'FAILURE: ' + reason.message},
54 host_info['HTTP_ORIGIN']);
55 });
58 function get_query_params(url) {
59 var search = (new URL(url)).search;
60 if (!search) {
61 return {};
63 var ret = {};
64 var params = search.substring(1).split('&');
65 params.forEach(function(param) {
66 var element = param.split('=');
67 ret[decodeURIComponent(element[0])] = decodeURIComponent(element[1]);
68 });
69 return ret;
71 </script>