blob: cc0fcdee6bd09f6224d6e73f64f379c6fbc98d47 [file] [log] [blame]
// Copyright 2022 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
console.log('Service Worker Script Started');
self.onmessage = function (event) {
console.log('Got onmessage event', event.source, event.target, event.data);
var options = {
includeUncontrolled: true, type: 'window'
};
self.clients.matchAll(options).then(function (clients) {
for (var i = 0; i < clients.length; i++) {
message = `Service Worker received a message : ${event.data}`;
console.log('Posting to client:', message);
clients[i].postMessage(message);
}
});
}
self.oninstall = function (e) {
console.log('oninstall event received', e);
}
self.onactivate = function (e) {
console.log('onactivate event received', e);
// Claim should pass here, since the state is activating.
console.log('self.clients.claim()');
self.clients.claim().then(function (result) {
console.log('(Expected) self.clients.claim():', result);
var options = {
includeUncontrolled: false, type: 'window'
};
console.log('self.clients.matchAll(options)');
self.clients.matchAll(options).then(function (clients) {
console.log('(Expected) self.clients.matchAll():', clients.length, clients);
for (var i = 0; i < clients.length; i++) {
console.log('Client with url', clients[i].url);
console.log('Client with frameType', clients[i].frameType);
console.log('Client with id', clients[i].id);
console.log('Client with type', clients[i].type);
clients[i].postMessage(`You have been claimed, client with id ${clients[i].id}`);
}
}, function (error) {
console.log(`(Unexpected) self.clients.matchAll(): ${error}`, error);
});
}, function (error) {
console.log(`(Unexpected) self.clients.claim(): ${error}`, error);
});
}
console.log('self.registration', self.registration);
console.log('self.serviceWorker', self.serviceWorker);
console.log('self.clients:', self.clients);
var options = {
includeUncontrolled: true, type: 'window'
};
console.log('self.clients.matchAll(options)');
self.clients.matchAll(options).then(function (clients) {
console.log('(Expected) self.clients.matchAll():', clients.length, clients);
for (var i = 0; i < clients.length; i++) {
console.log('Client with url', clients[i].url);
console.log('Client with frameType', clients[i].frameType);
console.log('Client with id', clients[i].id);
console.log('Client with type', clients[i].type);
}
}, function (error) {
console.log(`(Unexpected) self.clients.matchAll(): ${error}`, error);
});
// Expected to fail since the worker isn't expected to be activated yet.
console.log('self.clients.claim()');
self.clients.claim().then(function (clients) {
console.log('(Unexpected) self.clients.claim():', clients);
}, function (error) {
console.log(`(Expected) self.clients.claim() not yet activated: ${error}`, error);
});