Bug 723001 - Move privacy state checking from nsFormHistory to its callers. r=dolske
authorJosh Matthews <josh@joshmatthews.net>
Wed, 24 Oct 2012 00:27:58 -0400
changeset 111350 8fa504f1802e0be6ed9d7dff6814720e77a9b893
parent 111349 a3a24807434c3e03c216e3465d0a216940bbd5bf
child 111351 241e7e9d2c3042578ac6bc09db1378af07c117cc
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersdolske
bugs723001, 800669
milestone19.0a1
Bug 723001 - Move privacy state checking from nsFormHistory to its callers. r=dolske * * * Bug 800669 - "Remove recent addition of gPrivateBrowsingUI in PB search bar test" [r=josh]
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
toolkit/components/satchel/test/unit/test_bug_248970.js
toolkit/components/satchel/test/unit/xpcshell.ini
--- 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 && !PrivateBrowsingUtils.isWindowPrivate(window)) {
             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;
deleted file mode 100644
--- a/toolkit/components/satchel/test/unit/test_bug_248970.js
+++ /dev/null
@@ -1,62 +0,0 @@
-/* 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 _PBSvc = null;
-function get_PBSvc() {
-  if (_PBSvc)
-    return _PBSvc;
-
-  try {
-    _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
-             getService(Components.interfaces.nsIPrivateBrowsingService);
-    return _PBSvc;
-  } catch (e) {}
-  return null;
-}
-
-var _FHSvc = null;
-function get_FormHistory() {
-  if (_FHSvc)
-    return _FHSvc;
-
-  return _FHSvc = Components.classes["@mozilla.org/satchel/form-history;1"].
-                  getService(Components.interfaces.nsIFormHistory2);
-}
-
-function run_test() {
-  var pb = get_PBSvc();
-  if (pb) { // Private Browsing might not be available
-    var prefBranch = Cc["@mozilla.org/preferences-service;1"].
-                     getService(Ci.nsIPrefBranch);
-    prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
-
-    var fh = get_FormHistory();
-    do_check_neq(fh, null);
-
-    // remove possible Pair-A and Pair-B left-overs from previous tests
-    fh.removeEntriesForName("pair-A");
-    fh.removeEntriesForName("pair-B");
-
-    // save Pair-A
-    fh.addEntry("pair-A", "value-A");
-    // ensure that Pair-A exists
-    do_check_true(fh.entryExists("pair-A", "value-A"));
-    // enter private browsing mode
-    pb.privateBrowsingEnabled = true;
-    // make sure that Pair-A exists
-    do_check_true(fh.entryExists("pair-A", "value-A"));
-    // attempt to save Pair-B
-    fh.addEntry("pair-B", "value-B");
-    // make sure that Pair-B does not exist
-    do_check_false(fh.entryExists("pair-B", "value-B"));
-    // exit private browsing mode
-    pb.privateBrowsingEnabled = false;
-    // ensure that Pair-A exists
-    do_check_true(fh.entryExists("pair-A", "value-A"));
-    // make sure that Pair-B does not exist
-    do_check_false(fh.entryExists("pair-B", "value-B"));
-
-    prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");
-  }
-}
--- a/toolkit/components/satchel/test/unit/xpcshell.ini
+++ b/toolkit/components/satchel/test/unit/xpcshell.ini
@@ -1,16 +1,13 @@
 [DEFAULT]
 head = head_satchel.js
 tail = 
 
 [test_autocomplete.js]
-[test_bug_248970.js]
-# Bug 676989: test hangs on Android
-skip-if = os == "android"
 [test_db_corrupt.js]
 [test_db_update_v1.js]
 [test_db_update_v1b.js]
 [test_db_update_v2.js]
 [test_db_update_v2b.js]
 [test_db_update_v3.js]
 [test_db_update_v3b.js]
 [test_db_update_v4.js]