DOMStorageManager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. #ifndef nsDOMStorageManager_h__
  6. #define nsDOMStorageManager_h__
  7. #include "nsIDOMStorageManager.h"
  8. #include "DOMStorageObserver.h"
  9. #include "DOMStorageCache.h"
  10. #include "mozilla/dom/DOMStorage.h"
  11. #include "nsTHashtable.h"
  12. #include "nsDataHashtable.h"
  13. #include "nsClassHashtable.h"
  14. #include "nsHashKeys.h"
  15. namespace mozilla {
  16. class OriginAttributesPattern;
  17. namespace dom {
  18. const DOMStorage::StorageType SessionStorage = DOMStorage::SessionStorage;
  19. const DOMStorage::StorageType LocalStorage = DOMStorage::LocalStorage;
  20. class DOMStorageManager : public nsIDOMStorageManager
  21. , public DOMStorageObserverSink
  22. {
  23. NS_DECL_ISUPPORTS
  24. NS_DECL_NSIDOMSTORAGEMANAGER
  25. public:
  26. virtual DOMStorage::StorageType Type() { return mType; }
  27. // Reads the preference for DOM storage quota
  28. static uint32_t GetQuota();
  29. // Gets (but not ensures) cache for the given scope
  30. DOMStorageCache* GetCache(const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix);
  31. // Returns object keeping usage cache for the scope.
  32. already_AddRefed<DOMStorageUsage> GetOriginUsage(const nsACString& aOriginNoSuffix);
  33. static nsCString CreateOrigin(const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix);
  34. protected:
  35. explicit DOMStorageManager(DOMStorage::StorageType aType);
  36. virtual ~DOMStorageManager();
  37. private:
  38. // DOMStorageObserverSink, handler to various chrome clearing notification
  39. virtual nsresult Observe(const char* aTopic,
  40. const nsAString& aOriginAttributesPattern,
  41. const nsACString& aOriginScope) override;
  42. // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate
  43. // DOMStorageCache into the entry.
  44. class DOMStorageCacheHashKey : public nsCStringHashKey
  45. {
  46. public:
  47. explicit DOMStorageCacheHashKey(const nsACString* aKey)
  48. : nsCStringHashKey(aKey)
  49. , mCache(new DOMStorageCache(aKey))
  50. {}
  51. DOMStorageCacheHashKey(const DOMStorageCacheHashKey& aOther)
  52. : nsCStringHashKey(aOther)
  53. {
  54. NS_ERROR("Shouldn't be called");
  55. }
  56. DOMStorageCache* cache() { return mCache; }
  57. // Keep the cache referenced forever, used for sessionStorage.
  58. void HardRef() { mCacheRef = mCache; }
  59. private:
  60. // weak ref only since cache references its manager.
  61. DOMStorageCache* mCache;
  62. // hard ref when this is sessionStorage to keep it alive forever.
  63. RefPtr<DOMStorageCache> mCacheRef;
  64. };
  65. // Ensures cache for a scope, when it doesn't exist it is created and initalized,
  66. // this also starts preload of persistent data.
  67. already_AddRefed<DOMStorageCache> PutCache(const nsACString& aOriginSuffix,
  68. const nsACString& aOriginNoSuffix,
  69. nsIPrincipal* aPrincipal);
  70. // Helper for creation of DOM storage objects
  71. nsresult GetStorageInternal(bool aCreate,
  72. mozIDOMWindow* aWindow,
  73. nsIPrincipal* aPrincipal,
  74. const nsAString& aDocumentURI,
  75. bool aPrivate,
  76. nsIDOMStorage** aRetval);
  77. // Suffix->origin->cache map
  78. typedef nsTHashtable<DOMStorageCacheHashKey> CacheOriginHashtable;
  79. nsClassHashtable<nsCStringHashKey, CacheOriginHashtable> mCaches;
  80. const DOMStorage::StorageType mType;
  81. void ClearCaches(uint32_t aUnloadFlags,
  82. const OriginAttributesPattern& aPattern,
  83. const nsACString& aKeyPrefix);
  84. protected:
  85. // Keeps usage cache objects for eTLD+1 scopes we have touched.
  86. nsDataHashtable<nsCStringHashKey, RefPtr<DOMStorageUsage> > mUsages;
  87. friend class DOMStorageCache;
  88. // Releases cache since it is no longer referrered by any DOMStorage object.
  89. virtual void DropCache(DOMStorageCache* aCache);
  90. };
  91. // Derived classes to allow two different contract ids, one for localStorage and
  92. // one for sessionStorage management. localStorage manager is used as service
  93. // scoped to the application while sessionStorage managers are instantiated by each
  94. // top doc shell in the application since sessionStorages are isolated per top level
  95. // browsing context. The code may easily by shared by both.
  96. class DOMLocalStorageManager final : public DOMStorageManager
  97. {
  98. public:
  99. DOMLocalStorageManager();
  100. virtual ~DOMLocalStorageManager();
  101. // Global getter of localStorage manager service
  102. static DOMLocalStorageManager* Self() { return sSelf; }
  103. // Like Self, but creates an instance if we're not yet initialized.
  104. static DOMLocalStorageManager* Ensure();
  105. private:
  106. static DOMLocalStorageManager* sSelf;
  107. };
  108. class DOMSessionStorageManager final : public DOMStorageManager
  109. {
  110. public:
  111. DOMSessionStorageManager();
  112. };
  113. } // namespace dom
  114. } // namespace mozilla
  115. #endif /* nsDOMStorageManager_h__ */