Skip to content

Commit

Permalink
Bug 931249 - Patch 9 - Use ServiceWorker cache name as part of it's u…
Browse files Browse the repository at this point in the history
…nique name. r=khuey

When a SW script is updated (it gets a new cache), if there was already an existing running worker with the same scope and script, that would be reused and the update wouldn't happen.
  • Loading branch information
rmottola committed Jul 1, 2019
1 parent e5f62b6 commit d6481d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
36 changes: 29 additions & 7 deletions dom/workers/RuntimeService.cpp
Expand Up @@ -267,20 +267,23 @@ GetWorkerPref(const nsACString& aPref,
}

// This function creates a key for a SharedWorker composed by "shared|name|scriptSpec"
// and a key for a ServiceWorker composed by "service|scope|scriptSpec".
// and a key for a ServiceWorker composed by "service|scope|cache|scriptSpec".
// If the name contains a '|', this will be replaced by '||'.
void
GenerateSharedWorkerKey(const nsACString& aScriptSpec, const nsACString& aName,
WorkerType aWorkerType, nsCString& aKey)
const nsACString& aCacheName, WorkerType aWorkerType,
nsCString& aKey)
{
aKey.Truncate();
NS_NAMED_LITERAL_CSTRING(sharedPrefix, "shared|");
NS_NAMED_LITERAL_CSTRING(servicePrefix, "service|");
MOZ_ASSERT(servicePrefix.Length() > sharedPrefix.Length());
MOZ_ASSERT(aWorkerType == WorkerTypeShared ||
aWorkerType == WorkerTypeService);
MOZ_ASSERT_IF(aWorkerType == WorkerTypeShared, aCacheName.IsEmpty());
MOZ_ASSERT_IF(aWorkerType == WorkerTypeService, !aCacheName.IsEmpty());
aKey.SetCapacity(servicePrefix.Length() + aScriptSpec.Length() +
aName.Length() + 1);
aName.Length() + aCacheName.Length() + 1);

aKey.Append(aWorkerType == WorkerTypeService ? servicePrefix : sharedPrefix);

Expand All @@ -295,6 +298,11 @@ GenerateSharedWorkerKey(const nsACString& aScriptSpec, const nsACString& aName,
}
}

if (aWorkerType == WorkerTypeService) {
aKey.Append('|');
aKey.Append(aCacheName);
}

aKey.Append('|');
aKey.Append(aScriptSpec);
}
Expand Down Expand Up @@ -1471,10 +1479,14 @@ RuntimeService::RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)

if (isSharedOrServiceWorker) {
const nsCString& sharedWorkerName = aWorkerPrivate->SharedWorkerName();
const nsCString& cacheName =
aWorkerPrivate->IsServiceWorker() ?
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
EmptyCString();

nsAutoCString key;
GenerateSharedWorkerKey(sharedWorkerScriptSpec, sharedWorkerName,
aWorkerPrivate->Type(), key);
cacheName, aWorkerPrivate->Type(), key);
MOZ_ASSERT(!domainInfo->mSharedWorkerInfos.Get(key));

SharedWorkerInfo* sharedWorkerInfo =
Expand Down Expand Up @@ -1578,9 +1590,13 @@ RuntimeService::UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)

if (match.mSharedWorkerInfo) {
nsAutoCString key;
const nsCString& cacheName =
aWorkerPrivate->IsServiceWorker() ?
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
EmptyCString();
GenerateSharedWorkerKey(match.mSharedWorkerInfo->mScriptSpec,
match.mSharedWorkerInfo->mName,
aWorkerPrivate->Type(), key);
cacheName, aWorkerPrivate->Type(), key);
domainInfo->mSharedWorkerInfos.Remove(key);
}
}
Expand Down Expand Up @@ -2315,7 +2331,9 @@ RuntimeService::CreateSharedWorkerFromLoadInfo(JSContext* aCx,
NS_ENSURE_SUCCESS(rv, rv);

nsAutoCString key;
GenerateSharedWorkerKey(scriptSpec, aName, aType, key);
GenerateSharedWorkerKey(scriptSpec, aName,
NS_ConvertUTF16toUTF8(aLoadInfo->mServiceWorkerCacheName),
aType, key);

if (mDomainMap.Get(aLoadInfo->mDomain, &domainInfo) &&
domainInfo->mSharedWorkerInfos.Get(key, &sharedWorkerInfo)) {
Expand Down Expand Up @@ -2389,9 +2407,13 @@ RuntimeService::ForgetSharedWorker(WorkerPrivate* aWorkerPrivate)

if (match.mSharedWorkerInfo) {
nsAutoCString key;
const nsCString& cacheName =
aWorkerPrivate->IsServiceWorker() ?
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
EmptyCString();
GenerateSharedWorkerKey(match.mSharedWorkerInfo->mScriptSpec,
match.mSharedWorkerInfo->mName,
aWorkerPrivate->Type(), key);
cacheName, aWorkerPrivate->Type(), key);
domainInfo->mSharedWorkerInfos.Remove(key);
}
}
Expand Down
2 changes: 2 additions & 0 deletions dom/workers/ServiceWorkerManager.cpp
Expand Up @@ -1781,6 +1781,7 @@ ServiceWorkerManager::CreateServiceWorkerForWindow(nsPIDOMWindow* aWindow,
return rv;
}

MOZ_ASSERT(!aInfo->CacheName().IsEmpty());
loadInfo.mServiceWorkerCacheName = aInfo->CacheName();

RuntimeService* rs = RuntimeService::GetOrCreateService();
Expand Down Expand Up @@ -2543,6 +2544,7 @@ ServiceWorkerManager::CreateServiceWorker(nsIPrincipal* aPrincipal,
}

info.mResolvedScriptURI = info.mBaseURI;
MOZ_ASSERT(!aInfo->CacheName().IsEmpty());
info.mServiceWorkerCacheName = aInfo->CacheName();

rv = info.mBaseURI->GetHost(info.mDomain);
Expand Down

0 comments on commit d6481d3

Please sign in to comment.