diff --git a/js/xpconnect/loader/mozJSComponentLoader.cpp b/js/xpconnect/loader/mozJSComponentLoader.cpp index e68cab25a3d6..bc6677c3c70a 100644 --- a/js/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/xpconnect/loader/mozJSComponentLoader.cpp @@ -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 native; + rv = nsDOMMultipartFile::NewBlob(getter_AddRefs(native)); + if (NS_FAILED(rv)) { + XPCThrower::Throw(rv, cx); + return false; + } + + nsCOMPtr 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 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 }; diff --git a/js/xpconnect/tests/unit/component-blob.js b/js/xpconnect/tests/unit/component-blob.js new file mode 100644 index 000000000000..e153e01545a9 --- /dev/null +++ b/js/xpconnect/tests/unit/component-blob.js @@ -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 = "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); diff --git a/js/xpconnect/tests/unit/component-blob.manifest b/js/xpconnect/tests/unit/component-blob.manifest new file mode 100644 index 000000000000..ac264c06d558 --- /dev/null +++ b/js/xpconnect/tests/unit/component-blob.manifest @@ -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} diff --git a/js/xpconnect/tests/unit/test_blob.js b/js/xpconnect/tests/unit/test_blob.js new file mode 100644 index 000000000000..039f45102ef7 --- /dev/null +++ b/js/xpconnect/tests/unit/test_blob.js @@ -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()); + +} diff --git a/js/xpconnect/tests/unit/xpcshell.ini b/js/xpconnect/tests/unit/xpcshell.ini index d677ef9414d9..b7d0bc2d396f 100644 --- a/js/xpconnect/tests/unit/xpcshell.ini +++ b/js/xpconnect/tests/unit/xpcshell.ini @@ -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]