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"
|
|
|
|
|
|
|
|
#include "nsIDOMFile.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*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
|
|
|
|
sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, nsIDOMBlob* aBlob)
|
|
|
|
{
|
|
|
|
JS_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aBlob), aBlob));
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-01-17 12:17:33 -08:00
|
|
|
nsRefPtr<nsDOMMultipartFile> file = new nsDOMMultipartFile();
|
2012-12-11 13:21:03 -08:00
|
|
|
nsresult rv = file->InitBlob(aCx, aArgc, JS_ARGV(aCx, aVp), 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(obj));
|
|
|
|
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
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_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
|
|
|
{
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, 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
|
|
|
|
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)));
|
2011-08-15 20:40:38 -07:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "/IIS", &start,
|
2013-05-04 00:52:57 -07:00
|
|
|
&end, jsContentType.address())) {
|
2011-08-15 20:40:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentJSString contentType;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(rtnObj));
|
|
|
|
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*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
|
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, &sClass, Construct, 0,
|
|
|
|
sProperties, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, nsIDOMFile* aFile)
|
|
|
|
{
|
|
|
|
JS_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aFile), aFile));
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
|
|
|
|
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);
|
|
|
|
JS_ASSERT_IF(priv, file);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
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
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_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
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (NS_FAILED(file->GetLastModifiedDate(aCx, value.address()))) {
|
2012-11-04 07:19:00 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
aArgs.rval().set(value);
|
2012-11-04 07:19:00 -08:00
|
|
|
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);
|
|
|
|
JS_ASSERT_IF(priv, blob);
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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
|
|
|
{
|
|
|
|
JSObject* blobProto = Blob::InitClass(aCx, aGlobal);
|
|
|
|
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
|