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 #ifndef mozilla_dom_idbfactory_h__
8 #define mozilla_dom_idbfactory_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/BindingDeclarations.h"
12 #include "mozilla/UniquePtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsISupports.h"
18 #include "nsWrapperCache.h"
20 class nsIGlobalObject
;
22 class nsISerialEventTarget
;
23 class nsPIDOMWindowInner
;
31 class PBackgroundChild
;
38 struct IDBOpenDBOptions
;
39 class IDBOpenDBRequest
;
43 enum class CallerType
: uint32_t;
46 class BackgroundFactoryChild
;
47 class FactoryRequestParams
;
49 } // namespace indexedDB
51 class IDBFactory final
: public nsISupports
, public nsWrapperCache
{
52 using PBackgroundChild
= mozilla::ipc::PBackgroundChild
;
53 using PrincipalInfo
= mozilla::ipc::PrincipalInfo
;
55 class BackgroundCreateCallback
;
56 struct PendingRequestInfo
;
57 struct IDBFactoryGuard
{};
59 UniquePtr
<PrincipalInfo
> mPrincipalInfo
;
61 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
63 // This will only be set if the factory belongs to a window in a child
65 RefPtr
<BrowserChild
> mBrowserChild
;
67 indexedDB::BackgroundFactoryChild
* mBackgroundActor
;
69 // It is either set to a DocGroup-specific EventTarget if created by
70 // CreateForWindow() or set to GetCurrentSerialEventTarget() otherwise.
71 nsCOMPtr
<nsISerialEventTarget
> mEventTarget
;
73 uint64_t mInnerWindowID
;
74 uint32_t mActiveTransactionCount
;
75 uint32_t mActiveDatabaseCount
;
77 bool mBackgroundActorFailed
;
78 bool mPrivateBrowsingMode
;
81 explicit IDBFactory(const IDBFactoryGuard
&);
83 static Result
<RefPtr
<IDBFactory
>, nsresult
> CreateForWindow(
84 nsPIDOMWindowInner
* aWindow
);
86 static Result
<RefPtr
<IDBFactory
>, nsresult
> CreateForMainThreadJS(
87 nsIGlobalObject
* aGlobal
);
89 static Result
<RefPtr
<IDBFactory
>, nsresult
> CreateForWorker(
90 nsIGlobalObject
* aGlobal
, const PrincipalInfo
& aPrincipalInfo
,
91 uint64_t aInnerWindowID
);
93 static bool AllowedForWindow(nsPIDOMWindowInner
* aWindow
);
95 static bool AllowedForPrincipal(nsIPrincipal
* aPrincipal
,
96 bool* aIsSystemPrincipal
= nullptr);
98 void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(IDBFactory
); }
100 nsISerialEventTarget
* EventTarget() const {
101 AssertIsOnOwningThread();
102 MOZ_RELEASE_ASSERT(mEventTarget
);
106 void ClearBackgroundActor() {
107 AssertIsOnOwningThread();
109 mBackgroundActor
= nullptr;
112 // Increase/Decrease the number of active transactions for the decision
113 // making of preemption and throttling.
114 // Note: If the state of its actor is not committed or aborted, it could block
115 // IDB operations in other window.
116 void UpdateActiveTransactionCount(int32_t aDelta
);
118 // Increase/Decrease the number of active databases and IDBOpenRequests for
119 // the decision making of preemption and throttling.
120 // Note: A non-closed database or a pending IDBOpenRequest could block
121 // IDB operations in other window.
122 void UpdateActiveDatabaseCount(int32_t aDelta
);
124 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
126 BrowserChild
* GetBrowserChild() const { return mBrowserChild
; }
128 PrincipalInfo
* GetPrincipalInfo() const {
129 AssertIsOnOwningThread();
131 return mPrincipalInfo
.get();
134 uint64_t InnerWindowID() const {
135 AssertIsOnOwningThread();
137 return mInnerWindowID
;
140 bool IsChrome() const;
142 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> Open(JSContext
* aCx
,
143 const nsAString
& aName
,
145 CallerType aCallerType
,
148 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> Open(JSContext
* aCx
,
149 const nsAString
& aName
,
150 const IDBOpenDBOptions
& aOptions
,
151 CallerType aCallerType
,
154 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> DeleteDatabase(
155 JSContext
* aCx
, const nsAString
& aName
, const IDBOpenDBOptions
& aOptions
,
156 CallerType aCallerType
, ErrorResult
& aRv
);
158 int16_t Cmp(JSContext
* aCx
, JS::Handle
<JS::Value
> aFirst
,
159 JS::Handle
<JS::Value
> aSecond
, ErrorResult
& aRv
);
161 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> OpenForPrincipal(
162 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
163 uint64_t aVersion
, SystemCallerGuarantee
, ErrorResult
& aRv
);
165 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> OpenForPrincipal(
166 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
167 const IDBOpenDBOptions
& aOptions
, SystemCallerGuarantee
,
170 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> DeleteForPrincipal(
171 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
172 const IDBOpenDBOptions
& aOptions
, SystemCallerGuarantee
,
175 void DisconnectFromGlobal(nsIGlobalObject
* aOldGlobal
);
177 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
178 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory
)
181 virtual JSObject
* WrapObject(JSContext
* aCx
,
182 JS::Handle
<JSObject
*> aGivenProto
) override
;
187 static Result
<RefPtr
<IDBFactory
>, nsresult
> CreateForMainThreadJSInternal(
188 nsIGlobalObject
* aGlobal
, UniquePtr
<PrincipalInfo
> aPrincipalInfo
);
190 static Result
<RefPtr
<IDBFactory
>, nsresult
> CreateInternal(
191 nsIGlobalObject
* aGlobal
, UniquePtr
<PrincipalInfo
> aPrincipalInfo
,
192 uint64_t aInnerWindowID
);
194 static nsresult
AllowedForWindowInternal(nsPIDOMWindowInner
* aWindow
,
195 nsCOMPtr
<nsIPrincipal
>* aPrincipal
);
197 [[nodiscard
]] RefPtr
<IDBOpenDBRequest
> OpenInternal(
198 JSContext
* aCx
, nsIPrincipal
* aPrincipal
, const nsAString
& aName
,
199 const Optional
<uint64_t>& aVersion
, bool aDeleting
,
200 CallerType aCallerType
, ErrorResult
& aRv
);
202 nsresult
InitiateRequest(const NotNull
<RefPtr
<IDBOpenDBRequest
>>& aRequest
,
203 const indexedDB::FactoryRequestParams
& aParams
);
207 } // namespace mozilla
209 #endif // mozilla_dom_idbfactory_h__