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

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

Issue 785993003: [Push] Register: require user-visible to be present in the manifest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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();
Michael van Ouwerkerk 2014/12/10 18:36:33 Please turn on strict mode for this file - https:/
Miguel Garcia 2014/12/11 11:37:08 Done.
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 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then(
59 function(swRegistration) {
60 sendResultToTest('ok - service worker registered');
61 }, sendErrorToTest);
62 }
63
64 function unregisterServiceWorker() {
65 navigator.serviceWorker.getRegistration().then(function(swRegistration) {
66 swRegistration.unregister().then(function(result) {
67 sendResultToTest('service worker unregistration status: ' + result);
68 })
69 }).catch(sendErrorToTest);
70 }
71
72 function removeManifest() {
73 var element = document.querySelector('link[rel="manifest"]');
74 if (element) {
75 element.parentNode.removeChild(element);
76 sendResultToTest('manifest removed');
77 } else
78 sendResultToTest('unable to find manifest element');
79 }
80
81 function registerPush() {
82 navigator.serviceWorker.ready.then(function(swRegistration) {
83 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
84 var pushManager = swRegistration.pushManager || navigator.push;
85 return pushManager.register().then(function(pushRegistration) {
86 sendResultToTest(pushRegistration.pushEndpoint + ' - ' +
87 pushRegistration.pushRegistrationId);
88 });
89 }).catch(sendErrorToTest);
90 }
91
92 function hasPermission() {
93 navigator.serviceWorker.ready.then(function(swRegistration) {
94 // TODO(mvanouwerkerk): Cleanup once the final API is exposed.
95 var pushManager = swRegistration.pushManager || navigator.push;
96 return pushManager.hasPermission().then(function(permission) {
97 sendResultToTest('permission status - ' + permission);
98 });
99 }).catch(sendErrorToTest);
100 }
101
102 function isControlled() {
103 if (navigator.serviceWorker.controller) {
104 sendResultToTest('true - is controlled');
105 } else {
106 sendResultToTest('false - is not controlled');
107 }
108 }
109
110 addEventListener('message', function(event) {
111 var message = JSON.parse(event.data);
112 if (message.type == 'push') {
113 pushData.set(message.data);
114 }
115 }, false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698