Source code

Revision control

Other Tools

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 nsIGlobalObject;
22
class nsIEventTarget;
23
class nsIPrincipal;
24
class nsPIDOMWindowInner;
25
26
namespace mozilla {
27
28
class ErrorResult;
29
30
namespace ipc {
31
32
class PBackgroundChild;
33
class PrincipalInfo;
34
35
} // namespace ipc
36
37
namespace dom {
38
39
struct IDBOpenDBOptions;
40
class IDBOpenDBRequest;
41
template <typename>
42
class Optional;
43
class BrowserChild;
44
enum class CallerType : uint32_t;
45
46
namespace indexedDB {
47
class BackgroundFactoryChild;
48
class FactoryRequestParams;
49
class LoggingInfo;
50
} // namespace indexedDB
51
52
class IDBFactory final : public nsISupports, public nsWrapperCache {
53
typedef mozilla::dom::StorageType StorageType;
54
typedef mozilla::ipc::PBackgroundChild PBackgroundChild;
55
typedef mozilla::ipc::PrincipalInfo PrincipalInfo;
56
57
class BackgroundCreateCallback;
58
struct PendingRequestInfo;
59
60
nsAutoPtr<PrincipalInfo> mPrincipalInfo;
61
62
nsCOMPtr<nsIGlobalObject> mGlobal;
63
64
// This will only be set if the factory belongs to a window in a child
65
// process.
66
RefPtr<BrowserChild> mBrowserChild;
67
68
indexedDB::BackgroundFactoryChild* mBackgroundActor;
69
70
// It is either set to a DocGroup-specific EventTarget if created by
71
// CreateForWindow() or set to GetCurrentThreadEventTarget() otherwise.
72
nsCOMPtr<nsIEventTarget> mEventTarget;
73
74
uint64_t mInnerWindowID;
75
uint32_t mActiveTransactionCount;
76
uint32_t mActiveDatabaseCount;
77
78
bool mBackgroundActorFailed;
79
bool mPrivateBrowsingMode;
80
81
public:
82
static nsresult CreateForWindow(nsPIDOMWindowInner* aWindow,
83
IDBFactory** aFactory);
84
85
static nsresult CreateForMainThreadJS(nsIGlobalObject* aGlobal,
86
IDBFactory** aFactory);
87
88
static nsresult CreateForWorker(nsIGlobalObject* aGlobal,
89
const PrincipalInfo& aPrincipalInfo,
90
uint64_t aInnerWindowID,
91
IDBFactory** aFactory);
92
93
static bool AllowedForWindow(nsPIDOMWindowInner* aWindow);
94
95
static bool AllowedForPrincipal(nsIPrincipal* aPrincipal,
96
bool* aIsSystemPrincipal = nullptr);
97
98
void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(IDBFactory); }
99
100
nsIEventTarget* EventTarget() const {
101
AssertIsOnOwningThread();
102
MOZ_RELEASE_ASSERT(mEventTarget);
103
return mEventTarget;
104
}
105
106
void ClearBackgroundActor() {
107
AssertIsOnOwningThread();
108
109
mBackgroundActor = nullptr;
110
}
111
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);
117
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);
123
124
void IncrementParentLoggingRequestSerialNumber();
125
126
nsIGlobalObject* GetParentObject() const { return mGlobal; }
127
128
BrowserChild* GetBrowserChild() const { return mBrowserChild; }
129
130
PrincipalInfo* GetPrincipalInfo() const {
131
AssertIsOnOwningThread();
132
133
return mPrincipalInfo;
134
}
135
136
uint64_t InnerWindowID() const {
137
AssertIsOnOwningThread();
138
139
return mInnerWindowID;
140
}
141
142
bool IsChrome() const;
143
144
already_AddRefed<IDBOpenDBRequest> Open(JSContext* aCx,
145
const nsAString& aName,
146
uint64_t aVersion,
147
CallerType aCallerType,
148
ErrorResult& aRv);
149
150
already_AddRefed<IDBOpenDBRequest> Open(JSContext* aCx,
151
const nsAString& aName,
152
const IDBOpenDBOptions& aOptions,
153
CallerType aCallerType,
154
ErrorResult& aRv);
155
156
already_AddRefed<IDBOpenDBRequest> DeleteDatabase(
157
JSContext* aCx, const nsAString& aName, const IDBOpenDBOptions& aOptions,
158
CallerType aCallerType, ErrorResult& aRv);
159
160
int16_t Cmp(JSContext* aCx, JS::Handle<JS::Value> aFirst,
161
JS::Handle<JS::Value> aSecond, ErrorResult& aRv);
162
163
already_AddRefed<IDBOpenDBRequest> OpenForPrincipal(
164
JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
165
uint64_t aVersion, SystemCallerGuarantee, ErrorResult& aRv);
166
167
already_AddRefed<IDBOpenDBRequest> OpenForPrincipal(
168
JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
169
const IDBOpenDBOptions& aOptions, SystemCallerGuarantee,
170
ErrorResult& aRv);
171
172
already_AddRefed<IDBOpenDBRequest> DeleteForPrincipal(
173
JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
174
const IDBOpenDBOptions& aOptions, SystemCallerGuarantee,
175
ErrorResult& aRv);
176
177
void DisconnectFromGlobal(nsIGlobalObject* aOldGlobal);
178
179
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
180
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory)
181
182
// nsWrapperCache
183
virtual JSObject* WrapObject(JSContext* aCx,
184
JS::Handle<JSObject*> aGivenProto) override;
185
186
private:
187
IDBFactory();
188
~IDBFactory();
189
190
static nsresult CreateForMainThreadJSInternal(
191
nsIGlobalObject* aGlobal, nsAutoPtr<PrincipalInfo>& aPrincipalInfo,
192
IDBFactory** aFactory);
193
194
static nsresult CreateInternal(nsIGlobalObject* aGlobal,
195
nsAutoPtr<PrincipalInfo>& aPrincipalInfo,
196
uint64_t aInnerWindowID,
197
IDBFactory** aFactory);
198
199
static nsresult AllowedForWindowInternal(nsPIDOMWindowInner* aWindow,
200
nsIPrincipal** aPrincipal);
201
202
already_AddRefed<IDBOpenDBRequest> OpenInternal(
203
JSContext* aCx, nsIPrincipal* aPrincipal, const nsAString& aName,
204
const Optional<uint64_t>& aVersion,
205
const Optional<StorageType>& aStorageType, bool aDeleting,
206
CallerType aCallerType, ErrorResult& aRv);
207
208
nsresult InitiateRequest(IDBOpenDBRequest* aRequest,
209
const indexedDB::FactoryRequestParams& aParams);
210
};
211
212
} // namespace dom
213
} // namespace mozilla
214
215
#endif // mozilla_dom_idbfactory_h__