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