Bug 982874 - Import / export API for apps : Part 1, temporary blobs r=bent

This commit is contained in:
Fabrice Desré 2014-10-14 22:55:14 -07:00
Родитель 0b08fa46e2
Коммит b9d48b1ed6
6 изменённых файлов: 121 добавлений и 7 удалений

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

@ -106,7 +106,7 @@ public:
const nsAString& aContentType);
static already_AddRefed<File>
CreateFromFile(nsISupports* aParent, nsIFile* aFile);
CreateFromFile(nsISupports* aParent, nsIFile* aFile, bool aTemporary = false);
static already_AddRefed<File>
CreateFromFile(nsISupports* aParent, const nsAString& aContentType,
@ -631,11 +631,12 @@ public:
NS_DECL_ISUPPORTS_INHERITED
// Create as a file
explicit FileImplFile(nsIFile* aFile)
explicit FileImplFile(nsIFile* aFile, bool aTemporary = false)
: FileImplBase(EmptyString(), EmptyString(), UINT64_MAX, UINT64_MAX)
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(false)
, mIsTemporary(aTemporary)
{
NS_ASSERTION(mFile, "must have file");
// Lazily get the content type and size
@ -648,6 +649,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(true)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
NS_ASSERTION(aFileInfo, "must have file info");
@ -665,6 +667,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(false)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
}
@ -676,6 +679,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(false)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
}
@ -687,6 +691,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(false)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
if (aContentType.IsEmpty()) {
@ -703,6 +708,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(true)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
mFileInfos.AppendElement(aFileInfo);
@ -715,6 +721,7 @@ public:
, mFile(aFile)
, mWholeFile(true)
, mStoredFile(true)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
mFileInfos.AppendElement(aFileInfo);
@ -725,6 +732,7 @@ public:
: FileImplBase(EmptyString(), EmptyString(), UINT64_MAX, UINT64_MAX)
, mWholeFile(true)
, mStoredFile(false)
, mIsTemporary(false)
{
// Lazily get the content type and size
mContentType.SetIsVoid(true);
@ -742,7 +750,17 @@ public:
void SetPath(const nsAString& aFullPath);
protected:
virtual ~FileImplFile() {}
virtual ~FileImplFile() {
if (mFile && mIsTemporary) {
// Ignore errors if any, not much we can do. Clean-up will be done by
// https://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAnonymousTemporaryFile.cpp?rev=6c1c7e45c902#127
#ifdef DEBUG
nsresult rv =
#endif
mFile->Remove(false);
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to remove temporary DOMFile.");
}
}
private:
// Create slice
@ -752,6 +770,7 @@ private:
, mFile(aOther->mFile)
, mWholeFile(false)
, mStoredFile(aOther->mStoredFile)
, mIsTemporary(false)
{
NS_ASSERTION(mFile, "must have file");
mImmutable = aOther->mImmutable;
@ -790,6 +809,7 @@ private:
nsCOMPtr<nsIFile> mFile;
bool mWholeFile;
bool mStoredFile;
bool mIsTemporary;
};
class FileList MOZ_FINAL : public nsIDOMFileList,

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

@ -231,9 +231,9 @@ File::CreateTemporaryFileBlob(nsISupports* aParent, PRFileDesc* aFD,
}
/* static */ already_AddRefed<File>
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile)
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile, bool aTemporary)
{
nsRefPtr<File> file = new File(aParent, new FileImplFile(aFile));
nsRefPtr<File> file = new File(aParent, new FileImplFile(aFile, aTemporary));
return file.forget();
}

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

@ -310,7 +310,7 @@ MultipartFileImpl::InitializeChromeFile(nsPIDOMWindow* aWindow,
aFile->GetLeafName(mName);
}
nsRefPtr<File> blob = File::CreateFromFile(aWindow, aFile);
nsRefPtr<File> blob = File::CreateFromFile(aWindow, aFile, aBag.mTemporary);
// Pre-cache size.
uint64_t unused;

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

@ -58,4 +58,5 @@ skip-if = buildapp == 'mulet'
[test_document_register.xul]
[test_domparsing.xul]
[test_fileconstructor.xul]
[test_fileconstructor_tempfile.xul]
[test_title.xul]

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

@ -0,0 +1,93 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet
href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=982874
Tests building a DOMFile with the "temporary" option and checks that
the underlying file is removed when the DOMFile is gc'ed.
-->
<window title="Mozilla Bug 982874"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<body xmlns="http://www.w3.org/1999/xhtml">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=982874">
Mozilla Bug 982874</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
<script class="testbody" type="application/javascript">
<![CDATA[
/** Test for Bug 982874 **/
let Cc = Components.classes;
let Ci = Components.interfaces;
let Cu = Components.utils;
SimpleTest.waitForExplicitFinish();
function cleanup(tmp) {
// Force cycle and garbage collection and check that we removed the file.
for (let i = 0; i < 10; i++) {
Cu.forceCC();
Cu.forceGC();
}
if (tmp.exists()) {
ok(false, "Failed to remove temporary file!");
} else {
ok(true, "Temporary file removed when gc-ing the DOMFile");
}
SimpleTest.finish();
}
try {
// Create a file in $TMPDIR/mozilla-temp-files
let tmp = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
tmp.append("mozilla-temp-files");
tmp.append("test.txt");
tmp.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
// Add some content to the file.
let fileData = "I'm a temporary file!";
let outStream = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
outStream.init(tmp, 0x02 | 0x08 | 0x20, // write, create, truncate
0666, 0);
outStream.write(fileData, fileData.length);
outStream.close();
// Create a scoped DOMFile so the gc will happily get rid of it.
{
let dirfile = new File(tmp, { temporary: true });
ok(true, "Temporary File() created");
var reader = new FileReader();
reader.readAsArrayBuffer(dirfile);
reader.onload = function(event) {
let buffer = event.target.result;
ok(buffer.byteLength > 0,
"Blob size should be > 0 : " + buffer.byteLength);
cleanup(tmp);
}
}
} catch (e) {
ok(false, "Unable to create the File() object : " + e);
SimpleTest.finish();
}
]]>
</script>
</window>

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

@ -35,7 +35,7 @@ dictionary FilePropertyBag {
dictionary ChromeFilePropertyBag : FilePropertyBag {
DOMString name = "";
boolean temporary = false;
};
// Mozilla extensions