Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/IDBFileHandle.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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_idbfilehandle_h__
8
#define mozilla_dom_idbfilehandle_h__
9
10
#include "IDBFileRequest.h"
11
#include "js/TypeDecls.h"
12
#include "mozilla/Attributes.h"
13
#include "mozilla/dom/FileModeBinding.h"
14
#include "mozilla/DOMEventTargetHelper.h"
15
#include "nsCycleCollectionParticipant.h"
16
#include "nsIRunnable.h"
17
#include "nsWeakReference.h"
18
19
class nsPIDOMWindowInner;
20
21
namespace mozilla {
22
namespace dom {
23
24
class Blob;
25
class FileRequestData;
26
class FileRequestParams;
27
struct IDBFileMetadataParameters;
28
class IDBFileRequest;
29
class IDBMutableFile;
30
class StringOrArrayBufferOrArrayBufferViewOrBlob;
31
32
namespace indexedDB {
33
class BackgroundFileHandleChild;
34
}
35
36
class IDBFileHandle final
37
  : public DOMEventTargetHelper
38
  , public nsIRunnable
39
  , public nsSupportsWeakReference
40
{
41
public:
42
  enum ReadyState
43
  {
44
    INITIAL = 0,
45
    LOADING,
46
    FINISHING,
47
    DONE
48
  };
49
50
private:
51
  RefPtr<IDBMutableFile> mMutableFile;
52
53
  indexedDB::BackgroundFileHandleChild* mBackgroundActor;
54
55
  uint64_t mLocation;
56
57
  uint32_t mPendingRequestCount;
58
59
  ReadyState mReadyState;
60
  FileMode mMode;
61
62
  bool mAborted;
63
  bool mCreating;
64
65
#ifdef DEBUG
66
  bool mSentFinishOrAbort;
67
  bool mFiredCompleteOrAbort;
68
#endif
69
70
public:
71
  static already_AddRefed<IDBFileHandle>
72
  Create(IDBMutableFile* aMutableFile,
73
         FileMode aMode);
74
75
  static IDBFileHandle*
76
  GetCurrent();
77
78
  void
79
  AssertIsOnOwningThread() const
80
#ifdef DEBUG
81
  ;
82
#else
83
0
  { }
84
#endif
85
86
  void
87
  SetBackgroundActor(indexedDB::BackgroundFileHandleChild* aActor);
88
89
  void
90
  ClearBackgroundActor()
91
  {
92
    AssertIsOnOwningThread();
93
94
    mBackgroundActor = nullptr;
95
  }
96
97
  void
98
  StartRequest(IDBFileRequest* aFileRequest, const FileRequestParams& aParams);
99
100
  void
101
  OnNewRequest();
102
103
  void
104
  OnRequestFinished(bool aActorDestroyedNormally);
105
106
  void
107
  FireCompleteOrAbortEvents(bool aAborted);
108
109
  bool
110
  IsOpen() const;
111
112
  bool
113
  IsFinishingOrDone() const
114
  {
115
    AssertIsOnOwningThread();
116
117
    return mReadyState == FINISHING || mReadyState == DONE;
118
  }
119
120
  bool
121
  IsDone() const
122
  {
123
    AssertIsOnOwningThread();
124
125
    return mReadyState == DONE;
126
  }
127
128
  bool
129
  IsAborted() const
130
  {
131
    AssertIsOnOwningThread();
132
    return mAborted;
133
  }
134
135
  void
136
  Abort();
137
138
  // WebIDL
139
  nsPIDOMWindowInner*
140
  GetParentObject() const
141
0
  {
142
0
    AssertIsOnOwningThread();
143
0
    return GetOwner();
144
0
  }
145
146
  IDBMutableFile*
147
  GetMutableFile() const
148
0
  {
149
0
    AssertIsOnOwningThread();
150
0
    return mMutableFile;
151
0
  }
152
153
  IDBMutableFile*
154
  GetFileHandle() const
155
0
  {
156
0
    AssertIsOnOwningThread();
157
0
    return GetMutableFile();
158
0
  }
159
160
  FileMode
161
  Mode() const
162
0
  {
163
0
    AssertIsOnOwningThread();
164
0
    return mMode;
165
0
  }
166
167
  bool
168
  Active() const
169
0
  {
170
0
    AssertIsOnOwningThread();
171
0
    return IsOpen();
172
0
  }
173
174
  Nullable<uint64_t>
175
  GetLocation() const
176
0
  {
177
0
    AssertIsOnOwningThread();
178
0
179
0
    if (mLocation == UINT64_MAX) {
180
0
      return Nullable<uint64_t>();
181
0
    }
182
0
183
0
    return Nullable<uint64_t>(mLocation);
184
0
  }
185
186
  void
187
  SetLocation(const Nullable<uint64_t>& aLocation)
188
0
  {
189
0
    AssertIsOnOwningThread();
190
0
191
0
    // Null means the end-of-file.
192
0
    if (aLocation.IsNull()) {
193
0
      mLocation = UINT64_MAX;
194
0
    } else {
195
0
      mLocation = aLocation.Value();
196
0
    }
197
0
  }
198
199
  already_AddRefed<IDBFileRequest>
200
  GetMetadata(const IDBFileMetadataParameters& aParameters, ErrorResult& aRv);
201
202
  already_AddRefed<IDBFileRequest>
203
  ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv)
204
0
  {
205
0
    AssertIsOnOwningThread();
206
0
    return Read(aSize, false, VoidString(), aRv);
207
0
  }
208
209
  already_AddRefed<IDBFileRequest>
210
  ReadAsText(uint64_t aSize, const nsAString& aEncoding, ErrorResult& aRv)
211
0
  {
212
0
    AssertIsOnOwningThread();
213
0
    return Read(aSize, true, aEncoding, aRv);
214
0
  }
215
216
  already_AddRefed<IDBFileRequest>
217
  Write(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
218
        ErrorResult& aRv)
219
0
  {
220
0
    AssertIsOnOwningThread();
221
0
    return WriteOrAppend(aValue, false, aRv);
222
0
  }
223
224
  already_AddRefed<IDBFileRequest>
225
  Append(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
226
         ErrorResult& aRv)
227
0
  {
228
0
    AssertIsOnOwningThread();
229
0
    return WriteOrAppend(aValue, true, aRv);
230
0
  }
231
232
  already_AddRefed<IDBFileRequest>
233
  Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
234
235
  already_AddRefed<IDBFileRequest>
236
  Flush(ErrorResult& aRv);
237
238
  void
239
  Abort(ErrorResult& aRv);
240
241
  IMPL_EVENT_HANDLER(complete)
242
  IMPL_EVENT_HANDLER(abort)
243
  IMPL_EVENT_HANDLER(error)
244
245
  NS_DECL_ISUPPORTS_INHERITED
246
  NS_DECL_NSIRUNNABLE
247
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileHandle, DOMEventTargetHelper)
248
249
  // EventTarget
250
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
251
252
  // WrapperCache
253
  virtual JSObject*
254
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
255
256
private:
257
  IDBFileHandle(IDBMutableFile* aMutableFile,
258
                FileMode aMode);
259
  ~IDBFileHandle();
260
261
  bool
262
  CheckState(ErrorResult& aRv);
263
264
  bool
265
  CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
266
267
  bool
268
  CheckStateForWrite(ErrorResult& aRv);
269
270
  bool
271
  CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv);
