Bug 1194893 - Pref for default file upload directory. r=smaug

This commit is contained in:
Garrett Robinson 2015-12-16 11:58:40 -08:00
parent b184ba27ca
commit e507806a5b
4 changed files with 106 additions and 13 deletions

View File

@ -295,20 +295,25 @@ NS_IMPL_ISUPPORTS(UploadLastDir::ContentPrefCallback, nsIContentPrefCallback2)
NS_IMETHODIMP
UploadLastDir::ContentPrefCallback::HandleCompletion(uint16_t aReason)
{
nsCOMPtr<nsIFile> localFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
NS_ENSURE_STATE(localFile);
nsCOMPtr<nsIFile> localFile;
nsAutoString prefStr;
if (aReason == nsIContentPrefCallback2::COMPLETE_ERROR ||
!mResult) {
// Default to "desktop" directory for each platform
nsCOMPtr<nsIFile> homeDir;
NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(homeDir));
localFile = do_QueryInterface(homeDir);
} else {
nsAutoString prefStr;
nsCOMPtr<nsIVariant> pref;
mResult->GetValue(getter_AddRefs(pref));
pref->GetAsAString(prefStr);
if (aReason == nsIContentPrefCallback2::COMPLETE_ERROR || !mResult) {
prefStr = Preferences::GetString("dom.input.fallbackUploadDir");
if (prefStr.IsEmpty()) {
// If no custom directory was set through the pref, default to
// "desktop" directory for each platform.
NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(localFile));
}
}
if (!localFile) {
if (prefStr.IsEmpty() && mResult) {
nsCOMPtr<nsIVariant> pref;
mResult->GetValue(getter_AddRefs(pref));
pref->GetAsAString(prefStr);
}
localFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
localFile->InitWithPath(prefStr);
}

View File

@ -600,3 +600,5 @@ skip-if = buildapp == 'b2g' # bug 1129014
[test_image_clone_load.html]
[test_bug1203668.html]
[test_bug1166138.html]
[test_filepicker_default_directory.html]
skip-if = buildapp == 'mulet' || buildapp == 'b2g' || toolkit == 'android'

View File

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1194893
-->
<head>
<title>Test for filepicker default directory</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1194893">Mozilla Bug 1194893</a>
<div id="content">
<input type="file" id="f">
</div>
<pre id="text">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
const { Cc: Cc, Ci: Ci } = SpecialPowers;
// Platform-independent directory names are #define'd in xpcom/io/nsDirectoryServiceDefs.h
var defaultUploadDirectory = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("Desk", Ci.nsIFile);
// When we want to test an upload directory other than the default, we need to
// get a valid directory in a platform-independent way. Since NS_OS_DESKTOP_DIR
// may fallback to NS_OS_HOME_DIR, let's use NS_OS_TMP_DIR.
var customUploadDirectory = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIDirectoryService)
.QueryInterface(Ci.nsIProperties)
.get("TmpD", Ci.nsIFile);
// Useful for debugging
//info("defaultUploadDirectory" + defaultUploadDirectory.path);
//info("customUploadDirectory" + customUploadDirectory.path);
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);
// need to show the MockFilePicker so .displayDirectory gets set
var f = document.getElementById("f");
f.focus();
var testIndex = 0;
var tests = [
["", defaultUploadDirectory.path],
[customUploadDirectory.path, customUploadDirectory.path]
]
MockFilePicker.showCallback = function(filepicker) {
info(SpecialPowers.wrap(MockFilePicker).displayDirectory.path);
is(SpecialPowers.wrap(MockFilePicker).displayDirectory.path,
tests[testIndex][1]);
if (++testIndex == tests.length) {
MockFilePicker.cleanup();
SimpleTest.finish();
} else {
launchNextTest();
}
}
function launchNextTest() {
SpecialPowers.pushPrefEnv(
{ 'set': [
['dom.input.fallbackUploadDir', tests[testIndex][0]],
]},
function () {
f.click();
});
}
launchNextTest();
</script>
</pre>
</body>
</html>

View File

@ -5131,3 +5131,6 @@ pref("dom.mozKillSwitch.enabled", false);
pref("toolkit.pageThumbs.screenSizeDivisor", 7);
pref("toolkit.pageThumbs.minWidth", 0);
pref("toolkit.pageThumbs.minHeight", 0);
// Allow customization of the fallback directory for file uploads
pref("dom.input.fallbackUploadDir", "");