commit bb54c74efc0063c86f0ea8f85fdef1fe1505f81b
parent ba62ccaac5d8b2664680a716b47c229bcdeb6e66
Author: Harveer Singh <hsingh@mozilla.com>
Date: Fri, 14 Nov 2025 15:53:26 +0000
Bug 1360870: on-disk schema changes to persist new serviceworker type parameter.r=edenchuang,dom-worker-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D264456
Diffstat:
2 files changed, 64 insertions(+), 52 deletions(-)
diff --git a/dom/serviceworkers/ServiceWorkerRegistrar.cpp b/dom/serviceworkers/ServiceWorkerRegistrar.cpp
@@ -48,7 +48,7 @@ namespace mozilla::dom {
namespace {
static const uint32_t gSupportedRegistrarVersions[] = {
- SERVICEWORKERREGISTRAR_VERSION, 10, 9, 8, 7, 6, 5, 4, 3, 2};
+ SERVICEWORKERREGISTRAR_VERSION, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
static const uint32_t kInvalidGeneration = static_cast<uint32_t>(-1);
@@ -626,43 +626,15 @@ nsresult ServiceWorkerRegistrar::ReadData() {
return NS_ERROR_FAILURE; \
}
- nsAutoCString line;
- switch (version) {
- // to add new changes to the schema,
- // we incremented SERVICEWORKERREGISTRAR_VERSION,
- // added new changes on top of existing schema,
- // and [[fallthrough]] to the previous one (version 10)
- case SERVICEWORKERREGISTRAR_VERSION: {
- nsAutoCString numberOfAttemptedActivationsStr;
- GET_LINE(numberOfAttemptedActivationsStr);
- int64_t numberOfAttemptedActivations =
- numberOfAttemptedActivationsStr.ToInteger64(&rv);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return rv;
- }
- entry->mRegistration.numberOfAttemptedActivations() =
- numberOfAttemptedActivations;
- nsAutoCString isRegistrationBrokenStr;
- GET_LINE(isRegistrationBrokenStr);
- int64_t isBroken = isRegistrationBrokenStr.ToInteger64(&rv);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return rv;
- }
- entry->mRegistration.isBroken() = (isBroken != 0);
- nsAutoCString cacheAPIIdStr;
- GET_LINE(cacheAPIIdStr);
- int64_t cacheAPIId = cacheAPIIdStr.ToInteger64(&rv);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return rv;
- }
- entry->mRegistration.cacheAPIId() = cacheAPIId;
-
- [[fallthrough]];
- }
-
- case 10:
- [[fallthrough]];
+ // baseSchemaVersion represents the version where major schema changes
+ // happened and requires a different reading strategy as done below in the
+ // switch statement. Version 9 is the latest major schema version, versions
+ // 10 and 11 are just extensions to version 9 and that's why gets processed
+ // under the same block.
+ auto baseSchemaVersion = version >= 9 ? 9 : version;
+ nsAutoCString line;
+ switch (baseSchemaVersion) {
case 9: {
rv = CreatePrincipalInfo(lineInputStream, entry->mRegistration);
if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -732,10 +704,6 @@ nsresult ServiceWorkerRegistrar::ReadData() {
GET_LINE(entry->mRegistration.navigationPreloadState().headerValue());
- // expando was introcuded in version 10
- // but block was placed in version 9, since
- // the expando is placed at the end of data
- // and we cannot read it in case 10 block
if (version >= 10) {
nsAutoCString expandoCountStr;
GET_LINE(expandoCountStr);
@@ -761,6 +729,47 @@ nsresult ServiceWorkerRegistrar::ReadData() {
}
}
+ if (version >= 11) {
+ nsAutoCString numberOfAttemptedActivationsStr;
+ GET_LINE(numberOfAttemptedActivationsStr);
+ int64_t numberOfAttemptedActivations =
+ numberOfAttemptedActivationsStr.ToInteger64(&rv);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ entry->mRegistration.numberOfAttemptedActivations() =
+ numberOfAttemptedActivations;
+ nsAutoCString isRegistrationBrokenStr;
+ GET_LINE(isRegistrationBrokenStr);
+ int64_t isBroken = isRegistrationBrokenStr.ToInteger64(&rv);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ entry->mRegistration.isBroken() = (isBroken != 0);
+ nsAutoCString cacheAPIIdStr;
+ GET_LINE(cacheAPIIdStr);
+ int64_t cacheAPIId = cacheAPIIdStr.ToInteger64(&rv);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ entry->mRegistration.cacheAPIId() = cacheAPIId;
+ }
+
+ // if we are on latest version, get service worker type
+ if (version == SERVICEWORKERREGISTRAR_VERSION) {
+ nsAutoCString serviceWorkerTypeStr;
+ GET_LINE(serviceWorkerTypeStr);
+ uint32_t serviceWorkerType =
+ serviceWorkerTypeStr.ToUnsignedInteger(&rv);
+ if (NS_WARN_IF(NS_FAILED(rv))) {
+ return rv;
+ }
+ if (serviceWorkerType > static_cast<uint32_t>(WorkerType::Module)) {
+ return NS_ERROR_INVALID_ARG;
+ }
+ entry->mRegistration.type() =
+ static_cast<WorkerType>(serviceWorkerType);
+ }
break;
}
@@ -1445,16 +1454,6 @@ nsresult ServiceWorkerRegistrar::WriteData(
buffer.Truncate();
- buffer.AppendInt(static_cast<int32_t>(
- data.mRegistration.numberOfAttemptedActivations()));
- buffer.Append('\n');
-
- buffer.AppendInt(static_cast<int32_t>(data.mRegistration.isBroken()));
- buffer.Append('\n');
-
- buffer.AppendInt(static_cast<int32_t>(data.mRegistration.cacheAPIId()));
- buffer.Append('\n');
-
buffer.Append(suffix.get());
buffer.Append('\n');
@@ -1513,6 +1512,19 @@ nsresult ServiceWorkerRegistrar::WriteData(
buffer.Append('\n');
}
+ buffer.AppendInt(static_cast<int32_t>(
+ data.mRegistration.numberOfAttemptedActivations()));
+ buffer.Append('\n');
+
+ buffer.AppendInt(static_cast<int32_t>(data.mRegistration.isBroken()));
+ buffer.Append('\n');
+
+ buffer.AppendInt(static_cast<int32_t>(data.mRegistration.cacheAPIId()));
+ buffer.Append('\n');
+
+ buffer.AppendInt(static_cast<uint32_t>(data.mRegistration.type()));
+ buffer.Append('\n');
+
buffer.AppendLiteral(SERVICEWORKERREGISTRAR_TERMINATOR);
buffer.Append('\n');
diff --git a/dom/serviceworkers/ServiceWorkerRegistrar.h b/dom/serviceworkers/ServiceWorkerRegistrar.h
@@ -17,7 +17,7 @@
#include "nsTArray.h"
#define SERVICEWORKERREGISTRAR_FILE u"serviceworker.txt"
-#define SERVICEWORKERREGISTRAR_VERSION 11
+#define SERVICEWORKERREGISTRAR_VERSION 12
#define SERVICEWORKERREGISTRAR_TERMINATOR "#"
#define SERVICEWORKERREGISTRAR_TRUE "true"
#define SERVICEWORKERREGISTRAR_FALSE "false"