dom/src/storage/DOMStorageManager.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial