Bug 1558420 - Part 1: Add an API for creating a sessionstorage manager and a cache object; r=baku

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ehsan Akhgari 2019-11-12 15:40:02 +00:00
Родитель 2cf7e52582
Коммит f6ffd2ce8d
9 изменённых файлов: 114 добавлений и 68 удалений

Просмотреть файл

@ -266,6 +266,7 @@
#include "mozilla/net/CookieSettings.h"
#include "AccessCheck.h"
#include "SessionStorageCache.h"
#ifdef ANDROID
# include <android/log.h>
@ -4637,8 +4638,10 @@ Storage* nsGlobalWindowInner::GetLocalStorage(ErrorResult& aError) {
return nullptr;
}
RefPtr<SessionStorageCache> cache = new SessionStorageCache();
mLocalStorage =
new PartitionedLocalStorage(this, principal, storagePrincipal);
new PartitionedLocalStorage(this, principal, storagePrincipal, cache);
}
MOZ_ASSERT_IF(

Просмотреть файл

@ -10,6 +10,16 @@ interface mozIDOMWindow;
webidl Storage;
%{C++
namespace mozilla {
namespace dom {
class SessionStorageCache;
} // namespace dom
} // namespace mozilla
%}
native SessionStorageCacheAddRefed(RefPtr<mozilla::dom::SessionStorageCache>);
/**
* General purpose interface that has two implementations, for localStorage
* with "@mozilla.org/dom/localStorage-manager;1".
@ -105,3 +115,18 @@ interface nsIDOMStorageManager : nsISupports
bool checkStorage(in nsIPrincipal aPrincipal,
in Storage aStorage);
};
[uuid(b3bfbbd0-e738-4cbf-b0f0-b65f25265e82)]
interface nsIDOMSessionStorageManager : nsIDOMStorageManager {
/**
* Returns a SessionStorageCache object for the principal scope.
*
* @param aPrincipal
* Principal to bound storage to.
* @param aStoragePrincipal
* StoragePrincipal to bound storage to.
*/
[noscript]
SessionStorageCacheAddRefed getSessionStorageCache(in nsIPrincipal aPrincipal,
in nsIPrincipal aStoragePrincipal);
};

Просмотреть файл

@ -22,9 +22,8 @@ NS_IMPL_RELEASE_INHERITED(PartitionedLocalStorage, Storage)
PartitionedLocalStorage::PartitionedLocalStorage(
nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal)
: Storage(aWindow, aPrincipal, aStoragePrincipal),
mCache(new SessionStorageCache()) {}
nsIPrincipal* aStoragePrincipal, SessionStorageCache* aCache)
: Storage(aWindow, aPrincipal, aStoragePrincipal), mCache(aCache) {}
PartitionedLocalStorage::~PartitionedLocalStorage() {}

Просмотреть файл

@ -25,7 +25,8 @@ class PartitionedLocalStorage final : public Storage {
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PartitionedLocalStorage, Storage)
PartitionedLocalStorage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal);
nsIPrincipal* aStoragePrincipal,
SessionStorageCache* aCache);
StorageType Type() const override { return ePartitionedLocalStorage; }

Просмотреть файл

