service-worker-child.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. let { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  6. let swm = Cc["@mozilla.org/serviceworkers/manager;1"].
  7. getService(Ci.nsIServiceWorkerManager);
  8. addMessageListener("serviceWorkerRegistration:start", message => {
  9. let { data } = message;
  10. let array = swm.getAllRegistrations();
  11. // Find the service worker registration with the desired scope.
  12. for (let i = 0; i < array.length; i++) {
  13. let registration =
  14. array.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
  15. // XXX: In some rare cases, `registration.activeWorker` can be null for a
  16. // brief moment (e.g. while the service worker is first installing, or if
  17. // there was an unhandled exception during install that will cause the
  18. // registration to be removed). We can't do much about it here, simply
  19. // ignore these cases.
  20. if (registration.scope === data.scope && registration.activeWorker) {
  21. // Briefly attaching a debugger to the active service worker will cause
  22. // it to start running.
  23. registration.activeWorker.attachDebugger();
  24. registration.activeWorker.detachDebugger();
  25. return;
  26. }
  27. }
  28. });