2018-11-29 23:48:47 +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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_localstorage_LSSnapshot_h
|
|
|
|
#define mozilla_dom_localstorage_LSSnapshot_h
|
|
|
|
|
2020-10-21 16:17:18 +03:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include "ErrorList.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
2020-02-26 02:14:57 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2019-12-21 04:13:29 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2021-03-10 13:47:47 +03:00
|
|
|
#include "nsTHashMap.h"
|
2019-12-21 04:13:29 +03:00
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsIRunnable.h"
|
2020-10-21 16:17:18 +03:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsTArrayForwardDeclare.h"
|
2021-03-23 13:36:34 +03:00
|
|
|
#include "nsTHashSet.h"
|
2019-12-21 04:13:29 +03:00
|
|
|
|
|
|
|
class nsITimer;
|
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-11-29 23:49:10 +03:00
|
|
|
class LSDatabase;
|
|
|
|
class LSNotifyInfo;
|
2018-11-29 23:48:47 +03:00
|
|
|
class LSSnapshotChild;
|
2018-11-29 23:49:10 +03:00
|
|
|
class LSSnapshotInitInfo;
|
2019-05-15 07:11:10 +03:00
|
|
|
class LSWriteAndNotifyInfo;
|
2019-05-15 07:11:11 +03:00
|
|
|
class SnapshotWriteOptimizer;
|
2018-11-29 23:48:47 +03:00
|
|
|
|
2019-12-21 04:13:29 +03:00
|
|
|
template <typename>
|
|
|
|
class Optional;
|
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
class LSSnapshot final : public nsIRunnable {
|
2018-11-29 23:49:10 +03:00
|
|
|
public:
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* The LoadState expresses what subset of information a snapshot has from the
|
|
|
|
* authoritative Datastore in the parent process. The initial snapshot is
|
|
|
|
* populated heuristically based on the size of the keys and size of the items
|
|
|
|
* (inclusive of the key value; item is key+value, not just value) of the
|
|
|
|
* entire datastore relative to the configured prefill limit (via pref
|
|
|
|
* "dom.storage.snapshot_prefill" exposed as gSnapshotPrefill in bytes).
|
|
|
|
*
|
|
|
|
* If there's less data than the limit, we send both keys and values and end
|
|
|
|
* up as AllOrderedItems. If there's enough room for all the keys but not
|
|
|
|
* all the values, we end up as AllOrderedKeys with as many values present as
|
|
|
|
* would fit. If there's not enough room for all the keys, then we end up as
|
|
|
|
* Partial with as many key-value pairs as will fit.
|
|
|
|
*
|
|
|
|
* The state AllUnorderedItems can only be reached by code getting items one
|
|
|
|
* by one.
|
|
|
|
*/
|
2018-11-29 23:48:54 +03:00
|
|
|
enum class LoadState {
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Class constructed, Init(LSSnapshotInitInfo) has not been invoked yet.
|
|
|
|
*/
|
2018-11-29 23:48:54 +03:00
|
|
|
Initial,
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* Some keys and their values are known.
|
|
|
|
*/
|
2018-11-29 23:48:54 +03:00
|
|
|
Partial,
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* All the keys are known in order, but some values are unknown.
|
|
|
|
*/
|
2018-11-29 23:48:54 +03:00
|
|
|
AllOrderedKeys,
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* All keys and their values are known, but in an arbitrary order.
|
|
|
|
*/
|
2018-11-29 23:48:54 +03:00
|
|
|
AllUnorderedItems,
|
2018-11-05 22:04:39 +03:00
|
|
|
/**
|
|
|
|
* All keys and their values are known and are present in their canonical
|
|
|
|
* order. This is everything, and is the preferred case. The initial
|
|
|
|
* population will send this info when the size of all items is less than
|
|
|
|
* the prefill threshold.
|
|
|
|
*
|
|
|
|
* mValues will contain all keys and values, mLoadedItems and mUnknownItems
|
|
|
|
* are unused.
|
|
|
|
*/
|
2018-11-29 23:49:10 +03:00
|
|
|
AllOrderedItems,
|
|
|
|
EndGuard
|
2018-11-29 23:48:54 +03:00
|
|
|
};
|
|
|
|
|
2018-11-29 23:49:10 +03:00
|
|
|
private:
|
2018-11-29 23:48:54 +03:00
|
|
|
RefPtr<LSSnapshot> mSelfRef;
|
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
RefPtr<LSDatabase> mDatabase;
|
|
|
|
|
2022-01-20 00:32:15 +03:00
|
|
|
nsCOMPtr<nsITimer> mIdleTimer;
|
2018-11-29 23:49:31 +03:00
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
LSSnapshotChild* mActor;
|
|
|
|
|
2021-03-23 13:36:34 +03:00
|
|
|
nsTHashSet<nsString> mLoadedItems;
|
|
|
|
nsTHashSet<nsString> mUnknownItems;
|
2021-03-10 13:47:47 +03:00
|
|
|
nsTHashMap<nsStringHashKey, nsString> mValues;
|
2020-02-26 02:14:57 +03:00
|
|
|
UniquePtr<SnapshotWriteOptimizer> mWriteOptimizer;
|
|
|
|
UniquePtr<nsTArray<LSWriteAndNotifyInfo>> mWriteAndNotifyInfos;
|
2018-11-29 23:48:47 +03:00
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
uint32_t mInitLength;
|
|
|
|
uint32_t mLength;
|
2018-11-29 23:48:47 +03:00
|
|
|
int64_t mExactUsage;
|
|
|
|
int64_t mPeakUsage;
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
LoadState mLoadState;
|
2018-11-29 23:49:10 +03:00
|
|
|
|
2019-05-15 07:11:11 +03:00
|
|
|
bool mHasOtherProcessObservers;
|
2018-11-29 23:48:54 +03:00
|
|
|
bool mExplicit;
|
2018-11-29 23:49:31 +03:00
|
|
|
bool mHasPendingStableStateCallback;
|
2022-01-20 00:32:15 +03:00
|
|
|
bool mHasPendingIdleTimerCallback;
|
2018-11-29 23:49:31 +03:00
|
|
|
bool mDirty;
|
2018-11-29 23:48:54 +03:00
|
|
|
|
2019-04-04 04:45:08 +03:00
|
|
|
#ifdef DEBUG
|
2018-11-29 23:48:47 +03:00
|
|
|
bool mInitialized;
|
|
|
|
bool mSentFinish;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit LSSnapshot(LSDatabase* aDatabase);
|
|
|
|
|
|
|
|
void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(LSSnapshot); }
|
|
|
|
|
|
|
|
void SetActor(LSSnapshotChild* aActor);
|
|
|
|
|
|
|
|
void ClearActor() {
|
|
|
|
AssertIsOnOwningThread();
|
2019-04-04 04:45:08 +03:00
|
|
|
MOZ_ASSERT(mActor);
|
2018-11-29 23:48:47 +03:00
|
|
|
|
|
|
|
mActor = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
bool Explicit() const { return mExplicit; }
|
|
|
|
|
2019-04-09 07:18:29 +03:00
|
|
|
nsresult Init(const nsAString& aKey, const LSSnapshotInitInfo& aInitInfo,
|
|
|
|
bool aExplicit);
|
2018-11-29 23:48:47 +03:00
|
|
|
|
|
|
|
nsresult GetLength(uint32_t* aResult);
|
|
|
|
|
|
|
|
nsresult GetKey(uint32_t aIndex, nsAString& aResult);
|
|
|
|
|
|
|
|
nsresult GetItem(const nsAString& aKey, nsAString& aResult);
|
|
|
|
|
|
|
|
nsresult GetKeys(nsTArray<nsString>& aKeys);
|
|
|
|
|
|
|
|
nsresult SetItem(const nsAString& aKey, const nsAString& aValue,
|
|
|
|
LSNotifyInfo& aNotifyInfo);
|
|
|
|
|
|
|
|
nsresult RemoveItem(const nsAString& aKey, LSNotifyInfo& aNotifyInfo);
|
|
|
|
|
|
|
|
nsresult Clear(LSNotifyInfo& aNotifyInfo);
|
|
|
|
|
2018-11-29 23:49:31 +03:00
|
|
|
void MarkDirty();
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
nsresult End();
|
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
private:
|
|
|
|
~LSSnapshot();
|
|
|
|
|
2018-11-29 23:49:31 +03:00
|
|
|
void ScheduleStableStateCallback();
|
|
|
|
|
|
|
|
void MaybeScheduleStableStateCallback();
|
|
|
|
|
2018-11-29 23:49:20 +03:00
|
|
|
nsresult GetItemInternal(const nsAString& aKey,
|
|
|
|
const Optional<nsString>& aValue,
|
|
|
|
nsAString& aResult);
|
|
|
|
|
2018-11-29 23:48:54 +03:00
|
|
|
nsresult EnsureAllKeys();
|
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
nsresult UpdateUsage(int64_t aDelta);
|
|
|
|
|
2018-11-29 23:49:31 +03:00
|
|
|
nsresult Checkpoint();
|
|
|
|
|
|
|
|
nsresult Finish();
|
|
|
|
|
2022-01-20 00:32:15 +03:00
|
|
|
void CancelIdleTimer();
|
2018-11-29 23:49:31 +03:00
|
|
|
|
2022-01-20 00:32:15 +03:00
|
|
|
static void IdleTimerCallback(nsITimer* aTimer, void* aClosure);
|
2018-11-29 23:49:31 +03:00
|
|
|
|
2018-11-29 23:48:47 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_localstorage_LSSnapshot_h
|