2009-10-07 01:30:26 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
#ifndef nsDOMFileReader_h__
|
|
|
|
#define nsDOMFileReader_h__
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-06-10 20:59:45 -07:00
|
|
|
#include "nsISupportsUtils.h"
|
2011-09-29 09:06:35 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2014-06-10 20:59:45 -07:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "nsITimer.h"
|
2014-06-10 11:48:36 -07:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
#include "nsIDOMFileReader.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
#include "nsIDOMFileList.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
#include "FileIOObject.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2014-10-08 09:15:22 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-10-08 09:15:23 -07:00
|
|
|
class File;
|
2014-10-08 09:15:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
class nsDOMFileReader : public mozilla::dom::FileIOObject,
|
2009-11-09 12:05:16 -08:00
|
|
|
public nsIDOMFileReader,
|
|
|
|
public nsIInterfaceRequestor,
|
2013-04-13 00:06:31 -07:00
|
|
|
public nsSupportsWeakReference
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
2013-08-22 22:17:08 -07:00
|
|
|
typedef mozilla::dom::GlobalObject GlobalObject;
|
2014-10-08 09:15:23 -07:00
|
|
|
typedef mozilla::dom::File File;
|
2009-10-07 01:30:26 -07:00
|
|
|
public:
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader();
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_DECL_NSIDOMFILEREADER
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_REALLY_FORWARD_NSIDOMEVENTTARGET(mozilla::DOMEventTargetHelper)
|
2011-09-29 09:06:35 -07:00
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
// nsIInterfaceRequestor
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
// FileIOObject overrides
|
2013-04-13 00:06:31 -07:00
|
|
|
virtual void DoAbort(nsAString& aEvent) MOZ_OVERRIDE;
|
2014-06-10 11:48:36 -07:00
|
|
|
|
|
|
|
virtual nsresult DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
virtual nsresult DoOnLoadEnd(nsresult aStatus, nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent) MOZ_OVERRIDE;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-04-13 00:06:31 -07:00
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
static already_AddRefed<nsDOMFileReader>
|
2013-08-22 22:17:08 -07:00
|
|
|
Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
|
2014-10-08 09:15:23 -07:00
|
|
|
void ReadAsArrayBuffer(JSContext* aCx, File& aBlob, ErrorResult& aRv)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
2014-10-07 10:44:07 -07:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_ARRAYBUFFER, aRv);
|
2013-04-13 00:06:31 -07:00
|
|
|
}
|
2014-06-10 11:48:36 -07:00
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
void ReadAsText(File& aBlob, const nsAString& aLabel, ErrorResult& aRv)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
2014-10-07 10:44:07 -07:00
|
|
|
ReadFileContent(aBlob, aLabel, FILE_AS_TEXT, aRv);
|
2013-04-13 00:06:31 -07:00
|
|
|
}
|
2014-06-10 11:48:36 -07:00
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
void ReadAsDataURL(File& aBlob, ErrorResult& aRv)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
2014-10-07 10:44:07 -07:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_DATAURL, aRv);
|
2013-04-13 00:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using FileIOObject::Abort;
|
|
|
|
|
|
|
|
// Inherited ReadyState().
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 00:06:31 -07: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)
|
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
void ReadAsBinaryString(File& aBlob, ErrorResult& aRv)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
2014-10-07 10:44:07 -07:00
|
|
|
ReadFileContent(aBlob, EmptyString(), FILE_AS_BINARY, aRv);
|
2013-04-13 00:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
nsresult Init();
|
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(nsDOMFileReader,
|
2011-09-29 09:06:35 -07:00
|
|
|
FileIOObject)
|
2011-06-30 17:50:44 -07:00
|
|
|
void RootResultArrayBuffer();
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
protected:
|
2014-07-08 14:23:16 -07:00
|
|
|
virtual ~nsDOMFileReader();
|
|
|
|
|
2009-11-24 17:53:17 -08:00
|
|
|
enum eDataFormat {
|
2011-06-30 17:50:44 -07:00
|
|
|
FILE_AS_ARRAYBUFFER,
|
2009-11-24 17:53:17 -08:00
|
|
|
FILE_AS_BINARY,
|
|
|
|
FILE_AS_TEXT,
|
|
|
|
FILE_AS_DATAURL
|
|
|
|
};
|
|
|
|
|
2014-10-07 10:44:07 -07:00
|
|
|
void ReadFileContent(File& aBlob,
|
2013-04-13 00:06:31 -07:00
|
|
|
const nsAString &aCharset, eDataFormat aDataFormat,
|
|
|
|
ErrorResult& aRv);
|
2013-12-17 02:47:25 -08:00
|
|
|
nsresult GetAsText(nsIDOMBlob *aFile, const nsACString &aCharset,
|
2012-08-22 08:56:38 -07:00
|
|
|
const char *aFileData, uint32_t aDataLen, nsAString &aResult);
|
2014-06-10 20:59:45 -07:00
|
|
|
nsresult GetAsDataURL(nsIDOMBlob *aFile, const char *aFileData, uint32_t aDataLen, nsAString &aResult);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-22 22:23:23 -08:00
|
|
|
void FreeFileData() {
|
2012-12-05 14:20:57 -08:00
|
|
|
moz_free(mFileData);
|
2012-07-30 07:20:58 -07:00
|
|
|
mFileData = nullptr;
|
2009-11-22 22:23:23 -08:00
|
|
|
mDataLen = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
char *mFileData;
|
2010-10-13 16:25:33 -07:00
|
|
|
nsCOMPtr<nsIDOMBlob> mFile;
|
2010-10-07 09:36:23 -07:00
|
|
|
nsCString mCharset;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mDataLen;
|
2009-11-24 17:53:17 -08:00
|
|
|
|
|
|
|
eDataFormat mDataFormat;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
nsString mResult;
|
2014-06-10 11:48:36 -07:00
|
|
|
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Heap<JSObject*> mResultArrayBuffer;
|
2009-10-07 01:30:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|