From d7f01adb674714d8fe1838d6a09d9b2ec3a847c5 Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Sun, 2 Aug 2009 22:23:08 +0430 Subject: [PATCH] Bug 496123 - the last download directory from private browsing persists as the initial directory for the filepicker after stopping private browsing; r=mconnor --- browser/base/content/browser.js | 37 +++++- .../privatebrowsing/test/browser/Makefile.in | 1 + .../browser/browser_privatebrowsing_opendir.js | 142 +++++++++++++++++++++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 browser/components/privatebrowsing/test/browser/browser_privatebrowsing_opendir.js diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index b886ea6192..1466b42dc4 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -1840,6 +1840,35 @@ function delayedOpenTab(aUrl, aReferrer, aCharset, aPostData, aAllowThirdPartyFi gBrowser.loadOneTab(aUrl, aReferrer, aCharset, aPostData, false, aAllowThirdPartyFixup); } +var gLastOpenDirectory = { + _lastDir: null, + get path() { + if (!this._lastDir || !this._lastDir.exists()) { + try { + this._lastDir = gPrefService.getComplexValue("browser.open.lastDir", + Ci.nsILocalFile); + if (!this._lastDir.exists()) + this._lastDir = null; + } + catch(e) {} + } + return this._lastDir; + }, + set path(val) { + if (!val || !val.exists() || !val.isDirectory()) + return; + this._lastDir = val.clone(); + + // Don't save the last open directory pref inside the Private Browsing mode + if (!gPrivateBrowsingUI.privateBrowsingEnabled) + gPrefService.setComplexValue("browser.open.lastDir", Ci.nsILocalFile, + this._lastDir); + }, + reset: function() { + this._lastDir = null; + } +}; + function BrowserOpenFileWindow() { // Get filepicker component. @@ -1849,9 +1878,13 @@ function BrowserOpenFileWindow() fp.init(window, gNavigatorBundle.getString("openFile"), nsIFilePicker.modeOpen); fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText | nsIFilePicker.filterImages | nsIFilePicker.filterXML | nsIFilePicker.filterHTML); + fp.displayDirectory = gLastOpenDirectory.path; - if (fp.show() == nsIFilePicker.returnOK) + if (fp.show() == nsIFilePicker.returnOK) { + if (fp.file && fp.file.exists()) + gLastOpenDirectory.path = fp.file.parent.QueryInterface(Ci.nsILocalFile); openTopWin(fp.fileURL.spec); + } } catch (ex) { } } @@ -6966,6 +6999,8 @@ let gPrivateBrowsingUI = { .removeAttribute("disabled"); this._privateBrowsingAutoStarted = false; + + gLastOpenDirectory.reset(); }, _setPBMenuTitle: function PBUI__setPBMenuTitle(aMode) { diff --git a/browser/components/privatebrowsing/test/browser/Makefile.in b/browser/components/privatebrowsing/test/browser/Makefile.in index 11f6f26e48..f65cfec2f9 100644 --- a/browser/components/privatebrowsing/test/browser/Makefile.in +++ b/browser/components/privatebrowsing/test/browser/Makefile.in @@ -64,6 +64,7 @@ _BROWSER_TEST_FILES = \ browser_privatebrowsing_popupmode.js \ browser_privatebrowsing_geoprompt.js \ browser_privatebrowsing_geoprompt_page.html \ + browser_privatebrowsing_opendir.js \ $(NULL) libs:: $(_BROWSER_TEST_FILES) diff --git a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_opendir.js b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_opendir.js new file mode 100644 index 0000000000..cb83edf610 --- /dev/null +++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_opendir.js @@ -0,0 +1,142 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Private Browsing Tests. + * + * The Initial Developer of the Original Code is + * Ehsan Akhgari. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Ehsan Akhgari (Original Author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +// This test makes sure that the last open directory used inside the private +// browsing mode is not remembered after leaving that mode. + +function test() { + // initialization + let pb = Cc["@mozilla.org/privatebrowsing;1"]. + getService(Ci.nsIPrivateBrowsingService); + let ds = Cc["@mozilla.org/file/directory_service;1"]. + getService(Ci.nsIProperties); + let dir1 = ds.get("ProfD", Ci.nsIFile); + let dir2 = ds.get("TmpD", Ci.nsIFile); + let file = dir2.clone(); + file.append("pbtest.file"); + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); + + const kPrefName = "browser.open.lastDir"; + + function setupCleanSlate() { + gLastOpenDirectory.reset(); + gPrefService.clearUserPref(kPrefName); + } + + setupCleanSlate(); + + // Test 1: general workflow test + + // initial checks + ok(!gLastOpenDirectory.path, + "Last open directory path should be initially empty"); + gLastOpenDirectory.path = dir2; + is(gLastOpenDirectory.path.path, dir2.path, + "The path should be successfully set"); + gLastOpenDirectory.path = null; + is(gLastOpenDirectory.path.path, dir2.path, + "The path should be not change when assigning it to null"); + gLastOpenDirectory.path = dir1; + is(gLastOpenDirectory.path.path, dir1.path, + "The path should be successfully outside of the private browsing mode"); + + // enter private browsing mode + pb.privateBrowsingEnabled = true; + + is(gLastOpenDirectory.path.path, dir1.path, + "The path should not change when entering the private browsing mode"); + gLastOpenDirectory.path = dir2; + is(gLastOpenDirectory.path.path, dir2.path, + "The path should successfully change inside the private browsing mode"); + + // leave private browsing mode + pb.privateBrowsingEnabled = false; + + is(gLastOpenDirectory.path.path, dir1.path, + "The path should be reset to the same path as before entering the private browsing mode"); + + setupCleanSlate(); + + // Test 2: the user first tries to open a file inside the private browsing mode + + pb.privateBrowsingEnabled = true; + ok(!gLastOpenDirectory.path, + "No original path should exist inside the private browsing mode"); + gLastOpenDirectory.path = dir1; + is(gLastOpenDirectory.path.path, dir1.path, + "The path should be successfully set inside the private browsing mode"); + pb.privateBrowsingEnabled = false; + ok(!gLastOpenDirectory.path, + "The path set inside the private browsing mode should not leak when leaving that mode"); + + setupCleanSlate(); + + // Test 3: the last open directory is set from a previous session, it should be used + // in normal mode + + gPrefService.setComplexValue(kPrefName, Ci.nsILocalFile, dir1); + is(gLastOpenDirectory.path.path, dir1.path, + "The pref set from last session should take effect outside the private browsing mode"); + + setupCleanSlate(); + + // Test 4: the last open directory is set from a previous session, it should be used + // in private browsing mode mode + + gPrefService.setComplexValue(kPrefName, Ci.nsILocalFile, dir1); + pb.privateBrowsingEnabled = true; + is(gLastOpenDirectory.path.path, dir1.path, + "The pref set from last session should take effect inside the private browsing mode"); + pb.privateBrowsingEnabled = false; + is(gLastOpenDirectory.path.path, dir1.path, + "The pref set from last session should remain in effect after leaving the private browsing mode"); + + setupCleanSlate(); + + // Test 5: setting the path to a file shouldn't work + + gLastOpenDirectory.path = file; + ok(!gLastOpenDirectory.path, + "Setting the path to a file shouldn't work when it's originally null"); + gLastOpenDirectory.path = dir1; + gLastOpenDirectory.path = file; + is(gLastOpenDirectory.path.path, dir1.path, + "Setting the path to a file shouldn't work when it's not originally null"); + + // cleanup + file.remove(false); +} -- 2.11.4.GIT