author Jan de Mooij <>
Wed, 13 Feb 2019 06:41:44 +0000
changeset 516733 8c306186cbd7672259b3a730471cfcbe44a9b0d0
parent 505383 6f3709b3878117466168c40affa7bca0b60cf75b
child 532461 d99a6769beb4ec1e6b58591e3dc7be89861fb0a8
permissions -rw-r--r--
Bug 1526588 - Fix some issues with js::GetFirstGlobalInCompartment and XPCWrappedNativeScope::UpdateWeakPointersInAllScopesAfterGC. r=bzbarsky Differential Revision:

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at */

#ifndef mozilla_dom_StorageManager_h
#define mozilla_dom_StorageManager_h

#include "nsIDOMStorageManager.h"
#include "nsILocalStorageManager.h"
#include "StorageObserver.h"

#include "LocalStorage.h"
#include "LocalStorageCache.h"
#include "mozilla/dom/Storage.h"

#include "nsTHashtable.h"
#include "nsDataHashtable.h"
#include "nsClassHashtable.h"
#include "nsHashKeys.h"

namespace mozilla {

class OriginAttributesPattern;

namespace dom {

class LocalStorageManager final : public nsIDOMStorageManager,
                                  public nsILocalStorageManager,
                                  public StorageObserverSink {


  // Reads the preference for DOM storage quota
  static uint32_t GetQuota();

  // Gets (but not ensures) cache for the given scope
  LocalStorageCache* GetCache(const nsACString& aOriginSuffix,
                              const nsACString& aOriginNoSuffix);

  // Returns object keeping usage cache for the scope.
  already_AddRefed<StorageUsage> GetOriginUsage(
      const nsACString& aOriginNoSuffix);

  static nsAutoCString CreateOrigin(const nsACString& aOriginSuffix,
                                    const nsACString& aOriginNoSuffix);


  // StorageObserverSink, handler to various chrome clearing notification
  nsresult Observe(const char* aTopic,
                   const nsAString& aOriginAttributesPattern,
                   const nsACString& aOriginScope) override;

  // Since nsTHashtable doesn't like multiple inheritance, we have to aggregate
  // LocalStorageCache into the entry.
  class LocalStorageCacheHashKey : public nsCStringHashKey {
    explicit LocalStorageCacheHashKey(const nsACString* aKey)
        : nsCStringHashKey(aKey), mCache(new LocalStorageCache(aKey)) {}

    LocalStorageCacheHashKey(LocalStorageCacheHashKey&& aOther)
        : nsCStringHashKey(std::move(aOther)),
          mCacheRef(std::move(aOther.mCacheRef)) {
      NS_ERROR("Shouldn't be called");

    LocalStorageCache* cache() { return mCache; }
    // Keep the cache referenced forever, used for sessionStorage.
    void HardRef() { mCacheRef = mCache; }

    // weak ref only since cache references its manager.
    LocalStorageCache* mCache;
    // hard ref when this is sessionStorage to keep it alive forever.
    RefPtr<LocalStorageCache> mCacheRef;

  // Ensures cache for a scope, when it doesn't exist it is created and
  // initalized, this also starts preload of persistent data.
  already_AddRefed<LocalStorageCache> PutCache(
      const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix,
      nsIPrincipal* aPrincipal);

  enum class CreateMode {
    // GetStorage: do not create if it's not already in memory.
    // CreateStorage: Create it if it's not already in memory.
    // PrecacheStorage: Create only if the database says we ShouldPreloadOrigin.

  // Helper for creation of DOM storage objects
  nsresult GetStorageInternal(CreateMode aCreate, mozIDOMWindow* aWindow,
                              nsIPrincipal* aPrincipal,
                              const nsAString& aDocumentURI, bool aPrivate,
                              Storage** aRetval);

  // Suffix->origin->cache map
  typedef nsTHashtable<LocalStorageCacheHashKey> CacheOriginHashtable;
  nsClassHashtable<nsCStringHashKey, CacheOriginHashtable> mCaches;

  void ClearCaches(uint32_t aUnloadFlags,
                   const OriginAttributesPattern& aPattern,
                   const nsACString& aKeyPrefix);

  // Global getter of localStorage manager service
  static LocalStorageManager* Self();

  // Like Self, but creates an instance if we're not yet initialized.
  static LocalStorageManager* Ensure();

  // Keeps usage cache objects for eTLD+1 scopes we have touched.
  nsDataHashtable<nsCStringHashKey, RefPtr<StorageUsage> > mUsages;

  friend class LocalStorageCache;
  friend class StorageDBChild;
  // Releases cache since it is no longer referrered by any Storage object.
  virtual void DropCache(LocalStorageCache* aCache);

  static LocalStorageManager* sSelf;

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_StorageManager_h