2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2015-12-09 11:22:40 +00:00
|
|
|
#ifndef mozilla_dom_FileReader_h
|
|
|
|
#define mozilla_dom_FileReader_h
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-12-09 11:23:16 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
|
|
|
|
#include "nsIAsyncInputStream.h"
|
2014-06-11 03:59:45 +00:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2017-07-26 18:18:20 +00:00
|
|
|
#include "nsINamed.h"
|
2015-12-18 08:44:00 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2015-12-09 11:23:16 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
#define NS_PROGRESS_EVENT_INTERVAL 50
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
class nsITimer;
|
|
|
|
class nsIEventTarget;
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-12-09 11:22:40 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
class Blob;
|
2017-08-06 17:47:00 +00:00
|
|
|
class DOMException;
|
2018-03-13 20:17:06 +00:00
|
|
|
class StrongWorkerRef;
|
|
|
|
class WeakWorkerRef;
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
extern const uint64_t kUnknownSize;
|
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
class FileReaderDecreaseBusyCounter;
|
|
|
|
|
2018-09-08 22:44:23 +00:00
|
|
|
// 26a79031-c94b-47e9-850a-f04fe17bc026
|
|
|
|
#define FILEREADER_ID \
|
|
|
|
{ \
|
|
|
|
0x26a79031, 0xc94b, 0x47e9, { \
|
|
|
|
0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
class FileReader final : public DOMEventTargetHelper,
|
2015-12-09 11:22:40 +00:00
|
|
|
public nsIInterfaceRequestor,
|
2015-12-09 11:23:16 +00:00
|
|
|
public nsSupportsWeakReference,
|
|
|
|
public nsIInputStreamCallback,
|
2015-12-18 08:44:00 +00:00
|
|
|
public nsITimerCallback,
|
2018-03-13 20:17:06 +00:00
|
|
|
public nsINamed {
|
2015-12-18 08:44:00 +00:00
|
|
|
friend class FileReaderDecreaseBusyCounter;
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
public:
|
2016-01-20 18:17:57 +00:00
|
|
|
FileReader(nsIGlobalObject* aGlobal, WeakWorkerRef* aWorkerRef);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2017-07-26 18:18:20 +00:00
|
|
|
NS_DECL_NSINAMED
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2018-09-08 22:44:23 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID)
|
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader,
|
|
|
|
DOMEventTargetHelper)
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-13 07:06:31 +00:00
|
|
|
|
|
|
|
// WebIDL
|
2013-08-23 05:17:08 +00:00
|
|
|
static already_AddRefed<FileReader> Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& aRv);
|
2015-05-12 12:09:51 +00:00
|
|
|
void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 17:44:07 +00:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv);
|
2013-04-13 07:06:31 +00:00
|
|
|
}
|
2014-06-10 18:48:36 +00:00
|
|
|
|
2017-10-17 11:41:01 +00:00
|
|
|
void ReadAsText(Blob& aBlob, const Optional<nsAString>& aLabel,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (aLabel.WasPassed()) {
|
|
|
|
ReadFileContent(aBlob, aLabel.Value(), FILE_AS_TEXT, aRv);
|
|
|
|
} else {
|
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_TEXT, aRv);
|
|
|
|
}
|
2013-04-13 07:06:31 +00:00
|
|
|
}
|
2014-06-10 18:48:36 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
void ReadAsDataURL(Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 17:44:07 +00:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_DATAURL, aRv);
|
2013-04-13 07:06:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 13:16:59 +00:00
|
|
|
void Abort();
|
2015-12-09 11:23:16 +00:00
|
|
|
|
2015-12-09 20:52:15 +00:00
|
|
|
uint16_t ReadyState() const { return static_cast<uint16_t>(mReadyState); }
|
2013-04-13 07:06:31 +00:00
|
|
|
|
2017-08-06 17:47:00 +00:00
|
|
|
DOMException* GetError() const { return mError; }
|
2013-04-13 07:06:31 +00:00
|
|
|
|
2014-06-11 20:26:52 +00:00
|
|
|
void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 07:06:31 +00:00
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(loadstart)
|
2015-12-09 11:23:16 +00:00
|
|
|
IMPL_EVENT_HANDLER(progress)
|
2013-04-13 07:06:31 +00:00
|
|
|
IMPL_EVENT_HANDLER(load)
|
2015-12-09 11:23:16 +00:00
|
|
|
IMPL_EVENT_HANDLER(abort)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
2013-04-13 07:06:31 +00:00
|
|
|
IMPL_EVENT_HANDLER(loadend)
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
void ReadAsBinaryString(Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 17:44:07 +00:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
|
2013-04-13 07:06:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 22:44:23 +00:00
|
|
|
enum eDataFormat {
|
|
|
|
FILE_AS_ARRAYBUFFER,
|
|
|
|
FILE_AS_BINARY,
|
|
|
|
FILE_AS_TEXT,
|
|
|
|
FILE_AS_DATAURL
|
|
|
|
};
|
|
|
|
|
|
|
|
eDataFormat DataFormat() const { return mDataFormat; }
|
|
|
|
const nsString& Result() const { return mResult; }
|
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
private:
|
2015-12-09 11:22:40 +00:00
|
|
|
virtual ~FileReader();
|
2014-07-08 21:23:16 +00:00
|
|
|
|
2015-12-09 20:52:15 +00:00
|
|
|
// This must be in sync with dom/webidl/FileReader.webidl
|
|
|
|
enum eReadyState { EMPTY = 0, LOADING = 1, DONE = 2 };
|
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
void RootResultArrayBuffer();
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
void ReadFileContent(Blob& aBlob, const nsAString& aCharset,
|
2013-04-13 07:06:31 +00:00
|
|
|
eDataFormat aDataFormat, ErrorResult& aRv);
|
2015-05-19 14:36:37 +00:00
|
|
|
nsresult GetAsText(Blob* aBlob, const nsACString& aCharset,
|
2015-05-12 12:09:51 +00:00
|
|
|
const char* aFileData, uint32_t aDataLen,
|
|
|
|
nsAString& aResult);
|
2015-05-19 14:36:37 +00:00
|
|
|
nsresult GetAsDataURL(Blob* aBlob, const char* aFileData, uint32_t aDataLen,
|
2015-05-12 12:09:51 +00:00
|
|
|
nsAString& aResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2015-12-09 11:23:16 +00:00
|
|
|
nsresult OnLoadEnd(nsresult aStatus);
|
|
|
|
|
|
|
|
void StartProgressEventTimer();
|
|
|
|
void ClearProgressEventTimer();
|
2017-01-24 11:12:15 +00:00
|
|
|
|
|
|
|
void FreeDataAndDispatchSuccess();
|
|
|
|
void FreeDataAndDispatchError();
|
|
|
|
void FreeDataAndDispatchError(nsresult aRv);
|
2015-12-09 11:23:16 +00:00
|
|
|
nsresult DispatchProgressEvent(const nsAString& aType);
|
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
nsresult DoAsyncWait();
|
|
|
|
nsresult DoReadData(uint64_t aCount);
|
2015-12-09 11:23:16 +00:00
|
|
|
|
2017-01-24 11:12:15 +00:00
|
|
|
void OnLoadEndArrayBuffer();
|
2015-12-09 11:23:16 +00:00
|
|
|
|
2018-09-18 14:57:04 +00:00
|
|
|
void FreeFileData();
|
2009-11-23 06:23:23 +00:00
|
|
|
|
2015-12-18 08:44:00 +00:00
|
|
|
nsresult IncreaseBusyCounter();
|
|
|
|
void DecreaseBusyCounter();
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
char* mFileData;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> mBlob;
|
2010-10-07 16:36:23 +00:00
|
|
|
nsCString mCharset;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mDataLen;
|
2009-11-25 01:53:17 +00:00
|
|
|
|
|
|
|
eDataFormat mDataFormat;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
nsString mResult;
|
2014-06-10 18:48:36 +00:00
|
|
|
|
2013-06-18 10:00:38 +00:00
|
|
|
JS::Heap<JSObject*> mResultArrayBuffer;
|
2015-12-09 11:23:16 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mProgressNotifier;
|
|
|
|
bool mProgressEventWasDelayed;
|
|
|
|
bool mTimerIsActive;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
|
|
|
|
|
2017-08-06 17:47:00 +00:00
|
|
|
RefPtr<DOMException> mError;
|
2015-12-09 11:23:16 +00:00
|
|
|
|
2015-12-09 20:52:15 +00:00
|
|
|
eReadyState mReadyState;
|
2015-12-09 11:23:16 +00:00
|
|
|
|
|
|
|
uint64_t mTotal;
|
|
|
|
uint64_t mTransferred;
|
2015-12-18 08:44:00 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
|
|
|
|
|
|
|
uint64_t mBusyCount;
|
|
|
|
|
2018-03-13 20:17:06 +00:00
|
|
|
// This is set if FileReader is created on workers, but it is null if the
|
|
|
|
// worker is shutting down. The null value is checked in ReadFileContent()
|
|
|
|
// before starting any reading.
|
|
|
|
RefPtr<WeakWorkerRef> mWeakWorkerRef;
|
|
|
|
|
|
|
|
// This value is set when the reading starts in order to keep the worker alive
|
|
|
|
// during the process.
|
|
|
|
RefPtr<StrongWorkerRef> mStrongWorkerRef;
|
2009-10-07 08:30:26 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 22:44:23 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(FileReader, FILEREADER_ID)
|
|
|
|
|
2015-12-09 11:22:40 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FileReader_h
|