1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/src/storage/DOMStorageManager.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsDOMStorageManager_h__ 1.10 +#define nsDOMStorageManager_h__ 1.11 + 1.12 +#include "nsIDOMStorageManager.h" 1.13 +#include "DOMStorageObserver.h" 1.14 + 1.15 +#include "nsPIDOMStorage.h" 1.16 +#include "DOMStorageCache.h" 1.17 + 1.18 +#include "nsTHashtable.h" 1.19 +#include "nsDataHashtable.h" 1.20 +#include "nsHashKeys.h" 1.21 + 1.22 +namespace mozilla { 1.23 +namespace dom { 1.24 + 1.25 +const nsPIDOMStorage::StorageType SessionStorage = nsPIDOMStorage::SessionStorage; 1.26 +const nsPIDOMStorage::StorageType LocalStorage = nsPIDOMStorage::LocalStorage; 1.27 + 1.28 +class DOMStorage; 1.29 + 1.30 +class DOMStorageManager : public nsIDOMStorageManager 1.31 + , public DOMStorageObserverSink 1.32 +{ 1.33 + NS_DECL_ISUPPORTS 1.34 + NS_DECL_NSIDOMSTORAGEMANAGER 1.35 + 1.36 +public: 1.37 + virtual nsPIDOMStorage::StorageType Type() { return mType; } 1.38 + 1.39 + // Reads the preference for DOM storage quota 1.40 + static uint32_t GetQuota(); 1.41 + // Gets (but not ensures) cache for the given scope 1.42 + DOMStorageCache* GetCache(const nsACString& aScope) const; 1.43 + // Returns object keeping usage cache for the scope. 1.44 + already_AddRefed<DOMStorageUsage> GetScopeUsage(const nsACString& aScope); 1.45 + 1.46 +protected: 1.47 + DOMStorageManager(nsPIDOMStorage::StorageType aType); 1.48 + virtual ~DOMStorageManager(); 1.49 + 1.50 +private: 1.51 + // DOMStorageObserverSink, handler to various chrome clearing notification 1.52 + virtual nsresult Observe(const char* aTopic, const nsACString& aScopePrefix); 1.53 + 1.54 + // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate 1.55 + // DOMStorageCache into the entry. 1.56 + class DOMStorageCacheHashKey : public nsCStringHashKey 1.57 + { 1.58 + public: 1.59 + DOMStorageCacheHashKey(const nsACString* aKey) 1.60 + : nsCStringHashKey(aKey) 1.61 + , mCache(new DOMStorageCache(aKey)) 1.62 + {} 1.63 + 1.64 + DOMStorageCacheHashKey(const DOMStorageCacheHashKey& aOther) 1.65 + : nsCStringHashKey(aOther) 1.66 + { 1.67 + NS_ERROR("Shouldn't be called"); 1.68 + } 1.69 + 1.70 + DOMStorageCache* cache() { return mCache; } 1.71 + // Keep the cache referenced forever, used for sessionStorage. 1.72 + void HardRef() { mCacheRef = mCache; } 1.73 + 1.74 + private: 1.75 + // weak ref only since cache references its manager. 1.76 + DOMStorageCache* mCache; 1.77 + // hard ref when this is sessionStorage to keep it alive forever. 1.78 + nsRefPtr<DOMStorageCache> mCacheRef; 1.79 + }; 1.80 + 1.81 + // Ensures cache for a scope, when it doesn't exist it is created and initalized, 1.82 + // this also starts preload of persistent data. 1.83 + already_AddRefed<DOMStorageCache> PutCache(const nsACString& aScope, 1.84 + nsIURI* aFirstPartyIsolationURI, 1.85 + nsIPrincipal* aPrincipal); 1.86 + 1.87 + // Helper for creation of DOM storage objects 1.88 + nsresult GetStorageInternal(bool aCreate, 1.89 + nsIURI* aFirstPartyIsolationURI, 1.90 + nsIPrincipal* aPrincipal, 1.91 + const nsAString& aDocumentURI, 1.92 + bool aPrivate, 1.93 + nsIDOMStorage** aRetval); 1.94 + 1.95 + // Scope->cache map 1.96 + nsTHashtable<DOMStorageCacheHashKey> mCaches; 1.97 + const nsPIDOMStorage::StorageType mType; 1.98 + 1.99 + // If mLowDiskSpace is true it indicates a low device storage situation and 1.100 + // so no localStorage writes are allowed. sessionStorage writes are still 1.101 + // allowed. 1.102 + bool mLowDiskSpace; 1.103 + bool IsLowDiskSpace() const { return mLowDiskSpace; }; 1.104 + 1.105 + static PLDHashOperator ClearCacheEnumerator(DOMStorageCacheHashKey* aCache, 1.106 + void* aClosure); 1.107 + 1.108 +protected: 1.109 + // Keeps usage cache objects for eTLD+1 scopes we have touched. 1.110 + nsDataHashtable<nsCStringHashKey, nsRefPtr<DOMStorageUsage> > mUsages; 1.111 + 1.112 + friend class DOMStorageCache; 1.113 + // Releases cache since it is no longer referrered by any DOMStorage object. 1.114 + virtual void DropCache(DOMStorageCache* aCache); 1.115 +}; 1.116 + 1.117 +// Derived classes to allow two different contract ids, one for localStorage and 1.118 +// one for sessionStorage management. localStorage manager is used as service 1.119 +// scoped to the application while sessionStorage managers are instantiated by each 1.120 +// top doc shell in the application since sessionStorages are isolated per top level 1.121 +// browsing context. The code may easily by shared by both. 1.122 + 1.123 +class DOMLocalStorageManager MOZ_FINAL : public DOMStorageManager 1.124 +{ 1.125 +public: 1.126 + DOMLocalStorageManager(); 1.127 + virtual ~DOMLocalStorageManager(); 1.128 + 1.129 + // Global getter of localStorage manager service 1.130 + static DOMLocalStorageManager* Self() { return sSelf; } 1.131 + 1.132 +private: 1.133 + static DOMLocalStorageManager* sSelf; 1.134 +}; 1.135 + 1.136 +class DOMSessionStorageManager MOZ_FINAL : public DOMStorageManager 1.137 +{ 1.138 +public: 1.139 + DOMSessionStorageManager(); 1.140 +}; 1.141 + 1.142 +} // ::dom 1.143 +} // ::mozilla 1.144 + 1.145 +#endif /* nsDOMStorageManager_h__ */