Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/storage/LocalStorageManager.h
Line
Count
Source (jump to first uncovered line)
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 nsAutoCString 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
  class LocalStorageCacheHashKey : public nsCStringHashKey
62
  {
63
  public:
64
    explicit LocalStorageCacheHashKey(const nsACString* aKey)
65
      : nsCStringHashKey(aKey)
66
      , mCache(new LocalStorageCache(aKey))
67
0
    {}
68
69
    LocalStorageCacheHashKey(LocalStorageCacheHashKey&& aOther)
70
      : nsCStringHashKey(std::move(aOther))
71
      , mCache(std::move(aOther.mCache))
72
      , mCacheRef(std::move(aOther.mCacheRef))
73
0
    {
74
0
      NS_ERROR("Shouldn't be called");
75
0
    }
76
77
0
    LocalStorageCache* cache() { return mCache; }
78
    // Keep the cache referenced forever, used for sessionStorage.
79
0
    void HardRef() { mCacheRef = mCache; }
80
81
  private:
82
    // weak ref only since cache references its manager.
83
    LocalStorageCache* mCache;
84
    // hard ref when this is sessionStorage to keep it alive forever.
85
    RefPtr<LocalStorageCache> mCacheRef;
86
  };
87
88
  // Ensures cache for a scope, when it doesn't exist it is created and
89
  // initalized, this also starts preload of persistent data.
90
  already_AddRefed<LocalStorageCache> PutCache(const nsACString& aOriginSuffix,
91
                                               const nsACString& aOriginNoSuffix,
92
                                               nsIPrincipal* aPrincipal);
93
94
  enum class CreateMode {
95
    // GetStorage: do not create if it's not already in memory.
96
    UseIfExistsNeverCreate,
97
    // CreateStorage: Create it if it's not already in memory.
98
    CreateAlways,
99
    // PrecacheStorage: Create only if the database says we ShouldPreloadOrigin.
100
    CreateIfShouldPreload
101
  };
102
103
  // Helper for creation of DOM storage objects
104
  nsresult GetStorageInternal(CreateMode aCreate,
105
                              mozIDOMWindow* aWindow,
106
                              nsIPrincipal* aPrincipal,
107
                              const nsAString& aDocumentURI,
108
                              bool aPrivate,
109
                              Storage** aRetval);
110
111
  // Suffix->origin->cache map
112
  typedef nsTHashtable<LocalStorageCacheHashKey> CacheOriginHashtable;
113
  nsClassHashtable<nsCStringHashKey, CacheOriginHashtable> mCaches;
114
115
  void ClearCaches(uint32_t aUnloadFlags,
116
                   const OriginAttributesPattern& aPattern,
117
                   const nsACString& aKeyPrefix);
118
119
  // Global getter of localStorage manager service
120
0
  static LocalStorageManager* Self() { return sSelf; }
121
122
  // Like Self, but creates an instance if we're not yet initialized.
123
  static LocalStorageManager* Ensure();
124
125
private:
126
  // Keeps usage cache objects for eTLD+1 scopes we have touched.
127
  nsDataHashtable<nsCStringHashKey, RefPtr<StorageUsage> > mUsages;
128
129
  friend class LocalStorageCache;
130
  friend class StorageDBChild;
131
  // Releases cache since it is no longer referrered by any Storage object.
132
  virtual void DropCache(LocalStorageCache* aCache);
133
134
  static LocalStorageManager* sSelf;
135
};
136
137
} // namespace dom
138
} // namespace mozilla
139
140
#endif // mozilla_dom_StorageManager_h