Bug 1450070 - WIP: Add update method to ServiceWorker actor draft
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 18 May 2018 11:08:09 +0900
changeset 796720 58d738f0fa0160fe2c59fd291d5eaf16d4e86afc
parent 796719 8ee8556fd802532c230ea40b6da4b19902395e88
child 796721 36a561e30d14970330a3f11db8be854b92605a9c
push id110345
push userjdescottes@mozilla.com
push dateFri, 18 May 2018 06:04:47 +0000
bugs1450070
milestone62.0a1
Bug 1450070 - WIP: Add update method to ServiceWorker actor MozReview-Commit-ID: KAuPEhg3D7g
devtools/server/actors/worker.js
devtools/server/actors/worker/service-worker-process.js
devtools/shared/specs/worker.js
--- a/devtools/server/actors/worker.js
+++ b/devtools/server/actors/worker.js
@@ -370,16 +370,41 @@ protocol.ActorClassWithSpec(serviceWorke
       this._registration.principal);
 
     Services.ppmm.broadcastAsyncMessage("serviceWorkerRegistration:start", {
       scope: this._registration.scope
     });
     return { type: "started" };
   },
 
+  update() {
+    if (!_serviceWorkerProcessScriptLoaded) {
+      Services.ppmm.loadProcessScript(
+        "resource://devtools/server/actors/worker/service-worker-process.js", true);
+      _serviceWorkerProcessScriptLoaded = true;
+    }
+
+    // XXX: Send the permissions down to the content process before starting
+    // the service worker within the content process. As we don't know what
+    // content process we're starting the service worker in (as we're using a
+    // broadcast channel to talk to it), we just broadcast the permissions to
+    // everyone as well.
+    //
+    // This call should be replaced with a proper implementation when
+    // ServiceWorker debugging is improved to support multiple content processes
+    // correctly.
+    Services.perms.broadcastPermissionsForPrincipalToAllContentProcesses(
+      this._registration.principal);
+
+    Services.ppmm.broadcastAsyncMessage("serviceWorkerRegistration:update", {
+      scope: this._registration.scope
+    });
+    return { type: "updated" };
+  },
+
   unregister() {
     let { principal, scope } = this._registration;
     let unregisterCallback = {
       unregisterSucceeded: function() {},
       unregisterFailed: function() {
         console.error("Failed to unregister the service worker for " + scope);
       },
       QueryInterface: ChromeUtils.generateQI(
--- a/devtools/server/actors/worker/service-worker-process.js
+++ b/devtools/server/actors/worker/service-worker-process.js
@@ -31,8 +31,36 @@ addMessageListener("serviceWorkerRegistr
       // Briefly attaching a debugger to the active service worker will cause
       // it to start running.
       registration.activeWorker.attachDebugger();
       registration.activeWorker.detachDebugger();
       return;
     }
   }
 });
+
+function findRegistration(scope) {
+  let array = swm.getAllRegistrations();
+
+  // Find the service worker registration with the desired scope.
+  for (let i = 0; i < array.length; i++) {
+    let registration =
+      array.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
+    if (registration.scope === scope && registration.activeWorker) {
+      return registration;
+    }
+  }
+
+  return null;
+}
+
+addMessageListener("serviceWorkerRegistration:update", message => {
+  let { data } = message;
+  let registration = findRegistration(data.scope);
+
+  if (registration) {
+    let { principal, scope } = registration;
+    // Send update for the registration.
+    swm.propagateSoftUpdate(principal.originAttributes, scope);
+    registration.activeWorker.attachDebugger();
+    registration.activeWorker.detachDebugger();
+  }
+});
--- a/devtools/shared/specs/worker.js
+++ b/devtools/shared/specs/worker.js
@@ -54,16 +54,20 @@ const serviceWorkerRegistrationSpec = ge
     start: {
       request: {},
       response: RetVal("json")
     },
     unregister: {
       request: {},
       response: RetVal("json")
     },
+    update: {
+      request: {},
+      response: RetVal("json")
+    },
     getPushSubscription: {
       request: {},
       response: {
         subscription: RetVal("nullable:pushSubscription")
       }
     },
   },
 });