Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/IDBTransaction.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_idbtransaction_h__
8
#define mozilla_dom_idbtransaction_h__
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/dom/IDBTransactionBinding.h"
12
#include "mozilla/dom/IDBWrapperCache.h"
13
#include "nsAutoPtr.h"
14
#include "nsCycleCollectionParticipant.h"
15
#include "nsIRunnable.h"
16
#include "nsString.h"
17
#include "nsTArray.h"
18
19
class nsPIDOMWindowInner;
20
21
namespace mozilla {
22
23
class ErrorResult;
24
class EventChainPreVisitor;
25
26
namespace dom {
27
28
class DOMException;
29
class DOMStringList;
30
class IDBDatabase;
31
class IDBObjectStore;
32
class IDBOpenDBRequest;
33
class IDBRequest;
34
class StrongWorkerRef;
35
36
namespace indexedDB {
37
class BackgroundCursorChild;
38
class BackgroundRequestChild;
39
class BackgroundTransactionChild;
40
class BackgroundVersionChangeTransactionChild;
41
class IndexMetadata;
42
class ObjectStoreSpec;
43
class OpenCursorParams;
44
class RequestParams;
45
}
46
47
class IDBTransaction final
48
  : public IDBWrapperCache
49
  , public nsIRunnable
50
{
51
  friend class indexedDB::BackgroundCursorChild;
52
  friend class indexedDB::BackgroundRequestChild;
53
54
  class WorkerHolder;
55
  friend class WorkerHolder;
56
57
public:
58
  enum Mode
59
  {
60
    READ_ONLY = 0,
61
    READ_WRITE,
62
    READ_WRITE_FLUSH,
63
    CLEANUP,
64
    VERSION_CHANGE,
65
66
    // Only needed for IPC serialization helper, should never be used in code.
67
    MODE_INVALID
68
  };
69
70
  enum ReadyState
71
  {
72
    INITIAL = 0,
73
    LOADING,
74
    COMMITTING,
75
    DONE
76
  };
77
78
private:
79
  RefPtr<IDBDatabase> mDatabase;
80
  RefPtr<DOMException> mError;
81
  nsTArray<nsString> mObjectStoreNames;
82
  nsTArray<RefPtr<IDBObjectStore>> mObjectStores;
83
  nsTArray<RefPtr<IDBObjectStore>> mDeletedObjectStores;
84
  RefPtr<StrongWorkerRef> mWorkerRef;
85
86
  // Tagged with mMode. If mMode is VERSION_CHANGE then mBackgroundActor will be
87
  // a BackgroundVersionChangeTransactionChild. Otherwise it will be a
88
  // BackgroundTransactionChild.
89
  union {
90
    indexedDB::BackgroundTransactionChild* mNormalBackgroundActor;
91
    indexedDB::BackgroundVersionChangeTransactionChild* mVersionChangeBackgroundActor;
92
  } mBackgroundActor;
93
94
  const int64_t mLoggingSerialNumber;
95
96
  // Only used for VERSION_CHANGE transactions.
97
  int64_t mNextObjectStoreId;
98
  int64_t mNextIndexId;
99
100
  nsresult mAbortCode;
101
  uint32_t mPendingRequestCount;
102
103
  nsString mFilename;
104
  uint32_t mLineNo;
105
  uint32_t mColumn;
106
107
  ReadyState mReadyState;
108
  Mode mMode;
109
110
  bool mCreating;
111
  bool mRegistered;
112
  bool mAbortedByScript;
113
  bool mNotedActiveTransaction;
114
115
#ifdef DEBUG
116
  bool mSentCommitOrAbort;
117
  bool mFiredCompleteOrAbort;
118
#endif
119
120
public:
121
  static already_AddRefed<IDBTransaction>
122
  CreateVersionChange(IDBDatabase* aDatabase,
123
                      indexedDB::BackgroundVersionChangeTransactionChild* aActor,
124
                      IDBOpenDBRequest* aOpenRequest,
125
                      int64_t aNextObjectStoreId,
126
                      int64_t aNextIndexId);
127
128
  static already_AddRefed<IDBTransaction>
129
  Create(JSContext* aCx, IDBDatabase* aDatabase,
130
         const nsTArray<nsString>& aObjectStoreNames,
131
         Mode aMode);
132
133
  static IDBTransaction*
134
  GetCurrent();
135
136
  void
137
  AssertIsOnOwningThread() const
138
#ifdef DEBUG
139
  ;
140
#else
141
0
  { }
142
#endif
143
144
  void
145
  SetBackgroundActor(indexedDB::BackgroundTransactionChild* aBackgroundActor);
146
147
  void
148
  ClearBackgroundActor()
149
0
  {
150
0
    AssertIsOnOwningThread();
151
0
152
0
    if (mMode == VERSION_CHANGE) {
153
0
      mBackgroundActor.mVersionChangeBackgroundActor = nullptr;
154
0
    } else {
155
0
      mBackgroundActor.mNormalBackgroundActor = nullptr;
156
0
    }
157
0
158
0
    // Note inactive transaction here if we didn't receive the Complete message
159
0
    // from the parent.
160
0
    MaybeNoteInactiveTransaction();
161
0
  }
162
163
  indexedDB::BackgroundRequestChild*
164
  StartRequest(IDBRequest* aRequest, const indexedDB::RequestParams& aParams);
165
166
  void
167
  OpenCursor(indexedDB::BackgroundCursorChild* aBackgroundActor,
168
             const indexedDB::OpenCursorParams& aParams);
169
170
  void
171
  RefreshSpec(bool aMayDelete);
172
173
  bool
174
  IsOpen() const;
175
176
  bool
177
  IsCommittingOrDone() const
178
0
  {
179
0
    AssertIsOnOwningThread();
180
0
181
0
    return mReadyState == COMMITTING || mReadyState == DONE;
182
0
  }
183
184
  bool
185
  IsDone() const
186
0
  {
187
0
    AssertIsOnOwningThread();
188
0
189
0
    return mReadyState == DONE;
190
0
  }
191
192
  bool
193
  IsWriteAllowed() const
194
0
  {
195
0
    AssertIsOnOwningThread();
196
0
    return mMode == READ_WRITE ||
197
0
           mMode == READ_WRITE_FLUSH ||
198
0
           mMode == CLEANUP ||
199
0
           mMode == VERSION_CHANGE;
200
0
  }
201
202
  bool
203
  IsAborted() const
204
0
  {
205
0
    AssertIsOnOwningThread();
206
0
    return NS_FAILED(mAbortCode);
207
0
  }
208
209
  nsresult
210
  AbortCode() const
211
0
  {
212
0
    AssertIsOnOwningThread();
213
0
    return mAbortCode;
214
0
  }
215
216
  void
217
  GetCallerLocation(nsAString& aFilename, uint32_t* aLineNo,
218
                    uint32_t* aColumn) const;
219
220
  // 'Get' prefix is to avoid name collisions with the enum
221
  Mode
222
  GetMode() const
223
0
  {
224
0
    AssertIsOnOwningThread();
225
0
    return mMode;
226
0
  }
227
228
  IDBDatabase*
229
  Database() const
230
0
  {
231
0
    AssertIsOnOwningThread();
232
0
    return mDatabase;
233
0
  }
234
235
  IDBDatabase*
236
  Db() const
237
0
  {
238
0
    return Database();
239
0
  }
240
241
  const nsTArray<nsString>&
242
  ObjectStoreNamesInternal() const
243
0
  {
244
0
    AssertIsOnOwningThread();
245
0
    return mObjectStoreNames;
246
0
  }
247
248
  already_AddRefed<IDBObjectStore>
249
  CreateObjectStore(const indexedDB::ObjectStoreSpec& aSpec);
250
251
  void
252
  DeleteObjectStore(int64_t aObjectStoreId);
253
254
  void
255
  RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName);
256
257
  void
258
  CreateIndex(IDBObjectStore* aObjectStore, const indexedDB::IndexMetadata& aMetadata);
259
260
  void
261
  DeleteIndex(IDBObjectStore* aObjectStore, int64_t aIndexId);
262
263
  void
264
  RenameIndex(IDBObjectStore* aObjectStore, int64_t aIndexId, const nsAString& aName);
265
266
  void
267
  Abort(IDBRequest* aRequest);
268
269
  void
270
  Abort(nsresult aAbortCode);
271
272
  int64_t
273
  LoggingSerialNumber() const
274
0
  {
275
0
    AssertIsOnOwningThread();
276
0
277
0
    return mLoggingSerialNumber;
278
0
  }
279
280
  nsPIDOMWindowInner*
281
  GetParentObject() const;
282
283
  IDBTransactionMode
284
  GetMode(ErrorResult& aRv) const;
285
286
  DOMException*
287
  GetError() const;
288
289
  already_AddRefed<IDBObjectStore>
290
  ObjectStore(const nsAString& aName, ErrorResult& aRv);
291
292
  void
293
  Abort(ErrorResult& aRv);
294
295
  IMPL_EVENT_HANDLER(abort)
296
  IMPL_EVENT_HANDLER(complete)
297
  IMPL_EVENT_HANDLER(error)
298
299
  already_AddRefed<DOMStringList>
300
  ObjectStoreNames() const;
301
302
  void
303
  FireCompleteOrAbortEvents(nsresult aResult);
304
305
  // Only for VERSION_CHANGE transactions.
306
  int64_t
307
  NextObjectStoreId();
308
309
  // Only for VERSION_CHANGE transactions.
310
  int64_t
311
  NextIndexId();
312
313
  NS_DECL_ISUPPORTS_INHERITED
314
  NS_DECL_NSIRUNNABLE
315
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction, IDBWrapperCache)
316
317
  // nsWrapperCache
318
  virtual JSObject*
319
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
320
321
  // EventTarget
322
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
323
324
private:
325
  IDBTransaction(IDBDatabase* aDatabase,
326
                 const nsTArray<nsString>& aObjectStoreNames,
327
                 Mode aMode);
328
  ~IDBTransaction();
329
330
  void
331
  AbortInternal(nsresult aAbortCode, already_AddRefed<DOMException> aError);
332
333
  void
334
  SendCommit();
335
336
  void
337
  SendAbort(nsresult aResultCode);
338
339
  void
340
  NoteActiveTransaction();
341
342
  void
343
  MaybeNoteInactiveTransaction();
344
345
  void
346
  OnNewRequest();
347
348
  void
349
  OnRequestFinished(bool aActorDestroyedNormally);
350
};
351
352
} // namespace dom
353
} // namespace mozilla
354
355
#endif // mozilla_dom_idbtransaction_h__