LocalStorageManager.h (4961B)
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_StorageManager_h 8 #define mozilla_dom_StorageManager_h 9 10 #include "LocalStorage.h" 11 #include "LocalStorageCache.h" 12 #include "StorageObserver.h" 13 #include "mozilla/dom/Storage.h" 14 #include "nsClassHashtable.h" 15 #include "nsHashKeys.h" 16 #include "nsIDOMStorageManager.h" 17 #include "nsILocalStorageManager.h" 18 #include "nsTHashMap.h" 19 #include "nsTHashtable.h" 20 21 namespace mozilla { 22 23 class OriginAttributesPattern; 24 25 namespace dom { 26 27 class LocalStorageManager final : public nsIDOMStorageManager, 28 public nsILocalStorageManager, 29 public StorageObserverSink { 30 NS_DECL_ISUPPORTS 31 NS_DECL_NSIDOMSTORAGEMANAGER 32 NS_DECL_NSILOCALSTORAGEMANAGER 33 34 public: 35 LocalStorageManager(); 36 37 // Reads the preference for DOM storage quota 38 static uint32_t GetOriginQuota(); 39 40 // Reads the preference for DOM storage site quota 41 static uint32_t GetSiteQuota(); 42 43 // Gets (but not ensures) cache for the given scope 44 LocalStorageCache* GetCache(const nsACString& aOriginSuffix, 45 const nsACString& aOriginNoSuffix); 46 47 // Returns object keeping usage cache for the scope. 48 already_AddRefed<StorageUsage> GetOriginUsage( 49 const nsACString& aOriginNoSuffix, uint32_t aPrivateBrowsingId); 50 51 static nsAutoCString CreateOrigin(const nsACString& aOriginSuffix, 52 const nsACString& aOriginNoSuffix); 53 54 private: 55 ~LocalStorageManager(); 56 57 // StorageObserverSink, handler to various chrome clearing notification 58 nsresult Observe(const char* aTopic, 59 const nsAString& aOriginAttributesPattern, 60 const nsACString& aOriginScope) override; 61 62 // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate 63 // LocalStorageCache into the entry. 64 class LocalStorageCacheHashKey : public nsCStringHashKey { 65 public: 66 explicit LocalStorageCacheHashKey(const nsACString* aKey) 67 : nsCStringHashKey(aKey), mCache(new LocalStorageCache(aKey)) {} 68 69 LocalStorageCacheHashKey(LocalStorageCacheHashKey&& aOther) 70 : nsCStringHashKey(std::move(aOther)), 71 mCache(std::move(aOther.mCache)), 72 mCacheRef(std::move(aOther.mCacheRef)) { 73 NS_ERROR("Shouldn't be called"); 74 } 75 76 LocalStorageCache* cache() { return mCache; } 77 // Keep the cache referenced forever, used for sessionStorage. 78 void HardRef() { mCacheRef = mCache; } 79 80 private: 81 // weak ref only since cache references its manager. 82 LocalStorageCache* mCache; 83 // hard ref when this is sessionStorage to keep it alive forever. 84 RefPtr<LocalStorageCache> mCacheRef; 85 }; 86 87 // Ensures cache for a scope, when it doesn't exist it is created and 88 // initalized, this also starts preload of persistent data. 89 already_AddRefed<LocalStorageCache> PutCache( 90 const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix, 91 const nsACString& aQuotaKey, nsIPrincipal* aPrincipal); 92 93 enum class CreateMode { 94 // GetStorage: do not create if it's not already in memory. 95 UseIfExistsNeverCreate, 96 // CreateStorage: Create it if it's not already in memory. 97 CreateAlways, 98 // PrecacheStorage: Create only if the database says we ShouldPreloadOrigin. 99 CreateIfShouldPreload 100 }; 101 102 // Helper for creation of DOM storage objects 103 nsresult GetStorageInternal(CreateMode aCreate, mozIDOMWindow* aWindow, 104 nsIPrincipal* aPrincipal, 105 nsIPrincipal* aStoragePrincipal, 106 const nsAString& aDocumentURI, bool aPrivate, 107 Storage** aRetval); 108 109 // Suffix->origin->cache map 110 using CacheOriginHashtable = nsTHashtable<LocalStorageCacheHashKey>; 111 nsClassHashtable<nsCStringHashKey, CacheOriginHashtable> mCaches; 112 113 void ClearCaches(uint32_t aUnloadFlags, 114 const OriginAttributesPattern& aPattern, 115 const nsACString& aKeyPrefix); 116 117 // Global getter of localStorage manager service 118 static LocalStorageManager* Self(); 119 120 // Like Self, but creates an instance if we're not yet initialized. 121 static LocalStorageManager* Ensure(); 122 123 private: 124 // Keeps usage cache objects for eTLD+1 scopes we have touched. 125 nsTHashMap<nsCString, RefPtr<StorageUsage> > mUsages; 126 127 friend class LocalStorageCache; 128 friend class StorageDBChild; 129 // Releases cache since it is no longer referrered by any Storage object. 130 virtual void DropCache(LocalStorageCache* aCache); 131 132 static LocalStorageManager* sSelf; 133 }; 134 135 } // namespace dom 136 } // namespace mozilla 137 138 #endif // mozilla_dom_StorageManager_h