Bug 884936 - Add Blob/File support to xpcshell. r=bent

This commit is contained in:
Jan Varga 2013-06-21 17:15:46 +02:00
parent df016bacc3
commit 4d7db5e807
7 changed files with 213 additions and 4 deletions

View File

@ -12,6 +12,14 @@
#include "mozilla/Attributes.h"
#include <algorithm>
#define NS_DOMMULTIPARTBLOB_CID { 0x47bf0b43, 0xf37e, 0x49ef, \
{ 0x81, 0xa0, 0x18, 0xba, 0xc0, 0x57, 0xb5, 0xcc } }
#define NS_DOMMULTIPARTBLOB_CONTRACTID "@mozilla.org/dom/multipart-blob;1"
#define NS_DOMMULTIPARTFILE_CID { 0xc3361f77, 0x60d1, 0x4ea9, \
{ 0x94, 0x96, 0xdf, 0x5d, 0x6f, 0xcd, 0xd7, 0x8f } }
#define NS_DOMMULTIPARTFILE_CONTRACTID "@mozilla.org/dom/multipart-file;1"
class nsDOMMultipartFile : public nsDOMFile,
public nsIJSNativeInitializer
{

View File

@ -23,6 +23,7 @@
#include "nsIXPCScriptable.h"
#include "nsIInterfaceInfo.h"
#include "nsIInterfaceInfoManager.h"
#include "nsIJSNativeInitializer.h"
#include "nsIXPCScriptable.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
@ -820,6 +821,84 @@ Btoa(JSContext *cx, unsigned argc, jsval *vp)
return xpc::Base64Encode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
}
static JSBool
Blob(JSContext *cx, unsigned argc, jsval *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
nsCOMPtr<nsISupports> native =
do_CreateInstance("@mozilla.org/dom/multipart-blob;1");
if (!native) {
JS_ReportError(cx, "Could not create native object!");
return false;
}
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
NS_ASSERTION(initializer, "what?");
nsresult rv = initializer->Initialize(nullptr, cx, nullptr, args);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not initialize native object!");
return false;
}
nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID, &rv);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not get XPConnent service!");
return false;
}
JSObject* global = JS_GetGlobalForScopeChain(cx);
rv = xpc->WrapNativeToJSVal(cx, global, native, nullptr,
&NS_GET_IID(nsISupports), true,
args.rval().address(), nullptr);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not wrap native object!");
return false;
}
return true;
}
static JSBool
File(JSContext *cx, unsigned argc, jsval *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
nsCOMPtr<nsISupports> native =
do_CreateInstance("@mozilla.org/dom/multipart-file;1");
if (!native) {
JS_ReportError(cx, "Could not create native object!");
return false;
}
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
NS_ASSERTION(initializer, "what?");
nsresult rv = initializer->Initialize(nullptr, cx, nullptr, args);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not initialize native object!");
return false;
}
nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID, &rv);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not get XPConnent service!");
return false;
}
JSObject* global = JS_GetGlobalForScopeChain(cx);
rv = xpc->WrapNativeToJSVal(cx, global, native, nullptr,
&NS_GET_IID(nsISupports), true,
args.rval().address(), nullptr);
if (NS_FAILED(rv)) {
JS_ReportError(cx, "Could not wrap native object!");
return false;
}
return true;
}
static const JSFunctionSpec glob_functions[] = {
JS_FS("print", Print, 0,0),
JS_FS("readline", ReadLine, 1,0),
@ -842,6 +921,8 @@ static const JSFunctionSpec glob_functions[] = {
JS_FS("getChildGlobalObject", GetChildGlobalObject, 0,0),
JS_FS("atob", Atob, 1,0),
JS_FS("btoa", Btoa, 1,0),
JS_FS("Blob", Blob, 2,JSFUN_CONSTRUCTOR),
JS_FS("File", File, 2,JSFUN_CONSTRUCTOR),
JS_FS_END
};

View File

@ -47,10 +47,10 @@ FileComponent.prototype =
do_check_true(f3.name == "xpcshell.ini", "Should be the right file");
do_check_true(f4.name == "xpcshell.ini", "Should be the right file");
do_check_true(f1.type = "text/plain", "Should be the right type");
do_check_true(f2.type = "text/plain", "Should be the right type");
do_check_true(f3.type = "text/plain", "Should be the right type");
do_check_true(f4.type = "text/plain", "Should be the right type");
do_check_true(f1.type == "", "Should be the right type");
do_check_true(f2.type == "", "Should be the right type");
do_check_true(f3.type == "", "Should be the right type");
do_check_true(f4.type == "", "Should be the right type");
var threw = false;
try {

View File

@ -0,0 +1,40 @@
/* 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/. */
const Ci = Components.interfaces;
function run_test() {
// throw if anything goes wrong
let testContent = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
// should be able to construct a file
var f1 = Blob([testContent], {"type" : "text/xml"});
// with either constructor syntax
var f2 = new Blob([testContent], {"type" : "text/xml"});
// do some tests
do_check_true(f1 instanceof Ci.nsIDOMBlob, "Should be a DOM Blob");
do_check_true(f2 instanceof Ci.nsIDOMBlob, "Should be a DOM Blob");
do_check_true(!(f1 instanceof Ci.nsIDOMFile), "Should not be a DOM File");
do_check_true(!(f2 instanceof Ci.nsIDOMFile), "Should not be a DOM File");
do_check_true(f1.type == "text/xml", "Wrong type");
do_check_true(f2.type == "text/xml", "Wrong type");
do_check_true(f1.size == testContent.length, "Wrong content size");
do_check_true(f2.size == testContent.length, "Wrong content size");
var f3 = new Blob();
do_check_true(f3.size == 0, "Wrong size");
do_check_true(f3.type == "", "Wrong type");
var threw = false;
try {
// Needs a valid ctor argument
var f3 = Blob(Date(132131532));
} catch (e) {
threw = true;
}
do_check_true(threw, "Passing a random object should fail");
}

View File

@ -0,0 +1,69 @@
/* 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/. */
const Ci = Components.interfaces;
function run_test() {
// throw if anything goes wrong
// find the current directory path
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
file.append("xpcshell.ini");
// should be able to construct a file
var f1 = File(file.path);
// with either constructor syntax
var f2 = new File(file.path);
// and with nsIFiles
var f3 = File(file);
var f4 = new File(file);
// do some tests
do_check_true(f1 instanceof Ci.nsIDOMFile, "Should be a DOM File");
do_check_true(f2 instanceof Ci.nsIDOMFile, "Should be a DOM File");
do_check_true(f3 instanceof Ci.nsIDOMFile, "Should be a DOM File");
do_check_true(f4 instanceof Ci.nsIDOMFile, "Should be a DOM File");
do_check_true(f1.name == "xpcshell.ini", "Should be the right file");
do_check_true(f2.name == "xpcshell.ini", "Should be the right file");
do_check_true(f3.name == "xpcshell.ini", "Should be the right file");
do_check_true(f4.name == "xpcshell.ini", "Should be the right file");
do_check_true(f1.type == "", "Should be the right type");
do_check_true(f2.type == "", "Should be the right type");
do_check_true(f3.type == "", "Should be the right type");
do_check_true(f4.type == "", "Should be the right type");
var threw = false;
try {
// Needs a ctor argument
var f7 = File();
} catch (e) {
threw = true;
}
do_check_true(threw, "No ctor arguments should throw");
var threw = false;
try {
// Needs a valid ctor argument
var f7 = File(Date(132131532));
} catch (e) {
threw = true;
}
do_check_true(threw, "Passing a random object should fail");
var threw = false
try {
// Directories fail
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
var f7 = File(dir)
} catch (e) {
threw = true;
}
do_check_true(threw, "Can't create a File object for a directory");
}

View File

@ -26,6 +26,8 @@ tail =
[test_bug_442086.js]
[test_file.js]
[test_blob.js]
[test_blob2.js]
[test_file2.js]
[test_import.js]
[test_import_fail.js]
[test_js_weak_references.js]

View File

@ -67,6 +67,7 @@
#include "nsContentCreatorFunctions.h"
// DOM includes
#include "nsDOMBlobBuilder.h"
#include "nsDOMException.h"
#include "nsDOMFileReader.h"
@ -543,6 +544,8 @@ MAKE_CTOR(CreateHTMLDocument, nsIDocument, NS_NewHTM
MAKE_CTOR(CreateXMLDocument, nsIDocument, NS_NewXMLDocument)
MAKE_CTOR(CreateSVGDocument, nsIDocument, NS_NewSVGDocument)
MAKE_CTOR(CreateImageDocument, nsIDocument, NS_NewImageDocument)
MAKE_CTOR(CreateDOMBlob, nsISupports, nsDOMMultipartFile::NewBlob)
MAKE_CTOR(CreateDOMFile, nsISupports, nsDOMMultipartFile::NewFile)
MAKE_CTOR(CreateDOMSelection, nsISelection, NS_NewDomSelection)
MAKE_CTOR2(CreateContentIterator, nsIContentIterator, NS_NewContentIterator)
MAKE_CTOR2(CreatePreContentIterator, nsIContentIterator, NS_NewPreContentIterator)
@ -687,6 +690,8 @@ NS_DEFINE_NAMED_CID(NS_HTMLDOCUMENT_CID);
NS_DEFINE_NAMED_CID(NS_XMLDOCUMENT_CID);
NS_DEFINE_NAMED_CID(NS_SVGDOCUMENT_CID);
NS_DEFINE_NAMED_CID(NS_IMAGEDOCUMENT_CID);
NS_DEFINE_NAMED_CID(NS_DOMMULTIPARTBLOB_CID);
NS_DEFINE_NAMED_CID(NS_DOMMULTIPARTFILE_CID);
NS_DEFINE_NAMED_CID(NS_DOMSELECTION_CID);
NS_DEFINE_NAMED_CID(NS_CONTENTITERATOR_CID);
NS_DEFINE_NAMED_CID(NS_PRECONTENTITERATOR_CID);
@ -975,6 +980,8 @@ static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
{ &kNS_XMLDOCUMENT_CID, false, NULL, CreateXMLDocument },
{ &kNS_SVGDOCUMENT_CID, false, NULL, CreateSVGDocument },
{ &kNS_IMAGEDOCUMENT_CID, false, NULL, CreateImageDocument },
{ &kNS_DOMMULTIPARTBLOB_CID, false, NULL, CreateDOMBlob },
{ &kNS_DOMMULTIPARTFILE_CID, false, NULL, CreateDOMFile },
{ &kNS_DOMSELECTION_CID, false, NULL, CreateDOMSelection },
{ &kNS_CONTENTITERATOR_CID, false, NULL, CreateContentIterator },
{ &kNS_PRECONTENTITERATOR_CID, false, NULL, CreatePreContentIterator },
@ -1123,6 +1130,8 @@ static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
{ NS_NAMESPACEMANAGER_CONTRACTID, &kNS_NAMESPACEMANAGER_CID },
{ "@mozilla.org/xml/xml-document;1", &kNS_XMLDOCUMENT_CID },
{ "@mozilla.org/svg/svg-document;1", &kNS_SVGDOCUMENT_CID },
{ NS_DOMMULTIPARTBLOB_CONTRACTID, &kNS_DOMMULTIPARTBLOB_CID },
{ NS_DOMMULTIPARTFILE_CONTRACTID, &kNS_DOMMULTIPARTFILE_CID },
{ "@mozilla.org/content/dom-selection;1", &kNS_DOMSELECTION_CID },
{ "@mozilla.org/content/post-content-iterator;1", &kNS_CONTENTITERATOR_CID },
{ "@mozilla.org/content/pre-content-iterator;1", &kNS_PRECONTENTITERATOR_CID },