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"
19 #include "nsWeakReference.h"
21 #define NS_PROGRESS_EVENT_INTERVAL 50
26 namespace mozilla::dom
{
30 class OwningStringOrArrayBuffer
;
31 class StrongWorkerRef
;
34 extern const uint64_t kUnknownSize
;
36 class FileReaderDecreaseBusyCounter
;
38 // 26a79031-c94b-47e9-850a-f04fe17bc026
39 #define FILEREADER_ID \
41 0x26a79031, 0xc94b, 0x47e9, { \
42 0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
46 class FileReader final
: public DOMEventTargetHelper
,
47 public nsIInterfaceRequestor
,
48 public nsSupportsWeakReference
,
49 public nsIInputStreamCallback
,
50 public nsITimerCallback
,
52 friend class FileReaderDecreaseBusyCounter
;
55 FileReader(nsIGlobalObject
* aGlobal
, WeakWorkerRef
* aWorkerRef
);
57 NS_DECL_ISUPPORTS_INHERITED
59 NS_DECL_NSITIMERCALLBACK
60 NS_DECL_NSIINPUTSTREAMCALLBACK
61 NS_DECL_NSIINTERFACEREQUESTOR
64 NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID
)
66 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader
,
69 JSObject
* WrapObject(JSContext
* aCx
,
70 JS::Handle
<JSObject
*> aGivenProto
) override
;
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
,
80 if (aLabel
.WasPassed()) {
81 ReadFileContent(aBlob
, aLabel
.Value(), FILE_AS_TEXT
, aRv
);
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
);
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
);
117 eDataFormat
DataFormat() const { return mDataFormat
; }
118 const nsString
& Result() const { return mResult
; }
120 void InitialAsyncWait();
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
,
135 nsresult
GetAsDataURL(Blob
* aBlob
, const char* aFileData
, uint32_t aDataLen
,
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();
155 nsresult
IncreaseBusyCounter();
156 void DecreaseBusyCounter();
166 eDataFormat mDataFormat
;
170 JS::Heap
<JSObject
*> mResultArrayBuffer
;
172 nsCOMPtr
<nsITimer
> mProgressNotifier
;
173 bool mProgressEventWasDelayed
;
176 nsCOMPtr
<nsIAsyncInputStream
> mAsyncStream
;
178 RefPtr
<DOMException
> mError
;
180 eReadyState mReadyState
;
183 uint64_t mTransferred
;
185 nsCOMPtr
<nsIEventTarget
> mTarget
;
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