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
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
#ifndef nsDOMFileReader_h__
|
|
|
|
#define nsDOMFileReader_h__
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-06-11 03:59:45 +00:00
|
|
|
#include "nsISupportsUtils.h"
|
2011-09-29 16:06:35 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2014-06-11 03:59:45 +00:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "nsITimer.h"
|
2014-06-10 18:48:36 +00:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
#include "nsIDOMFileReader.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
#include "nsIDOMFileList.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
#include "FileIOObject.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-05-12 12:09:51 +00:00
|
|
|
class Blob;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-03-21 18:35:18 +00:00
|
|
|
class nsDOMFileReader final : public mozilla::dom::FileIOObject,
|
|
|
|
public nsIDOMFileReader,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsSupportsWeakReference
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
2013-08-23 05:17:08 +00:00
|
|
|
typedef mozilla::dom::GlobalObject GlobalObject;
|
2015-05-12 12:09:51 +00:00
|
|
|
typedef mozilla::dom::Blob Blob;
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
public:
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader();
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_DECL_NSIDOMFILEREADER
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_REALLY_FORWARD_NSIDOMEVENTTARGET(mozilla::DOMEventTargetHelper)
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
// nsIInterfaceRequestor
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
// FileIOObject overrides
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void DoAbort(nsAString& aEvent) override;
|
2014-06-10 18:48:36 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount) override;
|
2014-06-10 18:48:36 +00:00
|
|
|
|
|
|
|
virtual nsresult DoOnLoadEnd(nsresult aStatus, nsAString& aSuccessEvent,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aTerminationEvent) override;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-13 07:06:31 +00:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
static already_AddRefed<nsDOMFileReader>
|
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
|
|
|
}
|
|
|
|
|
|
|
|
using FileIOObject::Abort;
|
|
|
|
|
|
|
|
// Inherited ReadyState().
|
|
|
|
|
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
|
|
|
|
|
|
|
using FileIOObject::GetError;
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(loadstart)
|
|
|
|
using FileIOObject::GetOnprogress;
|
|
|
|
using FileIOObject::SetOnprogress;
|
|
|
|
IMPL_EVENT_HANDLER(load)
|
|
|
|
using FileIOObject::GetOnabort;
|
|
|
|
using FileIOObject::SetOnabort;
|
|
|
|
using FileIOObject::GetOnerror;
|
|
|
|
using FileIOObject::SetOnerror;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
nsresult Init();
|
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(nsDOMFileReader,
|
2011-09-29 16:06:35 +00:00
|
|
|
FileIOObject)
|
2011-07-01 00:50:44 +00:00
|
|
|
void RootResultArrayBuffer();
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
protected:
|
2014-07-08 21:23:16 +00:00
|
|
|
virtual ~nsDOMFileReader();
|
|
|
|
|
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-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
|
|
|
|
2009-11-23 06:23:23 +00:00
|
|
|
void FreeFileData() {
|
2015-02-19 04:51:06 +00:00
|
|
|
free(mFileData);
|
2012-07-30 14:20:58 +00:00
|
|
|
mFileData = nullptr;
|
2009-11-23 06:23:23 +00:00
|
|
|
mDataLen = 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-10-07 08:30:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|