2018-11-13 11:51:14 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PartitionedLocalStorage.h"
|
|
|
|
#include "SessionStorageCache.h"
|
2020-11-10 15:00:30 +03:00
|
|
|
#include "nsContentUtils.h"
|
2018-11-13 11:51:14 +03:00
|
|
|
|
|
|
|
#include "mozilla/dom/StorageBinding.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(PartitionedLocalStorage, Storage);
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PartitionedLocalStorage)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Storage)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PartitionedLocalStorage, Storage)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PartitionedLocalStorage, Storage)
|
|
|
|
|
2019-05-14 08:49:46 +03:00
|
|
|
PartitionedLocalStorage::PartitionedLocalStorage(
|
|
|
|
nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
|
2019-11-12 18:40:02 +03:00
|
|
|
nsIPrincipal* aStoragePrincipal, SessionStorageCache* aCache)
|
|
|
|
: Storage(aWindow, aPrincipal, aStoragePrincipal), mCache(aCache) {}
|
2018-11-13 11:51:14 +03:00
|
|
|
|
2020-02-12 13:33:31 +03:00
|
|
|
PartitionedLocalStorage::~PartitionedLocalStorage() = default;
|
2018-11-13 11:51:14 +03:00
|
|
|
|
|
|
|
int64_t PartitionedLocalStorage::GetOriginQuotaUsage() const {
|
2021-06-08 16:42:33 +03:00
|
|
|
return mCache->GetOriginQuotaUsage();
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PartitionedLocalStorage::GetLength(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CanUseStorage(aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:42:33 +03:00
|
|
|
return mCache->Length();
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::Key(uint32_t aIndex, nsAString& aResult,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CanUseStorage(aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:42:33 +03:00
|
|
|
mCache->Key(aIndex, aResult);
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::GetItem(const nsAString& aKey, nsAString& aResult,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CanUseStorage(aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:42:33 +03:00
|
|
|
mCache->GetItem(aKey, aResult);
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::GetSupportedNames(nsTArray<nsString>& aKeys) {
|
|
|
|
if (!CanUseStorage(*nsContentUtils::SubjectPrincipal())) {
|
|
|
|
// return just an empty array
|
|
|
|
aKeys.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:42:33 +03:00
|
|
|
mCache->GetKeys(aKeys);
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::SetItem(const nsAString& aKey,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CanUseStorage(aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString oldValue;
|
2021-06-08 16:42:33 +03:00
|
|
|
nsresult rv = mCache->SetItem(aKey, aValue, oldValue);
|
2018-11-13 11:51:14 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::RemoveItem(const nsAString& aKey,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CanUseStorage(aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString oldValue;
|
2021-06-08 16:42:33 +03:00
|
|
|
nsresult rv = mCache->RemoveItem(aKey, oldValue);
|
2018-11-13 11:51:14 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
if (rv == NS_SUCCESS_DOM_NO_OPERATION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartitionedLocalStorage::Clear(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
uint32_t length = GetLength(aSubjectPrincipal, aRv);
|
|
|
|
if (!length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:42:33 +03:00
|
|
|
mCache->Clear();
|
2018-11-13 11:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PartitionedLocalStorage::IsForkOf(const Storage* aOther) const {
|
|
|
|
MOZ_ASSERT(aOther);
|
|
|
|
if (aOther->Type() != eLocalStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCache == static_cast<const PartitionedLocalStorage*>(aOther)->mCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|