2015-04-11 06:19:28 +03:00
|
|
|
// Any copyright is dedicated to the Public Domain.
|
|
|
|
// http://creativecommons.org/licenses/publicdomain/
|
|
|
|
|
2016-01-28 19:06:32 +03:00
|
|
|
// This worker is used for two types of tests. `handlePush` sends messages to
|
|
|
|
// `frame.html`, which verifies that the worker can receive push messages.
|
|
|
|
|
|
|
|
// `handleMessage` receives messages from `test_push_manager_worker.html`
|
|
|
|
// and `test_data.html`, and verifies that `PushManager` can be used from
|
|
|
|
// the worker.
|
|
|
|
|
2015-04-23 21:43:40 +03:00
|
|
|
this.onpush = handlePush;
|
2015-09-17 15:10:42 +03:00
|
|
|
this.onmessage = handleMessage;
|
2016-01-29 06:26:17 +03:00
|
|
|
this.onpushsubscriptionchange = handlePushSubscriptionChange;
|
2015-09-17 15:10:42 +03:00
|
|
|
|
|
|
|
function getJSON(data) {
|
|
|
|
var result = {
|
|
|
|
ok: false,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
result.value = data.json();
|
|
|
|
result.ok = true;
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore syntax errors for invalid JSON.
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-04-23 21:43:40 +03:00
|
|
|
|
2016-01-28 19:06:32 +03:00
|
|
|
function assert(value, message) {
|
|
|
|
if (!value) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 06:26:17 +03:00
|
|
|
function broadcast(event, promise) {
|
|
|
|
event.waitUntil(Promise.resolve(promise).then(message => {
|
|
|
|
return self.clients.matchAll().then(clients => {
|
|
|
|
clients.forEach(client => client.postMessage(message));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-01-28 19:06:32 +03:00
|
|
|
function reply(event, promise) {
|
2016-01-29 06:26:17 +03:00
|
|
|
event.waitUntil(Promise.resolve(promise).then(result => {
|
2016-01-28 19:06:32 +03:00
|
|
|
event.ports[0].postMessage(result);
|
|
|
|
}).catch(error => {
|
|
|
|
event.ports[0].postMessage({
|
|
|
|
error: String(error),
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-04-23 21:43:40 +03:00
|
|
|
function handlePush(event) {
|
2016-01-29 06:26:17 +03:00
|
|
|
if (event instanceof PushEvent) {
|
|
|
|
if (!('data' in event)) {
|
|
|
|
broadcast(event, {type: "finished", okay: "yes"});
|
2016-02-01 12:06:43 +03:00
|
|
|
return;
|
2016-01-29 06:26:17 +03:00
|
|
|
}
|
2016-01-29 06:26:17 +03:00
|
|
|
var message = {
|
|
|
|
type: "finished",
|
|
|
|
okay: "yes",
|
|
|
|
};
|
|
|
|
if (event.data) {
|
|
|
|
message.data = {
|
|
|
|
text: event.data.text(),
|
|
|
|
arrayBuffer: event.data.arrayBuffer(),
|
|
|
|
json: getJSON(event.data),
|
|
|
|
blob: event.data.blob(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
broadcast(event, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
broadcast(event, {type: "finished", okay: "no"});
|
2015-04-23 21:43:40 +03:00
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
|
2016-03-23 02:29:16 +03:00
|
|
|
var testHandlers = {
|
|
|
|
publicKey(data) {
|
|
|
|
return self.registration.pushManager.getSubscription().then(
|
2016-01-28 19:06:32 +03:00
|
|
|
subscription => ({
|
2015-12-11 19:16:46 +03:00
|
|
|
p256dh: subscription.getKey("p256dh"),
|
|
|
|
auth: subscription.getKey("auth"),
|
2016-01-28 19:06:32 +03:00
|
|
|
})
|
2016-03-23 02:29:16 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
resubscribe(data) {
|
|
|
|
return self.registration.pushManager.getSubscription().then(
|
2016-01-28 19:06:32 +03:00
|
|
|
subscription => {
|
2016-03-23 02:29:16 +03:00
|
|
|
assert(subscription.endpoint == data.endpoint,
|
2016-01-28 19:06:32 +03:00
|
|
|
"Wrong push endpoint in worker");
|
|
|
|
return subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
).then(result => {
|
|
|
|
assert(result, "Error unsubscribing in worker");
|
|
|
|
return self.registration.pushManager.getSubscription();
|
|
|
|
}).then(subscription => {
|
|
|
|
assert(!subscription, "Subscription not removed in worker");
|
|
|
|
return self.registration.pushManager.subscribe();
|
|
|
|
}).then(subscription => {
|
|
|
|
return {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
};
|
2016-03-23 02:29:16 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
denySubscribe(data) {
|
|
|
|
return self.registration.pushManager.getSubscription().then(
|
2016-01-28 19:23:29 +03:00
|
|
|
subscription => {
|
|
|
|
assert(!subscription,
|
|
|
|
"Should not return worker subscription with revoked permission");
|
|
|
|
return self.registration.pushManager.subscribe().then(_ => {
|
|
|
|
assert(false, "Expected error subscribing with revoked permission");
|
|
|
|
}, error => {
|
|
|
|
return {
|
|
|
|
isDOMException: error instanceof DOMException,
|
|
|
|
name: error.name,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2016-03-23 02:29:16 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribeWithKey(data) {
|
|
|
|
return self.registration.pushManager.subscribe({
|
|
|
|
applicationServerKey: data.key,
|
|
|
|
}).then(subscription => {
|
|
|
|
return {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
key: subscription.options.applicationServerKey,
|
|
|
|
};
|
|
|
|
}, error => {
|
|
|
|
return {
|
|
|
|
isDOMException: error instanceof DOMException,
|
|
|
|
name: error.name,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleMessage(event) {
|
|
|
|
var handler = testHandlers[event.data.type];
|
|
|
|
if (handler) {
|
|
|
|
reply(event, handler(event.data));
|
|
|
|
} else {
|
|
|
|
reply(event, Promise.reject(
|
|
|
|
"Invalid message type: " + event.data.type));
|
2016-01-28 19:23:29 +03:00
|
|
|
}
|
2015-09-17 15:10:42 +03:00
|
|
|
}
|
2016-01-29 06:26:17 +03:00
|
|
|
|
|
|
|
function handlePushSubscriptionChange(event) {
|
|
|
|
broadcast(event, {type: "changed", okay: "yes"});
|
|
|
|
}
|