Bug 722988 - openLocationLastURL.jsm uses global Private Browsing state to make decisions; r=ehsan
authorSaurabh Anand <saurabhanandiit@gmail.com>
Thu, 28 Jun 2012 03:11:24 +0530
changeset 97862 f1bd5333433e60a3fa3bfee672fd004c9738e95a
parent 97861 fec0dc972e853e1f0cebe51419873fd029bb3b2e
child 97863 158a9986c6427cd8d98f1690d3a1e3a75db09319
push id23007
push useremorley@mozilla.com
push dateFri, 29 Jun 2012 07:42:23 +0000
treeherdermozilla-central@4a8e0d5fc954 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs722988
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 722988 - openLocationLastURL.jsm uses global Private Browsing state to make decisions; r=ehsan
browser/base/content/openLocation.js
browser/components/privatebrowsing/test/unit/test_openLocationLastURL.js
browser/modules/openLocationLastURL.jsm
--- a/browser/base/content/openLocation.js
+++ b/browser/base/content/openLocation.js
@@ -2,24 +2,26 @@
 #
 # 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/.
 
 var browser;
 var dialog = {};
 var pref = null;
+let openLocationModule = {};
 try {
   pref = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefBranch);
 } catch (ex) {
   // not critical, remain silent
 }
 
-Components.utils.import("resource:///modules/openLocationLastURL.jsm");
+Components.utils.import("resource:///modules/openLocationLastURL.jsm", openLocationModule);
+let gOpenLocationLastURL = new openLocationModule.OpenLocationLastURL(window.opener);
 
 function onLoad()
 {
   dialog.input         = document.getElementById("dialog.input");
   dialog.open          = document.documentElement.getButton("accept");
   dialog.openWhereList = document.getElementById("openWhereList");
   dialog.openTopWindow = document.getElementById("currentWindow");
   dialog.bundle        = document.getElementById("openLocationBundle");
--- a/browser/components/privatebrowsing/test/unit/test_openLocationLastURL.js
+++ b/browser/components/privatebrowsing/test/unit/test_openLocationLastURL.js
@@ -1,69 +1,78 @@
 /* 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/. */
 
 // Test the correct behavior of the openLocationLastURL.jsm JS module.
 
 function run_test_on_service()
 {
-  Cu.import("resource:///modules/openLocationLastURL.jsm");
-
+  let openLocationModule = {};
+  // This variable fakes the window required for getting the PB flag
+  let window = { gPrivateBrowsingUI: { privateWindow: false } };
+  Cu.import("resource:///modules/openLocationLastURL.jsm", openLocationModule);
+  let gOpenLocationLastURL = new openLocationModule.OpenLocationLastURL(window);
+  
   function clearHistory() {
     // simulate clearing the private data
     Cc["@mozilla.org/observer-service;1"].
     getService(Ci.nsIObserverService).
     notifyObservers(null, "browser:purge-session-history", "");
   }
-
+  
   let pb = Cc[PRIVATEBROWSING_CONTRACT_ID].
            getService(Ci.nsIPrivateBrowsingService);
   let pref = Cc["@mozilla.org/preferences-service;1"].
              getService(Ci.nsIPrefBranch);
   gOpenLocationLastURL.reset();
 
   do_check_eq(typeof gOpenLocationLastURL, "object");
   do_check_eq(gOpenLocationLastURL.value, "");
 
+  function switchPrivateBrowsing(flag) {
+    pb.privateBrowsingEnabled = flag;
+    window.gPrivateBrowsingUI.privateWindow = flag;
+  }
+
   const url1 = "mozilla.org";
   const url2 = "mozilla.com";
 
   gOpenLocationLastURL.value = url1;
   do_check_eq(gOpenLocationLastURL.value, url1);
 
   gOpenLocationLastURL.value = "";
   do_check_eq(gOpenLocationLastURL.value, "");
 
   gOpenLocationLastURL.value = url2;
   do_check_eq(gOpenLocationLastURL.value, url2);
 
   clearHistory();
   do_check_eq(gOpenLocationLastURL.value, "");
   gOpenLocationLastURL.value = url2;
 
-  pb.privateBrowsingEnabled = true;
+  switchPrivateBrowsing(true);
   do_check_eq(gOpenLocationLastURL.value, "");
-
-  pb.privateBrowsingEnabled = false;
+  
+  switchPrivateBrowsing(false);
   do_check_eq(gOpenLocationLastURL.value, url2);
-  pb.privateBrowsingEnabled = true;
+  switchPrivateBrowsing(true);
 
   gOpenLocationLastURL.value = url1;
   do_check_eq(gOpenLocationLastURL.value, url1);
 
-  pb.privateBrowsingEnabled = false;
+  switchPrivateBrowsing(false);
   do_check_eq(gOpenLocationLastURL.value, url2);
 
-  pb.privateBrowsingEnabled = true;
+  switchPrivateBrowsing(true);
   gOpenLocationLastURL.value = url1;
   do_check_neq(gOpenLocationLastURL.value, "");
   clearHistory();
   do_check_eq(gOpenLocationLastURL.value, "");
 
-  pb.privateBrowsingEnabled = false;
+  switchPrivateBrowsing(false);
   do_check_eq(gOpenLocationLastURL.value, "");
 }
 
 // Support running tests on both the service itself and its wrapper
 function run_test() {
   run_test_on_all_services();
 }
--- a/browser/modules/openLocationLastURL.jsm
+++ b/browser/modules/openLocationLastURL.jsm
@@ -1,65 +1,78 @@
 /* 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/. */
 
 const LAST_URL_PREF = "general.open_location.last_url";
 const nsISupportsString = Components.interfaces.nsISupportsString;
-
-var EXPORTED_SYMBOLS = [ "gOpenLocationLastURL" ];
+const Ci = Components.interfaces;
 
-let pbSvc = Components.classes["@mozilla.org/privatebrowsing;1"]
-                      .getService(Components.interfaces.nsIPrivateBrowsingService);
+var EXPORTED_SYMBOLS = [ "OpenLocationLastURL" ];
+
 let prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
                         .getService(Components.interfaces.nsIPrefBranch);
+let gOpenLocationLastURLData = "";
 
 let observer = {
   QueryInterface: function (aIID) {
     if (aIID.equals(Components.interfaces.nsIObserver) ||
         aIID.equals(Components.interfaces.nsISupports) ||
         aIID.equals(Components.interfaces.nsISupportsWeakReference))
       return this;
     throw Components.results.NS_NOINTERFACE;
   },
   observe: function (aSubject, aTopic, aData) {
     switch (aTopic) {
-      case "private-browsing":
+      case "last-pb-context-exited":
         gOpenLocationLastURLData = "";
         break;
       case "browser:purge-session-history":
-        gOpenLocationLastURL.reset();
+        prefSvc.clearUserPref(LAST_URL_PREF);
+        gOpenLocationLastURLData = "";
         break;
     }
   }
 };
 
 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);
 
