Bug 558321 - Tab Matches are not honoured in Private Browsing mode. r=mak77, a=blocking-betaN
authorBlair McBride <bmcbride@mozilla.com>
Wed, 12 Jan 2011 13:40:36 +1300
changeset 60842 2f9ad4aa07958fdc0761e224f720925929a66686
parent 60841 4c1dd250fef3a46bdc9d10b24a05a43d2b97e0e5
child 60843 c0547fd3a3ccf61d0e4699118e56fe6006fb58be
push idunknown
push userunknown
push dateunknown
reviewersmak77, blocking-betaN
bugs558321
milestone2.0b10pre
Bug 558321 - Tab Matches are not honoured in Private Browsing mode. r=mak77, a=blocking-betaN
browser/base/content/test/browser_tabMatchesInAwesomebar.js
toolkit/components/places/src/nsPlacesAutoComplete.js
--- a/browser/base/content/test/browser_tabMatchesInAwesomebar.js
+++ b/browser/base/content/test/browser_tabMatchesInAwesomebar.js
@@ -132,58 +132,131 @@ var gTestSteps = [
       gBrowser.swapBrowsersAndCloseOther(tabToKeep, tab);
       ensure_opentabs_match_db(function () {
         gBrowser.removeTab(tabToKeep);
         ensure_opentabs_match_db(nextStep);
       });
     }, true);
     tab.linkedBrowser.loadURI('about:robots');
   },
+  function() {
+    info("Running step 9 - enter private browsing mode, without keeping session");
+    let ps = Services.prefs;
+    ps.setBoolPref("browser.privatebrowsing.keep_current_session", false);
+    ps.setBoolPref("browser.tabs.warnOnClose", false);
+
+    Services.obs.addObserver(function(aSubject, aTopic, aData) {
+      Services.obs.removeObserver(arguments.callee, "private-browsing-transition-complete");
+
+      for (let i = 0; i < gBrowser.tabs.length; i++)
+        waitForRestoredTab(gBrowser.tabs[i]);
+    }, "private-browsing-transition-complete", false);
+
+    gPrivateBrowsing.privateBrowsingEnabled = true;
+  },
+  function() {
+    info("Running step 10 - open tabs in private browsing mode");
+    for (let i = 0; i < 3; i++) {
+      let tab = gBrowser.addTab();
+      loadTab(tab, TEST_URL_BASES[0] + (++gTabCounter));
+    }
+  },
+  function() {
+    info("Running step 11 - close tabs in private browsing mode");
+    gBrowser.removeCurrentTab();
+    ensure_opentabs_match_db(nextStep);
+  },
+  function() {
+    info("Running step 12 - leave private browsing mode");
+
+    Services.obs.addObserver(function(aSubject, aTopic, aData) {
+      Services.obs.removeObserver(arguments.callee, "private-browsing-transition-complete");
+
+      let ps = Services.prefs;
+      try {
+        ps.clearUserPref("browser.privatebrowsing.keep_current_session");
+      } catch (ex) {}
+      try {
+        ps.clearUserPref("browser.tabs.warnOnClose");
+      } catch (ex) {}
+
+      for (let i = 1; i < gBrowser.tabs.length; i++)
+        waitForRestoredTab(gBrowser.tabs[i]);
+
+    }, "private-browsing-transition-complete", false);
+
+    gPrivateBrowsing.privateBrowsingEnabled = false;
+  },
+  function() {
+    info("Running step 13 - close all tabs");
+    gBrowser.addTab("about:blank", {skipAnimation: true});
+    while (gBrowser.tabs.length > 1) {
+      info("Removing tab: " + gBrowser.tabs[0].linkedBrowser.currentURI.spec);
+      gBrowser.selectTabAtIndex(0);
+      gBrowser.removeCurrentTab();
+    }
+    ensure_opentabs_match_db(nextStep);
+  }
 ];
 
 
 
 function test() {
   waitForExplicitFinish();
   nextStep();
 }
 
 function loadTab(tab, url) {
   // Because adding visits is async, we will not be notified immediately.
-  let visited = false;
+  let visited = gPrivateBrowsing.privateBrowsingEnabled;
   let loaded = false;
 
   function maybeCheckResults() {
     if (visited && loaded && --gTabWaitCount == 0) {
       ensure_opentabs_match_db(nextStep);
     }
   }
 
   tab.linkedBrowser.addEventListener("load", function () {
     tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
     loaded = true;
     maybeCheckResults();
   }, true);
 
-  Services.obs.addObserver(
-    function (aSubject, aTopic, aData) {
-      if (url != aSubject.QueryInterface(Ci.nsIURI).spec)
-        return;
-      Services.obs.removeObserver(arguments.callee, aTopic);
-      visited = true;
-      maybeCheckResults();
-    },
-    "uri-visit-saved",
-    false
-  );
+  if (!visited) {
+    Services.obs.addObserver(
+      function (aSubject, aTopic, aData) {
+        if (url != aSubject.QueryInterface(Ci.nsIURI).spec)
+          return;
+        Services.obs.removeObserver(arguments.callee, aTopic);
+        visited = true;
+        maybeCheckResults();
+      },
+      "uri-visit-saved",
+      false
+    );
+  }
 
   gTabWaitCount++;
+  info("Loading page: " + url);
   tab.linkedBrowser.loadURI(url);
 }
 
