Bug 656647: File constructor should take nsIFiles. r=sicking

This commit is contained in:
Kyle Huey 2011-05-14 16:41:20 -07:00
Родитель cbd257817b
Коммит c582ba8b08
2 изменённых файлов: 42 добавлений и 20 удалений

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

@ -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<nsIFile> 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<nsILocalFile> 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<nsILocalFile> localFile;
nsresult rv = NS_NewLocalFile(xpcomStr,
PR_FALSE, getter_AddRefs(localFile));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> file = do_QueryInterface(localFile, &rv);
NS_ENSURE_SUCCESS(rv, rv);
PRBool exists;
rv = file->Exists(&exists);
NS_ENSURE_SUCCESS(rv, rv);

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

@ -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();