From 84065537b6706d20b124363e6c3770a5192e8556 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Wed, 31 Jan 2018 08:29:49 -0800 Subject: [PATCH] Bug 1433505 P2 Make various class members on ServiceWorkerRegistrationInfo private. r=asuth --- dom/serviceworkers/ServiceWorkerManager.cpp | 74 +++++++++---------- .../ServiceWorkerRegisterJob.cpp | 4 +- .../ServiceWorkerRegistrationInfo.cpp | 36 ++++++++- .../ServiceWorkerRegistrationInfo.h | 33 ++++++--- .../ServiceWorkerScriptCache.cpp | 8 +- .../ServiceWorkerUnregisterJob.cpp | 8 +- dom/serviceworkers/ServiceWorkerUpdateJob.cpp | 12 +-- 7 files changed, 109 insertions(+), 66 deletions(-) diff --git a/dom/serviceworkers/ServiceWorkerManager.cpp b/dom/serviceworkers/ServiceWorkerManager.cpp index ac9be28acc78..bdbff89ada2b 100644 --- a/dom/serviceworkers/ServiceWorkerManager.cpp +++ b/dom/serviceworkers/ServiceWorkerManager.cpp @@ -192,7 +192,7 @@ PopulateRegistrationData(nsIPrincipal* aPrincipal, return rv; } - aData.scope() = aRegistration->mScope; + aData.scope() = aRegistration->Scope(); RefPtr newest = aRegistration->Newest(); if (NS_WARN_IF(!newest)) { @@ -465,7 +465,7 @@ class ServiceWorkerResolveWindowPromiseOnRegisterCallback final : public Service RefPtr reg = registerJob->GetRegistration(); RefPtr swr = - window->GetServiceWorkerRegistration(NS_ConvertUTF8toUTF16(reg->mScope)); + window->GetServiceWorkerRegistration(NS_ConvertUTF8toUTF16(reg->Scope())); promise->MaybeResolve(swr); } @@ -1003,7 +1003,7 @@ public: for (uint32_t i = 0; i < data->mOrderedScopes.Length(); ++i) { RefPtr info = data->mInfos.GetWeak(data->mOrderedScopes[i]); - if (info->mPendingUninstall) { + if (info->IsPendingUninstall()) { continue; } @@ -1146,7 +1146,7 @@ public: return NS_OK; } - NS_ConvertUTF8toUTF16 scope(registration->mScope); + NS_ConvertUTF8toUTF16 scope(registration->Scope()); RefPtr swr = mWindow->GetServiceWorkerRegistration(scope); mPromise->MaybeResolve(swr); @@ -1463,7 +1463,7 @@ ServiceWorkerManager::CheckReadyPromise(nsPIDOMWindowInner* aWindow, GetServiceWorkerRegistrationInfo(principal, aURI); if (registration && registration->GetActive()) { - NS_ConvertUTF8toUTF16 scope(registration->mScope); + NS_ConvertUTF8toUTF16 scope(registration->Scope()); RefPtr swr = aWindow->GetServiceWorkerRegistration(scope); aPromise->MaybeResolve(swr); @@ -1651,7 +1651,7 @@ ServiceWorkerManager::WorkerIsIdle(ServiceWorkerInfo* aWorker) return; } - if (!reg->IsControllingClients() && reg->mPendingUninstall) { + if (!reg->IsControllingClients() && reg->IsPendingUninstall()) { RemoveRegistration(reg); return; } @@ -1840,8 +1840,8 @@ ServiceWorkerManager::LoadRegistration( const nsCString& currentWorkerURL = aRegistration.currentWorkerURL(); if (!currentWorkerURL.IsEmpty()) { registration->SetActive( - new ServiceWorkerInfo(registration->mPrincipal, - registration->mScope, + new ServiceWorkerInfo(registration->Principal(), + registration->Scope(), currentWorkerURL, aRegistration.cacheName(), importsLoadFlags)); @@ -1962,12 +1962,12 @@ ServiceWorkerManager::GetServiceWorkerRegistrationInfo(const nsACString& aScopeK #ifdef DEBUG nsAutoCString origin; - rv = registration->mPrincipal->GetOrigin(origin); + rv = registration->Principal()->GetOrigin(origin); MOZ_ASSERT(NS_SUCCEEDED(rv)); MOZ_ASSERT(origin.Equals(aScopeKey)); #endif - if (registration->mPendingUninstall) { + if (registration->IsPendingUninstall()) { return nullptr; } return registration.forget(); @@ -2015,7 +2015,7 @@ ServiceWorkerManager::AddScopeAndRegistration(const nsACString& aScope, ServiceWorkerRegistrationInfo* aInfo) { MOZ_ASSERT(aInfo); - MOZ_ASSERT(aInfo->mPrincipal); + MOZ_ASSERT(aInfo->Principal()); RefPtr swm = ServiceWorkerManager::GetInstance(); if (!swm) { @@ -2024,7 +2024,7 @@ ServiceWorkerManager::AddScopeAndRegistration(const nsACString& aScope, } nsAutoCString scopeKey; - nsresult rv = swm->PrincipalToScopeKey(aInfo->mPrincipal, scopeKey); + nsresult rv = swm->PrincipalToScopeKey(aInfo->Principal(), scopeKey); if (NS_WARN_IF(NS_FAILED(rv))) { return; } @@ -2118,7 +2118,7 @@ ServiceWorkerManager::RemoveScopeAndRegistration(ServiceWorkerRegistrationInfo* } nsAutoCString scopeKey; - nsresult rv = swm->PrincipalToScopeKey(aRegistration->mPrincipal, scopeKey); + nsresult rv = swm->PrincipalToScopeKey(aRegistration->Principal(), scopeKey); if (NS_WARN_IF(NS_FAILED(rv))) { return; } @@ -2128,7 +2128,7 @@ ServiceWorkerManager::RemoveScopeAndRegistration(ServiceWorkerRegistrationInfo* return; } - if (auto entry = data->mUpdateTimers.Lookup(aRegistration->mScope)) { + if (auto entry = data->mUpdateTimers.Lookup(aRegistration->Scope())) { entry.Data()->Cancel(); entry.Remove(); } @@ -2136,8 +2136,8 @@ ServiceWorkerManager::RemoveScopeAndRegistration(ServiceWorkerRegistrationInfo* // Verify there are no controlled clients for the purged registration. for (auto iter = swm->mControlledClients.Iter(); !iter.Done(); iter.Next()) { auto& reg = iter.UserData()->mRegistrationInfo; - if (reg->mScope.Equals(aRegistration->mScope) && - reg->mPrincipal->Equals(aRegistration->mPrincipal)) { + if (reg->Scope().Equals(aRegistration->Scope()) && + reg->Principal()->Equals(aRegistration->Principal())) { MOZ_DIAGNOSTIC_ASSERT(false, "controlled client when removing registration"); iter.Remove(); @@ -2146,8 +2146,8 @@ ServiceWorkerManager::RemoveScopeAndRegistration(ServiceWorkerRegistrationInfo* } RefPtr info; - data->mInfos.Remove(aRegistration->mScope, getter_AddRefs(info)); - data->mOrderedScopes.RemoveElement(aRegistration->mScope); + data->mInfos.Remove(aRegistration->Scope(), getter_AddRefs(info)); + data->mOrderedScopes.RemoveElement(aRegistration->Scope()); swm->NotifyListenersOnUnregister(info); swm->MaybeRemoveRegistrationInfo(scopeKey); @@ -2216,7 +2216,7 @@ ServiceWorkerManager::StopControllingRegistration(ServiceWorkerRegistrationInfo* return; } - if (aRegistration->mPendingUninstall) { + if (aRegistration->IsPendingUninstall()) { RemoveRegistration(aRegistration); return; } @@ -2246,7 +2246,7 @@ ServiceWorkerManager::GetScopeForUrl(nsIPrincipal* aPrincipal, return NS_ERROR_FAILURE; } - aScope = NS_ConvertUTF8toUTF16(r->mScope); + aScope = NS_ConvertUTF8toUTF16(r->Scope()); return NS_OK; } @@ -2302,7 +2302,7 @@ ServiceWorkerManager::FireUpdateFoundOnServiceWorkerRegistrations( MOZ_ASSERT(!regScope.IsEmpty()); NS_ConvertUTF16toUTF8 utf8Scope(regScope); - if (utf8Scope.Equals(aRegistration->mScope)) { + if (utf8Scope.Equals(aRegistration->Scope())) { target->UpdateFound(); } } @@ -2669,7 +2669,7 @@ ServiceWorkerManager::TransitionServiceWorkerRegistrationWorker(ServiceWorkerReg NS_ConvertUTF16toUTF8 utf8Scope(regScope); - if (utf8Scope.Equals(aRegistration->mScope)) { + if (utf8Scope.Equals(aRegistration->Scope())) { target->TransitionWorker(aWhichOne); } } @@ -2689,7 +2689,7 @@ ServiceWorkerManager::InvalidateServiceWorkerRegistrationWorker(ServiceWorkerReg NS_ConvertUTF16toUTF8 utf8Scope(regScope); - if (utf8Scope.Equals(aRegistration->mScope)) { + if (utf8Scope.Equals(aRegistration->Scope())) { target->InvalidateWorkers(aWhichOnes); } } @@ -2708,7 +2708,7 @@ ServiceWorkerManager::NotifyServiceWorkerRegistrationRemoved(ServiceWorkerRegist NS_ConvertUTF16toUTF8 utf8Scope(regScope); - if (utf8Scope.Equals(aRegistration->mScope)) { + if (utf8Scope.Equals(aRegistration->Scope())) { target->RegistrationRemoved(); } } @@ -2816,7 +2816,7 @@ ServiceWorkerManager::SoftUpdateInternal(const OriginAttributes& aOriginAttribut } // "If registration's uninstalling flag is set, abort these steps." - if (registration->mPendingUninstall) { + if (registration->IsPendingUninstall()) { return; } @@ -2841,7 +2841,7 @@ ServiceWorkerManager::SoftUpdateInternal(const OriginAttributes& aOriginAttribut aScope); RefPtr job = - new ServiceWorkerUpdateJob(principal, registration->mScope, + new ServiceWorkerUpdateJob(principal, registration->Scope(), newest->ScriptSpec(), nullptr, registration->GetUpdateViaCache()); @@ -2916,7 +2916,7 @@ ServiceWorkerManager::UpdateInternal(nsIPrincipal* aPrincipal, // "Invoke Update algorithm, or its equivalent, with client, registration as // its argument." RefPtr job = - new ServiceWorkerUpdateJob(aPrincipal, registration->mScope, + new ServiceWorkerUpdateJob(aPrincipal, registration->Scope(), newest->ScriptSpec(), nullptr, registration->GetUpdateViaCache()); @@ -2936,7 +2936,7 @@ ServiceWorkerManager::MaybeClaimClient(nsIDocument* aDocument, RefPtr ref; // Same origin check - if (!aWorkerRegistration->mPrincipal->Equals(aDocument->NodePrincipal())) { + if (!aWorkerRegistration->Principal()->Equals(aDocument->NodePrincipal())) { ref = GenericPromise::CreateAndReject(NS_ERROR_DOM_SECURITY_ERR, __func__); return ref.forget(); } @@ -3144,7 +3144,7 @@ ServiceWorkerManager::MaybeRemoveRegistration(ServiceWorkerRegistrationInfo* aRe { MOZ_ASSERT(aRegistration); RefPtr newest = aRegistration->Newest(); - if (!newest && HasScope(aRegistration->mPrincipal, aRegistration->mScope)) { + if (!newest && HasScope(aRegistration->Principal(), aRegistration->Scope())) { RemoveRegistration(aRegistration); } } @@ -3165,10 +3165,10 @@ ServiceWorkerManager::RemoveRegistration(ServiceWorkerRegistrationInfo* aRegistr // null workers (case 3). #ifdef DEBUG RefPtr newest = aRegistration->Newest(); - MOZ_ASSERT(aRegistration->mPendingUninstall || !newest); + MOZ_ASSERT(aRegistration->IsPendingUninstall() || !newest); #endif - MOZ_ASSERT(HasScope(aRegistration->mPrincipal, aRegistration->mScope)); + MOZ_ASSERT(HasScope(aRegistration->Principal(), aRegistration->Scope())); // When a registration is removed, we must clear its contents since the DOM // object may be held by content script. @@ -3236,7 +3236,7 @@ ServiceWorkerManager::GetAllRegistrations(nsIArray** aResult) ServiceWorkerRegistrationInfo* reg = it2.UserData(); MOZ_ASSERT(reg); - if (reg->mPendingUninstall) { + if (reg->IsPendingUninstall()) { continue; } @@ -3257,18 +3257,18 @@ ServiceWorkerManager::ForceUnregister(RegistrationDataPerPrincipal* aRegistratio MOZ_ASSERT(aRegistration); RefPtr queue; - aRegistrationData->mJobQueues.Get(aRegistration->mScope, getter_AddRefs(queue)); + aRegistrationData->mJobQueues.Get(aRegistration->Scope(), getter_AddRefs(queue)); if (queue) { queue->CancelAll(); } - if (auto entry = aRegistrationData->mUpdateTimers.Lookup(aRegistration->mScope)) { + if (auto entry = aRegistrationData->mUpdateTimers.Lookup(aRegistration->Scope())) { entry.Data()->Cancel(); entry.Remove(); } // Since Unregister is async, it is ok to call it in an enumeration. - Unregister(aRegistration->mPrincipal, nullptr, NS_ConvertUTF8toUTF16(aRegistration->mScope)); + Unregister(aRegistration->Principal(), nullptr, NS_ConvertUTF8toUTF16(aRegistration->Scope())); } NS_IMETHODIMP @@ -3345,10 +3345,10 @@ ServiceWorkerManager::RemoveAllRegistrations(OriginAttributesPattern* aPattern) ServiceWorkerRegistrationInfo* reg = it2.UserData(); MOZ_ASSERT(reg); - MOZ_ASSERT(reg->mPrincipal); + MOZ_ASSERT(reg->Principal()); bool matches = - aPattern->Matches(reg->mPrincipal->OriginAttributesRef()); + aPattern->Matches(reg->Principal()->OriginAttributesRef()); if (!matches) { continue; } diff --git a/dom/serviceworkers/ServiceWorkerRegisterJob.cpp b/dom/serviceworkers/ServiceWorkerRegisterJob.cpp index 7a86f24f23ec..41ae25eb20de 100644 --- a/dom/serviceworkers/ServiceWorkerRegisterJob.cpp +++ b/dom/serviceworkers/ServiceWorkerRegisterJob.cpp @@ -44,8 +44,8 @@ ServiceWorkerRegisterJob::AsyncExecute() // it to disk again. We preemptively removed it earlier during // unregister so that closing the window by shutting down the browser // results in the registration being gone on restart. - if (registration->mPendingUninstall) { - registration->mPendingUninstall = false; + if (registration->IsPendingUninstall()) { + registration->ClearPendingUninstall(); swm->StoreRegistration(mPrincipal, registration); // Its possible that a ready promise is created between when the // uninstalling flag is set and when we resurrect the registration diff --git a/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp b/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp index 2548217c784e..50724d84ee2f 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp @@ -85,14 +85,14 @@ ServiceWorkerRegistrationInfo::ServiceWorkerRegistrationInfo( const nsACString& aScope, nsIPrincipal* aPrincipal, ServiceWorkerUpdateViaCache aUpdateViaCache) - : mControlledClientsCounter(0) + : mScope(aScope) + , mPrincipal(aPrincipal) + , mControlledClientsCounter(0) , mUpdateState(NoUpdate) , mCreationTime(PR_Now()) , mCreationTimeStamp(TimeStamp::Now()) , mLastUpdateTime(0) , mUpdateViaCache(aUpdateViaCache) - , mScope(aScope) - , mPrincipal(aPrincipal) , mPendingUninstall(false) {} @@ -101,6 +101,36 @@ ServiceWorkerRegistrationInfo::~ServiceWorkerRegistrationInfo() MOZ_DIAGNOSTIC_ASSERT(!IsControllingClients()); } +const nsCString& +ServiceWorkerRegistrationInfo::Scope() const +{ + return mScope; +} + +nsIPrincipal* +ServiceWorkerRegistrationInfo::Principal() const +{ + return mPrincipal; +} + +bool +ServiceWorkerRegistrationInfo::IsPendingUninstall() const +{ + return mPendingUninstall; +} + +void +ServiceWorkerRegistrationInfo::SetPendingUninstall() +{ + mPendingUninstall = true; +} + +void +ServiceWorkerRegistrationInfo::ClearPendingUninstall() +{ + mPendingUninstall = false; +} + NS_IMPL_ISUPPORTS(ServiceWorkerRegistrationInfo, nsIServiceWorkerRegistrationInfo) NS_IMETHODIMP diff --git a/dom/serviceworkers/ServiceWorkerRegistrationInfo.h b/dom/serviceworkers/ServiceWorkerRegistrationInfo.h index 9393c023b8ad..c88fa33f95b4 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationInfo.h +++ b/dom/serviceworkers/ServiceWorkerRegistrationInfo.h @@ -18,6 +18,10 @@ namespace dom { class ServiceWorkerRegistrationInfo final : public nsIServiceWorkerRegistrationInfo { + const nsCString mScope; + nsCOMPtr mPrincipal; + nsTArray> mListeners; + uint32_t mControlledClientsCounter; enum @@ -42,25 +46,34 @@ class ServiceWorkerRegistrationInfo final virtual ~ServiceWorkerRegistrationInfo(); -public: - NS_DECL_ISUPPORTS - NS_DECL_NSISERVICEWORKERREGISTRATIONINFO - - const nsCString mScope; - - nsCOMPtr mPrincipal; - - nsTArray> mListeners; - // When unregister() is called on a registration, it is not immediately // removed since documents may be controlled. It is marked as // pendingUninstall and when all controlling documents go away, removed. bool mPendingUninstall; +public: + NS_DECL_ISUPPORTS + NS_DECL_NSISERVICEWORKERREGISTRATIONINFO + ServiceWorkerRegistrationInfo(const nsACString& aScope, nsIPrincipal* aPrincipal, ServiceWorkerUpdateViaCache aUpdateViaCache); + const nsCString& + Scope() const; + + nsIPrincipal* + Principal() const; + + bool + IsPendingUninstall() const; + + void + SetPendingUninstall(); + + void + ClearPendingUninstall(); + already_AddRefed Newest() const { diff --git a/dom/serviceworkers/ServiceWorkerScriptCache.cpp b/dom/serviceworkers/ServiceWorkerScriptCache.cpp index f50d0331e2ba..02ed4a26e9ad 100644 --- a/dom/serviceworkers/ServiceWorkerScriptCache.cpp +++ b/dom/serviceworkers/ServiceWorkerScriptCache.cpp @@ -981,8 +981,8 @@ CompareNetwork::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext statusAsText.AppendInt(status); ServiceWorkerManager::LocalizeAndReportToAllClients( - mRegistration->mScope, "ServiceWorkerRegisterNetworkError", - nsTArray { NS_ConvertUTF8toUTF16(mRegistration->mScope), + mRegistration->Scope(), "ServiceWorkerRegisterNetworkError", + nsTArray { NS_ConvertUTF8toUTF16(mRegistration->Scope()), statusAsText, mURL }); rv = NS_ERROR_FAILURE; @@ -1016,8 +1016,8 @@ CompareNetwork::OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext !mimeType.LowerCaseEqualsLiteral("application/x-javascript") && !mimeType.LowerCaseEqualsLiteral("application/javascript")) { ServiceWorkerManager::LocalizeAndReportToAllClients( - mRegistration->mScope, "ServiceWorkerRegisterMimeTypeError", - nsTArray { NS_ConvertUTF8toUTF16(mRegistration->mScope), + mRegistration->Scope(), "ServiceWorkerRegisterMimeTypeError", + nsTArray { NS_ConvertUTF8toUTF16(mRegistration->Scope()), NS_ConvertUTF8toUTF16(mimeType), mURL }); rv = NS_ERROR_DOM_SECURITY_ERR; return rv; diff --git a/dom/serviceworkers/ServiceWorkerUnregisterJob.cpp b/dom/serviceworkers/ServiceWorkerUnregisterJob.cpp index d0a1b0d85fb6..ec96eb06164c 100644 --- a/dom/serviceworkers/ServiceWorkerUnregisterJob.cpp +++ b/dom/serviceworkers/ServiceWorkerUnregisterJob.cpp @@ -120,17 +120,17 @@ ServiceWorkerUnregisterJob::Unregister() } // Note, we send the message to remove the registration from disk now even - // though we may only set the mPendingUninstall flag below. This is + // though we may only set the pending uninstall flag below. This is // necessary to ensure the registration is removed if the controlled // clients are closed by shutting down the browser. If the registration - // is resurrected by clearing mPendingUninstall then it should be saved + // is resurrected by clearing pending uninstall then it should be saved // to disk again. - if (mSendToParent && !registration->mPendingUninstall) { + if (mSendToParent && !registration->IsPendingUninstall()) { swm->MaybeSendUnregister(mPrincipal, mScope); } // "Set registration's uninstalling flag." - registration->mPendingUninstall = true; + registration->SetPendingUninstall(); // "Resolve promise with true" mResult = true; diff --git a/dom/serviceworkers/ServiceWorkerUpdateJob.cpp b/dom/serviceworkers/ServiceWorkerUpdateJob.cpp index 787b4ab9e296..70e9b3320182 100644 --- a/dom/serviceworkers/ServiceWorkerUpdateJob.cpp +++ b/dom/serviceworkers/ServiceWorkerUpdateJob.cpp @@ -260,7 +260,7 @@ ServiceWorkerUpdateJob::AsyncExecute() RefPtr registration = swm->GetRegistration(mPrincipal, mScope); - if (!registration || registration->mPendingUninstall) { + if (!registration || registration->IsPendingUninstall()) { ErrorResult rv; rv.ThrowTypeError(NS_ConvertUTF8toUTF16(mScope), NS_LITERAL_STRING("uninstalled")); @@ -399,9 +399,9 @@ ServiceWorkerUpdateJob::ComparisonResult(nsresult aStatus, } } - if (!StringBeginsWith(mRegistration->mScope, maxPrefix)) { + if (!StringBeginsWith(mRegistration->Scope(), maxPrefix)) { nsAutoString message; - NS_ConvertUTF8toUTF16 reportScope(mRegistration->mScope); + NS_ConvertUTF8toUTF16 reportScope(mRegistration->Scope()); NS_ConvertUTF8toUTF16 reportMaxPrefix(maxPrefix); const char16_t* params[] = { reportScope.get(), reportMaxPrefix.get() }; @@ -434,8 +434,8 @@ ServiceWorkerUpdateJob::ComparisonResult(nsresult aStatus, } RefPtr sw = - new ServiceWorkerInfo(mRegistration->mPrincipal, - mRegistration->mScope, + new ServiceWorkerInfo(mRegistration->Principal(), + mRegistration->Scope(), mScriptSpec, aNewCacheName, flags); @@ -475,7 +475,7 @@ ServiceWorkerUpdateJob::ContinueUpdateAfterScriptEval(bool aScriptEvaluationResu ErrorResult error; NS_ConvertUTF8toUTF16 scriptSpec(mScriptSpec); - NS_ConvertUTF8toUTF16 scope(mRegistration->mScope); + NS_ConvertUTF8toUTF16 scope(mRegistration->Scope()); error.ThrowTypeError(scriptSpec, scope); FailUpdateJob(error); return;