Bug 1335536 - File.createFromNsIFile and File.createFromFileName should be async - part 1 - tests, r=smaug

This commit is contained in:
Andrea Marchesini 2017-02-08 10:18:32 +01:00
Родитель 730840c3ac
Коммит 8cf1cb8c2f
31 изменённых файлов: 358 добавлений и 207 удалений

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

@ -541,28 +541,32 @@ Cookies.prototype = {
},
_readCookieFile(aFile, aCallback) {
let fileReader = new FileReader();
let onLoadEnd = () => {
fileReader.removeEventListener("loadend", onLoadEnd);
File.createFromNsIFile(aFile).then(aFile => {
let fileReader = new FileReader();
let onLoadEnd = () => {
fileReader.removeEventListener("loadend", onLoadEnd);
if (fileReader.readyState != fileReader.DONE) {
Cu.reportError("Could not read cookie contents: " + fileReader.error);
aCallback(false);
return;
}
if (fileReader.readyState != fileReader.DONE) {
Cu.reportError("Could not read cookie contents: " + fileReader.error);
aCallback(false);
return;
}
let success = true;
try {
this._parseCookieBuffer(fileReader.result);
} catch (ex) {
Components.utils.reportError("Unable to migrate cookie: " + ex);
success = false;
} finally {
aCallback(success);
}
};
fileReader.addEventListener("loadend", onLoadEnd);
fileReader.readAsText(File.createFromNsIFile(aFile));
let success = true;
try {
this._parseCookieBuffer(fileReader.result);
} catch (ex) {
Components.utils.reportError("Unable to migrate cookie: " + ex);
success = false;
} finally {
aCallback(success);
}
};
fileReader.addEventListener("loadend", onLoadEnd);
fileReader.readAsText(aFile);
}, () => {
aCallback(false);
});
},
/**

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

@ -8,10 +8,14 @@ var testFile = Cc["@mozilla.org/file/directory_service;1"]
testFile.append("prefs.js");
addMessageListener("file.open", function () {
sendAsyncMessage("file.opened", {
file: File.createFromNsIFile(testFile),
mtime: testFile.lastModifiedTime,
fileWithDate: File.createFromNsIFile(testFile, { lastModified: 123 }),
fileDate: 123,
File.createFromNsIFile(testFile).then(function(file) {
File.createFromNsIFile(testFile, { lastModified: 123 }).then(function(fileWithDate) {
sendAsyncMessage("file.opened", {
file,
mtime: testFile.lastModifiedTime,
fileWithDate,
fileDate: 123,
});
});
});
});

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

@ -7,7 +7,9 @@ addMessageListener("file.create", function (message) {
.get("TmpD", Components.interfaces.nsIFile);
file.append("foo.txt");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o600);
sendAsyncMessage("file.created", File.createFromNsIFile(file));
File.createFromNsIFile(file).then(function(domFile) {
sendAsyncMessage("file.created", domFile);
});
});
addMessageListener("file.remove", function (message) {

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

@ -35,13 +35,22 @@ function createFileWithData(fileData) {
/** Test for Bug 914381. File's created in JS using an nsIFile should allow mozGetFullPathInternal calls to succeed **/
var file = createFileWithData("Test bug 914381");
var f = File.createFromNsIFile(file);
is(f.mozFullPathInternal, undefined, "mozFullPathInternal is undefined from js");
is(f.mozFullPath, file.path, "mozFullPath returns path if created with nsIFile");
File.createFromNsIFile(file).then(f => {
is(f.mozFullPathInternal, undefined, "mozFullPathInternal is undefined from js");
is(f.mozFullPath, file.path, "mozFullPath returns path if created with nsIFile");
})
.then(() => {
return File.createFromFileName(file.path);
})
.then(f => {
is(f.mozFullPathInternal, undefined, "mozFullPathInternal is undefined from js");
is(f.mozFullPath, "", "mozFullPath returns blank if created with a string");
})
.then(() => {
SimpleTest.finish();
});
f = File.createFromFileName(file.path);
is(f.mozFullPathInternal, undefined, "mozFullPathInternal is undefined from js");
is(f.mozFullPath, "", "mozFullPath returns blank if created with a string");
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>

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

