LCOV - code coverage report
Current view: top level - dom/storage - LocalStorageManager.h (source / functions) Hit Total Coverage
Test: output.info Lines: 5 7 71.4 %
Date: 2017-07-14 16:53:18 Functions: 2 4 50.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13