Browse Source

feat: ServiceWorkerMain.scriptURL (#45863)

feat: serviceWorker.scriptURL
Sam Maddock 1 month ago
parent
commit
afca4e271e

+ 4 - 0
docs/api/service-worker-main.md

@@ -46,6 +46,10 @@ An [`IpcMainServiceWorker`](ipc-main-service-worker.md) instance scoped to the s
 
 A `string` representing the scope URL of the service worker.
 
+#### `serviceWorker.scriptURL` _Readonly_ _Experimental_
+
+A `string` representing the script URL of the service worker.
+
 #### `serviceWorker.versionId` _Readonly_ _Experimental_
 
 A `number` representing the ID of the specific version of the service worker script in its scope.

+ 7 - 0
shell/browser/api/electron_api_service_worker_main.cc

@@ -282,6 +282,12 @@ GURL ServiceWorkerMain::ScopeURL() const {
   return version_info()->scope;
 }
 
+GURL ServiceWorkerMain::ScriptURL() const {
+  if (version_destroyed_)
+    return {};
+  return version_info()->script_url;
+}
+
 // static
 gin::Handle<ServiceWorkerMain> ServiceWorkerMain::New(v8::Isolate* isolate) {
   return gin::Handle<ServiceWorkerMain>();
@@ -330,6 +336,7 @@ void ServiceWorkerMain::FillObjectTemplate(
                  &ServiceWorkerMain::CountExternalRequestsForTest)
       .SetProperty("versionId", &ServiceWorkerMain::VersionID)
       .SetProperty("scope", &ServiceWorkerMain::ScopeURL)
+      .SetProperty("scriptURL", &ServiceWorkerMain::ScriptURL)
       .Build();
 }
 

+ 1 - 0
shell/browser/api/electron_api_service_worker_main.h

@@ -147,6 +147,7 @@ class ServiceWorkerMain final
 
   int64_t VersionID() const;
   GURL ScopeURL() const;
+  GURL ScriptURL() const;
 
   // Version ID unique only to the StoragePartition.
   int64_t version_id_;

+ 11 - 0
spec/api-service-worker-main-spec.ts

@@ -314,6 +314,17 @@ describe('ServiceWorkerMain module', () => {
     });
   });
 
+  describe("'scriptURL' property", () => {
+    it('matches the expected value', async () => {
+      loadWorkerScript();
+      const serviceWorker = await waitForServiceWorker();
+      expect(serviceWorker).to.not.be.undefined();
+      if (!serviceWorker) return;
+      expect(serviceWorker).to.have.property('scriptURL').that.is.a('string');
+      expect(serviceWorker.scriptURL).to.equal(`${baseUrl}/sw.js`);
+    });
+  });
+
   describe('ipc', () => {
     beforeEach(() => {
       registerPreload('preload-tests.js');