Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/indexedDB/IDBRequest.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_idbrequest_h__
8
#define mozilla_dom_idbrequest_h__
9
10
#include "js/RootingAPI.h"
11
#include "mozilla/Attributes.h"
12
#include "mozilla/EventForwards.h"
13
#include "mozilla/dom/IDBRequestBinding.h"
14
#include "mozilla/dom/IDBWrapperCache.h"
15
#include "nsAutoPtr.h"
16
#include "nsCycleCollectionParticipant.h"
17
18
#define PRIVATE_IDBREQUEST_IID \
19
  {0xe68901e5, 0x1d50, 0x4ee9, {0xaf, 0x49, 0x90, 0x99, 0x4a, 0xff, 0xc8, 0x39}}
20
21
class nsPIDOMWindowInner;
22
23
namespace mozilla {
24
25
class ErrorResult;
26
27
namespace dom {
28
29
class DOMException;
30
class IDBCursor;
31
class IDBDatabase;
32
class IDBFactory;
33
class IDBIndex;
34
class IDBObjectStore;
35
class IDBTransaction;
36
template <typename> struct Nullable;
37
class OwningIDBObjectStoreOrIDBIndexOrIDBCursor;
38
class StrongWorkerRef;
39
40
class IDBRequest
41
  : public IDBWrapperCache
42
{
43
protected:
44
  // mSourceAsObjectStore and mSourceAsIndex are exclusive and one must always
45
  // be set. mSourceAsCursor is sometimes set also.
46
  RefPtr<IDBObjectStore> mSourceAsObjectStore;
47
  RefPtr<IDBIndex> mSourceAsIndex;
48
  RefPtr<IDBCursor> mSourceAsCursor;
49
50
  RefPtr<IDBTransaction> mTransaction;
51
52
  JS::Heap<JS::Value> mResultVal;
53
  RefPtr<DOMException> mError;
54
55
  nsString mFilename;
56
  uint64_t mLoggingSerialNumber;
57
  nsresult mErrorCode;
58
  uint32_t mLineNo;
59
  uint32_t mColumn;
60
  bool mHaveResultOrErrorCode;
61
62
public:
63
  class ResultCallback;
64
65
  static already_AddRefed<IDBRequest>
66
  Create(JSContext* aCx, IDBDatabase* aDatabase, IDBTransaction* aTransaction);
67
68
  static already_AddRefed<IDBRequest>
69
  Create(JSContext* aCx,
70
         IDBObjectStore* aSource,
71
         IDBDatabase* aDatabase,
72
         IDBTransaction* aTransaction);
73
74
  static already_AddRefed<IDBRequest>
75
  Create(JSContext* aCx,
76
         IDBIndex* aSource,
77
         IDBDatabase* aDatabase,
78
         IDBTransaction* aTransaction);
79
80
  static void
81
  CaptureCaller(JSContext* aCx, nsAString& aFilename, uint32_t* aLineNo,
82
                uint32_t* aColumn);
83
84
  static uint64_t
85
  NextSerialNumber();
86
87
  // EventTarget
88
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
89
90
  void
91
  GetSource(Nullable<OwningIDBObjectStoreOrIDBIndexOrIDBCursor>& aSource) const;
92
93
  void
94
  Reset();
95
96
  void
97
  SetResultCallback(ResultCallback* aCallback);
98
99
  void
100
  SetError(nsresult aRv);
101
102
  nsresult
103
  GetErrorCode() const
104
#ifdef DEBUG
105
  ;
106
#else
107
0
  {
108
0
    return mErrorCode;
109
0
  }
110
#endif
111
112
  DOMException*
113
  GetErrorAfterResult() const
114
#ifdef DEBUG
115
  ;
116
#else
117
0
  {
118
0
    return mError;
119
0
  }
120
#endif
121
122
  DOMException*
123
  GetError(ErrorResult& aRv);
124
125
  void
126
  GetCallerLocation(nsAString& aFilename, uint32_t* aLineNo,
127
                    uint32_t* aColumn) const;
128
129
  bool
130
  IsPending() const
131
0
  {
132
0
    return !mHaveResultOrErrorCode;
133
0
  }
134
135
  uint64_t
136
  LoggingSerialNumber() const
137
0
  {
138
0
    AssertIsOnOwningThread();
139
0
140
0
    return mLoggingSerialNumber;
141
0
  }
142
143
  void
144
  SetLoggingSerialNumber(uint64_t aLoggingSerialNumber);
145
146
  nsPIDOMWindowInner*
147
  GetParentObject() const
148
  {
149
    return GetOwner();
150
  }
151
152
  void
153
  GetResult(JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) const;
154
155
  void
156
  GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
157
            ErrorResult& aRv) const
158
  {
159
    GetResult(aResult, aRv);
160
  }
161
162
  IDBTransaction*
163
  GetTransaction() const
164
  {
165
    AssertIsOnOwningThread();
166
167
    return mTransaction;
168
  }
169
170
  IDBRequestReadyState
171
  ReadyState() const;
172
173
  void
174
  SetSource(IDBCursor* aSource);
175
176
  IMPL_EVENT_HANDLER(success);
177
  IMPL_EVENT_HANDLER(error);
178
179
  void
180
  AssertIsOnOwningThread() const
181
  {
182
    NS_ASSERT_OWNINGTHREAD(IDBRequest);
183
  }
184
185
  NS_DECL_ISUPPORTS_INHERITED
186
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(IDBRequest,
187
                                                         IDBWrapperCache)
188
189
  // nsWrapperCache
190
  virtual JSObject*
191
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
192
193
protected:
194
  explicit IDBRequest(IDBDatabase* aDatabase);
195
  explicit IDBRequest(nsPIDOMWindowInner* aOwner);
196
  ~IDBRequest();
197
198
  void
199
  InitMembers();
200
201
  void
202
  ConstructResult();
203
};
204
205
class NS_NO_VTABLE IDBRequest::ResultCallback
206
{
207
public:
208
  virtual nsresult
209
  GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) = 0;
210
211
protected:
212
  ResultCallback()
213
0
  { }
214
};
215
216
class IDBOpenDBRequest final
217
  : public IDBRequest
