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