Bug 1523562 [wpt PR 15079] - Pass the full path to the flake8 config files, a=testonly
[gecko.git] / dom / file / FileReader.h
blobdf2ad83ba4e9dc2202d87db2dcfcb34b197ad0a3
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/. */
7 #ifndef mozilla_dom_FileReader_h
8 #define mozilla_dom_FileReader_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsIInterfaceRequestor.h"
15 #include "nsINamed.h"
16 #include "nsCOMPtr.h"
17 #include "nsString.h"
18 #include "nsWeakReference.h"
20 #define NS_PROGRESS_EVENT_INTERVAL 50
22 class nsITimer;
23 class nsIEventTarget;
25 namespace mozilla {
26 namespace dom {
28 class Blob;
29 class DOMException;
30 class StrongWorkerRef;
31 class WeakWorkerRef;
33 extern const uint64_t kUnknownSize;
35 class FileReaderDecreaseBusyCounter;
37 // 26a79031-c94b-47e9-850a-f04fe17bc026
38 #define FILEREADER_ID \
39 { \
40 0x26a79031, 0xc94b, 0x47e9, { \
41 0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
42 } \
45 class FileReader final : public DOMEventTargetHelper,
46 public nsIInterfaceRequestor,
47 public nsSupportsWeakReference,
48 public nsIInputStreamCallback,
49 public nsITimerCallback,
50 public nsINamed {
51 friend class FileReaderDecreaseBusyCounter;
53 public:
54 FileReader(nsIGlobalObject* aGlobal, WeakWorkerRef* aWorkerRef);
56 NS_DECL_ISUPPORTS_INHERITED
58 NS_DECL_NSITIMERCALLBACK
59 NS_DECL_NSIINPUTSTREAMCALLBACK
60 NS_DECL_NSIINTERFACEREQUESTOR
61 NS_DECL_NSINAMED
63 NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID)
65 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader,
66 DOMEventTargetHelper)
68 virtual JSObject* WrapObject(JSContext* aCx,
69 JS::Handle<JSObject*> aGivenProto) override;
71 // WebIDL
72 static already_AddRefed<FileReader> Constructor(const GlobalObject& aGlobal,
73 ErrorResult& aRv);
74 void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv) {
75 ReadFileContent(aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv);
78 void ReadAsText(Blob& aBlob, const Optional<nsAString>& aLabel,
79 ErrorResult& aRv) {
80 if (aLabel.WasPassed()) {
81 ReadFileContent(aBlob, aLabel.Value(), FILE_AS_TEXT, aRv);
82 } else {
83 ReadFileContent(aBlob, EmptyString(), FILE_AS_TEXT, aRv);
87 void ReadAsDataURL(Blob& aBlob, ErrorResult& aRv) {
88 ReadFileContent(aBlob, EmptyString(), FILE_AS_DATAURL, aRv);
91 void Abort();
93 uint16_t ReadyState() const { return static_cast<uint16_t>(mReadyState); }
95 DOMException* GetError() const { return mError; }
97 void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
98 ErrorResult& aRv);
100 IMPL_EVENT_HANDLER(loadstart)
101 IMPL_EVENT_HANDLER(progress)
102 IMPL_EVENT_HANDLER(load)
103 IMPL_EVENT_HANDLER(abort)
104 IMPL_EVENT_HANDLER(error)
105 IMPL_EVENT_HANDLER(loadend)
107 void ReadAsBinaryString(Blob& aBlob, ErrorResult& aRv) {
108 ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
111 enum eDataFormat {
112 FILE_AS_ARRAYBUFFER,
113 FILE_AS_BINARY,
114 FILE_AS_TEXT,
115 FILE_AS_DATAURL
118 eDataFormat DataFormat() const { return mDataFormat; }
119 const nsString& Result() const { return mResult; }
121 private:
122 virtual ~FileReader();
124 // This must be in sync with dom/webidl/FileReader.webidl
125 enum eReadyState { EMPTY = 0, LOADING = 1, DONE = 2 };
127 void RootResultArrayBuffer();
129 void ReadFileContent(Blob& aBlob, const nsAString& aCharset,
130 eDataFormat aDataFormat, ErrorResult& aRv);
131 nsresult GetAsText(Blob* aBlob, const nsACString& aCharset,
132 const char* aFileData, uint32_t aDataLen,
133 nsAString& aResult);
134 nsresult GetAsDataURL(Blob* aBlob, const char* aFileData, uint32_t aDataLen,
135 nsAString& aResult);
137 nsresult OnLoadEnd(nsresult aStatus);
139 void StartProgressEventTimer();
140 void ClearProgressEventTimer();
142 void FreeDataAndDispatchSuccess();
143 void FreeDataAndDispatchError();
144 void FreeDataAndDispatchError(nsresult aRv);
145 nsresult DispatchProgressEvent(const nsAString& aType);
147 nsresult DoAsyncWait();
148 nsresult DoReadData(uint64_t aCount);
150 void OnLoadEndArrayBuffer();
152 void FreeFileData();
154 nsresult IncreaseBusyCounter();
155 void DecreaseBusyCounter();
157 void Shutdown();
159 char* mFileData;
160 RefPtr<Blob> mBlob;
161 nsCString mCharset;
162 uint32_t mDataLen;
164 eDataFormat mDataFormat;
166 nsString mResult;
168 JS::Heap<JSObject*> mResultArrayBuffer;
170 nsCOMPtr<nsITimer> mProgressNotifier;
171 bool mProgressEventWasDelayed;
172 bool mTimerIsActive;
174 nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
176 RefPtr<DOMException> mError;
178 eReadyState mReadyState;
180 uint64_t mTotal;
181 uint64_t mTransferred;
183 nsCOMPtr<nsIEventTarget> mTarget;
185 uint64_t mBusyCount;
187 // This is set if FileReader is created on workers, but it is null if the
188 // worker is shutting down. The null value is checked in ReadFileContent()
189 // before starting any reading.
190 RefPtr<WeakWorkerRef> mWeakWorkerRef;
192 // This value is set when the reading starts in order to keep the worker alive
193 // during the process.
194 RefPtr<StrongWorkerRef> mStrongWorkerRef;
197 NS_DEFINE_STATIC_IID_ACCESSOR(FileReader, FILEREADER_ID)
199 } // namespace dom
200 } // namespace mozilla
202 #endif // mozilla_dom_FileReader_h