Bug 1266857 Make Clients.claim() use observer document list instead of secondary hashtable. r=bz

This commit is contained in:
Ben Kelly 2016-04-22 00:50:13 -07:00
parent da6964ef2b
commit b4090694b7
2 changed files with 24 additions and 15 deletions

View File

@ -1946,12 +1946,7 @@ ServiceWorkerManager::MaybeStartControlling(nsIDocument* aDoc,
const nsAString& aDocumentId)
{
AssertIsOnMainThread();
// We keep a set of documents that service workers may choose to start
// controlling using claim().
MOZ_ASSERT(!mAllDocuments.Contains(aDoc));
mAllDocuments.PutEntry(aDoc);
MOZ_ASSERT(aDoc);
RefPtr<ServiceWorkerRegistrationInfo> registration =
GetServiceWorkerRegistrationInfo(aDoc);
if (registration) {
@ -1963,6 +1958,7 @@ ServiceWorkerManager::MaybeStartControlling(nsIDocument* aDoc,
void
ServiceWorkerManager::MaybeStopControlling(nsIDocument* aDoc)
{
AssertIsOnMainThread();
MOZ_ASSERT(aDoc);
RefPtr<ServiceWorkerRegistrationInfo> registration;
mControlledDocuments.Remove(aDoc, getter_AddRefs(registration));
@ -1972,8 +1968,6 @@ ServiceWorkerManager::MaybeStopControlling(nsIDocument* aDoc)
if (registration) {
StopControllingADocument(registration);
}
mAllDocuments.RemoveEntry(aDoc);
}
void
@ -2842,10 +2836,28 @@ ServiceWorkerManager::ClaimClients(nsIPrincipal* aPrincipal,
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
for (auto iter = mAllDocuments.Iter(); !iter.Done(); iter.Next()) {
nsCOMPtr<nsIDocument> document = do_QueryInterface(iter.Get()->GetKey());
swm->MaybeClaimClient(document, registration);
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (NS_WARN_IF(!obs)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsISimpleEnumerator> enumerator;
nsresult rv = obs->EnumerateObservers("service-worker-get-client",
getter_AddRefs(enumerator));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
bool loop = true;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&loop)) && loop) {
nsCOMPtr<nsISupports> ptr;
rv = enumerator->GetNext(getter_AddRefs(ptr));
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}
nsCOMPtr<nsIDocument> doc = do_QueryInterface(ptr);
MaybeClaimClient(doc, registration);
}
return NS_OK;

View File

@ -106,9 +106,6 @@ public:
nsRefPtrHashtable<nsISupportsHashKey, ServiceWorkerRegistrationInfo> mControlledDocuments;
// Set of all documents that may be controlled by a service worker.
nsTHashtable<nsISupportsHashKey> mAllDocuments;
// Track all documents that have attempted to register a service worker for a
// given scope.
typedef nsTArray<nsCOMPtr<nsIWeakReference>> WeakDocumentList;