@ -17,7 +17,8 @@ namespace dom {
using namespace StorageUtils;
NS_IMPL_ISUPPORTS(SessionStorageManager, nsIDOMStorageManager)
NS_IMPL_ISUPPORTS(SessionStorageManager, nsIDOMStorageManager,
nsIDOMSessionStorageManager)
SessionStorageManager::SessionStorageManager() {
StorageObserver* observer = StorageObserver::Self();
@ -70,11 +71,19 @@ SessionStorageManager::PrecacheStorage(nsIPrincipal* aPrincipal,
}
NS_IMETHODIMP
SessionStorageManager::CreateStorage(mozIDOMWindow* aWindow,
nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal,
const nsAString& aDocumentURI,
bool aPrivate, Storage** aRetval) {
SessionStorageManager::GetSessionStorageCache(
nsIPrincipal* aPrincipal, nsIPrincipal* aStoragePrincipal,
RefPtr<SessionStorageCache>* aRetVal) {
return GetSessionStorageCacheHelper(aPrincipal, aStoragePrincipal, true,
nullptr, aRetVal);
}
nsresult SessionStorageManager::GetSessionStorageCacheHelper(
nsIPrincipal* aPrincipal, nsIPrincipal* aStoragePrincipal,
bool aMakeIfNeeded, SessionStorageCache* aCloneFrom,
RefPtr<SessionStorageCache>* aRetVal) {
*aRetVal = nullptr;
nsAutoCString originKey;
nsAutoCString originAttributes;
nsresult rv = GenerateOriginKey(aPrincipal, originAttributes, originKey);
@ -84,14 +93,43 @@ SessionStorageManager::CreateStorage(mozIDOMWindow* aWindow,
OriginKeyHashTable* table;
if (!mOATable.Get(originAttributes, &table)) {
table = new OriginKeyHashTable();
mOATable.Put(originAttributes, table);
if (aMakeIfNeeded) {
table = new OriginKeyHashTable();
mOATable.Put(originAttributes, table);
} else {
return NS_OK;
}
}
RefPtr<SessionStorageCache> cache;
if (!table->Get(originKey, getter_AddRefs(cache))) {
cache = new SessionStorageCache();
table->Put(originKey, cache);
if (aMakeIfNeeded) {
if (aCloneFrom) {
cache = aCloneFrom->Clone();
} else {
cache = new SessionStorageCache();
}
table->Put(originKey, cache);
} else {
return NS_OK;
}
}
*aRetVal = std::move(cache);
return NS_OK;
}
NS_IMETHODIMP
SessionStorageManager::CreateStorage(mozIDOMWindow* aWindow,
nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal,
const nsAString& aDocumentURI,
bool aPrivate, Storage** aRetval) {
RefPtr<SessionStorageCache> cache;
nsresult rv = GetSessionStorageCache(aPrincipal, aStoragePrincipal, &cache);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
@ -111,21 +149,11 @@ SessionStorageManager::GetStorage(mozIDOMWindow* aWindow,
bool aPrivate, Storage** aRetval) {
*aRetval = nullptr;
nsAutoCString originKey;
nsAutoCString originAttributes;
nsresult rv = GenerateOriginKey(aPrincipal, originAttributes, originKey);
if (NS_FAILED(rv)) {
return rv;
}
OriginKeyHashTable* table;
if (!mOATable.Get(originAttributes, &table)) {
return NS_OK;
}
RefPtr<SessionStorageCache> cache;
if (!table->Get(originKey, getter_AddRefs(cache))) {
return NS_OK;
nsresult rv = GetSessionStorageCacheHelper(aPrincipal, aStoragePrincipal,
false, nullptr, &cache);
if (NS_FAILED(rv) || !cache) {
return rv;
}
nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
@ -147,31 +175,10 @@ SessionStorageManager::CloneStorage(Storage* aStorage) {
return NS_ERROR_UNEXPECTED;
}
nsAutoCString originKey;
nsAutoCString originAttributes;
nsresult rv =
GenerateOriginKey(aStorage->Principal(), originAttributes, originKey);
if (NS_FAILED(rv)) {
return rv;
}
OriginKeyHashTable* table;
if (!mOATable.Get(originAttributes, &table)) {
table = new OriginKeyHashTable();
mOATable.Put(originAttributes, table);
}
RefPtr<SessionStorageCache> cache;
if (table->Get(originKey, getter_AddRefs(cache))) {
// Do not replace an existing sessionStorage.
return NS_OK;
}
cache = static_cast<SessionStorage*>(aStorage)->Cache()->Clone();
MOZ_ASSERT(cache);
table->Put(originKey, cache);
return NS_OK;
return GetSessionStorageCacheHelper(
aStorage->Principal(), aStorage->StoragePrincipal(), true,
static_cast<SessionStorage*>(aStorage)->Cache(), &cache);
}
NS_IMETHODIMP
@ -185,23 +192,13 @@ SessionStorageManager::CheckStorage(nsIPrincipal* aPrincipal, Storage* aStorage,
return NS_ERROR_NOT_AVAILABLE;
}
nsAutoCString originKey;
nsAutoCString originAttributes;
nsresult rv = GenerateOriginKey(aPrincipal, originAttributes, originKey);
if (NS_FAILED(rv)) {
return rv;
}
*aRetval = false;
OriginKeyHashTable* table;
if (!mOATable.Get(originAttributes, &table)) {
return NS_OK;
}
RefPtr<SessionStorageCache> cache;
if (!table->Get(originKey, getter_AddRefs(cache))) {
return NS_OK;
nsresult rv = GetSessionStorageCacheHelper(
aPrincipal, aStorage->StoragePrincipal(), false, nullptr, &cache);
if (NS_FAILED(rv) || !cache) {
return rv;
}
if (aStorage->Type() != Storage::eSessionStorage) {

Просмотреть файл

@ -18,13 +18,14 @@ namespace dom {
class SessionStorageCache;
class SessionStorageObserver;
class SessionStorageManager final : public nsIDOMStorageManager,
class SessionStorageManager final : public nsIDOMSessionStorageManager,
public StorageObserverSink {
public:
SessionStorageManager();
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMSTORAGEMANAGER
NS_DECL_NSIDOMSESSIONSTORAGEMANAGER
private:
~SessionStorageManager();
@ -43,6 +44,12 @@ class SessionStorageManager final : public nsIDOMStorageManager,
const OriginAttributesPattern& aPattern,
const nsACString& aOriginScope);
nsresult GetSessionStorageCacheHelper(nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal,
bool aMakeIfNeeded,
SessionStorageCache* aCloneFrom,
RefPtr<SessionStorageCache>* aRetVal);
typedef nsRefPtrHashtable<nsCStringHashKey, SessionStorageCache>
OriginKeyHashTable;
nsClassHashtable<nsCStringHashKey, OriginKeyHashTable> mOATable;

Просмотреть файл

@ -162,6 +162,11 @@ Classes = [
'contract_ids': ['@mozilla.org/dom/localStorage-manager;1'],
'legacy_constructor': 'LocalStorageManagerConstructor',
},
{
'cid': '{64e4bf03-773e-408e-939a-e11652fdfd28}',
'contract_ids': ['@mozilla.org/dom/sessionStorage-manager;1'],
'legacy_constructor': 'SessionStorageManagerConstructor',
},
{
'cid': '{5a75c25a-5e7e-4d90-8f7c-07eb15cc0aa8}',
'contract_ids': ['@mozilla.org/dom/quota-manager-service;1'],

Просмотреть файл

@ -44,6 +44,7 @@
#include "mozilla/dom/LocalStorageCommon.h"
#include "mozilla/dom/LocalStorageManager.h"
#include "mozilla/dom/LocalStorageManager2.h"
#include "mozilla/dom/SessionStorageManager.h"
#ifdef MOZ_WEBSPEECH
# include "mozilla/dom/nsSynthVoiceRegistry.h"
@ -239,6 +240,12 @@ nsresult LocalStorageManagerConstructor(nsISupports* aOuter, REFNSIID aIID,
return manager->QueryInterface(aIID, aResult);
}
nsresult SessionStorageManagerConstructor(nsISupports* aOuter, REFNSIID aIID,
void** aResult) {
RefPtr<SessionStorageManager> manager = new SessionStorageManager();
return manager->QueryInterface(aIID, aResult);
}
static const mozilla::Module::CategoryEntry kLayoutCategories[] = {
// clang-format off
{ "clear-origin-attributes-data", "QuotaManagerService", "service," QUOTAMANAGER_SERVICE_CONTRACTID },

Просмотреть файл

@ -42,6 +42,8 @@ nsresult Construct_nsIScriptSecurityManager(nsISupports* aOuter,
const nsIID& aIID, void** aResult);
nsresult LocalStorageManagerConstructor(nsISupports* aOuter, const nsIID& aIID,
void** aResult);
nsresult SessionStorageManagerConstructor(nsISupports* aOuter,
const nsIID& aIID, void** aResult);
already_AddRefed<nsIPresentationService> NS_CreatePresentationService();