Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / permissions / chromium / resources / testrunner-helpers.js
blob2a28789af42ba5e30418d1c9de1d43aa0caffe58
1 (function() {
2     var pending_set_permission_resolver = null;
3     var shared_worker_port = null;
5     set_pending_permission_resolver = function(value) {
6         pending_set_permission_resolver = value;
7     };
9     run_pending_permission_resolver = function() {
10         pending_set_permission_resolver();
11         pending_set_permission_resolver = null;
12     };
14     send_from_shared_worker = function(message) {
15         wait_for_port = function() {
16             if (!shared_worker_port) {
17                 setTimeout(wait_for_port);
18                 return;
19             }
20             shared_worker_port.postMessage(message);
21         };
22         wait_for_port();
23     };
25     send_from_service_worker = function(message) {
26         wait_for_activation = function() {
27             if (!self.registration.active ||
28                 self.registration.active.state != 'activated') {
29                 setTimeout(wait_for_activation);
30                 return;
31             }
33             self.clients.claim();
34             self.clients.matchAll({ includeUncontrolled: true }).then(function(c) {
35                 c[0].postMessage(message);
36             });
37         };
38         wait_for_activation();
39     };
41     window_message_handler = function (message) {
42         if (message.data.name != 'setPermission')
43             return;
44         testRunner.setPermission(message.data.permission,
45                                  message.data.status,
46                                  message.data.origin,
47                                  message.data.embedding_origin);
49         this.postMessage('setPermission_ACK');
50     };
52     // Setup message handlers.
53     switch (get_current_scope()) {
54         case 'Window':
55             window.addEventListener('message', function(message) {
56                 testRunner.setPermission(message.data.permission,
57                                          message.data.status,
58                                          message.data.origin,
59                                          message.data.embedding_origin);
61                 navigator.serviceWorker.getRegistration(get_script_href()).then(function(registration) {
62                     registration.active.postMessage('setPermission_ACK');
63                 });
64             });
65             break;
66         case 'DedicatedWorker':
67             self.addEventListener('message', function(message) {
68                 if (message.data != 'setPermission_ACK')
69                     return;
70                 if (pending_set_permission_resolver)
71                     run_pending_permission_resolver();
72             });
73             break;
74         case 'SharedWorker':
75             self.addEventListener('connect', function(e) {
76                 shared_worker_port = e.ports[0];
77                 shared_worker_port.addEventListener('message', function(message) {
78                     if (message.data != 'setPermission_ACK')
79                         return;
80                     if (pending_set_permission_resolver)
81                         run_pending_permission_resolver();
82                 });
83                 shared_worker_port.start();
84             });
85             break;
86         case 'ServiceWorker':
87             self.skipWaiting();
88             self.addEventListener('message', function(message) {
89                 if (message.data != 'setPermission_ACK')
90                     return;
91                 if (pending_set_permission_resolver)
92                     run_pending_permission_resolver();
93             });
94             break;
95     }
96 })();
98 function setPermission(permission, status, origin, embedding_origin) {
99     var promise = new Promise(function(resolver) {
100         set_pending_permission_resolver(resolver);
101     });
103     switch (get_current_scope()) {
104         case 'Window':
105             testRunner.setPermission(permission, status, origin, embedding_origin);
106             run_pending_permission_resolver();
107             return promise;
108         case 'DedicatedWorker':
109             self.postMessage({ name: 'setPermission',
110                                permission: permission,
111                                status: status,
112                                origin: origin,
113                                embedding_origin: embedding_origin });
114             return promise;
115         case 'SharedWorker':
116             send_from_shared_worker({ name: 'setPermission',
117                                       permission: permission,
118                                       status: status,
119                                       origin: origin,
120                                       embedding_origin: embedding_origin });
121             return promise;
122         case 'ServiceWorker':
123             send_from_service_worker({ name: 'setPermission',
124                                        permission: permission,
125                                        status: status,
126                                        origin: origin,
127                                        embedding_origin: embedding_origin });
128             return promise;
129     }
131     throw new Error('unknown scope');