Bug 1618241 - Move CreateQuotaDBKey to nsIPrincipal r=ckerschb,baku

Differential Revision: https://phabricator.services.mozilla.com/D64391

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sebastian Streich 2020-02-27 00:22:27 +00:00
parent b57e52d440
commit db60c34f97
4 changed files with 42 additions and 37 deletions

View File

@ -24,6 +24,7 @@
#include "mozilla/dom/nsMixedContentBlocker.h"
#include "mozilla/Components.h"
#include "nsIURIFixup.h"
#include "mozilla/dom/StorageUtils.h"
#include "prnetdb.h"
@ -928,6 +929,38 @@ void BasePrincipal::FinishInit(BasePrincipal* aOther,
mHasExplicitDomain = aOther->mHasExplicitDomain;
}
NS_IMETHODIMP
BasePrincipal::GetLocalStorageQuotaKey(nsACString& aKey) {
aKey.Truncate();
nsresult rv;
nsCOMPtr<nsIEffectiveTLDService> eTLDService(
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> uri;
rv = GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
nsAutoCString eTLDplusOne;
rv = eTLDService->GetBaseDomain(uri, 0, eTLDplusOne);
if (NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS == rv) {
// XXX bug 357323 - what to do for localhost/file exactly?
rv = uri->GetAsciiHost(eTLDplusOne);
}
NS_ENSURE_SUCCESS(rv, rv);
OriginAttributesRef().CreateSuffix(aKey);
nsAutoCString subdomainsDBKey;
dom::StorageUtils::CreateReversedDomain(eTLDplusOne, subdomainsDBKey);
aKey.Append(':');
aKey.Append(subdomainsDBKey);
return NS_OK;
}
bool SiteIdentifier::Equals(const SiteIdentifier& aOther) const {
MOZ_ASSERT(IsInitialized());
MOZ_ASSERT(aOther.IsInitialized());

View File

@ -147,6 +147,7 @@ class BasePrincipal : public nsJSPrincipals {
NS_IMETHOD GetPrefLightCacheKey(nsIURI* aURI, bool aWithCredentials,
nsACString& _retval) override;
NS_IMETHOD GetAsciiHost(nsACString& aAsciiHost) override;
NS_IMETHOD GetLocalStorageQuotaKey(nsACString& aRes) override;
nsresult ToJSON(nsACString& aJSON);
static already_AddRefed<BasePrincipal> FromJSON(const nsACString& aJSON);

View File

@ -292,6 +292,13 @@ interface nsIPrincipal : nsISerializable
*/
ACString getPrefLightCacheKey(in nsIURI aURI ,in bool aWithCredentials);
/*
* Returns a Key for the LocalStorage Manager, used to
* check the Principals Origin Storage usage.
*/
readonly attribute ACString localStorageQuotaKey;
/**
* Implementation of
* https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy

View File

@ -70,42 +70,6 @@ LocalStorageManager::~LocalStorageManager() {
sSelf = nullptr;
}
namespace {
nsresult CreateQuotaDBKey(nsIPrincipal* aPrincipal, nsACString& aKey) {
nsresult rv;
nsCOMPtr<nsIEffectiveTLDService> eTLDService(
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> uri;
rv = aPrincipal->GetURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
nsAutoCString eTLDplusOne;
rv = eTLDService->GetBaseDomain(uri, 0, eTLDplusOne);
if (NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS == rv) {
// XXX bug 357323 - what to do for localhost/file exactly?
rv = uri->GetAsciiHost(eTLDplusOne);
}
NS_ENSURE_SUCCESS(rv, rv);
aKey.Truncate();
aPrincipal->OriginAttributesRef().CreateSuffix(aKey);
nsAutoCString subdomainsDBKey;
CreateReversedDomain(eTLDplusOne, subdomainsDBKey);
aKey.Append(':');
aKey.Append(subdomainsDBKey);
return NS_OK;
}
} // namespace
// static
nsAutoCString LocalStorageManager::CreateOrigin(
const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix) {
@ -158,7 +122,7 @@ already_AddRefed<LocalStorageCache> LocalStorageManager::PutCache(
RefPtr<LocalStorageCache> cache = entry->cache();
nsAutoCString quotaOrigin;
CreateQuotaDBKey(aPrincipal, quotaOrigin);
aPrincipal->GetLocalStorageQuotaKey(quotaOrigin);
// Lifetime handled by the cache, do persist
cache->Init(this, true, aPrincipal, quotaOrigin);