272
273
  bool
274
  CheckWindow();
275
276
  already_AddRefed<IDBFileRequest>
277
  Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
278
       ErrorResult& aRv);
279
280
  already_AddRefed<IDBFileRequest>
281
  WriteOrAppend(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
282
                bool aAppend,
283
                ErrorResult& aRv);
284
285
  already_AddRefed<IDBFileRequest>
286
  WriteOrAppend(const nsAString& aValue, bool aAppend, ErrorResult& aRv);
287
288
  already_AddRefed<IDBFileRequest>
289
  WriteOrAppend(const ArrayBuffer& aValue, bool aAppend, ErrorResult& aRv);
290
291
  already_AddRefed<IDBFileRequest>
292
  WriteOrAppend(const ArrayBufferView& aValue, bool aAppend, ErrorResult& aRv);
293
294
  already_AddRefed<IDBFileRequest>
295
  WriteOrAppend(Blob& aValue, bool aAppend, ErrorResult& aRv);
296
297
  already_AddRefed<IDBFileRequest>
298
  WriteInternal(const FileRequestData& aData, uint64_t aDataLength,
299
                bool aAppend, ErrorResult& aRv);
300
301
  void
302
  SendFinish();
303
304
  void
305
  SendAbort();
306
};
307
308
} // namespace dom
309
} // namespace mozilla
310
311
#endif // mozilla_dom_idbfilehandle_h__