Bug 248970 - Private Browsing mode (global toggle for saving/caching everything) [satchel part]; r=mconnor
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Mon, 20 Oct 2008 00:47:36 +0330
changeset 20639 ee936d0fd358
parent 20638 81e6937843a7
child 20640 ec964583650f
push id3021
push userehsan.akhgari@gmail.com
push date2008-10-19 21:18 +0000
Treeherderresults
reviewersmconnor
bugs248970
milestone1.9.1b2pre
Bug 248970 - Private Browsing mode (global toggle for saving/caching everything) [satchel part]; r=mconnor
toolkit/components/satchel/src/nsStorageFormHistory.cpp
toolkit/components/satchel/test/unit/test_bug_248970.js
--- a/toolkit/components/satchel/src/nsStorageFormHistory.cpp
+++ b/toolkit/components/satchel/src/nsStorageFormHistory.cpp
@@ -17,16 +17,18 @@
  * The Initial Developer of the Original Code is
  * Netscape Communications Corporation.
  * Portions created by the Initial Developer are Copyright (C) 1998
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Joe Hewitt <hewitt@netscape.com> (Original Author)
  *   Brett Wilson <brettw@gmail.com>
+ *   Michael Ventnor <m.ventnor@gmail.com>
+ *   Ehsan Akhgari <ehsan.akhgari@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either 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
@@ -56,16 +58,18 @@
 #include "nsIPrefBranch.h"
 #include "nsIPrefBranch2.h"
 #include "nsVoidArray.h"
 #include "nsCOMArray.h"
 #include "mozStorageHelper.h"
 #include "mozStorageCID.h"
 #include "nsIAutoCompleteSimpleResult.h"
 #include "nsTArray.h"
+#include "nsIPrivateBrowsingService.h"
+#include "nsNetCID.h"
 
 // The size of the database cache. This is the number of database PAGES that
 // can be cached in memory. Normally, pages are 1K unless the size has been
 // explicitly changed.
 //
 // 4MB should be much larger than normal form histories. Normal form histories
 // will be several hundered KB at most. If the form history is smaller, the
 // extra memory will never be allocated, so there is no penalty for larger
@@ -219,16 +223,25 @@ nsFormHistory::GetHasEntries(PRBool *aHa
   PRBool hasMore;
   *aHasEntries = NS_SUCCEEDED(mDBSelectEntries->ExecuteStep(&hasMore)) && hasMore;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsFormHistory::AddEntry(const nsAString &aName, const nsAString &aValue)
 {
+  // If the user is in private browsing mode, don't add any entry.
+  PRBool inPrivateBrowsing = PR_FALSE;
+  nsCOMPtr<nsIPrivateBrowsingService> pbs =
+    do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
+  if (pbs)
+    pbs->GetPrivateBrowsingEnabled(&inPrivateBrowsing);
+  if (inPrivateBrowsing)
+    return NS_OK;
+
   if (!FormHistoryEnabled())
     return NS_OK;
 
   PRBool exists = PR_TRUE;
   EntryExists(aName, aValue, &exists);
   if (!exists) {
     mozStorageStatementScoper scope(mDBInsertNameValue);
     nsresult rv = mDBInsertNameValue->BindStringParameter(0, aName);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/satchel/test/unit/test_bug_248970.js
@@ -0,0 +1,107 @@
+/* ***** 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) 2008
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Ehsan Akhgari <ehsan.akhgari@gmail.com> (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 ***** */
+
+var _PBSvc = null;
+function get_PBSvc() {
+  if (_PBSvc)
+    return _PBSvc;
+
+  try {
+    _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
+             getService(Components.interfaces.nsIPrivateBrowsingService);
+    if (_PBSvc) {
+      var observer = {
+        QueryInterface: function (iid) {
+          const interfaces = [Components.interfaces.nsIObserver,
+                              Components.interfaces.nsISupports];
+          if (!interfaces.some(function(v) iid.equals(v)))
+            throw Components.results.NS_ERROR_NO_INTERFACE;
+          return this;
+        },
+        observe: function (subject, topic, data) {
+          subject.QueryInterface(Components.interfaces.nsISupportsPRUint32);
+          subject.data = 0;
+        }
+      };
+      var os = Components.classes["@mozilla.org/observer-service;1"].
+               getService(Components.interfaces.nsIObserverService);
+      os.addObserver(observer, "private-browsing-enter", false);
+    }
+    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 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"));
+  }
+}