Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/budget/resources/instrumentation-service-worker.js

Issue 2273743002: Framework and tests for WorkerNavigatorBudget (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo_service
Patch Set: rebased Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Allows a document to exercise the Budget API within a service worker by sendi ng commands.
2
3 self.addEventListener('message', function(workerEvent) {
4 port = workerEvent.data;
5
6 // Listen to incoming commands on the message port.
7 port.onmessage = function(event) {
8 if (typeof event.data != 'object' || !event.data.command)
9 return;
10 var options = event.data.options || {};
11 switch (event.data.command) {
12
13 case 'getCost':
14 navigator.budget.getCost('silent-push').then(function(cost) {
15 port.postMessage({
16 command: event.data.command,
17 success: true,
18 cost: cost });
19 }).catch(makeErrorHandler(event.data.command));
20 break;
21
22 case 'getCostInvalidType':
23 navigator.budget.getCost('frobinator').then(function(cost) {
24 port.postMessage({
25 command: event.data.command,
26 success: true,
27 cost: cost });
28 }).catch(makeErrorHandler(event.data.command));
29 break;
30
31 case 'getBudget':
32 navigator.budget.getBudget().then(budget => {
33 port.postMessage({
34 command: event.data.command,
35 success: true,
36 budgetAt: budget[0].budgetAt,
37 time: budget[0].time });
38 }).catch(makeErrorHandler(event.data.command));
39 break;
40
41 default:
42 port.postMessage({
43 command: 'error',
44 errorMessage: 'Invalid command: ' + event.data.command });
45 break;
46 }
47 };
48
49 // Notify the controller that the worker is now available.
50 port.postMessage('ready');
51 });
52
53 function makeErrorHandler(command) {
54 return function(error) {
55 var errorMessage = error ? error.message : 'unknown error';
56 port.postMessage({
57 command: command,
58 success: false,
59 errorMessage: errorMessage });
60 };
61 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698