2011-08-15 20:40:38 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
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/. */
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
#include "FileReaderSync.h"
|
|
|
|
|
|
|
|
#include "nsIDOMFile.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2012-01-15 00:13:08 -08:00
|
|
|
#include "jsfriendapi.h"
|
2011-08-15 20:40:38 -07:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
|
|
|
|
#include "Exceptions.h"
|
|
|
|
#include "File.h"
|
|
|
|
#include "FileReaderSyncPrivate.h"
|
|
|
|
#include "WorkerInlines.h"
|
|
|
|
|
|
|
|
#define FUNCTION_FLAGS \
|
|
|
|
JSPROP_ENUMERATE
|
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
using mozilla::dom::workers::exceptions::ThrowDOMExceptionForNSResult;
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
EnsureSucceededOrThrow(JSContext* aCx, nsresult rv)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
rv = rv == NS_ERROR_FILE_NOT_FOUND ?
|
|
|
|
NS_ERROR_DOM_FILE_NOT_FOUND_ERR :
|
|
|
|
NS_ERROR_DOM_FILE_NOT_READABLE_ERR;
|
|
|
|
ThrowDOMExceptionForNSResult(aCx, rv);
|
2011-08-15 20:40:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsIDOMBlob*
|
|
|
|
GetDOMBlobFromJSObject(JSContext* aCx, JSObject* aObj) {
|
2012-02-03 10:00:08 -08:00
|
|
|
// aObj can be null as JS_ConvertArguments("o") successfully converts JS
|
|
|
|
// null to a null pointer to JSObject
|
2011-08-15 20:40:38 -07:00
|
|
|
if (aObj) {
|
2012-02-05 12:07:23 -08:00
|
|
|
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aObj);
|
2011-08-15 20:40:38 -07:00
|
|
|
if (blob) {
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
|
2012-02-03 16:54:57 -08:00
|
|
|
aObj ? JS_GetClass(aObj)->name : "Object", "not a Blob.");
|
2011-08-15 20:40:38 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileReaderSync
|
|
|
|
{
|
|
|
|
// FileReaderSync should not be instantiated.
|
|
|
|
FileReaderSync();
|
|
|
|
~FileReaderSync();
|
|
|
|
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0,
|
|
|
|
NULL, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FileReaderSyncPrivate*
|
2012-02-05 12:07:23 -08:00
|
|
|
GetPrivate(JSObject* aObj)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
if (aObj) {
|
2012-02-03 16:54:57 -08:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2011-08-15 20:40:38 -07:00
|
|
|
if (classPtr == &sClass) {
|
|
|
|
FileReaderSyncPrivate* fileReader =
|
2012-02-05 12:07:23 -08:00
|
|
|
GetJSPrivateSafeish<FileReaderSyncPrivate>(aObj);
|
2011-08-15 20:40:38 -07:00
|
|
|
return fileReader;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static FileReaderSyncPrivate*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
FileReaderSyncPrivate* fileReader = GetPrivate(aObj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (fileReader) {
|
|
|
|
return fileReader;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_GetClass(aObj)->name);
|
2011-08-15 20:40:38 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderSyncPrivate* fileReader = new FileReaderSyncPrivate();
|
|
|
|
NS_ADDREF(fileReader);
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, fileReader);
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(obj));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == &sClass);
|
2011-08-15 20:40:38 -07:00
|
|
|
FileReaderSyncPrivate* fileReader =
|
2012-02-05 12:07:23 -08:00
|
|
|
GetJSPrivateSafeish<FileReaderSyncPrivate>(aObj);
|
2011-08-15 20:40:38 -07:00
|
|
|
NS_IF_RELEASE(fileReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
ReadAsArrayBuffer(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
FileReaderSyncPrivate* fileReader =
|
|
|
|
GetInstancePrivate(aCx, obj, "readAsArrayBuffer");
|
|
|
|
if (!fileReader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* jsBlob;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "o", &jsBlob)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMBlob* blob = GetDOMBlobFromJSObject(aCx, jsBlob);
|
|
|
|
if (!blob) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t blobSize;
|
2011-08-15 20:40:38 -07:00
|
|
|
nsresult rv = blob->GetSize(&blobSize);
|
|
|
|
if (!EnsureSucceededOrThrow(aCx, rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:43:00 -08:00
|
|
|
JSObject* jsArrayBuffer = JS_NewArrayBuffer(aCx, blobSize);
|
2011-08-15 20:40:38 -07:00
|
|
|
if (!jsArrayBuffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:43:00 -08:00
|
|
|
uint32_t bufferLength = JS_GetArrayBufferByteLength(jsArrayBuffer, aCx);
|
|
|
|
uint8_t* arrayBuffer = JS_GetArrayBufferData(jsArrayBuffer, aCx);
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
rv = fileReader->ReadAsArrayBuffer(blob, bufferLength, arrayBuffer);
|
|
|
|
if (!EnsureSucceededOrThrow(aCx, rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(jsArrayBuffer));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
ReadAsDataURL(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
FileReaderSyncPrivate* fileReader =
|
|
|
|
GetInstancePrivate(aCx, obj, "readAsDataURL");
|
|
|
|
if (!fileReader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* jsBlob;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "o", &jsBlob)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMBlob* blob = GetDOMBlobFromJSObject(aCx, jsBlob);
|
|
|
|
if (!blob) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString blobText;
|
|
|
|
nsresult rv = fileReader->ReadAsDataURL(blob, blobText);
|
|
|
|
if (!EnsureSucceededOrThrow(aCx, rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsBlobText = JS_NewUCStringCopyN(aCx, blobText.get(),
|
|
|
|
blobText.Length());
|
|
|
|
if (!jsBlobText) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(jsBlobText));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
ReadAsBinaryString(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
FileReaderSyncPrivate* fileReader =
|
|
|
|
GetInstancePrivate(aCx, obj, "readAsBinaryString");
|
|
|
|
if (!fileReader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* jsBlob;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "o", &jsBlob)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMBlob* blob = GetDOMBlobFromJSObject(aCx, jsBlob);
|
|
|
|
if (!blob) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString blobText;
|
|
|
|
nsresult rv = fileReader->ReadAsBinaryString(blob, blobText);
|
|
|
|
if (!EnsureSucceededOrThrow(aCx, rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsBlobText = JS_NewUCStringCopyN(aCx, blobText.get(),
|
|
|
|
blobText.Length());
|
|
|
|
if (!jsBlobText) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(jsBlobText));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
ReadAsText(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
FileReaderSyncPrivate* fileReader =
|
|
|
|
GetInstancePrivate(aCx, obj, "readAsText");
|
|
|
|
if (!fileReader) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* jsBlob;
|
|
|
|
JSString* jsEncoding = JS_GetEmptyString(JS_GetRuntime(aCx));
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "o/S", &jsBlob,
|
|
|
|
&jsEncoding)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentJSString encoding;
|
|
|
|
if (!encoding.init(aCx, jsEncoding)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMBlob* blob = GetDOMBlobFromJSObject(aCx, jsBlob);
|
|
|
|
if (!blob) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString blobText;
|
|
|
|
nsresult rv = fileReader->ReadAsText(blob, encoding, blobText);
|
|
|
|
if (!EnsureSucceededOrThrow(aCx, rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsBlobText = JS_NewUCStringCopyN(aCx, blobText.get(),
|
|
|
|
blobText.Length());
|
|
|
|
if (!jsBlobText) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(jsBlobText));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSClass FileReaderSync::sClass = {
|
|
|
|
"FileReaderSync",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-08-15 20:40:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec FileReaderSync::sFunctions[] = {
|
|
|
|
JS_FN("readAsArrayBuffer", ReadAsArrayBuffer, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("readAsBinaryString", ReadAsBinaryString, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("readAsText", ReadAsText, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("readAsDataURL", ReadAsDataURL, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace filereadersync {
|
|
|
|
|
|
|
|
bool
|
|
|
|
InitClass(JSContext* aCx, JSObject* aGlobal)
|
|
|
|
{
|
|
|
|
return !!FileReaderSync::InitClass(aCx, aGlobal);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace filereadersync
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|