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

Unified 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: Changed formatting of javascript in html files Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/http/tests/budget/resources/instrumentation-service-worker.js
diff --git a/third_party/WebKit/LayoutTests/http/tests/budget/resources/instrumentation-service-worker.js b/third_party/WebKit/LayoutTests/http/tests/budget/resources/instrumentation-service-worker.js
new file mode 100644
index 0000000000000000000000000000000000000000..87b4e4a165b539a12f0f6cb6caae826a1148fc92
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/budget/resources/instrumentation-service-worker.js
@@ -0,0 +1,56 @@
+// Allows a document to exercise the Budget API within a service worker by sending commands.
+
+self.addEventListener('message', function(workerEvent) {
+ port = workerEvent.data;
+
+ // Listen to incoming commands on the message port.
+ port.onmessage = function(event) {
+ if (typeof event.data != 'object' || !event.data.command)
+ return;
+ var options = event.data.options || {}
Peter Beverloo 2016/08/24 15:28:23 ;
harkness 2016/08/24 16:22:50 Done.
+ switch (event.data.command) {
+
+ case 'getCost':
+ navigator.budget.getCost('silent-push').then(function(cost) {
+ port.postMessage({ command: event.data.command,
+ success: true,
+ cost: cost });
Peter Beverloo 2016/08/24 15:28:23 The alignment of this (and lines 23+, 31+, 39+ and
harkness 2016/08/24 16:22:50 Done.
+ }).catch(makeErrorHandler(event.data.command));
+ break;
+
+ case 'getCostInvalidType':
+ navigator.budget.getCost('frobinator').then(function(cost) {
+ port.postMessage({ command: event.data.command,
+ success: true,
+ cost: cost });
+ }).catch(makeErrorHandler(event.data.command));
+ break;
+
+ case 'getBudget':
+ navigator.budget.getBudget().then(budget => {
+ port.postMessage({ command: event.data.command,
+ success: true,
+ budgetAt: budget[0].budgetAt,
+ time: budget[0].time });
+ }).catch(makeErrorHandler(event.data.command));
+ break;
+
+ default:
+ port.postMessage({ command: 'error',
+ errorMessage: 'Invalid command: ' + event.data.command });
+ break;
+ }
+ };
+
+ // Notify the controller that the worker is now available.
+ port.postMessage('ready');
+});
+
+function makeErrorHandler(command) {
+ return function(error) {
+ var errorMessage = error ? error.message : 'unknown error';
+ port.postMessage({ command: command,
+ success: false,
+ errorMessage: errorMessage });
+ };
+}

Powered by Google App Engine
This is Rietveld 408576698