+function waitForRestoredTab(tab) {
+  gTabWaitCount++;
+
+  tab.linkedBrowser.addEventListener("load", function () {
+    tab.linkedBrowser.removeEventListener("load", arguments.callee, true);
+    if (--gTabWaitCount == 0) {
+      ensure_opentabs_match_db(nextStep);
+    }
+  }, true);
+}
+
+
 function nextStep() {
   if (gTestSteps.length == 0) {
     while (gBrowser.tabs.length > 1) {
       gBrowser.selectTabAtIndex(1);
       gBrowser.removeCurrentTab();
     }
 
     waitForClearHistory(finish);
--- a/toolkit/components/places/src/nsPlacesAutoComplete.js
+++ b/toolkit/components/places/src/nsPlacesAutoComplete.js
@@ -101,22 +101,16 @@ const kQueryTypeFiltered = 1;
 
 // This separator is used as an RTL-friendly way to split the title and tags.
 // It can also be used by an nsIAutoCompleteResult consumer to re-split the
 // "comment" back into the title and the tag.
 const kTitleTagsSeparator = " \u2013 ";
 
 const kBrowserUrlbarBranch = "browser.urlbar.";
 
-////////////////////////////////////////////////////////////////////////////////
-//// Globals and Lazy Getters
-
-XPCOMUtils.defineLazyServiceGetter(this, "pb",
-                                   "@mozilla.org/privatebrowsing;1",
-                                   "nsIPrivateBrowsingService");
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Helpers
 
 /**
  * Initializes our temporary table on a given database.
  *
  * @param aDatabase
@@ -148,28 +142,16 @@ function initTempTable(aDatabase)
   +   "DELETE FROM moz_openpages_temp "
   +   "WHERE url = NEW.url; "
   + "END "
   );
   stmt.executeAsync();
   stmt.finalize();
 }
 
-/**
- * @return true if private browsing is active, false otherwise.
- */
-function inPrivateBrowsingMode()
-{
-  try {
-    return pb.privateBrowsingEnabled;
-  }
-  catch (ex) {
-    return false;
-  }
-}
 
 ////////////////////////////////////////////////////////////////////////////////
 //// AutoCompleteStatementCallbackWrapper class
 
 /**
  * Wraps a callback and ensures that handleCompletion is not dispatched if the
  * query is no longer tracked.
  *
@@ -532,38 +514,24 @@ nsPlacesAutoComplete.prototype = {
       this._bh.removePage(this._ioService.newURI(aURISpec, null, null));
   },
 
   //////////////////////////////////////////////////////////////////////////////
   //// mozIPlacesAutoComplete
 
   registerOpenPage: function PAC_registerOpenPage(aURI)
   {
-    // Don't add any pages while in Private Browsing mode, so as to avoid
-    // leaking information about other windows that might otherwise stay hidden
-    // and private.
-    if (inPrivateBrowsingMode()) {
-      return;
-    }
-
     let stmt = this._registerOpenPageQuery;
     stmt.params.page_url = aURI.spec;
 
     stmt.executeAsync();
   },
 
   unregisterOpenPage: function PAC_unregisterOpenPage(aURI)
   {
-    // Entering Private Browsing mode will unregister all open pages, therefore
-    // there should not be anything in the moz_openpages_temp table.  As a
-    // result, we can stop now without doing any unnecessary work.
-    if (inPrivateBrowsingMode()) {
-      return;
-    }
-
     let stmt = this._unregisterOpenPageQuery;
     stmt.params.page_url = aURI.spec;
 
     stmt.executeAsync();
   },
 
   //////////////////////////////////////////////////////////////////////////////
   //// mozIStorageStatementCallback