/src/mozilla-central/dom/indexedDB/IDBFactory.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_idbfactory_h__ |
8 | | #define mozilla_dom_idbfactory_h__ |
9 | | |
10 | | #include "mozilla/Attributes.h" |
11 | | #include "mozilla/dom/BindingDeclarations.h" |
12 | | #include "mozilla/dom/StorageTypeBinding.h" |
13 | | #include "nsAutoPtr.h" |
14 | | #include "nsCOMPtr.h" |
15 | | #include "nsCycleCollectionParticipant.h" |
16 | | #include "nsISupports.h" |
17 | | #include "nsString.h" |
18 | | #include "nsTArray.h" |
19 | | #include "nsWrapperCache.h" |
20 | | |
21 | | class nsIEventTarget; |
22 | | class nsIPrincipal; |
23 | | class nsPIDOMWindowInner; |
24 | | |
25 | | namespace mozilla { |
26 | | |
27 | | class ErrorResult; |
28 | | |
29 | | namespace ipc { |
30 | | |
31 | | class PBackgroundChild; |
32 | | class PrincipalInfo; |
33 | | |
34 | | } // namespace ipc |
35 | | |
36 | | namespace dom { |
37 | | |
38 | | struct IDBOpenDBOptions; |
39 | | class IDBOpenDBRequest; |
40 | | template <typename> class Optional; |
41 | | class TabChild; |
42 | | enum class CallerType : uint32_t; |
43 | | |
44 | | namespace indexedDB { |
45 | | class BackgroundFactoryChild; |
46 | | class FactoryRequestParams; |
47 | | class LoggingInfo; |
48 | | } |
49 | | |
50 | | class IDBFactory final |
51 | | : public nsISupports |
52 | | , public nsWrapperCache |
53 | | { |
54 | | typedef mozilla::dom::StorageType StorageType; |
55 | | typedef mozilla::ipc::PBackgroundChild PBackgroundChild; |
56 | | typedef mozilla::ipc::PrincipalInfo PrincipalInfo; |
57 | | |
58 | | class BackgroundCreateCallback; |
59 | | struct PendingRequestInfo; |
60 | | |
61 | | nsAutoPtr<PrincipalInfo> mPrincipalInfo; |
62 | | |
63 | | // If this factory lives on a window then mWindow must be non-null. Otherwise |
64 | | // mOwningObject must be non-null. |
65 | | nsCOMPtr<nsPIDOMWindowInner> mWindow; |
66 | | JS::Heap<JSObject*> mOwningObject; |
67 | | |
68 | | // This will only be set if the factory belongs to a window in a child |
69 | | // process. |
70 | | RefPtr<TabChild> mTabChild; |
71 | | |
72 | | indexedDB::BackgroundFactoryChild* mBackgroundActor; |
73 | | |
74 | | // It is either set to a DocGroup-specific EventTarget if created by |
75 | | // CreateForWindow() or set to GetCurrentThreadEventTarget() otherwise. |
76 | | nsCOMPtr<nsIEventTarget> mEventTarget; |
77 | | |
78 | | uint64_t mInnerWindowID; |
79 | | uint32_t mActiveTransactionCount; |
80 | | uint32_t mActiveDatabaseCount; |
81 | | |
82 | | bool mBackgroundActorFailed; |
83 | | bool mPrivateBrowsingMode; |
84 | | |
85 | | public: |
86 | | static nsresult |
87 | | CreateForWindow(nsPIDOMWindowInner* aWindow, |
88 | | IDBFactory** aFactory); |
89 | | |
90 | | static nsresult |
91 | | CreateForMainThreadJS(JSContext* aCx, |
92 | | JS::Handle<JSObject*> aOwningObject, |
93 | | IDBFactory** aFactory); |
94 | | |
95 | | static nsresult |
96 | | CreateForWorker(JSContext* aCx, |
97 | | JS::Handle<JSObject*> aOwningObject, |
98 | | const PrincipalInfo& aPrincipalInfo, |
99 | | uint64_t aInnerWindowID, |
100 | | IDBFactory** aFactory); |
101 | | |
102 | | static bool |
103 | | AllowedForWindow(nsPIDOMWindowInner* aWindow); |
104 | | |
105 | | static bool |
106 | | AllowedForPrincipal(nsIPrincipal* aPrincipal, |
107 | | bool* aIsSystemPrincipal = nullptr); |
108 | | |
109 | | void |
110 | | AssertIsOnOwningThread() const |
111 | 0 | { |
112 | 0 | NS_ASSERT_OWNINGTHREAD(IDBFactory); |
113 | 0 | } |
114 | | |
115 | | nsIEventTarget* |
116 | | EventTarget() const |
117 | 0 | { |
118 | 0 | AssertIsOnOwningThread(); |
119 | 0 | MOZ_RELEASE_ASSERT(mEventTarget); |
120 | 0 | return mEventTarget; |
121 | 0 | } |
122 | | |
123 | | void |
124 | | ClearBackgroundActor() |
125 | 0 | { |
126 | 0 | AssertIsOnOwningThread(); |
127 | 0 |
|
128 | 0 | mBackgroundActor = nullptr; |
129 | 0 | } |
130 | | |
131 | | // Increase/Decrease the number of active transactions for the decision |
132 | | // making of preemption and throttling. |
133 | | // Note: If the state of its actor is not committed or aborted, it could block |
134 | | // IDB operations in other window. |
135 | | void |
136 | | UpdateActiveTransactionCount(int32_t aDelta); |
137 | | |
138 | | // Increase/Decrease the number of active databases and IDBOpenRequests for |
139 | | // the decision making of preemption and throttling. |
140 | | // Note: A non-closed database or a pending IDBOpenRequest could block |
141 | | // IDB operations in other window. |
142 | | void |
143 | | UpdateActiveDatabaseCount(int32_t aDelta); |
144 | | |
145 | | void |
146 | | IncrementParentLoggingRequestSerialNumber(); |
147 | | |
148 | | nsPIDOMWindowInner* |
149 | | GetParentObject() const |
150 | | { |
151 | | return mWindow; |
152 | | } |
153 | | |
154 | | TabChild* |
155 | | GetTabChild() const |
156 | 0 | { |
157 | 0 | return mTabChild; |
158 | 0 | } |
159 | | |
160 | | PrincipalInfo* |
161 | | GetPrincipalInfo() const |
162 | 0 | { |
163 | 0 | AssertIsOnOwningThread(); |
164 | 0 |
|
165 | 0 | return mPrincipalInfo; |
166 | 0 | } |
167 | | |
168 | | uint64_t |
169 | | InnerWindowID() const |
170 | 0 | { |
171 | 0 | AssertIsOnOwningThread(); |
172 | 0 |
|
173 | 0 | return mInnerWindowID; |
174 | 0 | } |
175 | | |
176 | | bool |
177 | | IsChrome() const; |
178 | | |
179 | | already_AddRefed<IDBOpenDBRequest> |
180 | | Open(JSContext* aCx, |
181 | | const nsAString& aName, |
182 | | uint64_t aVersion, |
183 | | CallerType aCallerType, |
184 | | ErrorResult& aRv); |
185 | | |
186 | | already_AddRefed<IDBOpenDBRequest> |
187 | | Open(JSContext* aCx, |
188 | | const nsAString& aName, |
189 | | const IDBOpenDBOptions& aOptions, |
190 | | CallerType aCallerType, |
191 | | ErrorResult& aRv); |
192 | | |
193 | | already_AddRefed<IDBOpenDBRequest> |
194 | | DeleteDatabase(JSContext* aCx, |
195 | | const nsAString& aName, |
196 | | const IDBOpenDBOptions& aOptions, |
197 | | CallerType aCallerType, |
198 | | ErrorResult& aRv); |
199 | | |
200 | | int16_t |
201 | | Cmp(JSContext* aCx, |
202 | | JS::Handle<JS::Value> aFirst, |
203 | | JS::Handle<JS::Value> aSecond, |
204 | | ErrorResult& aRv); |
205 | | |
206 | | already_AddRefed<IDBOpenDBRequest> |
207 | | OpenForPrincipal(JSContext* aCx, |
208 | | nsIPrincipal* aPrincipal, |
209 | | const nsAString& aName, |
210 | | uint64_t aVersion, |
211 | | SystemCallerGuarantee, |
212 | | ErrorResult& aRv); |
213 | | |
214 | | already_AddRefed<IDBOpenDBRequest> |
215 | | OpenForPrincipal(JSContext* aCx, |
216 | | nsIPrincipal* aPrincipal, |
217 | | const nsAString& aName, |
218 | | const IDBOpenDBOptions& aOptions, |
219 | | SystemCallerGuarantee, |
220 | | ErrorResult& aRv); |
221 | | |
222 | | already_AddRefed<IDBOpenDBRequest> |
223 | | DeleteForPrincipal(JSContext* aCx, |
224 | | nsIPrincipal* aPrincipal, |
225 | | const nsAString& aName, |
226 | | const IDBOpenDBOptions& aOptions, |
227 | | SystemCallerGuarantee, |
228 | | ErrorResult& aRv); |
229 | | |
230 | | void |
231 | | RebindToNewWindow(nsPIDOMWindowInner* aNewWindow); |
232 | | |
233 | | void |
234 | | DisconnectFromWindow(nsPIDOMWindowInner* aOldWindow); |
235 | | |
236 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
237 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory) |
238 | | |
239 | | // nsWrapperCache |
240 | | virtual JSObject* |
241 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
242 | | |
243 | | private: |
244 | | IDBFactory(); |
245 | | ~IDBFactory(); |
246 | | |
247 | | static nsresult |
248 | | CreateForMainThreadJSInternal(JSContext* aCx, |
249 | | JS::Handle<JSObject*> aOwningObject, |
250 | | nsAutoPtr<PrincipalInfo>& aPrincipalInfo, |
251 | | IDBFactory** aFactory); |
252 | | |
253 | | static nsresult |
254 | | CreateForJSInternal(JSContext* aCx, |
255 | | JS::Handle<JSObject*> aOwningObject, |
256 | | nsAutoPtr<PrincipalInfo>& aPrincipalInfo, |
257 | | uint64_t aInnerWindowID, |
258 | | IDBFactory** aFactory); |
259 | | |
260 | | static nsresult |
261 | | AllowedForWindowInternal(nsPIDOMWindowInner* aWindow, |
262 | | nsIPrincipal** aPrincipal); |
263 | | |
264 | | already_AddRefed<IDBOpenDBRequest> |
265 | | OpenInternal(JSContext* aCx, |
266 | | nsIPrincipal* aPrincipal, |
267 | | const nsAString& aName, |
268 | | const Optional<uint64_t>& aVersion, |
269 | | const Optional<StorageType>& aStorageType, |
270 | | bool aDeleting, |
271 | | CallerType aCallerType, |
272 | | ErrorResult& aRv); |
273 | | |
274 | | nsresult |
275 | | InitiateRequest(IDBOpenDBRequest* aRequest, |
276 | | const indexedDB::FactoryRequestParams& aParams); |
277 | | }; |
278 | | |
279 | | } // namespace dom |
280 | | } // namespace mozilla |
281 | | |
282 | | #endif // mozilla_dom_idbfactory_h__ |