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"
18 #include "nsWeakReference.h"
20 #define NS_PROGRESS_EVENT_INTERVAL 50
30 class StrongWorkerRef
;
33 extern const uint64_t kUnknownSize
;
35 class FileReaderDecreaseBusyCounter
;
37 // 26a79031-c94b-47e9-850a-f04fe17bc026
38 #define FILEREADER_ID \
40 0x26a79031, 0xc94b, 0x47e9, { \
41 0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
45 class FileReader final
: public DOMEventTargetHelper
,
46 public nsIInterfaceRequestor
,
47 public nsSupportsWeakReference
,
48 public nsIInputStreamCallback
,
49 public nsITimerCallback
,
51 friend class FileReaderDecreaseBusyCounter
;
54 FileReader(nsIGlobalObject
* aGlobal
, WeakWorkerRef
* aWorkerRef
);
56 NS_DECL_ISUPPORTS_INHERITED
58 NS_DECL_NSITIMERCALLBACK
59 NS_DECL_NSIINPUTSTREAMCALLBACK
60 NS_DECL_NSIINTERFACEREQUESTOR
63 NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID
)
65 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader
,
68 virtual JSObject
* WrapObject(JSContext
* aCx
,
69 JS::Handle
<JSObject
*> aGivenProto
) override
;
72 static already_AddRefed
<FileReader
> Constructor(const GlobalObject
& aGlobal
,
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
,
80 if (aLabel
.WasPassed()) {
81 ReadFileContent(aBlob
, aLabel
.Value(), FILE_AS_TEXT
, aRv
);
83 ReadFileContent(aBlob
, EmptyString(), FILE_AS_TEXT
, aRv
);
87 void ReadAsDataURL(Blob
& aBlob
, ErrorResult
& aRv
) {
88 ReadFileContent(aBlob
, EmptyString(), 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
, JS::MutableHandle
<JS::Value
> aResult
,
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
);
118 eDataFormat
DataFormat() const { return mDataFormat
; }
119 const nsString
& Result() const { return mResult
; }
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
,
134 nsresult
GetAsDataURL(Blob
* aBlob
, const char* aFileData
, uint32_t aDataLen
,
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();
154 nsresult
IncreaseBusyCounter();
155 void DecreaseBusyCounter();
164 eDataFormat mDataFormat
;
168 JS::Heap
<JSObject
*> mResultArrayBuffer
;
170 nsCOMPtr
<nsITimer
> mProgressNotifier
;
171 bool mProgressEventWasDelayed
;
174 nsCOMPtr
<nsIAsyncInputStream
> mAsyncStream
;
176 RefPtr
<DOMException
> mError
;
178 eReadyState mReadyState
;
181 uint64_t mTransferred
;
183 nsCOMPtr
<nsIEventTarget
> mTarget
;
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
)
200 } // namespace mozilla
202 #endif // mozilla_dom_FileReader_h