Bug 1843230 - Remove IsWin8OrLater checks from dom/geolocation/ r=emk
[gecko.git] / dom / file / FileReader.h
blob1c07b6193f2c3533edce000e89fa746800918407
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 "nsITimer.h"
17 #include "nsCOMPtr.h"
18 #include "nsString.h"
19 #include "nsWeakReference.h"
21 #define NS_PROGRESS_EVENT_INTERVAL 50
23 class nsITimer;
24 class nsIEventTarget;
26 namespace mozilla::dom {
28 class Blob;
29 class DOMException;
30 class OwningStringOrArrayBuffer;
31 class StrongWorkerRef;
32 class WeakWorkerRef;
34 extern const uint64_t kUnknownSize;
36 class FileReaderDecreaseBusyCounter;
38 // 26a79031-c94b-47e9-850a-f04fe17bc026
39 #define FILEREADER_ID \
40 { \
41 0x26a79031, 0xc94b, 0x47e9, { \
42 0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
43 } \
46 class FileReader final : public DOMEventTargetHelper,
47 public nsIInterfaceRequestor,
48 public nsSupportsWeakReference,
49 public nsIInputStreamCallback,
50 public nsITimerCallback,
51 public nsINamed {
52 friend class FileReaderDecreaseBusyCounter;
54 public:
55 FileReader(nsIGlobalObject* aGlobal, WeakWorkerRef* aWorkerRef);
57 NS_DECL_ISUPPORTS_INHERITED
59 NS_DECL_NSITIMERCALLBACK
60 NS_DECL_NSIINPUTSTREAMCALLBACK
61 NS_DECL_NSIINTERFACEREQUESTOR
62 NS_DECL_NSINAMED
64 NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID)
66 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader,
67 DOMEventTargetHelper)
69 JSObject* WrapObject(JSContext* aCx,
70 JS::Handle<JSObject*> aGivenProto) override;
72 // WebIDL
73 static already_AddRefed<FileReader> Constructor(const GlobalObject& aGlobal);
74 void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv) {
75 ReadFileContent(aBlob, u""_ns, 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, u""_ns, FILE_AS_TEXT, aRv);
87 void ReadAsDataURL(Blob& aBlob, ErrorResult& aRv) {
88 ReadFileContent(aBlob, u""_ns, 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, Nullable<OwningStringOrArrayBuffer>& aResult);
99 IMPL_EVENT_HANDLER(loadstart)
100 IMPL_EVENT_HANDLER(progress)
101 IMPL_EVENT_HANDLER(load)
102 IMPL_EVENT_HANDLER(abort)
103 IMPL_EVENT_HANDLER(error)
104 IMPL_EVENT_HANDLER(loadend)
106 void ReadAsBinaryString(Blob& aBlob, ErrorResult& aRv) {
107 ReadFileContent(aBlob, u""_ns, FILE_AS_BINARY, aRv);
110 enum eDataFormat {
111 FILE_AS_ARRAYBUFFER,
112 FILE_AS_BINARY,
113 FILE_AS_TEXT,
114 FILE_AS_DATAURL
117 eDataFormat DataFormat() const { return mDataFormat; }
118 const nsString& Result() const { return mResult; }
120 void InitialAsyncWait();
122 private:
123 ~FileReader() override;
125 // This must be in sync with dom/webidl/FileReader.webidl
126 enum eReadyState { EMPTY = 0, LOADING = 1, DONE = 2 };
128 void RootResultArrayBuffer();
130 void ReadFileContent(Blob& aBlob, const nsAString& aCharset,
131 eDataFormat aDataFormat, ErrorResult& aRv);
132 nsresult GetAsText(Blob* aBlob, const nsACString& aCharset,
133 const char* aFileData, uint32_t aDataLen,
134 nsAString& aResult);
135 nsresult GetAsDataURL(Blob* aBlob, const char* aFileData, uint32_t aDataLen,
136 nsAString& aResult);
138 void OnLoadEnd(nsresult aStatus);
140 void StartProgressEventTimer();
141 void ClearProgressEventTimer();
143 void FreeDataAndDispatchSuccess();
144 void FreeDataAndDispatchError();
145 void FreeDataAndDispatchError(nsresult aRv);
146 nsresult DispatchProgressEvent(const nsAString& aType);
148 nsresult DoAsyncWait();
149 nsresult DoReadData(uint64_t aCount);
151 void OnLoadEndArrayBuffer();
153 void FreeFileData();
155 nsresult IncreaseBusyCounter();
156 void DecreaseBusyCounter();
158 void Cleanup();
159 void Shutdown();
161 char* mFileData;
162 RefPtr<Blob> mBlob;
163 nsCString mCharset;
164 uint32_t mDataLen;
166 eDataFormat mDataFormat;
168 nsString mResult;
170 JS::Heap<JSObject*> mResultArrayBuffer;
172 nsCOMPtr<nsITimer> mProgressNotifier;
173 bool mProgressEventWasDelayed;
174 bool mTimerIsActive;
176 nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
178 RefPtr<DOMException> mError;
180 eReadyState mReadyState;
182 uint64_t mTotal;
183 uint64_t mTransferred;
185 nsCOMPtr<nsIEventTarget> mTarget;
187 uint64_t mBusyCount;
189 // This is set if FileReader is created on workers, but it is null if the
190 // worker is shutting down. The null value is checked in ReadFileContent()
191 // before starting any reading.
192 RefPtr<WeakWorkerRef> mWeakWorkerRef;
194 // This value is set when the reading starts in order to keep the worker alive
195 // during the process.
196 RefPtr<StrongWorkerRef> mStrongWorkerRef;
198 // Runnable to start the reading asynchronous.
199 class AsyncWaitRunnable;
200 RefPtr<AsyncWaitRunnable> mAsyncWaitRunnable;
203 NS_DEFINE_STATIC_IID_ACCESSOR(FileReader, FILEREADER_ID)
205 } // namespace mozilla::dom
207 #endif // mozilla_dom_FileReader_h