Storage.h (6353B)
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_Storage_h 8 #define mozilla_dom_Storage_h 9 10 #include "mozilla/ErrorResult.h" 11 #include "nsCOMPtr.h" 12 #include "nsCycleCollectionParticipant.h" 13 #include "nsISupports.h" 14 #include "nsString.h" 15 #include "nsTArrayForwardDeclare.h" 16 #include "nsWrapperCache.h" 17 18 class nsIPrincipal; 19 class nsPIDOMWindowInner; 20 21 namespace mozilla::dom { 22 23 class Storage : public nsISupports, public nsWrapperCache { 24 public: 25 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 26 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Storage) 27 28 Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal, 29 nsIPrincipal* aStoragePrincipal); 30 31 static bool StoragePrefIsEnabled(); 32 33 enum StorageType { 34 eSessionStorage, 35 eLocalStorage, 36 ePartitionedLocalStorage, 37 }; 38 39 virtual StorageType Type() const = 0; 40 41 virtual bool IsForkOf(const Storage* aStorage) const = 0; 42 43 virtual int64_t GetOriginQuotaUsage() const = 0; 44 45 virtual void Disconnect() {} 46 47 nsIPrincipal* Principal() const { return mPrincipal; } 48 49 nsIPrincipal* StoragePrincipal() const { return mStoragePrincipal; } 50 51 bool IsPrivateBrowsing() const { return mPrivateBrowsing; } 52 53 bool IsPrivateBrowsingOrLess() const { return mPrivateBrowsingOrLess; } 54 55 // WebIDL 56 JSObject* WrapObject(JSContext* aCx, 57 JS::Handle<JSObject*> aGivenProto) override; 58 59 nsPIDOMWindowInner* GetParentObject() const { return mWindow; } 60 61 virtual uint32_t GetLength(nsIPrincipal& aSubjectPrincipal, 62 ErrorResult& aRv) = 0; 63 64 virtual void Key(uint32_t aIndex, nsAString& aResult, 65 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0; 66 67 virtual void GetItem(const nsAString& aKey, nsAString& aResult, 68 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0; 69 70 virtual void GetSupportedNames(nsTArray<nsString>& aKeys) = 0; 71 72 void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult, 73 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { 74 GetItem(aKey, aResult, aSubjectPrincipal, aRv); 75 aFound = !aResult.IsVoid(); 76 } 77 78 virtual void SetItem(const nsAString& aKey, const nsAString& aValue, 79 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0; 80 81 void NamedSetter(const nsAString& aKey, const nsAString& aValue, 82 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { 83 SetItem(aKey, aValue, aSubjectPrincipal, aRv); 84 } 85 86 virtual void RemoveItem(const nsAString& aKey, 87 nsIPrincipal& aSubjectPrincipal, 88 ErrorResult& aRv) = 0; 89 90 void NamedDeleter(const nsAString& aKey, bool& aFound, 91 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { 92 RemoveItem(aKey, aSubjectPrincipal, aRv); 93 94 aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION); 95 } 96 97 virtual void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0; 98 99 ////////////////////////////////////////////////////////////////////////////// 100 // Testing Methods: 101 // 102 // These methods are exposed on the `Storage` WebIDL interface behind a 103 // preference for the benefit of automated-tests. They are not exposed to 104 // content. See `Storage.webidl` for more details. 105 106 virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {} 107 108 virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {} 109 110 virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal, 111 ErrorResult& aRv) {} 112 113 virtual void CheckpointExplicitSnapshot(nsIPrincipal& aSubjectPrincipal, 114 ErrorResult& aRv) {} 115 116 virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal, 117 ErrorResult& aRv) {} 118 119 virtual bool GetHasSnapshot(nsIPrincipal& aSubjectPrincipal, 120 ErrorResult& aRv) { 121 return false; 122 } 123 124 virtual int64_t GetSnapshotUsage(nsIPrincipal& aSubjectPrincipal, 125 ErrorResult& aRv); 126 127 ////////////////////////////////////////////////////////////////////////////// 128 129 // Dispatch storage notification events on all impacted pages in the current 130 // process as well as for consumption by devtools. Pages receive the 131 // notification via StorageNotifierService (not observers like in the past), 132 // while devtools does receive the notification via the observer service. 133 // 134 // aStorage can be null if this method is called by LocalStorageCacheChild. 135 // 136 // aImmediateDispatch is for use by child IPC code (LocalStorageCacheChild) 137 // so that PBackground ordering can be maintained. Without this, the event 138 // would be enqueued and run in a future turn of the event loop, potentially 139 // allowing other PBackground Recv* methods to trigger script that wants to 140 // assume our localstorage changes have already been applied. This is the 141 // case for message manager messages which are used by ContentTask testing 142 // logic and webextensions. 143 static void NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal, 144 const nsAString& aKey, const nsAString& aOldValue, 145 const nsAString& aNewValue, 146 const char16_t* aStorageType, 147 const nsAString& aDocumentURI, bool aIsPrivate, 148 bool aImmediateDispatch); 149 150 protected: 151 virtual ~Storage(); 152 153 // The method checks whether the caller can use a storage. 154 bool CanUseStorage(nsIPrincipal& aSubjectPrincipal); 155 156 virtual void LastRelease() {} 157 158 private: 159 nsCOMPtr<nsPIDOMWindowInner> mWindow; 160 nsCOMPtr<nsIPrincipal> mPrincipal; 161 nsCOMPtr<nsIPrincipal> mStoragePrincipal; 162 163 bool mPrivateBrowsing : 1; 164 165 // Whether storage is set to persist data only per session, may change 166 // dynamically and is set by CanUseStorage function that is called 167 // before any operation on the storage. 168 bool mPrivateBrowsingOrLess : 1; 169 }; 170 171 } // namespace mozilla::dom 172 173 #endif // mozilla_dom_Storage_h