dom/src/storage/DOMStorageManager.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsDOMStorageManager_h__
michael@0 7 #define nsDOMStorageManager_h__
michael@0 8
michael@0 9 #include "nsIDOMStorageManager.h"
michael@0 10 #include "DOMStorageObserver.h"
michael@0 11
michael@0 12 #include "nsPIDOMStorage.h"
michael@0 13 #include "DOMStorageCache.h"
michael@0 14
michael@0 15 #include "nsTHashtable.h"
michael@0 16 #include "nsDataHashtable.h"
michael@0 17 #include "nsHashKeys.h"
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20 namespace dom {
michael@0 21
michael@0 22 const nsPIDOMStorage::StorageType SessionStorage = nsPIDOMStorage::SessionStorage;
michael@0 23 const nsPIDOMStorage::StorageType LocalStorage = nsPIDOMStorage::LocalStorage;
michael@0 24
michael@0 25 class DOMStorage;
michael@0 26
michael@0 27 class DOMStorageManager : public nsIDOMStorageManager
michael@0 28 , public DOMStorageObserverSink
michael@0 29 {
michael@0 30 NS_DECL_ISUPPORTS
michael@0 31 NS_DECL_NSIDOMSTORAGEMANAGER
michael@0 32
michael@0 33 public:
michael@0 34 virtual nsPIDOMStorage::StorageType Type() { return mType; }
michael@0 35
michael@0 36 // Reads the preference for DOM storage quota
michael@0 37 static uint32_t GetQuota();
michael@0 38 // Gets (but not ensures) cache for the given scope
michael@0 39 DOMStorageCache* GetCache(const nsACString& aScope) const;
michael@0 40 // Returns object keeping usage cache for the scope.
michael@0 41 already_AddRefed<DOMStorageUsage> GetScopeUsage(const nsACString& aScope);
michael@0 42
michael@0 43 protected:
michael@0 44 DOMStorageManager(nsPIDOMStorage::StorageType aType);
michael@0 45 virtual ~DOMStorageManager();
michael@0 46
michael@0 47 private:
michael@0 48 // DOMStorageObserverSink, handler to various chrome clearing notification
michael@0 49 virtual nsresult Observe(const char* aTopic, const nsACString& aScopePrefix);
michael@0 50
michael@0 51 // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate
michael@0 52 // DOMStorageCache into the entry.
michael@0 53 class DOMStorageCacheHashKey : public nsCStringHashKey
michael@0 54 {
michael@0 55 public:
michael@0 56 DOMStorageCacheHashKey(const nsACString* aKey)
michael@0 57 : nsCStringHashKey(aKey)
michael@0 58 , mCache(new DOMStorageCache(aKey))
michael@0 59 {}
michael@0 60
michael@0 61 DOMStorageCacheHashKey(const DOMStorageCacheHashKey& aOther)
michael@0 62 : nsCStringHashKey(aOther)
michael@0 63 {
michael@0 64 NS_ERROR("Shouldn't be called");
michael@0 65 }
michael@0 66
michael@0 67 DOMStorageCache* cache() { return mCache; }
michael@0 68 // Keep the cache referenced forever, used for sessionStorage.
michael@0 69 void HardRef() { mCacheRef = mCache; }
michael@0 70
michael@0 71 private:
michael@0 72 // weak ref only since cache references its manager.
michael@0 73 DOMStorageCache* mCache;
michael@0 74 // hard ref when this is sessionStorage to keep it alive forever.
michael@0 75 nsRefPtr<DOMStorageCache> mCacheRef;
michael@0 76 };
michael@0 77
michael@0 78 // Ensures cache for a scope, when it doesn't exist it is created and initalized,
michael@0 79 // this also starts preload of persistent data.
michael@0 80 already_AddRefed<DOMStorageCache> PutCache(const nsACString& aScope,
michael@0 81 nsIURI* aFirstPartyIsolationURI,
michael@0 82 nsIPrincipal* aPrincipal);
michael@0 83
michael@0 84 // Helper for creation of DOM storage objects
michael@0 85 nsresult GetStorageInternal(bool aCreate,
michael@0 86 nsIURI* aFirstPartyIsolationURI,
michael@0 87 nsIPrincipal* aPrincipal,
michael@0 88 const nsAString& aDocumentURI,
michael@0 89 bool aPrivate,
michael@0 90 nsIDOMStorage** aRetval);
michael@0 91
michael@0 92 // Scope->cache map
michael@0 93 nsTHashtable<DOMStorageCacheHashKey> mCaches;
michael@0 94 const nsPIDOMStorage::StorageType mType;
michael@0 95
michael@0 96 // If mLowDiskSpace is true it indicates a low device storage situation and
michael@0 97 // so no localStorage writes are allowed. sessionStorage writes are still
michael@0 98 // allowed.
michael@0 99 bool mLowDiskSpace;
michael@0 100 bool IsLowDiskSpace() const { return mLowDiskSpace; };
michael@0 101
michael@0 102 static PLDHashOperator ClearCacheEnumerator(DOMStorageCacheHashKey* aCache,
michael@0 103 void* aClosure);
michael@0 104
michael@0 105 protected:
michael@0 106 // Keeps usage cache objects for eTLD+1 scopes we have touched.
michael@0 107 nsDataHashtable<nsCStringHashKey, nsRefPtr<DOMStorageUsage> > mUsages;
michael@0 108
michael@0 109 friend class DOMStorageCache;
michael@0 110 // Releases cache since it is no longer referrered by any DOMStorage object.
michael@0 111 virtual void DropCache(DOMStorageCache* aCache);
michael@0 112 };
michael@0 113
michael@0 114 // Derived classes to allow two different contract ids, one for localStorage and
michael@0 115 // one for sessionStorage management. localStorage manager is used as service
michael@0 116 // scoped to the application while sessionStorage managers are instantiated by each
michael@0 117 // top doc shell in the application since sessionStorages are isolated per top level
michael@0 118 // browsing context. The code may easily by shared by both.
michael@0 119
michael@0 120 class DOMLocalStorageManager MOZ_FINAL : public DOMStorageManager
michael@0 121 {
michael@0 122 public:
michael@0 123 DOMLocalStorageManager();
michael@0 124 virtual ~DOMLocalStorageManager();
michael@0 125
michael@0 126 // Global getter of localStorage manager service
michael@0 127 static DOMLocalStorageManager* Self() { return sSelf; }
michael@0 128
michael@0 129 private:
michael@0 130 static DOMLocalStorageManager* sSelf;
michael@0 131 };
michael@0 132
michael@0 133 class DOMSessionStorageManager MOZ_FINAL : public DOMStorageManager
michael@0 134 {
michael@0 135 public:
michael@0 136 DOMSessionStorageManager();
michael@0 137 };
michael@0 138
michael@0 139 } // ::dom
michael@0 140 } // ::mozilla
michael@0 141
michael@0 142 #endif /* nsDOMStorageManager_h__ */

mercurial