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