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, PRUint32 aArgc,
jsval* aArgv) jsval* aArgv)
{ {
nsresult rv;
if (!nsContentUtils::IsCallerChrome()) { if (!nsContentUtils::IsCallerChrome()) {
return NS_ERROR_DOM_SECURITY_ERR; // Real short trip return NS_ERROR_DOM_SECURITY_ERR; // Real short trip
} }
NS_ENSURE_TRUE(aArgc > 0, NS_ERROR_UNEXPECTED); NS_ENSURE_TRUE(aArgc > 0, NS_ERROR_UNEXPECTED);
// We expect to get a path to represent as a File object // We expect to get a path to represent as a File object,
if (!JSVAL_IS_STRING(aArgv[0])) // or an nsIFile
return NS_ERROR_UNEXPECTED; 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]); JSObject* obj = JSVAL_TO_OBJECT(aArgv[0]);
NS_ENSURE_TRUE(str, NS_ERROR_XPC_BAD_CONVERT_JS); NS_ASSERTION(obj, "This is a bit odd");
nsDependentJSString xpcomStr; // Is it an nsIFile
if (!xpcomStr.init(aCx, str)) { file = do_QueryInterface(
return NS_ERROR_XPC_BAD_CONVERT_JS; 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; PRBool exists;
rv = file->Exists(&exists); rv = file->Exists(&exists);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);

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

@ -45,10 +45,13 @@ file.append("test");
file.append("chrome"); file.append("chrome");
file.append("fileconstructor_file.png"); file.append("fileconstructor_file.png");
var domfile = new File(file.path); doTest(new File(file.path));
ok(domfile instanceof File, "File() should return a File"); doTest(new File(file));
is(domfile.type, "image/png", "File should be a PNG"); function doTest(domfile) {
is(domfile.size, 95, "File has size 95 (and more importantly we can read it)"); 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 { try {
var boomfile = new File(); var boomfile = new File();