tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit bf551670f43fd2576802584a37bd4d66cb6eee7f
parent 040663a23b2fd939acff1c4bb1cf1a86519221ee
Author: Harveer Singh <hsingh@mozilla.com>
Date:   Wed, 29 Oct 2025 19:40:11 +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:
Mdom/serviceworkers/ServiceWorkerRegistrar.cpp | 114++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mdom/serviceworkers/ServiceWorkerRegistrar.h | 2+-
2 files changed, 64 insertions(+), 52 deletions(-)

diff --git a/dom/serviceworkers/ServiceWorkerRegistrar.cpp b/dom/serviceworkers/ServiceWorkerRegistrar.cpp @@ -49,7 +49,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); @@ -627,43 +627,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))) { @@ -733,10 +705,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); @@ -762,6 +730,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; } @@ -1446,16 +1455,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'); @@ -1514,6 +1513,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"