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

Side by Side Diff: chrome/test/data/push_messaging/test.js

Issue 778243002: Push API: Return cached registration if available. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@store2
Patch Set: Add test, refactor and rebase Created 6 years 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 var pushData = new FutureData();
2
3 // Sends data back to the test. This must be in response to an earlier
4 // request, but it's ok to respond asynchronously. The request blocks until
5 // the response is sent.
6 function sendResultToTest(result) {
7 console.log('sendResultToTest: ' + result);
8 if (window.domAutomationController) {
9 domAutomationController.send('' + result);
10 }
11 }
12
13 function sendErrorToTest(error) {
14 sendResultToTest(error.name + ' - ' + error.message);
15 }
16
17 // A container for a single piece of data. The data does not have to be
18 // available yet when the getter is called, as all responses to the test are
19 // asynchronous.
20 function FutureData() {
21 this.data = null;
22 this.waiting = false;
23 }
24
25 // Sends the data to the test if it is available. Otherwise sets the
26 // |waiting| flag.
27 FutureData.prototype.get = function() {
28 if (this.data) {
29 sendResultToTest(this.data);
30 } else {
31 this.waiting = true;
32 }
33 };
34
35 // Sets a new data value. If the |waiting| flag is on, it is turned off and
36 // the data is sent to the test.
37 FutureData.prototype.set = function(data) {
38 this.data = data;
39 if (this.waiting) {
40 sendResultToTest(data);
41 this.waiting = false;
42 }
43 };
44
45 FutureData.prototype.getImmediately = function() {
46 sendResultToTest(this.data);
47 };
48
49 // Notification permission has been coalesced with Push permission. After
50 // this is granted, Push API registration can succeed.
51 function requestNotificationPermission() {
52 Notification.requestPermission(function(permission) {
53 sendResultToTest('permission status - ' + permission);
54 });
55 }
56
57 function registerServiceWorker() {
58 // The base dir used to resolve service_worker.js and the scope depends on
59 // whether this script is included from ./test.html, subscope1/test.html,
60 // or subscope2/test.html.
61 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
62 function(swRegistration) {
63 sendResultToTest('ok - service worker registered');
64 }, sendErrorToTest);
65 }
66
67 function unregisterServiceWorker() {
68 navigator.serviceWorker.getRegistration().then(function(swRegistration) {
69 swRegistration.unregister().then(function(result) {
70 sendResultToTest('service worker unregistration status: ' + result);
71 })
72 }).catch(sendErrorToTest);
73 }
74
75 function removeManifest() {
76 var element = document.querySelector('link[rel="manifest"]');
77 if (element) {
78 element.parentNode.removeChild(element);
79 sendResultToTest('manifest removed');
80 } else
81 sendResultToTest('unable to find manifest element');
82 }
83
84 function registerPush() {
85 navigator.serviceWorker.ready.then(function(swRegistration) {
86 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
87 var pushManager = swRegistration.pushManager || navigator.push;
88 pushManager.register().then(function(pushRegistration) {
89 sendResultToTest(pushRegistration.pushEndpoint + ' - ' +
90 pushRegistration.pushRegistrationId);
91 }, sendErrorToTest);
92 }, sendErrorToTest);
93 }
94
95 function hasPermission() {
96 navigator.serviceWorker.ready.then(function(swRegistration) {
97 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
98 var pushManager = swRegistration.pushManager || navigator.push;
99 pushManager.hasPermission().then(function(permission) {
100 sendResultToTest('permission status - ' + permission);
101 }, sendErrorToTest);
102 }, sendErrorToTest);
103 }
104
105 function isControlled() {
106 if (navigator.serviceWorker.controller) {
107 sendResultToTest('true - is controlled');
108 } else {
109 sendResultToTest('false - is not controlled');
110 }
111 }
112
113 addEventListener('message', function(event) {
114 var message = JSON.parse(event.data);
115 if (message.type == 'push') {
116 pushData.set(message.data);
117 }
118 }, false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698