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