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;
9 run_pending_permission_resolver = function() {
10 pending_set_permission_resolver();
11 pending_set_permission_resolver = null;
14 send_from_shared_worker = function(message) {
15 wait_for_port = function() {
16 if (!shared_worker_port) {
17 setTimeout(wait_for_port);
20 shared_worker_port.postMessage(message);
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);
34 self.clients.matchAll({ includeUncontrolled: true }).then(function(c) {
35 c[0].postMessage(message);
38 wait_for_activation();
41 window_message_handler = function (message) {
42 if (message.data.name != 'setPermission')
44 testRunner.setPermission(message.data.permission,
47 message.data.embedding_origin);
49 this.postMessage('setPermission_ACK');
52 // Setup message handlers.
53 switch (get_current_scope()) {
55 window.addEventListener('message', function(message) {
56 testRunner.setPermission(message.data.permission,
59 message.data.embedding_origin);
61 navigator.serviceWorker.getRegistration(get_script_href()).then(function(registration) {
62 registration.active.postMessage('setPermission_ACK');
66 case 'DedicatedWorker':
67 self.addEventListener('message', function(message) {
68 if (message.data != 'setPermission_ACK')
70 if (pending_set_permission_resolver)
71 run_pending_permission_resolver();
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')
80 if (pending_set_permission_resolver)
81 run_pending_permission_resolver();
83 shared_worker_port.start();
88 self.addEventListener('message', function(message) {
89 if (message.data != 'setPermission_ACK')
91 if (pending_set_permission_resolver)
92 run_pending_permission_resolver();
98 function setPermission(permission, status, origin, embedding_origin) {
99 var promise = new Promise(function(resolver) {
100 set_pending_permission_resolver(resolver);
103 switch (get_current_scope()) {
105 testRunner.setPermission(permission, status, origin, embedding_origin);
106 run_pending_permission_resolver();
108 case 'DedicatedWorker':
109 self.postMessage({ name: 'setPermission',
110 permission: permission,
113 embedding_origin: embedding_origin });
116 send_from_shared_worker({ name: 'setPermission',
117 permission: permission,
120 embedding_origin: embedding_origin });
122 case 'ServiceWorker':
123 send_from_service_worker({ name: 'setPermission',
124 permission: permission,
127 embedding_origin: embedding_origin });
131 throw new Error('unknown scope');