@ -42,30 +42,40 @@ file.append("test");
file.append("chrome");
file.append("fileconstructor_file.png");
doTest(File.createFromFileName(file.path));
doTest(File.createFromNsIFile(file));
function doTest(domfile) {
File.createFromFileName(file.path).then(function(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 nonexistentfile = File.createFromFileName("i/sure/hope/this/does/not/exist/anywhere.txt");
})
.then(() => {
return File.createFromNsIFile(file);
})
.then(function(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)");
})
.then(function() {
return File.createFromFileName("i/sure/hope/this/does/not/exist/anywhere.txt");
})
.then(function() {
ok(false, "This should never be reached!");
} catch (e) {
}, function() {
ok(true, "Attempt to construct a non-existent file should fail.");
}
try {
}).then(function() {
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("CurWorkD", Components.interfaces.nsIFile);
var dirfile = File.createFromNsIFile(dir);
return File.createFromNsIFile(dir);
}).then(function() {
ok(false, "This should never be reached!");
} catch (e) {
}, function() {
ok(true, "Attempt to construct a file from a directory should fail.");
}
}).then(function() {
SimpleTest.finish();
});
SimpleTest.waitForExplicitFinish();
]]>
</script>

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

@ -13,14 +13,17 @@ function createFileWithData(message) {
outStream.write(message, message.length);
outStream.close();
var domFile = File.createFromNsIFile(testFile);
return domFile;
return File.createFromNsIFile(testFile);
}
addMessageListener("file.open", function (message) {
sendAsyncMessage("file.opened", createFileWithData(message));
createFileWithData(message).then(function(file) {
sendAsyncMessage("file.opened", file);
});
});
addMessageListener("file.modify", function (message) {
sendAsyncMessage("file.modified", createFileWithData(message));
createFileWithData(message).then(function(file) {
sendAsyncMessage("file.modified", file);
});
});

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

