SessionStorageCache.h (2571B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_SessionStorageCache_h 8 #define mozilla_dom_SessionStorageCache_h 9 10 #include "mozilla/dom/LSWriteOptimizerImpl.h" 11 #include "nsTHashMap.h" 12 13 namespace mozilla::dom { 14 15 class SSSetItemInfo; 16 class SSWriteInfo; 17 class SessionStorageCacheChild; 18 19 /** 20 * Coalescing manipulation queue used by `SessionStorageCache`. Used by 21 * `SessionStorageCache` to buffer and coalesce manipulations before they 22 * are sent to the parent process. 23 */ 24 class SSWriteOptimizer final : public LSWriteOptimizer<nsAString, nsString> { 25 public: 26 void Enumerate(nsTArray<SSWriteInfo>& aWriteInfos); 27 }; 28 29 class SessionStorageCache final { 30 public: 31 NS_INLINE_DECL_REFCOUNTING(SessionStorageCache) 32 33 SessionStorageCache(); 34 35 int64_t GetOriginQuotaUsage(); 36 37 uint32_t Length(); 38 39 void Key(uint32_t aIndex, nsAString& aResult); 40 41 void GetItem(const nsAString& aKey, nsAString& aResult); 42 43 void GetKeys(nsTArray<nsString>& aKeys); 44 45 nsresult SetItem(const nsAString& aKey, const nsAString& aValue, 46 nsString& aOldValue, bool aRecordWriteInfo = true); 47 48 nsresult RemoveItem(const nsAString& aKey, nsString& aOldValue, 49 bool aRecordWriteInfo = true); 50 51 void Clear(bool aByUserInteraction = true, bool aRecordWriteInfo = true); 52 53 void ResetWriteInfos(); 54 55 already_AddRefed<SessionStorageCache> Clone() const; 56 57 nsTArray<SSSetItemInfo> SerializeData(); 58 59 nsTArray<SSWriteInfo> SerializeWriteInfos(); 60 61 void DeserializeData(const nsTArray<SSSetItemInfo>& aData); 62 63 void DeserializeWriteInfos(const nsTArray<SSWriteInfo>& aInfos); 64 65 void SetActor(SessionStorageCacheChild* aActor); 66 67 SessionStorageCacheChild* Actor() const { return mActor; } 68 69 void ClearActor(); 70 71 void SetLoadedOrCloned() { mLoadedOrCloned = true; } 72 73 bool WasLoadedOrCloned() const { return mLoadedOrCloned; } 74 75 private: 76 ~SessionStorageCache(); 77 78 struct DataSet { 79 DataSet() : mOriginQuotaUsage(0) {} 80 81 bool ProcessUsageDelta(int64_t aDelta); 82 83 nsTHashMap<nsStringHashKey, nsString> mKeys; 84 85 SSWriteOptimizer mWriteOptimizer; 86 87 int64_t mOriginQuotaUsage; 88 }; 89 90 DataSet mDataSet; 91 92 SessionStorageCacheChild* mActor; 93 bool mLoadedOrCloned; 94 }; 95 96 } // namespace mozilla::dom 97 98 #endif // mozilla_dom_SessionStorageCache_h