Bug 1843230 - Remove IsWin8OrLater checks from dom/geolocation/ r=emk
[gecko.git] / dom / storage / LocalStorageManager.cpp
blob58b57df76a646bca0a2edfc1b3bb4d08ccfebbc3
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/. */
7 #include "LocalStorageManager.h"
8 #include "LocalStorage.h"
9 #include "StorageDBThread.h"
10 #include "StorageIPC.h"
11 #include "StorageUtils.h"
13 #include "nsIEffectiveTLDService.h"
15 #include "nsPIDOMWindow.h"
16 #include "nsNetUtil.h"
17 #include "nsNetCID.h"
18 #include "nsPrintfCString.h"
19 #include "nsXULAppAPI.h"
20 #include "nsThreadUtils.h"
21 #include "nsIObserverService.h"
22 #include "mozilla/ipc/BackgroundChild.h"
23 #include "mozilla/ipc/PBackgroundChild.h"
24 #include "mozilla/Services.h"
25 #include "mozilla/StaticPrefs_dom.h"
26 #include "mozilla/dom/LocalStorageCommon.h"
28 namespace mozilla::dom {
30 using namespace StorageUtils;
32 LocalStorageManager* LocalStorageManager::sSelf = nullptr;
34 // static
35 uint32_t LocalStorageManager::GetOriginQuota() {
36 return StaticPrefs::dom_storage_default_quota() * 1024; // pref is in kBs
39 // static
40 uint32_t LocalStorageManager::GetSiteQuota() {
41 return std::max(StaticPrefs::dom_storage_default_quota(),
42 StaticPrefs::dom_storage_default_site_quota()) *
43 1024; // pref is in kBs
46 NS_IMPL_ISUPPORTS(LocalStorageManager, nsIDOMStorageManager,
47 nsILocalStorageManager)
49 LocalStorageManager::LocalStorageManager() : mCaches(8) {
50 MOZ_ASSERT(!NextGenLocalStorageEnabled());
52 StorageObserver* observer = StorageObserver::Self();
53 NS_ASSERTION(
54 observer,
55 "No StorageObserver, cannot observe private data delete notifications!");
57 if (observer) {
58 observer->AddSink(this);
61 NS_ASSERTION(!sSelf,
62 "Somebody is trying to "
63 "do_CreateInstance(\"@mozilla/dom/localStorage-manager;1\"");
64 sSelf = this;
66 if (!XRE_IsParentProcess()) {
67 // Do this only on the child process. The thread IPC bridge
68 // is also used to communicate chrome observer notifications.
69 // Note: must be called after we set sSelf
70 for (const uint32_t id : {0, 1}) {
71 StorageDBChild::GetOrCreate(id);
76 LocalStorageManager::~LocalStorageManager() {
77 StorageObserver* observer = StorageObserver::Self();
78 if (observer) {
79 observer->RemoveSink(this);
82 sSelf = nullptr;
85 // static
86 nsAutoCString LocalStorageManager::CreateOrigin(
87 const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix) {
88 // Note: some hard-coded sqlite statements are dependent on the format this
89 // method returns. Changing this without updating those sqlite statements
90 // will cause malfunction.
92 nsAutoCString scope;
93 scope.Append(aOriginSuffix);
94 scope.Append(':');
95 scope.Append(aOriginNoSuffix);
96 return scope;
99 LocalStorageCache* LocalStorageManager::GetCache(
100 const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix) {
101 CacheOriginHashtable* table = mCaches.GetOrInsertNew(aOriginSuffix);
102 LocalStorageCacheHashKey* entry = table->GetEntry(aOriginNoSuffix);
103 if (!entry) {
104 return nullptr;
107 return entry->cache();
110 already_AddRefed<StorageUsage> LocalStorageManager::GetOriginUsage(
111 const nsACString& aOriginNoSuffix, const uint32_t aPrivateBrowsingId) {
112 return do_AddRef(mUsages.LookupOrInsertWith(aOriginNoSuffix, [&] {
113 auto usage = MakeRefPtr<StorageUsage>(aOriginNoSuffix);
115 StorageDBChild* storageChild =
116 StorageDBChild::GetOrCreate(aPrivateBrowsingId);
117 if (storageChild) {
118 storageChild->AsyncGetUsage(usage);
121 return usage;
122 }));
125 already_AddRefed<LocalStorageCache> LocalStorageManager::PutCache(
126 const nsACString& aOriginSuffix, const nsACString& aOriginNoSuffix,
127 const nsACString& aQuotaKey, nsIPrincipal* aPrincipal) {
128 CacheOriginHashtable* table = mCaches.GetOrInsertNew(aOriginSuffix);
129 LocalStorageCacheHashKey* entry = table->PutEntry(aOriginNoSuffix);
130 RefPtr<LocalStorageCache> cache = entry->cache();
132 // Lifetime handled by the cache, do persist
133 cache->Init(this, true, aPrincipal, aQuotaKey);
134 return cache.forget();
137 void LocalStorageManager::DropCache(LocalStorageCache* aCache) {
138 if (!NS_IsMainThread()) {
139 NS_WARNING(
140 "StorageManager::DropCache called on a non-main thread, shutting "
141 "down?");
144 CacheOriginHashtable* table = mCaches.GetOrInsertNew(aCache->OriginSuffix());
145 table->RemoveEntry(aCache->OriginNoSuffix());
148 nsresult LocalStorageManager::GetStorageInternal(
149 CreateMode aCreateMode, mozIDOMWindow* aWindow, nsIPrincipal* aPrincipal,
150 nsIPrincipal* aStoragePrincipal, const nsAString& aDocumentURI,
151 bool aPrivate, Storage** aRetval) {
152 nsAutoCString originAttrSuffix;
153 nsAutoCString originKey;
154 nsAutoCString quotaKey;
156 aStoragePrincipal->OriginAttributesRef().CreateSuffix(originAttrSuffix);
158 nsresult rv = aStoragePrincipal->GetStorageOriginKey(originKey);
159 if (NS_WARN_IF(NS_FAILED(rv))) {
160 return NS_ERROR_NOT_AVAILABLE;
163 rv = aStoragePrincipal->GetLocalStorageQuotaKey(quotaKey);
164 if (NS_WARN_IF(NS_FAILED(rv))) {
165 return NS_ERROR_NOT_AVAILABLE;
168 RefPtr<LocalStorageCache> cache = GetCache(originAttrSuffix, originKey);
170 // Get or create a cache for the given scope
171 if (!cache) {
172 if (aCreateMode == CreateMode::UseIfExistsNeverCreate) {
173 *aRetval = nullptr;
174 return NS_OK;
177 if (aCreateMode == CreateMode::CreateIfShouldPreload) {
178 const uint32_t privateBrowsingId =
179 aStoragePrincipal->GetPrivateBrowsingId();
181 // This is a demand to just preload the cache, if the scope has
182 // no data stored, bypass creation and preload of the cache.
183 StorageDBChild* db = StorageDBChild::Get(privateBrowsingId);
184 if (db) {
185 if (!db->ShouldPreloadOrigin(LocalStorageManager::CreateOrigin(
186 originAttrSuffix, originKey))) {
187 return NS_OK;
189 } else {
190 if (originKey.EqualsLiteral("knalb.:about")) {
191 return NS_OK;
196 #if !defined(MOZ_WIDGET_ANDROID)
197 ::mozilla::ipc::PBackgroundChild* backgroundActor =
198 ::mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
199 if (NS_WARN_IF(!backgroundActor)) {
200 return NS_ERROR_FAILURE;
203 ::mozilla::ipc::PrincipalInfo principalInfo;
204 rv = mozilla::ipc::PrincipalToPrincipalInfo(aStoragePrincipal,
205 &principalInfo);
206 if (NS_WARN_IF(NS_FAILED(rv))) {
207 return rv;
210 uint32_t privateBrowsingId;
211 rv = aStoragePrincipal->GetPrivateBrowsingId(&privateBrowsingId);
212 if (NS_WARN_IF(NS_FAILED(rv))) {
213 return rv;
215 #endif
217 // There is always a single instance of a cache per scope
218 // in a single instance of a DOM storage manager.
219 cache = PutCache(originAttrSuffix, originKey, quotaKey, aStoragePrincipal);
221 #if !defined(MOZ_WIDGET_ANDROID)
222 LocalStorageCacheChild* actor = new LocalStorageCacheChild(cache);
224 MOZ_ALWAYS_TRUE(
225 backgroundActor->SendPBackgroundLocalStorageCacheConstructor(
226 actor, principalInfo, originKey, privateBrowsingId));
228 cache->SetActor(actor);
229 #endif
232 if (aRetval) {
233 nsCOMPtr<nsPIDOMWindowInner> inner = nsPIDOMWindowInner::From(aWindow);
235 RefPtr<Storage> storage =
236 new LocalStorage(inner, this, cache, aDocumentURI, aPrincipal,
237 aStoragePrincipal, aPrivate);
238 storage.forget(aRetval);
241 return NS_OK;
244 NS_IMETHODIMP
245 LocalStorageManager::PrecacheStorage(nsIPrincipal* aPrincipal,
246 nsIPrincipal* aStoragePrincipal,
247 Storage** aRetval) {
248 return GetStorageInternal(CreateMode::CreateIfShouldPreload, nullptr,
249 aPrincipal, aStoragePrincipal, u""_ns, false,
250 aRetval);
253 NS_IMETHODIMP
254 LocalStorageManager::CreateStorage(mozIDOMWindow* aWindow,
255 nsIPrincipal* aPrincipal,
256 nsIPrincipal* aStoragePrincipal,
257 const nsAString& aDocumentURI, bool aPrivate,
258 Storage** aRetval) {
259 return GetStorageInternal(CreateMode::CreateAlways, aWindow, aPrincipal,
260 aStoragePrincipal, aDocumentURI, aPrivate, aRetval);
263 NS_IMETHODIMP
264 LocalStorageManager::GetStorage(mozIDOMWindow* aWindow,
265 nsIPrincipal* aPrincipal,
266 nsIPrincipal* aStoragePrincipal, bool aPrivate,
267 Storage** aRetval) {
268 return GetStorageInternal(CreateMode::UseIfExistsNeverCreate, aWindow,
269 aPrincipal, aStoragePrincipal, u""_ns, aPrivate,
270 aRetval);
273 NS_IMETHODIMP
274 LocalStorageManager::CloneStorage(Storage* aStorage) {
275 // Cloning is supported only for sessionStorage
276 return NS_ERROR_NOT_IMPLEMENTED;
279 NS_IMETHODIMP
280 LocalStorageManager::CheckStorage(nsIPrincipal* aPrincipal, Storage* aStorage,
281 bool* aRetval) {
282 MOZ_ASSERT(NS_IsMainThread());
283 MOZ_ASSERT(aPrincipal);
284 MOZ_ASSERT(aStorage);
285 MOZ_ASSERT(aRetval);
287 // Only used by sessionStorage.
288 return NS_ERROR_NOT_IMPLEMENTED;
291 NS_IMETHODIMP
292 LocalStorageManager::GetNextGenLocalStorageEnabled(bool* aResult) {
293 MOZ_ASSERT(NS_IsMainThread());
294 MOZ_ASSERT(aResult);
296 *aResult = NextGenLocalStorageEnabled();
297 return NS_OK;
300 NS_IMETHODIMP
301 LocalStorageManager::Preload(nsIPrincipal* aPrincipal, JSContext* aContext,
302 Promise** _retval) {
303 MOZ_ASSERT(NS_IsMainThread());
304 MOZ_ASSERT(aPrincipal);
305 MOZ_ASSERT(_retval);
307 return NS_ERROR_NOT_IMPLEMENTED;
310 NS_IMETHODIMP
311 LocalStorageManager::IsPreloaded(nsIPrincipal* aPrincipal, JSContext* aContext,
312 Promise** _retval) {
313 MOZ_ASSERT(NS_IsMainThread());
314 MOZ_ASSERT(aPrincipal);
315 MOZ_ASSERT(_retval);
317 return NS_ERROR_NOT_IMPLEMENTED;
320 NS_IMETHODIMP
321 LocalStorageManager::GetState(nsIPrincipal* aPrincipal, JSContext* aContext,
322 Promise** _retval) {
323 MOZ_ASSERT(NS_IsMainThread());
324 MOZ_ASSERT(aPrincipal);
325 MOZ_ASSERT(_retval);
327 return NS_ERROR_NOT_IMPLEMENTED;
330 void LocalStorageManager::ClearCaches(uint32_t aUnloadFlags,
331 const OriginAttributesPattern& aPattern,
332 const nsACString& aOriginScope) {
333 for (const auto& cacheEntry : mCaches) {
334 OriginAttributes oa;
335 DebugOnly<bool> rv = oa.PopulateFromSuffix(cacheEntry.GetKey());
336 MOZ_ASSERT(rv);
337 if (!aPattern.Matches(oa)) {
338 // This table doesn't match the given origin attributes pattern
339 continue;
342 CacheOriginHashtable* table = cacheEntry.GetWeak();
344 for (auto iter2 = table->Iter(); !iter2.Done(); iter2.Next()) {
345 LocalStorageCache* cache = iter2.Get()->cache();
347 if (aOriginScope.IsEmpty() ||
348 StringBeginsWith(cache->OriginNoSuffix(), aOriginScope)) {
349 cache->UnloadItems(aUnloadFlags);
355 nsresult LocalStorageManager::Observe(const char* aTopic,
356 const nsAString& aOriginAttributesPattern,
357 const nsACString& aOriginScope) {
358 OriginAttributesPattern pattern;
359 if (!pattern.Init(aOriginAttributesPattern)) {
360 NS_ERROR("Cannot parse origin attributes pattern");
361 return NS_ERROR_FAILURE;
364 // Clear everything, caches + database
365 if (!strcmp(aTopic, "cookie-cleared")) {
366 ClearCaches(LocalStorageCache::kUnloadComplete, pattern, ""_ns);
367 return NS_OK;
370 // Clear everything, caches + database
371 if (!strcmp(aTopic, "extension:purge-localStorage-caches")) {
372 ClearCaches(LocalStorageCache::kUnloadComplete, pattern, aOriginScope);
373 return NS_OK;
376 if (!strcmp(aTopic, "browser:purge-sessionStorage")) {
377 // This is only meant for SessionStorageManager.
378 return NS_OK;
381 // Clear from caches everything that has been stored
382 // while in session-only mode
383 if (!strcmp(aTopic, "session-only-cleared")) {
384 ClearCaches(LocalStorageCache::kUnloadSession, pattern, aOriginScope);
385 return NS_OK;
388 // Clear all private-browsing caches
389 if (!strcmp(aTopic, "private-browsing-data-cleared")) {
390 ClearCaches(LocalStorageCache::kUnloadComplete, pattern, ""_ns);
391 return NS_OK;
394 // Clear localStorage data belonging to an origin pattern
395 if (!strcmp(aTopic, "clear-origin-attributes-data") ||
396 !strcmp(aTopic, "dom-storage:clear-origin-attributes-data")) {
397 ClearCaches(LocalStorageCache::kUnloadComplete, pattern, ""_ns);
398 return NS_OK;
401 if (!strcmp(aTopic, "profile-change")) {
402 // For case caches are still referenced - clear them completely
403 ClearCaches(LocalStorageCache::kUnloadComplete, pattern, ""_ns);
404 mCaches.Clear();
405 return NS_OK;
408 #ifdef DOM_STORAGE_TESTS
409 if (!strcmp(aTopic, "test-reload")) {
410 // This immediately completely reloads all caches from the database.
411 ClearCaches(LocalStorageCache::kTestReload, pattern, ""_ns);
412 return NS_OK;
415 if (!strcmp(aTopic, "test-flushed")) {
416 if (!XRE_IsParentProcess()) {
417 nsCOMPtr<nsIObserverService> obs =
418 mozilla::services::GetObserverService();
419 if (obs) {
420 obs->NotifyObservers(nullptr, "domstorage-test-flushed", nullptr);
424 return NS_OK;
426 #endif
428 NS_ERROR("Unexpected topic");
429 return NS_ERROR_UNEXPECTED;
432 // static
433 LocalStorageManager* LocalStorageManager::Self() {
434 MOZ_ASSERT(!NextGenLocalStorageEnabled());
436 return sSelf;
439 LocalStorageManager* LocalStorageManager::Ensure() {
440 MOZ_ASSERT(!NextGenLocalStorageEnabled());
442 if (sSelf) {
443 return sSelf;
446 // Cause sSelf to be populated.
447 nsCOMPtr<nsIDOMStorageManager> initializer =
448 do_GetService("@mozilla.org/dom/localStorage-manager;1");
449 MOZ_ASSERT(sSelf, "Didn't initialize?");
451 return sSelf;
454 } // namespace mozilla::dom