-let gOpenLocationLastURLData = "";
-let gOpenLocationLastURL = {
+
+function OpenLocationLastURL(aWindow) {
+  this.window = aWindow;
+}
+
+OpenLocationLastURL.prototype = {
+  isPrivate: function OpenLocationLastURL_isPrivate() {
+    // Assume not in private browsing mode, unless the browser window is
+    // in private mode.
+    if (!this.window || !("gPrivateBrowsingUI" in this.window))
+      return false;
+  
+    return this.window.gPrivateBrowsingUI.privateWindow;
+  },
   get value() {
-    if (pbSvc.privateBrowsingEnabled)
+    if (this.isPrivate())
       return gOpenLocationLastURLData;
     else {
       try {
         return prefSvc.getComplexValue(LAST_URL_PREF, nsISupportsString).data;
       }
       catch (e) {
         return "";
       }
     }
   },
   set value(val) {
     if (typeof val != "string")
       val = "";
-    if (pbSvc.privateBrowsingEnabled)
+    if (this.isPrivate())
       gOpenLocationLastURLData = val;
     else {
       let str = Components.classes["@mozilla.org/supports-string;1"]
                           .createInstance(Components.interfaces.nsISupportsString);
       str.data = val;
       prefSvc.setComplexValue(LAST_URL_PREF, nsISupportsString, str);
     }
   },