2011-08-16 03:40:38 +00: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 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/. */
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
#include "File.h"
|
|
|
|
|
|
|
|
#include "nsIDOMFile.h"
|
2012-03-24 11:33:59 +00:00
|
|
|
#include "nsDOMBlobBuilder.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-10-04 14:06:54 +00:00
|
|
|
#include "jsfriendapi.h"
|
2011-08-16 03:40:38 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsJSUtils.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2013-09-09 03:29:21 +00:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2011-08-16 03:40:38 +00:00
|
|
|
#include "WorkerInlines.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
2013-09-09 03:29:21 +00:00
|
|
|
using mozilla::dom::Throw;
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Blob
|
|
|
|
{
|
|
|
|
// Blob should never be instantiated.
|
|
|
|
Blob();
|
|
|
|
~Blob();
|
|
|
|
|
2013-09-11 12:49:05 +00:00
|
|
|
static const JSClass sClass;
|
2013-04-22 21:15:36 +00:00
|
|
|
static const JSPropertySpec sProperties[];
|
2013-04-22 21:15:49 +00:00
|
|
|
static const JSFunctionSpec sFunctions[];
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static JSObject*
|
2014-01-29 10:00:40 +00:00
|
|
|
InitClass(JSContext* aCx, JS::Handle<JSObject*> aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-01-29 10:00:40 +00:00
|
|
|
return JS_InitClass(aCx, aObj, JS::NullPtr(), &sClass, Construct, 0,
|
2013-10-28 14:04:47 +00:00
|
|
|
sProperties, sFunctions, nullptr, nullptr);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, nsIDOMBlob* aBlob)
|
|
|
|
{
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aBlob), aBlob));
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2014-01-16 17:48:58 +00:00
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, JS::NullPtr(), JS::NullPtr());
|
2011-08-16 03:40:38 +00:00
|
|
|
if (obj) {
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_SetPrivate(obj, aBlob);
|
2011-08-16 03:40:38 +00:00
|
|
|
NS_ADDREF(aBlob);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIDOMBlob*
|
2012-02-05 20:07:23 +00:00
|
|
|
GetPrivate(JSObject* aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static nsIDOMBlob*
|
2013-05-11 02:39:45 +00:00
|
|
|
GetInstancePrivate(JSContext* aCx, JS::Handle<JSObject*> aObj, const char* aFunctionName)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
nsIDOMBlob* blob = GetPrivate(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (blob) {
|
|
|
|
return blob;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 14:04:47 +00:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-08-16 03:40:38 +00:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_GetClass(aObj)->name);
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 11:33:59 +00:00
|
|
|
static nsIDOMBlob*
|
|
|
|
Unwrap(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
return GetPrivate(aObj);
|
|
|
|
}
|
|
|
|
|
2013-08-02 07:41:57 +00:00
|
|
|
static bool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-03-19 17:05:04 +00:00
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
2013-01-17 20:17:33 +00:00
|
|
|
nsRefPtr<nsDOMMultipartFile> file = new nsDOMMultipartFile();
|
2014-03-19 17:05:04 +00:00
|
|
|
nsresult rv = file->InitBlob(aCx, args.length(), args.array(), Unwrap);
|
2012-03-24 11:33:59 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 03:29:21 +00:00
|
|
|
return Throw(aCx, rv);
|
2012-03-24 11:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* obj = file::CreateBlob(aCx, file);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-19 17:05:04 +00:00
|
|
|
args.rval().setObject(*obj);
|
2012-03-24 11:33:59 +00:00
|
|
|
return true;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT(JS_GetClass(aObj) == &sClass);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
nsIDOMBlob* blob = GetPrivate(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
NS_IF_RELEASE(blob);
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
IsBlob(JS::Handle<JS::Value> v)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
return v.isObject() && GetPrivate(&v.toObject()) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetSizeImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMBlob* blob = GetInstancePrivate(aCx, obj, "size");
|
|
|
|
MOZ_ASSERT(blob);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t size;
|
2011-08-16 03:40:38 +00:00
|
|
|
if (NS_FAILED(blob->GetSize(&size))) {
|
2013-09-09 03:29:21 +00:00
|
|
|
return Throw(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
aArgs.rval().setNumber(double(size));
|
2011-08-16 03:40:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
GetSize(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsBlob, GetSizeImpl>(aCx, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetTypeImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMBlob* blob = GetInstancePrivate(aCx, obj, "type");
|
|
|
|
MOZ_ASSERT(blob);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
nsString type;
|
|
|
|
if (NS_FAILED(blob->GetType(type))) {
|
2013-09-09 03:29:21 +00:00
|
|
|
return Throw(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsType = JS_NewUCStringCopyN(aCx, type.get(), type.Length());
|
|
|
|
if (!jsType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
aArgs.rval().setString(jsType);
|
2011-08-16 03:40:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
static bool
|
|
|
|
GetType(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsBlob, GetTypeImpl>(aCx, args);
|
|
|
|
}
|
|
|
|
|
2013-08-02 07:41:57 +00:00
|
|
|
static bool
|
2012-02-28 23:11:11 +00:00
|
|
|
Slice(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-02-10 16:42:27 +00:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, args.thisv().toObjectOrNull());
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2012-02-16 10:22:25 +00:00
|
|
|
nsIDOMBlob* blob = GetInstancePrivate(aCx, obj, "slice");
|
2011-08-16 03:40:38 +00:00
|
|
|
if (!blob) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-24 22:19:52 +00:00
|
|
|
double start = 0, end = 0;
|
2013-05-04 07:52:57 +00:00
|
|
|
JS::Rooted<JSString*> jsContentType(aCx, JS_GetEmptyString(JS_GetRuntime(aCx)));
|
2014-02-10 16:42:27 +00:00
|
|
|
if (!JS_ConvertArguments(aCx, args, "/IIS", &start,
|
2013-05-04 07:52:57 +00:00
|
|
|
&end, jsContentType.address())) {
|
2011-08-16 03:40:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentJSString contentType;
|
|
|
|
if (!contentType.init(aCx, jsContentType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t optionalArgc = aArgc;
|
2011-08-16 03:40:38 +00:00
|
|
|
nsCOMPtr<nsIDOMBlob> rtnBlob;
|
2012-08-22 15:56:38 +00:00
|
|
|
if (NS_FAILED(blob->Slice(static_cast<uint64_t>(start),
|
|
|
|
static_cast<uint64_t>(end),
|
2012-02-16 10:22:25 +00:00
|
|
|
contentType, optionalArgc,
|
|
|
|
getter_AddRefs(rtnBlob)))) {
|
2013-09-09 03:29:21 +00:00
|
|
|
return Throw(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* rtnObj = file::CreateBlob(aCx, rtnBlob);
|
|
|
|
if (!rtnObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 16:42:27 +00:00
|
|
|
args.rval().setObject(*rtnObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-11 12:49:05 +00:00
|
|
|
const JSClass Blob::sClass = {
|
2011-08-16 03:40:38 +00:00
|
|
|
"Blob",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
2013-04-06 04:22:55 +00:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-08-16 03:40:38 +00:00
|
|
|
};
|
|
|
|
|
2013-04-22 21:15:36 +00:00
|
|
|
const JSPropertySpec Blob::sProperties[] = {
|
2013-08-08 22:55:23 +00:00
|
|
|
JS_PSGS("size", GetSize, GetterOnlyJSNative, JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("type", GetType, GetterOnlyJSNative, JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2011-08-16 03:40:38 +00:00
|
|
|
};
|
|
|
|
|
2013-04-22 21:15:49 +00:00
|
|
|
const JSFunctionSpec Blob::sFunctions[] = {
|
2012-02-16 10:22:25 +00:00
|
|
|
JS_FN("slice", Slice, 1, JSPROP_ENUMERATE),
|
2011-08-16 03:40:38 +00:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
class File : public Blob
|
|
|
|
{
|
|
|
|
// File should never be instantiated.
|
|
|
|
File();
|
|
|
|
~File();
|
|
|
|
|
2013-09-11 12:49:05 +00:00
|
|
|
static const JSClass sClass;
|
2013-04-22 21:15:36 +00:00
|
|
|
static const JSPropertySpec sProperties[];
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static JSObject*
|
2014-01-29 10:00:40 +00:00
|
|
|
InitClass(JSContext* aCx, JS::Handle<JSObject*> aObj, JS::Handle<JSObject*> aParentProto)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, &sClass, Construct, 0,
|
2013-10-28 14:04:47 +00:00
|
|
|
sProperties, nullptr, nullptr, nullptr);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, nsIDOMFile* aFile)
|
|
|
|
{
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aFile), aFile));
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2014-01-16 17:48:58 +00:00
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, JS::NullPtr(), JS::NullPtr());
|
2011-08-16 03:40:38 +00:00
|
|
|
if (obj) {
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_SetPrivate(obj, aFile);
|
2011-08-16 03:40:38 +00:00
|
|
|
NS_ADDREF(aFile);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIDOMFile*
|
2012-02-05 20:07:23 +00:00
|
|
|
GetPrivate(JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
|
|
|
if (aObj) {
|
2013-09-11 12:49:05 +00:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
if (classPtr == &sClass) {
|
2012-02-05 20:07:23 +00:00
|
|
|
nsISupports* priv = static_cast<nsISupports*>(JS_GetPrivate(aObj));
|
2011-08-16 03:40:38 +00:00
|
|
|
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(priv);
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT_IF(priv, file);
|
2011-08-16 03:40:38 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 12:49:05 +00:00
|
|
|
static const JSClass*
|
2011-08-16 03:40:38 +00:00
|
|
|
Class()
|
|
|
|
{
|
|
|
|
return &sClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static nsIDOMFile*
|
2013-05-11 02:39:45 +00:00
|
|
|
GetInstancePrivate(JSContext* aCx, JS::Handle<JSObject*> aObj, const char* aFunctionName)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
nsIDOMFile* file = GetPrivate(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (file) {
|
|
|
|
return file;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 14:04:47 +00:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-08-16 03:40:38 +00:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_GetClass(aObj)->name);
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 07:41:57 +00:00
|
|
|
static bool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2013-10-28 14:04:47 +00:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_WRONG_CONSTRUCTOR,
|
2011-08-16 03:40:38 +00:00
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT(JS_GetClass(aObj) == &sClass);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
nsIDOMFile* file = GetPrivate(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
NS_IF_RELEASE(file);
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
IsFile(JS::Handle<JS::Value> v)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
return v.isObject() && GetPrivate(&v.toObject()) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetMozFullPathImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMFile* file = GetInstancePrivate(aCx, obj, "mozFullPath");
|
|
|
|
MOZ_ASSERT(file);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
nsString fullPath;
|
|
|
|
|
|
|
|
if (GetWorkerPrivateFromContext(aCx)->UsesSystemPrincipal() &&
|
|
|
|
NS_FAILED(file->GetMozFullPathInternal(fullPath))) {
|
2013-09-09 03:29:21 +00:00
|
|
|
return Throw(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsFullPath = JS_NewUCStringCopyN(aCx, fullPath.get(),
|
|
|
|
fullPath.Length());
|
|
|
|
if (!jsFullPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
aArgs.rval().setString(jsFullPath);
|
2011-08-16 03:40:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
GetMozFullPath(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsFile, GetMozFullPathImpl>(aCx, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetNameImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMFile* file = GetInstancePrivate(aCx, obj, "name");
|
|
|
|
MOZ_ASSERT(file);
|
2011-08-16 03:40:38 +00:00
|
|
|
|
|
|
|
nsString name;
|
|
|
|
if (NS_FAILED(file->GetName(name))) {
|
|
|
|
name.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsName = JS_NewUCStringCopyN(aCx, name.get(), name.Length());
|
|
|
|
if (!jsName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
aArgs.rval().setString(jsName);
|
2011-08-16 03:40:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-04 15:19:00 +00:00
|
|
|
|
2013-08-23 07:41:17 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
GetName(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2013-08-23 07:41:17 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsFile, GetNameImpl>(aCx, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetPathImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMFile* file = GetInstancePrivate(aCx, obj, "path");
|
|
|
|
MOZ_ASSERT(file);
|
2013-08-23 07:41:17 +00:00
|
|
|
|
|
|
|
nsString path;
|
|
|
|
if (NS_FAILED(file->GetPath(path))) {
|
|
|
|
path.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* jsPath = JS_NewUCStringCopyN(aCx, path.get(), path.Length());
|
|
|
|
if (!jsPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:55:23 +00:00
|
|
|
aArgs.rval().setString(jsPath);
|
2013-08-23 07:41:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-08-08 22:55:23 +00:00
|
|
|
GetPath(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2012-11-04 15:19:00 +00:00
|
|
|
{
|
2013-08-08 22:55:23 +00:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsFile, GetPathImpl>(aCx, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetLastModifiedDateImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
|
|
|
nsIDOMFile* file = GetInstancePrivate(aCx, obj, "lastModifiedDate");
|
|
|
|
MOZ_ASSERT(file);
|
2012-11-04 15:19:00 +00:00
|
|
|
|
2014-01-09 17:39:36 +00:00
|
|
|
if (NS_FAILED(file->GetLastModifiedDate(aCx, aArgs.rval()))) {
|
2012-11-04 15:19:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-08 22:55:23 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
GetLastModifiedDate(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsFile, GetLastModifiedDateImpl>(aCx, args);
|
|
|
|
}
|
2011-08-16 03:40:38 +00:00
|
|
|
};
|
|
|
|
|
2013-09-11 12:49:05 +00:00
|
|
|
const JSClass File::sClass = {
|
2011-08-16 03:40:38 +00:00
|
|
|
"File",
|
|
|
|
JSCLASS_HAS_PRIVATE,
|
2013-04-06 04:22:55 +00:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-08-16 03:40:38 +00:00
|
|
|
};
|
|
|
|
|
2013-04-22 21:15:36 +00:00
|
|
|
const JSPropertySpec File::sProperties[] = {
|
2013-08-08 22:55:23 +00:00
|
|
|
JS_PSGS("name", GetName, GetterOnlyJSNative, JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("path", GetPath, GetterOnlyJSNative, JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("lastModifiedDate", GetLastModifiedDate, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("mozFullPath", GetMozFullPath, GetterOnlyJSNative, JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2011-08-16 03:40:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nsIDOMBlob*
|
2012-02-05 20:07:23 +00:00
|
|
|
Blob::GetPrivate(JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
|
|
|
if (aObj) {
|
2013-09-11 12:49:05 +00:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
if (classPtr == &sClass || classPtr == File::Class()) {
|
2012-02-05 20:07:23 +00:00
|
|
|
nsISupports* priv = static_cast<nsISupports*>(JS_GetPrivate(aObj));
|
2011-08-16 03:40:38 +00:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(priv);
|
2014-02-18 06:09:06 +00:00
|
|
|
MOZ_ASSERT_IF(priv, blob);
|
2011-08-16 03:40:38 +00:00
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 14:04:47 +00:00
|
|
|
return nullptr;
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace file {
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateBlob(JSContext* aCx, nsIDOMBlob* aBlob)
|
|
|
|
{
|
|
|
|
return Blob::Create(aCx, aBlob);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-11 02:39:45 +00:00
|
|
|
InitClasses(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2014-01-29 10:00:40 +00:00
|
|
|
JS::Rooted<JSObject*> blobProto(aCx, Blob::InitClass(aCx, aGlobal));
|
2011-08-16 03:40:38 +00:00
|
|
|
return blobProto && File::InitClass(aCx, aGlobal, blobProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMBlob*
|
2012-02-05 20:07:23 +00:00
|
|
|
GetDOMBlobFromJSObject(JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return Blob::GetPrivate(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateFile(JSContext* aCx, nsIDOMFile* aFile)
|
|
|
|
{
|
|
|
|
return File::Create(aCx, aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMFile*
|
2012-02-05 20:07:23 +00:00
|
|
|
GetDOMFileFromJSObject(JSObject* aObj)
|
2011-08-16 03:40:38 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return File::GetPrivate(aObj);
|
2011-08-16 03:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace file
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|