Bug 825836 - Add Blob into JS component. r=sicking

This commit is contained in:
Chia-hung Tai 2013-01-04 15:53:25 +08:00
Родитель 4fd575f0c0
Коммит fda0d9b634
5 изменённых файлов: 148 добавлений и 0 удалений

Просмотреть файл

@ -282,12 +282,56 @@ File(JSContext *cx, unsigned argc, jsval *vp)
return true;
}
static JSBool
Blob(JSContext *cx, unsigned argc, jsval *vp)
{
nsresult rv;
nsCOMPtr<nsISupports> native;
rv = nsDOMMultipartFile::NewBlob(getter_AddRefs(native));
if (NS_FAILED(rv)) {
XPCThrower::Throw(rv, cx);
return false;
}
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
NS_ASSERTION(initializer, "what?");
rv = initializer->Initialize(nullptr, cx, nullptr, argc, JS_ARGV(cx, vp));
if (NS_FAILED(rv)) {
XPCThrower::Throw(rv, cx);
return false;
}
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
if (!xpc) {
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
return false;
}
JSObject* glob = JS_GetGlobalForScopeChain(cx);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
jsval retval;
rv = xpc->WrapNativeToJSVal(cx, glob, native, nullptr,
&NS_GET_IID(nsISupports),
true, &retval, nullptr);
if (NS_FAILED(rv)) {
XPCThrower::Throw(rv, cx);
return false;
}
JS_SET_RVAL(cx, vp, retval);
return true;
}
static JSFunctionSpec gGlobalFun[] = {
JS_FS("dump", Dump, 1,0),
JS_FS("debug", Debug, 1,0),
JS_FS("atob", Atob, 1,0),
JS_FS("btoa", Btoa, 1,0),
JS_FS("File", File, 1,JSFUN_CONSTRUCTOR),
JS_FS("Blob", Blob, 2,JSFUN_CONSTRUCTOR),
JS_FS_END
};

Просмотреть файл

@ -0,0 +1,85 @@
/* 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/. */
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
function do_check_true(cond, text) {
// we don't have the test harness' utilities in this scope, so we need this
// little helper. In the failure case, the exception is propagated to the
// caller in the main run_test() function, and the test fails.
if (!cond)
throw "Failed check: " + text;
}
function BlobComponent() {
this.wrappedJSObject = this;
}
BlobComponent.prototype =
{
doTest: function() {
// 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");
return true;
},
// nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm
classDescription: "Blob in components scope code",
classID: Components.ID("{06215993-a3c2-41e3-bdfd-0a3a2cc0b65c}"),
contractID: "@mozilla.org/tests/component-blob;1",
// nsIClassInfo
implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
flags: 0,
getInterfaces: function getInterfaces(aCount) {
var interfaces = [Components.interfaces.nsIClassInfo];
aCount.value = interfaces.length;
return interfaces;
},
getHelperForLanguage: function getHelperForLanguage(aLanguage) {
return null;
},
// nsISupports
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo])
};
var gComponentsArray = [BlobComponent];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

Просмотреть файл

@ -0,0 +1,2 @@
component {06215993-a3c2-41e3-bdfd-0a3a2cc0b65c} component-blob.js
contract @mozilla.org/tests/component-blob;1 {06215993-a3c2-41e3-bdfd-0a3a2cc0b65c}

Просмотреть файл

@ -0,0 +1,16 @@
/* 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/. */
function run_test() {
do_load_manifest("component-blob.manifest");
const contractID = "@mozilla.org/tests/component-blob;1";
do_check_true(contractID in Components.classes);
var foo = Components.classes[contractID]
.createInstance(Components.interfaces.nsIClassInfo);
do_check_true(Boolean(foo));
do_check_true(foo.contractID == contractID);
do_check_true(!!foo.wrappedJSObject);
do_check_true(foo.wrappedJSObject.doTest());
}

Просмотреть файл

@ -15,6 +15,7 @@ tail =
[test_bug780370.js]
[test_bug_442086.js]
[test_file.js]
[test_blob.js]
[test_import.js]
[test_import_fail.js]
[test_js_weak_references.js]