@ -10,8 +10,8 @@ function writeFile(text, answer) {
stream.write(text, text.length);
stream.close();
sendAsyncMessage(answer, {
file: File.createFromNsIFile(tmpFile)
File.createFromNsIFile(tmpFile).then(function(file) {
sendAsyncMessage(answer, { file });
});
}

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

@ -8,8 +8,8 @@ addMessageListener("file.open", function () {
.get("ProfD", Ci.nsIFile);
testFile.append("prefs.js");
sendAsyncMessage("file.opened", {
file: File.createFromNsIFile(testFile)
File.createFromNsIFile(testFile).then(function(file) {
sendAsyncMessage("file.opened", { file });
});
});

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

@ -37,6 +37,7 @@
#include "mozilla/dom/DOMError.h"
#include "mozilla/dom/FileBinding.h"
#include "mozilla/dom/FileSystemUtils.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "nsThreadUtils.h"
@ -581,7 +582,7 @@ File::Constructor(const GlobalObject& aGlobal,
return file.forget();
}
/* static */ already_AddRefed<File>
/* static */ already_AddRefed<Promise>
File::CreateFromNsIFile(const GlobalObject& aGlobal,
nsIFile* aData,
const ChromeFilePropertyBag& aBag,
@ -603,11 +604,19 @@ File::CreateFromNsIFile(const GlobalObject& aGlobal,
impl->SetLastModified(aBag.mLastModified.Value());
}
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
return domFile.forget();
promise->MaybeResolve(domFile);
return promise.forget();
}
/* static */ already_AddRefed<File>
/* static */ already_AddRefed<Promise>
File::CreateFromFileName(const GlobalObject& aGlobal,
const nsAString& aData,
const ChromeFilePropertyBag& aBag,
@ -627,8 +636,16 @@ File::CreateFromFileName(const GlobalObject& aGlobal,
impl->SetLastModified(aBag.mLastModified.Value());
}
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
return domFile.forget();
promise->MaybeResolve(domFile);
return promise.forget();
}
////////////////////////////////////////////////////////////////////////////

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

@ -43,6 +43,7 @@ struct FilePropertyBag;
class BlobImpl;
class File;
class OwningArrayBufferOrArrayBufferViewOrBlobOrUSVString;
class Promise;
class Blob : public nsIDOMBlob
, public nsIXHRSendable
@ -204,7 +205,7 @@ public:
ErrorResult& aRv);
// ChromeOnly
static already_AddRefed<File>
static already_AddRefed<Promise>
CreateFromFileName(const GlobalObject& aGlobal,
const nsAString& aData,
const ChromeFilePropertyBag& aBag,
@ -212,7 +213,7 @@ public:
ErrorResult& aRv);
// ChromeOnly
static already_AddRefed<File>
static already_AddRefed<Promise>
CreateFromNsIFile(const GlobalObject& aGlobal,
nsIFile* aData,
const ChromeFilePropertyBag& aBag,

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

@ -2,9 +2,14 @@ Components.utils.importGlobalProperties(['File']);
addMessageListener("create-file-objects", function(message) {
let files = []
let promises = [];
for (fileName of message.fileNames) {
files.push(File.createFromFileName(fileName));
promises.push(File.createFromFileName(fileName).then(function(file) {
files.push(file);
}));
}
sendAsyncMessage("created-file-objects", files);
Promise.all(promises).then(function() {
sendAsyncMessage("created-file-objects", files);
});
});

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

@ -17,13 +17,26 @@ function createFileWithData(fileData) {
}
outStream.write(fileData, fileData.length);
outStream.close();
var domFile = File.createFromNsIFile(testFile);
if (willDelete) {
testFile.remove(/* recursive: */ false);
}
return domFile;
return File.createFromNsIFile(testFile).then(domFile => {
if (willDelete) {
testFile.remove(/* recursive: */ false);
}
return domFile;
});
}
addMessageListener("files.open", function (message) {
sendAsyncMessage("files.opened", message.map(createFileWithData));
let promises = [];
let list = [];
for (let fileData of message) {
promises.push(createFileWithData(fileData).then(domFile => {
list.push(domFile);
}));
}
Promise.all(promises).then(() => {
sendAsyncMessage("files.opened", list);
});
});

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

@ -35,8 +35,10 @@ addMessageListener("entries.open", function (e) {
dir2.append('subsubdir');
dir2.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0o700);
sendAsyncMessage("entries.opened", {
data: [ new Directory(tmpDir.path), File.createFromNsIFile(tmpFile) ]
File.createFromNsIFile(tmpFile).then(function(file) {
sendAsyncMessage("entries.opened", {
data: [ new Directory(tmpDir.path), file ]
});
});
});

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

@ -123,7 +123,7 @@ addMessageListener("file.open", function (e) {
.get("ProfD", Ci.nsIFile);
testFile.append("prefs.js");
sendAsyncMessage("file.opened", {
file: File.createFromNsIFile(testFile)
File.createFromNsIFile(testFile).then(function(file) {
sendAsyncMessage("file.opened", { file });
});
});

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

@ -2,5 +2,15 @@ var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.importGlobalProperties(["File"]);
addMessageListener("files.open", function (message) {
sendAsyncMessage("files.opened", message.map(path => File.createFromFileName(path)));
let list = [];
let promises = [];
for (let path of message) {
promises.push(File.createFromFileName(path).then(file => {
list.push(file);
}));
}
Promise.all(promises).then(() => {
sendAsyncMessage("files.opened", list);
});
});

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

@ -7,7 +7,8 @@ addMessageListener("file.open", function (e) {
.QueryInterface(Ci.nsIProperties)
.get('ProfD', Ci.nsIFile);
tmpFile.append('prefs.js');
sendAsyncMessage("file.opened", {
data: [ File.createFromNsIFile(tmpFile) ]
File.createFromNsIFile(tmpFile).then(file => {
sendAsyncMessage("file.opened", { data: [ file ] });
});
});

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

@ -11,10 +11,13 @@ addMessageListener("file.open", function (stem) {
file.append(stem);
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
}
sendAsyncMessage("file.opened", {
fullPath: file.path,
leafName: file.leafName,
domFile: File.createFromNsIFile(file),
File.createFromNsIFile(file).then(function(domFile) {
sendAsyncMessage("file.opened", {
fullPath: file.path,
leafName: file.leafName,
domFile,
});
});
} catch(e) {
sendAsyncMessage("fail", e.toString());

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

@ -48,7 +48,12 @@ function testSteps()
let fileReader = new FileReader();
fileReader.onload = continueToNextStepSync;
fileReader.readAsArrayBuffer(File.createFromNsIFile(file));
let domFile;
File.createFromNsIFile(file).then(file => { domFile = file; }).then(continueToNextStepSync);
yield undefined;
fileReader.readAsArrayBuffer(domFile);
yield undefined;
@ -82,7 +87,11 @@ function testSteps()
fileReader = new FileReader();
fileReader.onload = continueToNextStepSync;
fileReader.readAsArrayBuffer(File.createFromNsIFile(file));
File.createFromNsIFile(file).then(file => { domFile = file; }).then(continueToNextStepSync);
yield undefined;
fileReader.readAsArrayBuffer(domFile);
yield undefined;
@ -108,7 +117,11 @@ function testSteps()
fileReader = new FileReader();
fileReader.onload = continueToNextStepSync;
fileReader.readAsArrayBuffer(File.createFromNsIFile(file));
File.createFromNsIFile(file).then(file => { domFile = file; }).then(continueToNextStepSync);
yield undefined;
fileReader.readAsArrayBuffer(domFile);
yield undefined;

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

@ -646,6 +646,7 @@ var SpecialPowers = {
this._createdFiles = new Array;
}
let createdFiles = this._createdFiles;
let promises = [];
requests.forEach(function(request) {
const filePerms = 0o666;
let testFile = dirSvc.get("ProfD", Ci.nsIFile);
@ -654,20 +655,24 @@ var SpecialPowers = {
} else {
testFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, filePerms);
}
let outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
outStream.init(testFile, 0x02 | 0x08 | 0x20, // PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
filePerms, 0);
if (request.data) {
outStream.write(request.data, request.data.length);
outStream.close();
}
filePaths.push(File.createFromFileName(testFile.path, request.options));
createdFiles.push(testFile);
let outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
outStream.init(testFile, 0x02 | 0x08 | 0x20, // PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
filePerms, 0);
if (request.data) {
outStream.write(request.data, request.data.length);
outStream.close();
}
promises.push(File.createFromFileName(testFile.path, request.options).then(function(file) {
filePaths.push(file);
}));
createdFiles.push(testFile);
});
setTimeout(function () {
callback(filePaths);
}, 0);
Promise.all(promises).then(function() {
setTimeout(function () {
callback(filePaths);
}, 0);
});
},
removeFiles: function() {

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

@ -40,10 +40,10 @@ partial interface File {
readonly attribute DOMString mozFullPath;
[ChromeOnly, Throws, NeedsCallerType]
static File createFromNsIFile(nsIFile file,
optional ChromeFilePropertyBag options);
static Promise<File> createFromNsIFile(nsIFile file,
optional ChromeFilePropertyBag options);
[ChromeOnly, Throws, NeedsCallerType]
static File createFromFileName(USVString fileName,
optional ChromeFilePropertyBag options);
static Promise<File> createFromFileName(USVString fileName,
optional ChromeFilePropertyBag options);
};

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

@ -17,13 +17,26 @@ function createFileWithData(fileData) {
}
outStream.write(fileData, fileData.length);
outStream.close();
var domFile = File.createFromNsIFile(testFile);
if (willDelete) {
testFile.remove(/* recursive: */ false);
}
return domFile;
return File.createFromNsIFile(testFile).then(function(domFile) {
if (willDelete) {
testFile.remove(/* recursive: */ false);
}
return domFile;
});
}
addMessageListener("files.open", function (message) {
sendAsyncMessage("files.opened", message.map(createFileWithData));
let promises = [];
let list = [];
for (let fileData of message) {
promises.push(createFileWithData(fileData).then(domFile => {
list.push(domFile);
}));
}
Promise.all(promises).then(() => {
sendAsyncMessage("files.opened", list);
});
});

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

@ -9,7 +9,7 @@ addMessageListener("file.open", function (e) {
tmpFile.append('file.txt');
tmpFile.createUnique(Components.interfaces.nsIFile.FILE_TYPE, 0o600);
sendAsyncMessage("file.opened", {
data: File.createFromNsIFile(tmpFile)
File.createFromNsIFile(tmpFile).then(function(file) {
sendAsyncMessage("file.opened", { data: file });
});
});

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

@ -21,7 +21,7 @@ function FileComponent() {
}
FileComponent.prototype =
{
doTest: function() {
doTest: function(cb) {
// throw if anything goes wrong
// find the current directory path
@ -31,51 +31,55 @@ FileComponent.prototype =
file.append("xpcshell.ini");
// should be able to construct a file
var f1 = File.createFromFileName(file.path);
// and with nsIFiles
var f2 = File.createFromNsIFile(file);
var f1, f2;
Promise.all([
File.createFromFileName(file.path).then(f => { f1 = f; }),
File.createFromNsIFile(file).then(f => { f2 = f; }),
])
.then(() => {
// do some tests
do_check_true(f1 instanceof File, "Should be a DOM File");
do_check_true(f2 instanceof File, "Should be a DOM File");
// do some tests
do_check_true(f1 instanceof File, "Should be a DOM File");
do_check_true(f2 instanceof File, "Should be a DOM File");
do_check_true(f1.name == "xpcshell.ini", "Should be the right file");
do_check_true(f2.name == "xpcshell.ini", "Should be the right file");
do_check_true(f1.name == "xpcshell.ini", "Should be the right file");
do_check_true(f2.name == "xpcshell.ini", "Should be the right file");
do_check_true(f1.type == "", "Should be the right type");
do_check_true(f2.type == "", "Should be the right type");
})
.then(() => {
var threw = false;
try {
// Needs a ctor argument
var f7 = new File();
} catch (e) {
threw = true;
}
do_check_true(threw, "No ctor arguments should throw");
do_check_true(f1.type == "", "Should be the right type");
do_check_true(f2.type == "", "Should be the right type");
var threw = false;
try {
// Needs a valid ctor argument
var f7 = new File(Date(132131532));
} catch (e) {
threw = true;
}
do_check_true(threw, "Passing a random object should fail");
var threw = false;
try {
// Needs a ctor argument
var f7 = new File();
} catch (e) {
threw = true;
}
do_check_true(threw, "No ctor arguments should throw");
var threw = false;
try {
// Needs a valid ctor argument
var f7 = new File(Date(132131532));
} catch (e) {
threw = true;
}
do_check_true(threw, "Passing a random object should fail");
var threw = false
try {
// Directories fail
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
var f7 = File.createFromNsIFile(dir)
} catch (e) {
threw = true;
}
do_check_true(threw, "Can't create a File object for a directory");
return true;
return File.createFromNsIFile(dir)
})
.then(() => {
do_check_true(false, "Can't create a File object for a directory");
}, () => {
do_check_true(true, "Can't create a File object for a directory");
})
.then(() => {
cb(true);
});
},
// nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm

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

@ -2,7 +2,7 @@
* 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() {
add_task(function() {
do_load_manifest("component-file.manifest");
const contractID = "@mozilla.org/tests/component-file;1";
do_check_true(contractID in Components.classes);
@ -11,5 +11,9 @@ function run_test() {
do_check_true(Boolean(foo));
do_check_true(foo.contractID == contractID);
do_check_true(!!foo.wrappedJSObject);
do_check_true(foo.wrappedJSObject.doTest());
}
foo.wrappedJSObject.doTest(result => {
do_check_true(result);
run_next_test();
});
});

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

@ -6,7 +6,7 @@ Components.utils.importGlobalProperties(['File']);
const Ci = Components.interfaces;
function run_test() {
add_task(function*() {
// throw if anything goes wrong
// find the current directory path
@ -16,9 +16,9 @@ function run_test() {
file.append("xpcshell.ini");
// should be able to construct a file
var f1 = File.createFromFileName(file.path);
var f1 = yield File.createFromFileName(file.path);
// and with nsIFiles
var f2 = File.createFromNsIFile(file);
var f2 = yield File.createFromNsIFile(file);
// do some tests
do_check_true(f1 instanceof File, "Should be a DOM File");
@ -54,9 +54,9 @@ function run_test() {
var dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("CurWorkD", Ci.nsIFile);
var f7 = File.createFromNsIFile(dir)
var f7 = yield File.createFromNsIFile(dir)
} catch (e) {
threw = true;
}
do_check_true(threw, "Can't create a File object for a directory");
}
});

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

@ -21,6 +21,7 @@ FilePicker.prototype = {
_extensionsFilter: "",
_defaultString: "",
_domWin: null,
_domFile: null,
_defaultExtension: null,
_displayDirectory: null,
_filePath: null,
@ -135,37 +136,16 @@ FilePicker.prototype = {
},
get files() {
return this.getEnumerator([this.file], function(file) {
return file;
});
return this.getEnumerator([this.file]);
},
// We don't support directory selection yet.
get domFileOrDirectory() {
let f = this.file;
if (!f) {
return null;
}
let win = this._domWin;
if (win) {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
return utils.wrapDOMFile(f);
}
return File.createFromNsIFile(f);
return this._domFile;
},
get domFileOrDirectoryEnumerator() {
let win = this._domWin;
return this.getEnumerator([this.file], function(file) {
if (win) {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
return utils.wrapDOMFile(file);
}
return File.createFromNsIFile(file);
});
return this.getEnumerator([this._domFile]);
},
get addToRecentDocs() {
@ -244,15 +224,30 @@ FilePicker.prototype = {
this._filePath = file || null;
this._promptActive = false;
if (!file) {
return;
}
if (this._domWin) {
let utils = this._domWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
this._domFile = utils.wrapDOMFile(this.file);
return;
}
return File.createFromNsIFile(this.file).then(domFile => {
this._domFile = domFile;
});
}).catch(() => {
}).then(() => {
if (this._callback) {
this._callback.done(this._filePath ?
Ci.nsIFilePicker.returnOK : Ci.nsIFilePicker.returnCancel);
Ci.nsIFilePicker.returnOK : Ci.nsIFilePicker.returnCancel);
}
delete this._callback;
});
},
getEnumerator: function(files, mapFunction) {
getEnumerator: function(files) {
return {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
mFiles: files,
@ -264,7 +259,7 @@ FilePicker.prototype = {
if (this.mIndex >= this.mFiles.length) {
throw Components.results.NS_ERROR_FAILURE;
}
return mapFunction(this.mFiles[this.mIndex++]);
return this.mFiles[this.mIndex++];
}
};
},

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

@ -232,10 +232,13 @@ interaction.selectOption = function (el) {
* Full path to file.
*/
interaction.uploadFile = function (el, path) {
let file;
try {
file = File.createFromFileName(path);
} catch (e) {
let file = yield File.createFromFileName(path).then(file => {
return file;
}, () => {
return null;
});
if (!file) {
throw new InvalidArgumentError("File not found: " + path);
}

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

@ -253,6 +253,7 @@ SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
}
let createdFiles = this._createdFiles;
try {
let promises = [];
aMessage.data.forEach(function(request) {
const filePerms = 0666;
let testFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
@ -268,14 +269,25 @@ SpecialPowersObserver.prototype.receiveMessage = function(aMessage) {
outStream.write(request.data, request.data.length);
}
outStream.close();
filePaths.push(File.createFromFileName(testFile.path, request.options));
promises.push(File.createFromFileName(testFile.path, request.options).then(function(file) {
filePaths.push(file);
}));
createdFiles.push(testFile);
});
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SpecialPowers.FilesCreated", filePaths);
Promise.all(promises).then(function() {
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SpecialPowers.FilesCreated", filePaths);
}, function(e) {
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader
.messageManager
.sendAsyncMessage("SpecialPowers.FilesError", e.toString());
});
} catch (e) {
aMessage.target
.QueryInterface(Ci.nsIFrameLoaderOwner)

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

@ -640,7 +640,9 @@ function loadMemoryReportsFromFile(aFilename, aTitleNote, aFn) {
// If it doesn't have a .gz suffix, read it as a (legacy) ungzipped file.
if (!aFilename.endsWith(".gz")) {
reader.readAsText(File.createFromFileName(aFilename));
File.createFromFileName(aFilename).then(file => {
reader.readAsText(file);
});
return;
}

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

@ -284,16 +284,25 @@ Submitter.prototype = {
formData.append("Throttleable", "0");
}
// add the minidumps
formData.append("upload_file_minidump", File.createFromFileName(this.dump.path));
let promises = [
File.createFromFileName(this.dump.path).then(file => {
formData.append("upload_file_minidump", file);
})
]
if (this.memory) {
formData.append("memory_report", File.createFromFileName(this.memory.path));
promises.push(File.createFromFileName(this.memory.path).then(file => {
formData.append("memory_report", file);
}));
}
if (this.additionalDumps.length > 0) {
let names = [];
for (let i of this.additionalDumps) {
names.push(i.name);
formData.append("upload_file_minidump_" + i.name,
File.createFromFileName(i.dump.path));
promises.push(File.createFromFileName(i.dump.path).then(file => {
formData.append("upload_file_minidump_" + i.name, file);
}));
}
}
@ -328,7 +337,7 @@ Submitter.prototype = {
}
});
let p = Promise.resolve();
let p = Promise.all(promises);
let id = this.id;
if (this.recordSubmission) {

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

@ -91,15 +91,8 @@ this.PropertyListUtils = Object.freeze({
// We guarantee not to throw directly for any other exceptions, and always
// call aCallback.
Services.tm.mainThread.dispatch(function() {
let file = aFile;
try {
if (file instanceof Ci.nsILocalFile) {
if (!file.exists())
throw new Error("The file pointed by aFile does not exist");
file = File.createFromNsIFile(file);
}
let self = this;
function readDOMFile(aFile) {
let fileReader = new FileReader();
let onLoadEnd = function() {
let root = null;
@ -108,13 +101,27 @@ this.PropertyListUtils = Object.freeze({
if (fileReader.readyState != fileReader.DONE)
throw new Error("Could not read file contents: " + fileReader.error);
root = this._readFromArrayBufferSync(fileReader.result);
root = self._readFromArrayBufferSync(fileReader.result);
} finally {
aCallback(root);
}
}.bind(this);
}
fileReader.addEventListener("loadend", onLoadEnd);
fileReader.readAsArrayBuffer(file);
fileReader.readAsArrayBuffer(aFile);
}
try {
if (aFile instanceof Ci.nsILocalFile) {
if (!aFile.exists()) {
throw new Error("The file pointed by aFile does not exist");
}
File.createFromNsIFile(aFile).then(function(aFile) {
readDOMFile(aFile);
});
return;
}
readDOMFile(aFile);
} catch (ex) {
aCallback(null);
throw ex;