Bug 854299 - Part 2. Update DownloadLastDir.getFile. r=adw

This commit is contained in:
Felipe Gomes 2013-04-01 03:08:49 -03:00
parent 647dddbcc7
commit 3f9c4806fc

View File

@ -101,6 +101,8 @@ DownloadLastDir.prototype = {
cleanupPrivateFile: function () {
gDownloadLastDirFile = null;
},
// This function is now deprecated as it uses the sync nsIContentPrefService
// interface. New consumers should use the getFileAsync function.
getFile: function (aURI) {
if (aURI && isContentPrefEnabled()) {
let loadContext = this.window
@ -126,16 +128,51 @@ DownloadLastDir.prototype = {
else
return readLastDirPref();
},
getFileAsync: function(aURI, aCallback) {
let plainPrefFile = this.getFile();
if (!aURI || !isContentPrefEnabled()) {
Services.tm.mainThread.dispatch(function() aCallback(plainPrefFile),
Components.interfaces.nsIThread.DISPATCH_NORMAL);
return;
}
let uri = aURI instanceof Components.interfaces.nsIURI ? aURI.spec : aURI;
let cps2 = Components.classes["@mozilla.org/content-pref/service;1"]
.getService(Components.interfaces.nsIContentPrefService2);
let loadContext = this.window
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsILoadContext);
let result = null;
cps2.getByDomainAndName(uri, LAST_DIR_PREF, loadContext, {
handleResult: function(aResult) result = aResult,
handleCompletion: function(aReason) {
let file = plainPrefFile;
if (aReason == Components.interfaces.nsIContentPrefCallback2.COMPLETE_OK &&
result instanceof Components.interfaces.nsIContentPref) {
file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
file.initWithPath(result.value);
}
aCallback(file);
}
});
},
setFile: function (aURI, aFile) {
if (aURI && isContentPrefEnabled()) {
let uri = aURI instanceof Components.interfaces.nsIURI ? aURI.spec : aURI;
let cps2 = Components.classes["@mozilla.org/content-pref/service;1"]
.getService(Components.interfaces.nsIContentPrefService2);
let loadContext = this.window
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsILoadContext);
if (aFile instanceof Components.interfaces.nsIFile)
Services.contentPrefs.setPref(aURI, LAST_DIR_PREF, aFile.path, loadContext);
cps2.set(uri, LAST_DIR_PREF, aFile.path, loadContext);
else
Services.contentPrefs.removePref(aURI, LAST_DIR_PREF, loadContext);
cps2.removeByDomainAndName(uri, LAST_DIR_PREF, loadContext);
}
if (this.isPrivate()) {
if (aFile instanceof Components.interfaces.nsIFile)