Bug 723001 - Move privacy state checking from nsFormHistory to its callers. r=dolske
authorJosh Matthews <josh@joshmatthews.net>
Thu, 11 Oct 2012 21:04:56 -0400
changeset 110136 f0ca98653903d781adfb0687450cf6c8ea1fd77c
parent 110135 31bfd182d836781b59b4f4aad787c29a8606f4c4
child 110137 c33c86c1a020a5eb95d751784dd41dd7c9073151
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersdolske
bugs723001
milestone19.0a1
Bug 723001 - Move privacy state checking from nsFormHistory to its callers. r=dolske
browser/components/search/content/search.xml
browser/components/search/test/Makefile.in
browser/components/search/test/browser_contextmenu.js
browser/components/search/test/browser_private_search.js
toolkit/components/satchel/formSubmitListener.js
toolkit/components/satchel/nsFormHistory.js
--- a/browser/components/search/content/search.xml
+++ b/browser/components/search/content/search.xml
@@ -431,17 +431,17 @@
           }
           else {
             var newTabPref = textBox._prefBranch.getBoolPref("browser.search.openintab");
             if ((aEvent && aEvent.altKey) ^ newTabPref)
               where = "tab";
           }
 
           // Save the current value in the form history
-          if (textValue) {
+          if (textValue && !gPrivateBrowsingUI.privateWindow) {
             try {
               textBox._formHistSvc.addEntry(textBox.getAttribute("autocompletesearchparam"),
                                             textValue);
             } catch (ex) {
               Components.utils.reportError("Saving search to form history failed: " + ex);
             }
           }
 
--- a/browser/components/search/test/Makefile.in
+++ b/browser/components/search/test/Makefile.in
@@ -17,11 +17,12 @@ MOCHITEST_BROWSER_FILES = 	  browser_405
 	                  testEngine_mozsearch.xml \
 	                  testEngine.src \
 	                  browser_426329.js \
 	                  426329.xml \
 	                  browser_483086.js \
 	                  483086-1.xml \
 	                  483086-2.xml \
 	                  test.html \
+	                  browser_private_search.js \
 	                  $(NULL)
 
 include $(topsrcdir)/config/rules.mk
--- a/browser/components/search/test/browser_contextmenu.js
+++ b/browser/components/search/test/browser_contextmenu.js
@@ -54,16 +54,17 @@ function test() {
 
     // check the search menu item and then perform a search
     function checkContextMenu() {
       var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0];
       ok(searchItem, "Got search context menu item");
       is(searchItem.label, 'Search ' + ENGINE_NAME + ' for "test search"', "Check context menu label");
       is(searchItem.disabled, false, "Check that search context menu item is enabled");
       searchItem.click();
+      contextMenu.hidePopup();
     }
 
     function checkSearchURL(event){
       is(event.originalTarget.URL,
          "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&client=app&channel=contextsearch",
          "Checking context menu search URL");
       finalize();
     }
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser_private_search.js
@@ -0,0 +1,101 @@
+// This test performs a search in a public window, then a different
+// search in a private window, and then checks in the public window
+// whether there is an autocomplete entry for the private search.
+
+function onLoad(callback) {
+  gBrowser.addEventListener("DOMContentLoaded", function load() {
+    gBrowser.removeEventListener("DOMContentLoaded", load, false);
+    callback();
+  }, false);
+}
+
+function doPrivateTest(searchBar) {
+  gPrivateBrowsingUI.toggleMode();
+  Services.prefs.clearUserPref("browser.privatebrowsing.keep_current_session");
+
+  searchBar.value = "p";
+  searchBar.focus();
+  let popup = searchBar.textbox.popup;
+  //searchBar.textbox.closePopup();
+
+  info("adding listener");
+  popup.addEventListener("popupshowing", function showing() {
+    let entries = getMenuEntries(searchBar);
+    for (var i = 0; i < entries.length; i++)
+      isnot(entries[0], "private test", "shouldn't see private autocomplete entries");
+    popup.removeEventListener("popupshowing", showing, false);
+
+    searchBar.textbox.toggleHistoryPopup();
+    executeSoon(function() {
+    searchBar.value = "";
+    gBrowser.addTab();
+    gBrowser.removeCurrentTab();
+    content.location.href = "about:blank";
+    var engine = Services.search.getEngineByName("Bug 426329");
+    Services.search.removeEngine(engine);
+    finish();
+                  
+                });
+  }, false);
+
+  info("triggering popup");
+  searchBar.textbox.showHistoryPopup();  
+}
+
+function doTest() {
+  let searchBar = BrowserSearch.searchBar;
+  ok(searchBar, "got search bar");
+  
+  onLoad(function() {
+    Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
+    gPrivateBrowsingUI.toggleMode();
+
+    onLoad(function() {
+      doPrivateTest(searchBar);
+    });
+ 
+    searchBar.value = "private test";
+    searchBar.focus();
+    EventUtils.synthesizeKey("VK_RETURN", {});
+  });
+    
+  searchBar.value = "public test";
+  searchBar.focus();
+  EventUtils.synthesizeKey("VK_RETURN", {});
+}
+
+function test() {
+  waitForExplicitFinish();
+  //gBrowser.addTab();
+  //let testBrowser = gBrowser.selectedBrowser;
+ 
+  function observer(aSub, aTopic, aData) {
+    switch (aData) {
+      case "engine-current":
+        ok(ss.currentEngine.name == "Bug 426329", "currentEngine set");
+        doTest();
+        break;
+      default:
+      break;
+    }
+  }
+
+  var ss = Services.search;
+  Services.obs.addObserver(observer, "browser-search-engine-modified", false);
+  ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/426329.xml",
+               Ci.nsISearchEngine.DATA_XML, "data:image/x-icon,%00",
+               false);
+}
+
+function getMenuEntries(searchBar) {
+  var entries = [];
+  var autocompleteMenu = searchBar.textbox.popup;
+  // Could perhaps pull values directly from the controller, but it seems
+  // more reliable to test the values that are actually in the tree?
+  var column = autocompleteMenu.tree.columns[0];
+  var numRows = autocompleteMenu.tree.view.rowCount;
+  for (var i = 0; i < numRows; i++) {
+    entries.push(autocompleteMenu.tree.view.getValueAt(i, column));
+  }
+  return entries;
+}
--- a/toolkit/components/satchel/formSubmitListener.js
+++ b/toolkit/components/satchel/formSubmitListener.js
@@ -4,16 +4,17 @@
 
 (function(){
 
 var Cc = Components.classes;
 var Ci = Components.interfaces;
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 var satchelFormListener = {
     QueryInterface : XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver,
                                             Ci.nsIDOMEventListener,
                                             Ci.nsObserver,
                                             Ci.nsISupportsWeakReference]),
 
     debug          : true,
@@ -98,16 +99,18 @@ var satchelFormListener = {
             // Even though the global context is for a specific browser, we
             // can receive observer events from other tabs! Ensure this event
             // is about our content.
             if (domWin.top != content)
                 return;
             if (!this.enabled)
                 return;
 
+            if (PrivateBrowsingUtils.isWindowPrivate(domWin))
+                return;
 
             this.log("Form submit observer notified.");
 
             if (!this.saveHttpsForms) {
                 if (actionURI.schemeIs("https"))
                     return;
                 if (form.ownerDocument.documentURIObject.schemeIs("https"))
                     return;
--- a/toolkit/components/satchel/nsFormHistory.js
+++ b/toolkit/components/satchel/nsFormHistory.js
@@ -68,31 +68,16 @@ FormHistory.prototype = {
     _uuidService: null,
     get uuidService() {
         if (!this._uuidService)
             this._uuidService = Cc["@mozilla.org/uuid-generator;1"].
                                 getService(Ci.nsIUUIDGenerator);
         return this._uuidService;
     },
 
-    // Private Browsing Service
-    // If the service is not available, null will be returned.
-    _privBrowsingSvc : undefined,
-    get privBrowsingSvc() {
-        if (this._privBrowsingSvc == undefined) {
-            if ("@mozilla.org/privatebrowsing;1" in Cc)
-                this._privBrowsingSvc = Cc["@mozilla.org/privatebrowsing;1"].
-                                        getService(Ci.nsIPrivateBrowsingService);
-            else
-                this._privBrowsingSvc = null;
-        }
-        return this._privBrowsingSvc;
-    },
-
-
     log : function log(message) {
         if (!this.debug)
             return;
         dump("FormHistory: " + message + "\n");
         Services.console.logStringMessage("FormHistory: " + message);
     },
 
 
@@ -138,18 +123,17 @@ FormHistory.prototype = {
 
 
     get hasEntries() {
         return (this.countAllEntries() > 0);
     },
 
 
     addEntry : function addEntry(name, value) {
-        if (!this.enabled ||
-            this.privBrowsingSvc && this.privBrowsingSvc.privateBrowsingEnabled)
+        if (!this.enabled)
             return;
 
         this.log("addEntry for " + name + "=" + value);
 
         let now = Date.now() * 1000; // microseconds
 
         let [id, guid] = this.getExistingEntryID(name, value);
         let stmt;