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 "IDBFactory.h"
9 #include "BackgroundChildImpl.h"
10 #include "IDBRequest.h"
11 #include "IndexedDatabaseManager.h"
12 #include "mozilla/BasePrincipal.h"
13 #include "mozilla/ErrorResult.h"
14 #include "mozilla/Preferences.h"
15 #include "mozilla/dom/BindingDeclarations.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/IDBFactoryBinding.h"
18 #include "mozilla/dom/quota/PersistenceType.h"
19 #include "mozilla/dom/quota/QuotaManager.h"
20 #include "mozilla/dom/BrowserChild.h"
21 #include "mozilla/dom/WorkerPrivate.h"
22 #include "mozilla/ipc/BackgroundChild.h"
23 #include "mozilla/ipc/BackgroundUtils.h"
24 #include "mozilla/ipc/PBackground.h"
25 #include "mozilla/ipc/PBackgroundChild.h"
26 #include "mozilla/StaticPrefs_dom.h"
27 #include "mozilla/StorageAccess.h"
28 #include "mozilla/Telemetry.h"
29 #include "nsAboutProtocolUtils.h"
30 #include "nsContentUtils.h"
31 #include "nsGlobalWindowInner.h"
32 #include "nsIAboutModule.h"
33 #include "nsILoadContext.h"
35 #include "nsIUUIDGenerator.h"
36 #include "nsIWebNavigation.h"
37 #include "nsNetUtil.h"
38 #include "nsSandboxFlags.h"
39 #include "nsServiceManagerUtils.h"
40 #include "ProfilerHelpers.h"
41 #include "ReportInternalError.h"
42 #include "ThreadLocal.h"
44 // Include this last to avoid path problems on Windows.
45 #include "ActorsChild.h"
47 namespace mozilla::dom
{
49 using namespace mozilla::dom::indexedDB
;
50 using namespace mozilla::dom::quota
;
51 using namespace mozilla::ipc
;
55 Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT
IdentifyPrincipalType(
56 const mozilla::ipc::PrincipalInfo
& aPrincipalInfo
) {
57 switch (aPrincipalInfo
.type()) {
58 case PrincipalInfo::TSystemPrincipalInfo
:
59 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::system
;
60 case PrincipalInfo::TContentPrincipalInfo
: {
61 const ContentPrincipalInfo
& info
=
62 aPrincipalInfo
.get_ContentPrincipalInfo();
66 if (NS_WARN_IF(NS_FAILED(NS_NewURI(getter_AddRefs(uri
), info
.spec())))) {
67 // This could be discriminated as an extra error value, but this is
68 // extremely unlikely to fail, so we just misuse ContentOther
69 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
73 // TODO Are there constants defined for the schemes somewhere?
74 if (uri
->SchemeIs("file")) {
75 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
78 if (uri
->SchemeIs("http") || uri
->SchemeIs("https")) {
79 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
82 if (uri
->SchemeIs("moz-extension")) {
83 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
86 if (uri
->SchemeIs("about")) {
87 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
90 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::
93 case PrincipalInfo::TExpandedPrincipalInfo
:
94 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::expanded
;
96 return Telemetry::LABELS_IDB_CUSTOM_OPEN_WITH_OPTIONS_COUNT::other
;
102 struct IDBFactory::PendingRequestInfo
{
103 RefPtr
<IDBOpenDBRequest
> mRequest
;
104 FactoryRequestParams mParams
;
106 PendingRequestInfo(IDBOpenDBRequest
* aRequest
,
107 const FactoryRequestParams
& aParams
)
108 : mRequest(aRequest
), mParams(aParams
) {
109 MOZ_ASSERT(aRequest
);
110 MOZ_ASSERT(aParams
.type() != FactoryRequestParams::T__None
);
114 IDBFactory::IDBFactory(const IDBFactoryGuard
&)
115 : mBackgroundActor(nullptr),
117 mActiveTransactionCount(0),
118 mActiveDatabaseCount(0),
119 mBackgroundActorFailed(false),
120 mPrivateBrowsingMode(false) {
121 AssertIsOnOwningThread();
124 IDBFactory::~IDBFactory() {
125 MOZ_ASSERT_IF(mBackgroundActorFailed
, !mBackgroundActor
);
127 if (mBackgroundActor
) {
128 mBackgroundActor
->SendDeleteMeInternal();
129 MOZ_ASSERT(!mBackgroundActor
, "SendDeleteMeInternal should have cleared!");
134 Result
<RefPtr
<IDBFactory
>, nsresult
> IDBFactory::CreateForWindow(
135 nsPIDOMWindowInner
* aWindow
) {
136 MOZ_ASSERT(NS_IsMainThread());
139 nsCOMPtr
<nsIPrincipal
> principal
;
140 nsresult rv
= AllowedForWindowInternal(aWindow
, &principal
);
142 if (rv
== NS_ERROR_DOM_NOT_SUPPORTED_ERR
) {
143 NS_WARNING("IndexedDB is not permitted in a third-party window.");
144 return RefPtr
<IDBFactory
>{};
147 if (NS_WARN_IF(NS_FAILED(rv
))) {
148 if (rv
== NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
) {
149 IDB_REPORT_INTERNAL_ERR();
154 MOZ_ASSERT(principal
);
156 auto principalInfo
= MakeUnique
<PrincipalInfo
>();
157 rv
= PrincipalToPrincipalInfo(principal
, principalInfo
.get());
158 if (NS_WARN_IF(NS_FAILED(rv
))) {
159 IDB_REPORT_INTERNAL_ERR();
160 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
163 MOZ_ASSERT(principalInfo
->type() == PrincipalInfo::TContentPrincipalInfo
||
164 principalInfo
->type() == PrincipalInfo::TSystemPrincipalInfo
);
166 if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(*principalInfo
))) {
167 IDB_REPORT_INTERNAL_ERR();
168 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
171 nsCOMPtr
<nsIWebNavigation
> webNav
= do_GetInterface(aWindow
);
172 nsCOMPtr
<nsILoadContext
> loadContext
= do_QueryInterface(webNav
);
174 auto factory
= MakeRefPtr
<IDBFactory
>(IDBFactoryGuard
{});
175 factory
->mPrincipalInfo
= std::move(principalInfo
);
177 factory
->BindToOwner(aWindow
->AsGlobal());
179 factory
->mBrowserChild
= BrowserChild::GetFrom(aWindow
);
180 factory
->mEventTarget
=
181 nsGlobalWindowInner::Cast(aWindow
)->SerialEventTarget();
182 factory
->mInnerWindowID
= aWindow
->WindowID();
183 factory
->mPrivateBrowsingMode
=
184 loadContext
&& loadContext
->UsePrivateBrowsing();
190 Result
<RefPtr
<IDBFactory
>, nsresult
> IDBFactory::CreateForMainThreadJS(
191 nsIGlobalObject
* aGlobal
) {
192 MOZ_ASSERT(NS_IsMainThread());
195 nsCOMPtr
<nsIScriptObjectPrincipal
> sop
= do_QueryInterface(aGlobal
);
196 if (NS_WARN_IF(!sop
)) {
197 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
200 auto principalInfo
= MakeUnique
<PrincipalInfo
>();
201 nsIPrincipal
* principal
= sop
->GetEffectiveStoragePrincipal();
202 MOZ_ASSERT(principal
);
204 if (!AllowedForPrincipal(principal
, &isSystem
)) {
205 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
208 nsresult rv
= PrincipalToPrincipalInfo(principal
, principalInfo
.get());
209 if (NS_WARN_IF(NS_FAILED(rv
))) {
213 if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(*principalInfo
))) {
214 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
217 return CreateForMainThreadJSInternal(aGlobal
, std::move(principalInfo
));
221 Result
<RefPtr
<IDBFactory
>, nsresult
> IDBFactory::CreateForWorker(
222 nsIGlobalObject
* aGlobal
, const PrincipalInfo
& aPrincipalInfo
,
223 uint64_t aInnerWindowID
) {
224 MOZ_ASSERT(!NS_IsMainThread());
226 MOZ_ASSERT(aPrincipalInfo
.type() != PrincipalInfo::T__None
);
228 return CreateInternal(aGlobal
, MakeUnique
<PrincipalInfo
>(aPrincipalInfo
),
233 Result
<RefPtr
<IDBFactory
>, nsresult
> IDBFactory::CreateForMainThreadJSInternal(
234 nsIGlobalObject
* aGlobal
, UniquePtr
<PrincipalInfo
> aPrincipalInfo
) {
235 MOZ_ASSERT(NS_IsMainThread());
237 MOZ_ASSERT(aPrincipalInfo
);
239 IndexedDatabaseManager
* mgr
= IndexedDatabaseManager::GetOrCreate();
240 if (NS_WARN_IF(!mgr
)) {
241 IDB_REPORT_INTERNAL_ERR();
242 return Err(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
245 return CreateInternal(aGlobal
, std::move(aPrincipalInfo
),
246 /* aInnerWindowID */ 0);
250 Result
<RefPtr
<IDBFactory
>, nsresult
> IDBFactory::CreateInternal(
251 nsIGlobalObject
* aGlobal
, UniquePtr
<PrincipalInfo
> aPrincipalInfo
,
252 uint64_t aInnerWindowID
) {
254 MOZ_ASSERT(aPrincipalInfo
);
255 MOZ_ASSERT(aPrincipalInfo
->type() != PrincipalInfo::T__None
);
257 if (aPrincipalInfo
->type() != PrincipalInfo::TContentPrincipalInfo
&&
258 aPrincipalInfo
->type() != PrincipalInfo::TSystemPrincipalInfo
) {
259 NS_WARNING("IndexedDB not allowed for this principal!");
260 return RefPtr
<IDBFactory
>{};
263 auto factory
= MakeRefPtr
<IDBFactory
>(IDBFactoryGuard
{});
264 factory
->mPrincipalInfo
= std::move(aPrincipalInfo
);
265 factory
->BindToOwner(aGlobal
);
266 factory
->mEventTarget
= GetCurrentSerialEventTarget();
267 factory
->mInnerWindowID
= aInnerWindowID
;
273 bool IDBFactory::AllowedForWindow(nsPIDOMWindowInner
* aWindow
) {
274 MOZ_ASSERT(NS_IsMainThread());
277 return !NS_WARN_IF(NS_FAILED(AllowedForWindowInternal(aWindow
, nullptr)));
281 nsresult
IDBFactory::AllowedForWindowInternal(
282 nsPIDOMWindowInner
* aWindow
, nsCOMPtr
<nsIPrincipal
>* aPrincipal
) {
283 MOZ_ASSERT(NS_IsMainThread());
286 if (NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate())) {
287 return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
;
290 StorageAccess access
= StorageAllowedForWindow(aWindow
);
292 // the factory callsite records whether the browser is in private browsing.
293 // and thus we don't have to respect that setting here. IndexedDB has no
294 // concept of session-local storage, and thus ignores it.
295 if (access
== StorageAccess::eDeny
) {
296 return NS_ERROR_DOM_SECURITY_ERR
;
299 if (ShouldPartitionStorage(access
) &&
300 !StoragePartitioningEnabled(
301 access
, aWindow
->GetExtantDoc()->CookieJarSettings())) {
302 return NS_ERROR_DOM_SECURITY_ERR
;
305 nsCOMPtr
<nsIScriptObjectPrincipal
> sop
= do_QueryInterface(aWindow
);
308 nsCOMPtr
<nsIPrincipal
> principal
= sop
->GetEffectiveStoragePrincipal();
309 if (NS_WARN_IF(!principal
)) {
310 return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
;
313 if (principal
->IsSystemPrincipal()) {
314 *aPrincipal
= std::move(principal
);
318 // About URIs shouldn't be able to access IndexedDB unless they have the
319 // nsIAboutModule::ENABLE_INDEXED_DB flag set on them.
321 if (principal
->SchemeIs("about")) {
323 if (NS_SUCCEEDED(principal
->GetAboutModuleFlags(&flags
))) {
324 if (!(flags
& nsIAboutModule::ENABLE_INDEXED_DB
)) {
325 return NS_ERROR_DOM_NOT_SUPPORTED_ERR
;
328 return NS_ERROR_DOM_NOT_SUPPORTED_ERR
;
333 *aPrincipal
= std::move(principal
);
339 bool IDBFactory::AllowedForPrincipal(nsIPrincipal
* aPrincipal
,
340 bool* aIsSystemPrincipal
) {
341 MOZ_ASSERT(NS_IsMainThread());
342 MOZ_ASSERT(aPrincipal
);
344 if (NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate())) {
348 if (aPrincipal
->IsSystemPrincipal()) {
349 if (aIsSystemPrincipal
) {
350 *aIsSystemPrincipal
= true;
355 if (aIsSystemPrincipal
) {
356 *aIsSystemPrincipal
= false;
359 return !aPrincipal
->GetIsNullPrincipal();
362 void IDBFactory::UpdateActiveTransactionCount(int32_t aDelta
) {
363 AssertIsOnOwningThread();
364 MOZ_DIAGNOSTIC_ASSERT(aDelta
> 0 || (mActiveTransactionCount
+ aDelta
) <
365 mActiveTransactionCount
);
366 mActiveTransactionCount
+= aDelta
;
369 void IDBFactory::UpdateActiveDatabaseCount(int32_t aDelta
) {
370 AssertIsOnOwningThread();
371 MOZ_DIAGNOSTIC_ASSERT(aDelta
> 0 ||
372 (mActiveDatabaseCount
+ aDelta
) < mActiveDatabaseCount
);
373 mActiveDatabaseCount
+= aDelta
;
376 GetOwner()->UpdateActiveIndexedDBDatabaseCount(aDelta
);
380 bool IDBFactory::IsChrome() const {
381 AssertIsOnOwningThread();
382 MOZ_ASSERT(mPrincipalInfo
);
384 return mPrincipalInfo
->type() == PrincipalInfo::TSystemPrincipalInfo
;
387 RefPtr
<IDBOpenDBRequest
> IDBFactory::Open(JSContext
* aCx
,
388 const nsAString
& aName
,
390 CallerType aCallerType
,
392 return OpenInternal(aCx
,
393 /* aPrincipal */ nullptr, aName
,
394 Optional
<uint64_t>(aVersion
),
395 /* aDeleting */ false, aCallerType
, aRv
);
398 RefPtr
<IDBOpenDBRequest
> IDBFactory::Open(JSContext
* aCx
,
399 const nsAString
& aName
,
400 const IDBOpenDBOptions
& aOptions
,
401 CallerType aCallerType
,
403 // This overload is nonstandard, see bug 1275496.
404 // Ignore calls with empty options for telemetry of usage count.
405 // Unfortunately, we cannot distinguish between the use of the method with
406 // only a single argument (which actually is a standard overload we don't want
407 // to count) an empty dictionary passed explicitly (which is the custom
408 // overload we would like to count). However, we assume that the latter is so
409 // rare that it can be neglected.
410 if (aOptions
.IsAnyMemberPresent()) {
411 Telemetry::AccumulateCategorical(IdentifyPrincipalType(*mPrincipalInfo
));
414 return OpenInternal(aCx
,
415 /* aPrincipal */ nullptr, aName
, aOptions
.mVersion
,
416 /* aDeleting */ false, aCallerType
, aRv
);
419 RefPtr
<IDBOpenDBRequest
> IDBFactory::DeleteDatabase(
420 JSContext
* aCx
, const nsAString
& aName
, const IDBOpenDBOptions
& aOptions
,
421 CallerType aCallerType
, ErrorResult
& aRv
) {
422 return OpenInternal(aCx
,
423 /* aPrincipal */ nullptr, aName
, Optional
<uint64_t>(),
424 /* aDeleting */ true, aCallerType
, aRv
);
427 int16_t IDBFactory::Cmp(JSContext
* aCx
, JS::Handle
<JS::Value
> aFirst
,
428 JS::Handle
<JS::Value
> aSecond
, ErrorResult
& aRv
) {
430 auto result
= first
.SetFromJSVal(aCx
, aFirst
);
431 if (result
.isErr()) {
432 aRv
= result
.unwrapErr().ExtractErrorResult(
433 InvalidMapsTo
<NS_ERROR_DOM_INDEXEDDB_DATA_ERR
>);
437 result
= second
.SetFromJSVal(aCx
, aSecond
);
438 if (result
.isErr()) {
439 aRv
= result
.unwrapErr().ExtractErrorResult(
440 InvalidMapsTo
<NS_ERROR_DOM_INDEXEDDB_DATA_ERR
>);
444 if (first
.IsUnset() || second
.IsUnset()) {
445 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR
);
449 return Key::CompareKeys(first
, second
);
452 RefPtr
<IDBOpenDBRequest
> IDBFactory::OpenForPrincipal(
453 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
454 uint64_t aVersion
, SystemCallerGuarantee aGuarantee
, ErrorResult
& aRv
) {
455 MOZ_ASSERT(aPrincipal
);
456 if (!NS_IsMainThread()) {
458 "Figure out security checks for workers! What's this aPrincipal "
459 "we have on a worker thread?");
462 return OpenInternal(aCx
, aPrincipal
, aName
, Optional
<uint64_t>(aVersion
),
463 /* aDeleting */ false, aGuarantee
, aRv
);
466 RefPtr
<IDBOpenDBRequest
> IDBFactory::OpenForPrincipal(
467 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
468 const IDBOpenDBOptions
& aOptions
, SystemCallerGuarantee aGuarantee
,
470 MOZ_ASSERT(aPrincipal
);
471 if (!NS_IsMainThread()) {
473 "Figure out security checks for workers! What's this aPrincipal "
474 "we have on a worker thread?");
477 return OpenInternal(aCx
, aPrincipal
, aName
, aOptions
.mVersion
,
478 /* aDeleting */ false, aGuarantee
, aRv
);
481 RefPtr
<IDBOpenDBRequest
> IDBFactory::DeleteForPrincipal(
482 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
483 const IDBOpenDBOptions
& aOptions
, SystemCallerGuarantee aGuarantee
,
485 MOZ_ASSERT(aPrincipal
);
486 if (!NS_IsMainThread()) {
488 "Figure out security checks for workers! What's this aPrincipal "
489 "we have on a worker thread?");
492 return OpenInternal(aCx
, aPrincipal
, aName
, Optional
<uint64_t>(),
493 /* aDeleting */ true, aGuarantee
, aRv
);
496 RefPtr
<IDBOpenDBRequest
> IDBFactory::OpenInternal(
497 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
498 const Optional
<uint64_t>& aVersion
, bool aDeleting
, CallerType aCallerType
,
500 if (NS_WARN_IF(!GetOwnerGlobal())) {
501 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
505 CommonFactoryRequestParams commonParams
;
507 PrincipalInfo
& principalInfo
= commonParams
.principalInfo();
510 if (!NS_IsMainThread()) {
512 "Figure out security checks for workers! What's this "
513 "aPrincipal we have on a worker thread?");
515 MOZ_ASSERT(aCallerType
== CallerType::System
);
516 MOZ_DIAGNOSTIC_ASSERT(mPrivateBrowsingMode
==
517 (aPrincipal
->GetPrivateBrowsingId() > 0));
520 NS_FAILED(PrincipalToPrincipalInfo(aPrincipal
, &principalInfo
)))) {
521 IDB_REPORT_INTERNAL_ERR();
522 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
526 if (principalInfo
.type() != PrincipalInfo::TContentPrincipalInfo
&&
527 principalInfo
.type() != PrincipalInfo::TSystemPrincipalInfo
) {
528 IDB_REPORT_INTERNAL_ERR();
529 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
533 if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(principalInfo
))) {
534 IDB_REPORT_INTERNAL_ERR();
535 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
539 if (GetOwnerGlobal()->GetStorageAccess() ==
540 StorageAccess::ePrivateBrowsing
) {
541 if (NS_IsMainThread()) {
543 GetOwnerGlobal()->GetGlobalJSObject(),
545 ? eUseCounter_custom_PrivateBrowsingIDBFactoryOpen
546 : eUseCounter_custom_PrivateBrowsingIDBFactoryDeleteDatabase
);
549 aDeleting
? UseCounterWorker::Custom_PrivateBrowsingIDBFactoryOpen
551 Custom_PrivateBrowsingIDBFactoryDeleteDatabase
);
554 principalInfo
= *mPrincipalInfo
;
557 uint64_t version
= 0;
558 if (!aDeleting
&& aVersion
.WasPassed()) {
559 if (aVersion
.Value() < 1) {
560 aRv
.ThrowTypeError("0 (Zero) is not a valid database version.");
563 version
= aVersion
.Value();
566 // Nothing can be done here if we have previously failed to create a
568 if (mBackgroundActorFailed
) {
569 IDB_REPORT_INTERNAL_ERR();
570 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
574 PersistenceType persistenceType
;
576 bool isInternal
= principalInfo
.type() == PrincipalInfo::TSystemPrincipalInfo
;
578 principalInfo
.type() == PrincipalInfo::TContentPrincipalInfo
) {
580 principalInfo
.get_ContentPrincipalInfo().originNoSuffix();
581 isInternal
= QuotaManager::IsOriginInternal(origin
);
584 const bool isPrivate
=
585 principalInfo
.type() == PrincipalInfo::TContentPrincipalInfo
&&
586 principalInfo
.get_ContentPrincipalInfo().attrs().mPrivateBrowsingId
> 0;
589 // Chrome privilege and internal origins always get persistent storage.
590 persistenceType
= PERSISTENCE_TYPE_PERSISTENT
;
591 } else if (isPrivate
) {
592 persistenceType
= PERSISTENCE_TYPE_PRIVATE
;
594 persistenceType
= PERSISTENCE_TYPE_DEFAULT
;
597 DatabaseMetadata
& metadata
= commonParams
.metadata();
598 metadata
.name() = aName
;
599 metadata
.persistenceType() = persistenceType
;
601 FactoryRequestParams params
;
603 metadata
.version() = 0;
604 params
= DeleteDatabaseRequestParams(commonParams
);
606 metadata
.version() = version
;
607 params
= OpenDatabaseRequestParams(commonParams
);
610 if (!mBackgroundActor
) {
611 BackgroundChildImpl::ThreadLocal
* threadLocal
=
612 BackgroundChildImpl::GetThreadLocalForCurrentThread();
614 UniquePtr
<ThreadLocal
> newIDBThreadLocal
;
615 ThreadLocal
* idbThreadLocal
;
617 if (threadLocal
&& threadLocal
->mIndexedDBThreadLocal
) {
618 idbThreadLocal
= threadLocal
->mIndexedDBThreadLocal
.get();
620 nsCOMPtr
<nsIUUIDGenerator
> uuidGen
=
621 do_GetService("@mozilla.org/uuid-generator;1");
625 MOZ_ALWAYS_SUCCEEDS(uuidGen
->GenerateUUIDInPlace(&id
));
627 newIDBThreadLocal
= WrapUnique(new ThreadLocal(id
));
628 idbThreadLocal
= newIDBThreadLocal
.get();
631 PBackgroundChild
* backgroundActor
=
632 BackgroundChild::GetOrCreateForCurrentThread();
633 if (NS_WARN_IF(!backgroundActor
)) {
634 IDB_REPORT_INTERNAL_ERR();
635 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
640 BackgroundFactoryChild
* actor
= new BackgroundFactoryChild(*this);
642 mBackgroundActor
= static_cast<BackgroundFactoryChild
*>(
643 backgroundActor
->SendPBackgroundIDBFactoryConstructor(
644 actor
, idbThreadLocal
->GetLoggingInfo()));
646 if (NS_WARN_IF(!mBackgroundActor
)) {
647 mBackgroundActorFailed
= true;
648 IDB_REPORT_INTERNAL_ERR();
649 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
654 if (newIDBThreadLocal
) {
656 threadLocal
= BackgroundChildImpl::GetThreadLocalForCurrentThread();
658 MOZ_ASSERT(threadLocal
);
659 MOZ_ASSERT(!threadLocal
->mIndexedDBThreadLocal
);
661 threadLocal
->mIndexedDBThreadLocal
= std::move(newIDBThreadLocal
);
665 RefPtr
<IDBOpenDBRequest
> request
= IDBOpenDBRequest::Create(
666 aCx
, SafeRefPtr
{this, AcquireStrongRefFromRawPtr
{}}, GetOwnerGlobal());
668 MOZ_ASSERT(!NS_IsMainThread());
669 aRv
.ThrowUncatchableException();
676 IDB_LOG_MARK_CHILD_REQUEST(
677 "indexedDB.deleteDatabase(\"%s\")", "IDBFactory.deleteDatabase(%.0s)",
678 request
->LoggingSerialNumber(), NS_ConvertUTF16toUTF8(aName
).get());
680 IDB_LOG_MARK_CHILD_REQUEST(
681 "indexedDB.open(\"%s\", %s)", "IDBFactory.open(%.0s%.0s)",
682 request
->LoggingSerialNumber(), NS_ConvertUTF16toUTF8(aName
).get(),
683 IDB_LOG_STRINGIFY(aVersion
));
686 nsresult rv
= InitiateRequest(WrapNotNull(request
), params
);
687 if (NS_WARN_IF(NS_FAILED(rv
))) {
688 IDB_REPORT_INTERNAL_ERR();
689 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
696 nsresult
IDBFactory::InitiateRequest(
697 const NotNull
<RefPtr
<IDBOpenDBRequest
>>& aRequest
,
698 const FactoryRequestParams
& aParams
) {
699 MOZ_ASSERT(mBackgroundActor
);
700 MOZ_ASSERT(!mBackgroundActorFailed
);
703 uint64_t requestedVersion
;
705 switch (aParams
.type()) {
706 case FactoryRequestParams::TDeleteDatabaseRequestParams
: {
707 const DatabaseMetadata
& metadata
=
708 aParams
.get_DeleteDatabaseRequestParams().commonParams().metadata();
710 requestedVersion
= metadata
.version();
714 case FactoryRequestParams::TOpenDatabaseRequestParams
: {
715 const DatabaseMetadata
& metadata
=
716 aParams
.get_OpenDatabaseRequestParams().commonParams().metadata();
718 requestedVersion
= metadata
.version();
723 MOZ_CRASH("Should never get here!");
726 auto actor
= new BackgroundFactoryRequestChild(
727 SafeRefPtr
{this, AcquireStrongRefFromRawPtr
{}}, aRequest
, deleting
,
730 if (!mBackgroundActor
->SendPBackgroundIDBFactoryRequestConstructor(actor
,
732 aRequest
->DispatchNonTransactionError(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
);
733 return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR
;
739 NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBFactory
)
740 NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBFactory
)
742 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFactory
)
743 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
744 NS_INTERFACE_MAP_ENTRY(nsISupports
)
747 NS_IMPL_CYCLE_COLLECTION_CLASS(IDBFactory
)
749 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBFactory
)
750 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBrowserChild
)
751 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventTarget
)
752 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
754 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBFactory
)
755 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
756 NS_IMPL_CYCLE_COLLECTION_UNLINK(mBrowserChild
)
757 NS_IMPL_CYCLE_COLLECTION_UNLINK(mEventTarget
)
758 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
760 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBFactory
)
761 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
762 NS_IMPL_CYCLE_COLLECTION_TRACE_END
764 JSObject
* IDBFactory::WrapObject(JSContext
* aCx
,
765 JS::Handle
<JSObject
*> aGivenProto
) {
766 return IDBFactory_Binding::Wrap(aCx
, this, aGivenProto
);
769 } // namespace mozilla::dom