Backed out 22 changesets (bug 1839396) for causing build bustages on js/Printer.h...
[gecko.git] / dom / indexedDB / IDBFactory.h
blob4fd25adf2869abd3440ac13a6649151ceb809cb9
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"
13 #include "nsCOMPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsISupports.h"
16 #include "nsString.h"
17 #include "nsTArray.h"
18 #include "nsWrapperCache.h"
20 class nsIGlobalObject;
21 class nsIPrincipal;
22 class nsISerialEventTarget;
23 class nsPIDOMWindowInner;
25 namespace mozilla {
27 class ErrorResult;
29 namespace ipc {
31 class PBackgroundChild;
32 class PrincipalInfo;
34 } // namespace ipc
36 namespace dom {
38 struct IDBOpenDBOptions;
39 class IDBOpenDBRequest;
40 template <typename>
41 class Optional;
42 class BrowserChild;
43 enum class CallerType : uint32_t;
45 namespace indexedDB {
46 class BackgroundFactoryChild;
47 class FactoryRequestParams;
48 class LoggingInfo;
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
64 // process.
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;
80 public:
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);
103 return 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,
144 uint64_t aVersion,
145 CallerType aCallerType,
146 ErrorResult& aRv);
148 [[nodiscard]] RefPtr<IDBOpenDBRequest> Open(JSContext* aCx,
149 const nsAString& aName,
150 const IDBOpenDBOptions& aOptions,
151 CallerType aCallerType,
152 ErrorResult& aRv);
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,
168 ErrorResult& aRv);
170 [[nodiscard]] RefPtr<IDBOpenDBRequest> DeleteForPrincipal(
171 JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
172 const IDBOpenDBOptions& aOptions, SystemCallerGuarantee,
173 ErrorResult& aRv);
175 void DisconnectFromGlobal(nsIGlobalObject* aOldGlobal);
177 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
178 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory)
180 // nsWrapperCache
181 virtual JSObject* WrapObject(JSContext* aCx,
182 JS::Handle<JSObject*> aGivenProto) override;
184 private:
185 ~IDBFactory();
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);
206 } // namespace dom
207 } // namespace mozilla
209 #endif // mozilla_dom_idbfactory_h__