diff --git a/content/base/src/nsDOMFile.cpp b/content/base/src/nsDOMFile.cpp index 82c28859be3a..1a6077b17e0a 100644 --- a/content/base/src/nsDOMFile.cpp +++ b/content/base/src/nsDOMFile.cpp @@ -629,32 +629,51 @@ nsDOMFile::Initialize(nsISupports* aOwner, PRUint32 aArgc, jsval* aArgv) { + nsresult rv; + if (!nsContentUtils::IsCallerChrome()) { return NS_ERROR_DOM_SECURITY_ERR; // Real short trip } NS_ENSURE_TRUE(aArgc > 0, NS_ERROR_UNEXPECTED); - // We expect to get a path to represent as a File object - if (!JSVAL_IS_STRING(aArgv[0])) - return NS_ERROR_UNEXPECTED; + // We expect to get a path to represent as a File object, + // or an nsIFile + nsCOMPtr file; + if (!JSVAL_IS_STRING(aArgv[0])) { + // Lets see if it's an nsIFile + if (!JSVAL_IS_OBJECT(aArgv[0])) { + return NS_ERROR_UNEXPECTED; // We're not interested + } - JSString* str = JS_ValueToString(aCx, aArgv[0]); - NS_ENSURE_TRUE(str, NS_ERROR_XPC_BAD_CONVERT_JS); + JSObject* obj = JSVAL_TO_OBJECT(aArgv[0]); + NS_ASSERTION(obj, "This is a bit odd"); - nsDependentJSString xpcomStr; - if (!xpcomStr.init(aCx, str)) { - return NS_ERROR_XPC_BAD_CONVERT_JS; + // Is it an nsIFile + file = do_QueryInterface( + nsContentUtils::XPConnect()-> + GetNativeOfWrapper(aCx, obj)); + if (!file) + return NS_ERROR_UNEXPECTED; + } else { + // It's a string + JSString* str = JS_ValueToString(aCx, aArgv[0]); + NS_ENSURE_TRUE(str, NS_ERROR_XPC_BAD_CONVERT_JS); + + nsDependentJSString xpcomStr; + if (!xpcomStr.init(aCx, str)) { + return NS_ERROR_XPC_BAD_CONVERT_JS; + } + + nsCOMPtr localFile; + nsresult rv = NS_NewLocalFile(xpcomStr, + PR_FALSE, getter_AddRefs(localFile)); + NS_ENSURE_SUCCESS(rv, rv); + + file = do_QueryInterface(localFile); + NS_ASSERTION(file, "This should never happen"); } - nsCOMPtr localFile; - nsresult rv = NS_NewLocalFile(xpcomStr, - PR_FALSE, getter_AddRefs(localFile)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr file = do_QueryInterface(localFile, &rv); - NS_ENSURE_SUCCESS(rv, rv); - PRBool exists; rv = file->Exists(&exists); NS_ENSURE_SUCCESS(rv, rv); diff --git a/content/base/test/chrome/test_fileconstructor.xul b/content/base/test/chrome/test_fileconstructor.xul index 9fe58716241f..9c916358a16f 100644 --- a/content/base/test/chrome/test_fileconstructor.xul +++ b/content/base/test/chrome/test_fileconstructor.xul @@ -45,10 +45,13 @@ file.append("test"); file.append("chrome"); file.append("fileconstructor_file.png"); -var domfile = new File(file.path); -ok(domfile instanceof File, "File() should return a File"); -is(domfile.type, "image/png", "File should be a PNG"); -is(domfile.size, 95, "File has size 95 (and more importantly we can read it)"); +doTest(new File(file.path)); +doTest(new File(file)); +function doTest(domfile) { + ok(domfile instanceof File, "File() should return a File"); + is(domfile.type, "image/png", "File should be a PNG"); + is(domfile.size, 95, "File has size 95 (and more importantly we can read it)"); +} try { var boomfile = new File();