218
{
219
  // Only touched on the owning thread.
220
  RefPtr<IDBFactory> mFactory;
221
222
  RefPtr<StrongWorkerRef> mWorkerRef;
223
224
  const bool mFileHandleDisabled;
225
  bool mIncreasedActiveDatabaseCount;
226
227
public:
228
  static already_AddRefed<IDBOpenDBRequest>
229
  CreateForWindow(JSContext* aCx,
230
                  IDBFactory* aFactory,
231
                  nsPIDOMWindowInner* aOwner,
232
                  JS::Handle<JSObject*> aScriptOwner);
233
234
  static already_AddRefed<IDBOpenDBRequest>
235
  CreateForJS(JSContext* aCx,
236
              IDBFactory* aFactory,
237
              JS::Handle<JSObject*> aScriptOwner);
238
239
  bool
240
  IsFileHandleDisabled() const
241
0
  {
242
0
    return mFileHandleDisabled;
243
0
  }
244
245
  void
246
  SetTransaction(IDBTransaction* aTransaction);
247
248
  void
249
  DispatchNonTransactionError(nsresult aErrorCode);
250
251
  void
252
  NoteComplete();
253
254
  // EventTarget
255
  virtual nsresult
256
  PostHandleEvent(EventChainPostVisitor& aVisitor) override;
257
258
  IDBFactory*
259
  Factory() const
260
  {
261
    return mFactory;
262
  }
263
264
  IMPL_EVENT_HANDLER(blocked);
265
  IMPL_EVENT_HANDLER(upgradeneeded);
266
267
  NS_DECL_ISUPPORTS_INHERITED
268
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBOpenDBRequest, IDBRequest)
269
270
  // nsWrapperCache
271
  virtual JSObject*
272
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
273
274
private:
275
  IDBOpenDBRequest(IDBFactory* aFactory,
276
                   nsPIDOMWindowInner* aOwner,
277
                   bool aFileHandleDisabled);
278
279
  ~IDBOpenDBRequest();
280
281
  void
282
  IncreaseActiveDatabaseCount();
283
284
  void
285
  MaybeDecreaseActiveDatabaseCount();
286
};
287
288
} // namespace dom
289
} // namespace mozilla
290
291
#endif // mozilla_dom_idbrequest_h__