Bug 722995 - DownloadLastDir.jsm uses global Private Browsing state to make decisions, r=ehsan

This commit is contained in:
Saurabh Anand
2012-07-21 11:25:37 +05:30
parent c2f6aeb463
commit d8da9a21bb
3 changed files with 42 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
@@ -431,7 +431,7 @@ function internalPersist(persistArgs)
* Structure for holding info about automatically supplied parameters for
* internalSave(...). This allows parameters to be supplied so the user does not
* need to be prompted for file info.
* @param aFileAutoChosen This is an nsILocalFile object that has been
* @param aFileAutoChosen This is an nsIFile object that has been
* pre-determined as the filename for the target to save to
* @param aUriAutoChosen This is the nsIURI object for the target
*/
@@ -525,13 +525,13 @@ function initFileInfo(aFI, aURL, aURLCharset, aDocument,
*/
function getTargetFile(aFpP, /* optional */ aSkipPrompt, /* optional */ aRelatedURI)
{
if (!getTargetFile.gDownloadLastDir)
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm", getTargetFile);
var gDownloadLastDir = getTargetFile.gDownloadLastDir;
let downloadModule = {};
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm", downloadModule);
var gDownloadLastDir = new downloadModule.DownloadLastDir(window);
var prefs = getPrefsBrowserDownload("browser.download.");
var useDownloadDir = prefs.getBoolPref("useDownloadDir");
const nsILocalFile = Components.interfaces.nsILocalFile;
const nsIFile = Components.interfaces.nsIFile;
if (!aSkipPrompt)
useDownloadDir = false;
@@ -568,7 +568,7 @@ function getTargetFile(aFpP, /* optional */ aSkipPrompt, /* optional */ aRelated
// Default to desktop.
var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
dir = fileLocator.get("Desk", nsILocalFile);
dir = fileLocator.get("Desk", nsIFile);
}
var fp = makeFilePicker();
@@ -600,11 +600,11 @@ function getTargetFile(aFpP, /* optional */ aSkipPrompt, /* optional */ aRelated
prefs.setIntPref("save_converter_index", fp.filterIndex);
// Do not store the last save directory as a pref inside the private browsing mode
var directory = fp.file.parent.QueryInterface(nsILocalFile);
var directory = fp.file.parent.QueryInterface(nsIFile);
gDownloadLastDir.setFile(aRelatedURI, directory);
fp.file.leafName = validateFileName(fp.file.leafName);
aFpP.saveAsType = fp.filterIndex;
aFpP.file = fp.file;
aFpP.fileURL = fp.fileURL;

View File

@@ -1,3 +1,4 @@
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
@@ -25,18 +26,12 @@
const LAST_DIR_PREF = "browser.download.lastDir";
const SAVE_PER_SITE_PREF = LAST_DIR_PREF + ".savePerSite";
const PBSVC_CID = "@mozilla.org/privatebrowsing;1";
const nsILocalFile = Components.interfaces.nsILocalFile;
const nsIFile = Components.interfaces.nsIFile;
var EXPORTED_SYMBOLS = [ "gDownloadLastDir" ];
var EXPORTED_SYMBOLS = [ "DownloadLastDir" ];
Components.utils.import("resource://gre/modules/Services.jsm");
let pbSvc = null;
if (PBSVC_CID in Components.classes) {
pbSvc = Components.classes[PBSVC_CID]
.getService(Components.interfaces.nsIPrivateBrowsingService);
}
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
let observer = {
QueryInterface: function (aIID) {
@@ -48,12 +43,8 @@ let observer = {
},
observe: function (aSubject, aTopic, aData) {
switch (aTopic) {
case "private-browsing":
if (aData == "enter")
gDownloadLastDirFile = readLastDirPref();
else if (aData == "exit") {
gDownloadLastDirFile = null;
}
case "last-pb-context-exited":
gDownloadLastDirFile = null;
break;
case "browser:purge-session-history":
gDownloadLastDirFile = null;
@@ -67,12 +58,12 @@ let observer = {
let os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
os.addObserver(observer, "private-browsing", true);
os.addObserver(observer, "last-pb-context-exited", true);
os.addObserver(observer, "browser:purge-session-history", true);
function readLastDirPref() {
try {
return Services.prefs.getComplexValue(LAST_DIR_PREF, nsILocalFile);
return Services.prefs.getComplexValue(LAST_DIR_PREF, nsIFile);
}
catch (e) {
return null;
@@ -82,23 +73,34 @@ function readLastDirPref() {
function isContentPrefEnabled() {
try {
return Services.prefs.getBoolPref(SAVE_PER_SITE_PREF);
}
}
catch (e) {
return true;
}
}
let gDownloadLastDirFile = readLastDirPref();
let gDownloadLastDir = {
function DownloadLastDir(aWindow) {
this.window = aWindow;
}
DownloadLastDir.prototype = {
isPrivate: function DownloadLastDir_isPrivate() {
return PrivateBrowsingUtils.isWindowPrivate(this.window);
},
// compat shims
get file() { return this.getFile(); },
set file(val) { this.setFile(null, val); },
cleanupPrivateFile: function () {
gDownloadLastDirFile = null;
},
getFile: function (aURI) {
if (aURI && isContentPrefEnabled()) {
let lastDir = Services.contentPrefs.getPref(aURI, LAST_DIR_PREF);
if (lastDir) {
var lastDirFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
.createInstance(Components.interfaces.nsIFile);
lastDirFile.initWithPath(lastDir);
return lastDirFile;
}
@@ -106,8 +108,11 @@ let gDownloadLastDir = {
if (gDownloadLastDirFile && !gDownloadLastDirFile.exists())
gDownloadLastDirFile = null;
if (pbSvc && pbSvc.privateBrowsingEnabled)
if (this.isPrivate()) {
if (!gDownloadLastDirFile)
gDownloadLastDirFile = readLastDirPref();
return gDownloadLastDirFile;
}
else
return readLastDirPref();
},
@@ -118,14 +123,14 @@ let gDownloadLastDir = {
else
Services.contentPrefs.removePref(aURI, LAST_DIR_PREF);
}
if (pbSvc && pbSvc.privateBrowsingEnabled) {
if (this.isPrivate()) {
if (aFile instanceof Components.interfaces.nsIFile)
gDownloadLastDirFile = aFile.clone();
else
gDownloadLastDirFile = null;
} else {
if (aFile instanceof Components.interfaces.nsIFile)
Services.prefs.setComplexValue(LAST_DIR_PREF, nsILocalFile, aFile);
Services.prefs.setComplexValue(LAST_DIR_PREF, nsIFile, aFile);
else if (Services.prefs.prefHasUserValue(LAST_DIR_PREF))
Services.prefs.clearUserPref(LAST_DIR_PREF);
}

View File

@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/*
# This Source Code Form is subject to the terms of the Mozilla Public
@@ -97,8 +97,9 @@ nsUnknownContentTypeDialogProgressListener.prototype = {
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
const nsITimer = Components.interfaces.nsITimer;
let downloadModule = {};
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm");
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm", downloadModule);
Components.utils.import("resource://gre/modules/DownloadPaths.jsm");
Components.utils.import("resource://gre/modules/DownloadUtils.jsm");
@@ -245,6 +246,8 @@ nsUnknownContentTypeDialog.prototype = {
picker.init(parent, windowTitle, nsIFilePicker.modeSave);
picker.defaultString = aDefaultFile;
let gDownloadLastDir = new downloadModule.DownloadLastDir(parent);
if (aSuggestedFileExtension) {
// aSuggestedFileExtension includes the period, so strip it
picker.defaultExtension = aSuggestedFileExtension.substring(1);