Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/FileReader.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_FileReader_h
8
#define mozilla_dom_FileReader_h
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
13
#include "nsIAsyncInputStream.h"
14
#include "nsIInterfaceRequestor.h"
15
#include "nsINamed.h"
16
#include "nsCOMPtr.h"
17
#include "nsString.h"
18
#include "nsWeakReference.h"
19
20
#define NS_PROGRESS_EVENT_INTERVAL 50
21
22
class nsITimer;
23
class nsIEventTarget;
24
25
namespace mozilla {
26
namespace dom {
27
28
class Blob;
29
class DOMException;
30
class StrongWorkerRef;
31
class WeakWorkerRef;
32
33
extern const uint64_t kUnknownSize;
34
35
class FileReaderDecreaseBusyCounter;
36
37
// 26a79031-c94b-47e9-850a-f04fe17bc026
38
#define FILEREADER_ID                                         \
39
 { 0x26a79031, 0xc94b, 0x47e9, {0x85, 0x0a, 0xf0, 0x4f, 0xe1, \
40
                                0x7b, 0xc0, 0x26} }
41
42
class FileReader final : public DOMEventTargetHelper,
43
                         public nsIInterfaceRequestor,
44
                         public nsSupportsWeakReference,
45
                         public nsIInputStreamCallback,
46
                         public nsITimerCallback,
47
                         public nsINamed
48
{
49
  friend class FileReaderDecreaseBusyCounter;
50
51
public:
52
  FileReader(nsIGlobalObject* aGlobal,
53
             WeakWorkerRef* aWorkerRef);
54
55
  NS_DECL_ISUPPORTS_INHERITED
56
57
  NS_DECL_NSITIMERCALLBACK
58
  NS_DECL_NSIINPUTSTREAMCALLBACK
59
  NS_DECL_NSIINTERFACEREQUESTOR
60
  NS_DECL_NSINAMED
61
62
  NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID)
63
64
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader,
65
                                                         DOMEventTargetHelper)
66
67
  virtual JSObject* WrapObject(JSContext* aCx,
68
                               JS::Handle<JSObject*> aGivenProto) override;
69
70
  // WebIDL
71
  static already_AddRefed<FileReader>
72
  Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
73
  void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv)
74
0
  {
75
0
    ReadFileContent(aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv);
76
0
  }
77
78
  void ReadAsText(Blob& aBlob, const Optional<nsAString>& aLabel,
79
                  ErrorResult& aRv)
80
0
  {
81
0
    if (aLabel.WasPassed()) {
82
0
      ReadFileContent(aBlob, aLabel.Value(), FILE_AS_TEXT, aRv);
83
0
    } else {
84
0
      ReadFileContent(aBlob, EmptyString(), FILE_AS_TEXT, aRv);
85
0
    }
86
0
  }
87
88
  void ReadAsDataURL(Blob& aBlob, ErrorResult& aRv)
89
0
  {
90
0
    ReadFileContent(aBlob, EmptyString(), FILE_AS_DATAURL, aRv);
91
0
  }
92
93
  void Abort();
94
95
  uint16_t ReadyState() const
96
0
  {
97
0
    return static_cast<uint16_t>(mReadyState);
98
0
  }
99
100
  DOMException* GetError() const
101
0
  {
102
0
    return mError;
103
0
  }
104
105
  void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
106
                 ErrorResult& aRv);
107
108
  IMPL_EVENT_HANDLER(loadstart)
109
  IMPL_EVENT_HANDLER(progress)
110
  IMPL_EVENT_HANDLER(load)
111
  IMPL_EVENT_HANDLER(abort)
112
  IMPL_EVENT_HANDLER(error)
113
  IMPL_EVENT_HANDLER(loadend)
114
115
  void ReadAsBinaryString(Blob& aBlob, ErrorResult& aRv)
116
0
  {
117
0
    ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
118
0
  }
119
120
  enum eDataFormat
121
  {
122
    FILE_AS_ARRAYBUFFER,
123
    FILE_AS_BINARY,
124
    FILE_AS_TEXT,
125
    FILE_AS_DATAURL
126
  };
127
128
0
  eDataFormat DataFormat() const { return mDataFormat; }
129
0
  const nsString& Result() const { return mResult; }
130
131
private:
132
  virtual ~FileReader();
133
134
  // This must be in sync with dom/webidl/FileReader.webidl
135
  enum eReadyState {
136
    EMPTY = 0,
137
    LOADING = 1,
138
    DONE = 2
139
  };
140
141
  void RootResultArrayBuffer();
142
143
  void ReadFileContent(Blob& aBlob,
144
                       const nsAString &aCharset, eDataFormat aDataFormat,
145
                       ErrorResult& aRv);
146
  nsresult GetAsText(Blob *aBlob, const nsACString &aCharset,
147
                     const char *aFileData, uint32_t aDataLen,
148
                     nsAString &aResult);
149
  nsresult GetAsDataURL(Blob *aBlob, const char *aFileData,
150
                        uint32_t aDataLen, nsAString &aResult);
151
152
  nsresult OnLoadEnd(nsresult aStatus);
153
154
  void StartProgressEventTimer();
155
  void ClearProgressEventTimer();
156
157
  void FreeDataAndDispatchSuccess();
158
  void FreeDataAndDispatchError();
159
  void FreeDataAndDispatchError(nsresult aRv);
160
  nsresult DispatchProgressEvent(const nsAString& aType);
161
162
  nsresult DoAsyncWait();
163
  nsresult DoReadData(uint64_t aCount);
164
165
  void OnLoadEndArrayBuffer();
166
167
  void FreeFileData();
168
169
  nsresult IncreaseBusyCounter();
170
  void DecreaseBusyCounter();
171
172
  void Shutdown();
173
174
  char *mFileData;
175
  RefPtr<Blob> mBlob;
176
  nsCString mCharset;
177
  uint32_t mDataLen;
178
179
  eDataFormat mDataFormat;
180
181
  nsString mResult;
182
183
  JS::Heap<JSObject*> mResultArrayBuffer;
184
185
  nsCOMPtr<nsITimer> mProgressNotifier;
186
  bool mProgressEventWasDelayed;
187
  bool mTimerIsActive;
188
189
  nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
190
191
  RefPtr<DOMException> mError;
192
193
  eReadyState mReadyState;
194
195
  uint64_t mTotal;
196
  uint64_t mTransferred;
197
198
  nsCOMPtr<nsIEventTarget> mTarget;
199
200
  uint64_t mBusyCount;
201
202
  // This is set if FileReader is created on workers, but it is null if the
203
  // worker is shutting down. The null value is checked in ReadFileContent()
204
  // before starting any reading.
205
  RefPtr<WeakWorkerRef> mWeakWorkerRef;
206
207
  // This value is set when the reading starts in order to keep the worker alive
208
  // during the process.
209
  RefPtr<StrongWorkerRef> mStrongWorkerRef;
210
};
211
212
NS_DEFINE_STATIC_IID_ACCESSOR(FileReader, FILEREADER_ID)
213
214
} // dom namespace
215
} // mozilla namespace
216
217
#endif // mozilla_dom_FileReader_h