2015-05-03 22:32:37 +03: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 15:12:37 +04: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 12:30:26 +04:00
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
#ifndef mozilla_dom_FileReader_h
|
|
|
|
#define mozilla_dom_FileReader_h
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-12-09 14:23:16 +03:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
|
|
|
|
#include "nsIAsyncInputStream.h"
|
2014-06-11 07:59:45 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2015-12-09 14:23:16 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
#define NS_PROGRESS_EVENT_INTERVAL 50
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
class nsITimer;
|
|
|
|
class nsIEventTarget;
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-12-09 14:22:40 +03:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
class Blob;
|
2017-08-06 20:47:00 +03:00
|
|
|
class DOMException;
|
2018-03-13 23:17:06 +03:00
|
|
|
class StrongWorkerRef;
|
|
|
|
class WeakWorkerRef;
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
extern const uint64_t kUnknownSize;
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
class FileReaderDecreaseBusyCounter;
|
|
|
|
|
2018-09-09 01:44:23 +03:00
|
|
|
// 26a79031-c94b-47e9-850a-f04fe17bc026
|
|
|
|
#define FILEREADER_ID \
|
|
|
|
{ \
|
|
|
|
0x26a79031, 0xc94b, 0x47e9, { \
|
|
|
|
0x85, 0x0a, 0xf0, 0x4f, 0xe1, 0x7b, 0xc0, 0x26 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
class FileReader final : public DOMEventTargetHelper,
|
2015-12-09 14:22:40 +03:00
|
|
|
public nsIInterfaceRequestor,
|
2015-12-09 14:23:16 +03:00
|
|
|
public nsSupportsWeakReference,
|
|
|
|
public nsIInputStreamCallback,
|
2015-12-18 11:44:00 +03:00
|
|
|
public nsITimerCallback,
|
2018-03-13 23:17:06 +03:00
|
|
|
public nsINamed {
|
2015-12-18 11:44:00 +03:00
|
|
|
friend class FileReaderDecreaseBusyCounter;
|
|
|
|
|
2009-10-07 12:30:26 +04:00
|
|
|
public:
|
2016-01-20 21:17:57 +03:00
|
|
|
FileReader(nsIGlobalObject* aGlobal, WeakWorkerRef* aWorkerRef);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_DECL_NSINAMED
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2018-09-09 01:44:23 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(FILEREADER_ID)
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(FileReader,
|
|
|
|
DOMEventTargetHelper)
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-13 11:06:31 +04:00
|
|
|
|
|
|
|
// WebIDL
|
2013-08-23 09:17:08 +04:00
|
|
|
static already_AddRefed<FileReader> Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& aRv);
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 21:44:07 +04:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2017-10-17 14:41:01 +03: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 11:06:31 +04:00
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadAsDataURL(Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 21:44:07 +04:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_DATAURL, aRv);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|
|
|
|
|
2016-12-02 16:16:59 +03:00
|
|
|
void Abort();
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
uint16_t ReadyState() const { return static_cast<uint16_t>(mReadyState); }
|
2013-04-13 11:06:31 +04:00
|
|
|
|
2017-08-06 20:47:00 +03:00
|
|
|
DOMException* GetError() const { return mError; }
|
2013-04-13 11:06:31 +04:00
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 11:06:31 +04:00
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(loadstart)
|
2015-12-09 14:23:16 +03:00
|
|
|
IMPL_EVENT_HANDLER(progress)
|
2013-04-13 11:06:31 +04:00
|
|
|
IMPL_EVENT_HANDLER(load)
|
2015-12-09 14:23:16 +03:00
|
|
|
IMPL_EVENT_HANDLER(abort)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
2013-04-13 11:06:31 +04:00
|
|
|
IMPL_EVENT_HANDLER(loadend)
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadAsBinaryString(Blob& aBlob, ErrorResult& aRv) {
|
2014-10-07 21:44:07 +04:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|
|
|
|
|
2018-09-09 01:44:23 +03: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 14:23:16 +03:00
|
|
|
private:
|
2015-12-09 14:22:40 +03:00
|
|
|
virtual ~FileReader();
|
2014-07-09 01:23:16 +04:00
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
// This must be in sync with dom/webidl/FileReader.webidl
|
|
|
|
enum eReadyState { EMPTY = 0, LOADING = 1, DONE = 2 };
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
void RootResultArrayBuffer();
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadFileContent(Blob& aBlob, const nsAString& aCharset,
|
2013-04-13 11:06:31 +04:00
|
|
|
eDataFormat aDataFormat, ErrorResult& aRv);
|
2015-05-19 17:36:37 +03:00
|
|
|
nsresult GetAsText(Blob* aBlob, const nsACString& aCharset,
|
2015-05-12 15:09:51 +03:00
|
|
|
const char* aFileData, uint32_t aDataLen,
|
|
|
|
nsAString& aResult);
|
2015-05-19 17:36:37 +03:00
|
|
|
nsresult GetAsDataURL(Blob* aBlob, const char* aFileData, uint32_t aDataLen,
|
2015-05-12 15:09:51 +03:00
|
|
|
nsAString& aResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
nsresult OnLoadEnd(nsresult aStatus);
|
|
|
|
|
|
|
|
void StartProgressEventTimer();
|
|
|
|
void ClearProgressEventTimer();
|
2017-01-24 14:12:15 +03:00
|
|
|
|
|
|
|
void FreeDataAndDispatchSuccess();
|
|
|
|
void FreeDataAndDispatchError();
|
|
|
|
void FreeDataAndDispatchError(nsresult aRv);
|
2015-12-09 14:23:16 +03:00
|
|
|
nsresult DispatchProgressEvent(const nsAString& aType);
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
nsresult DoAsyncWait();
|
|
|
|
nsresult DoReadData(uint64_t aCount);
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
void OnLoadEndArrayBuffer();
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2018-09-18 17:57:04 +03:00
|
|
|
void FreeFileData();
|
2009-11-23 09:23:23 +03:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
nsresult IncreaseBusyCounter();
|
|
|
|
void DecreaseBusyCounter();
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
2009-10-07 12:30:26 +04:00
|
|
|
char* mFileData;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> mBlob;
|
2010-10-07 20:36:23 +04:00
|
|
|
nsCString mCharset;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mDataLen;
|
2009-11-25 04:53:17 +03:00
|
|
|
|
|
|
|
eDataFormat mDataFormat;
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
nsString mResult;
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2013-06-18 14:00:38 +04:00
|
|
|
JS::Heap<JSObject*> mResultArrayBuffer;
|
2015-12-09 14:23:16 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mProgressNotifier;
|
|
|
|
bool mProgressEventWasDelayed;
|
|
|
|
bool mTimerIsActive;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
|
|
|
|
|
2017-08-06 20:47:00 +03:00
|
|
|
RefPtr<DOMException> mError;
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
eReadyState mReadyState;
|
2015-12-09 14:23:16 +03:00
|
|
|
|
|
|
|
uint64_t mTotal;
|
|
|
|
uint64_t mTransferred;
|
2015-12-18 11:44:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
|
|
|
|
|
|
|
uint64_t mBusyCount;
|
|
|
|
|
2018-03-13 23:17:06 +03: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 12:30:26 +04:00
|
|
|
};
|
|
|
|
|
2018-09-09 01:44:23 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(FileReader, FILEREADER_ID)
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FileReader_h
|