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"
|
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"
|
2016-06-23 11:53:14 +03:00
|
|
|
#include "WorkerHolder.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;
|
2015-12-18 11:44:00 +03:00
|
|
|
class DOMError;
|
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class WorkerPrivate;
|
|
|
|
}
|
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;
|
|
|
|
|
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,
|
2016-06-23 11:53:14 +03:00
|
|
|
public workers::WorkerHolder
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
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,
|
2015-12-18 11:44:00 +03:00
|
|
|
workers::WorkerPrivate* aWorkerPrivate);
|
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
|
|
|
|
|
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
|
2015-12-09 14:22:40 +03:00
|
|
|
static already_AddRefed<FileReader>
|
2013-08-23 09:17:08 +04:00
|
|
|
Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadAsArrayBuffer(JSContext* aCx, Blob& aBlob, ErrorResult& aRv)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
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
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadAsText(Blob& aBlob, const nsAString& aLabel, ErrorResult& aRv)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
2014-10-07 21:44:07 +04:00
|
|
|
ReadFileContent(aBlob, aLabel, 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)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
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
|
|
|
|
|
|
|
uint16_t ReadyState() const
|
|
|
|
{
|
2015-12-09 23:52:15 +03:00
|
|
|
return static_cast<uint16_t>(mReadyState);
|
2015-12-09 14:23:16 +03:00
|
|
|
}
|
2013-04-13 11:06:31 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
DOMError* 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)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
2014-10-07 21:44:07 +04:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
// WorkerHolder
|
2016-03-28 20:28:14 +03:00
|
|
|
bool Notify(workers::Status) override;
|
2015-12-18 11:44:00 +03:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2009-11-25 04:53:17 +03:00
|
|
|
enum eDataFormat {
|
2011-07-01 04:50:44 +04:00
|
|
|
FILE_AS_ARRAYBUFFER,
|
2009-11-25 04:53:17 +03:00
|
|
|
FILE_AS_BINARY,
|
|
|
|
FILE_AS_TEXT,
|
|
|
|
FILE_AS_DATAURL
|
|
|
|
};
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
void RootResultArrayBuffer();
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void ReadFileContent(Blob& aBlob,
|
2013-04-13 11:06:31 +04:00
|
|
|
const nsAString &aCharset, 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,
|
2015-05-12 15:09:51 +03:00
|
|
|
uint32_t aDataLen, 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
|
|
|
|
|
|
|
void FreeFileData()
|
|
|
|
{
|
2015-12-17 16:13:59 +03:00
|
|
|
free(mFileData);
|
|
|
|
mFileData = nullptr;
|
|
|
|
mDataLen = 0;
|
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;
|
|
|
|
|
|
|
|
RefPtr<DOMError> mError;
|
|
|
|
|
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;
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
// Kept alive with a WorkerHolder.
|
2015-12-18 11:44:00 +03:00
|
|
|
workers::WorkerPrivate* mWorkerPrivate;
|
2009-10-07 12:30:26 +04:00
|
|
|
};
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FileReader_h
|