Bug 248970 - Private Browsing mode (global toggle for saving/caching everything) [cookie part]; r=dwitte,mconnor sr=bzbarsky

This commit is contained in:
Ehsan Akhgari 2008-10-20 00:42:53 +03:30
Родитель f434cfd85b
Коммит 1b7a5323d9
5 изменённых файлов: 289 добавлений и 26 удалений

Просмотреть файл

@ -59,20 +59,7 @@ var gCookiesWindow = {
this._bundle = document.getElementById("bundlePreferences");
this._tree = document.getElementById("cookiesList");
this._loadCookies();
this._tree.treeBoxObject.view = this._view;
this.sort("rawHost");
if (this._view.rowCount > 0)
this._tree.view.selection.select(0);
if ("arguments" in window && window.arguments[0] &&
window.arguments[0].filterString)
{
document.getElementById("filter").value = window.arguments[0].filterString;
this.filter();
}
this._saveState();
this._populateList(true);
document.getElementById("filter").focus();
},
@ -85,6 +72,31 @@ var gCookiesWindow = {
os.removeObserver(this, "perm-changed");
},
_populateList: function (aInitialLoad)
{
this._loadCookies();
this._tree.treeBoxObject.view = this._view;
if (aInitialLoad)
this.sort("rawHost");
if (this._view.rowCount > 0)
this._tree.view.selection.select(0);
if (aInitialLoad) {
if ("arguments" in window && window.arguments[0] &&
window.arguments[0].filterString)
{
document.getElementById("filter").value = window.arguments[0].filterString;
this.filter();
}
}
else {
if (document.getElementById("filter").value != "")
this.filter();
}
this._saveState();
},
_cookieEquals: function (aCookieA, aCookieB, aStrippedHost)
{
return aCookieA.rawHost == aStrippedHost &&
@ -113,6 +125,13 @@ var gCookiesWindow = {
this._tree.treeBoxObject.rowCountChanged(0, -oldRowCount);
this._view.selection.clearSelection();
}
else if (aData == "reload") {
// first, clear any existing entries
this.observe(aCookie, aTopic, "cleared");
// then, reload the list
this._populateList(false);
}
// We don't yet handle aData == "deleted" - it's a less common case
// and is rather complicated as selection tracking is difficult

Просмотреть файл

@ -66,6 +66,8 @@ interface nsIChannel;
* a cookie was changed. the subject is the new cookie.
* "cleared"
* the entire cookie list was cleared. the subject is null.
* "reload"
* the entire cookie list should be reloaded. the subject is null.
*
* topic : "cookie-rejected"
* broadcast whenever a cookie was rejected from being set as a

Просмотреть файл

@ -22,6 +22,8 @@
* Contributor(s):
* Daniel Witte (dwitte@stanford.edu)
* Michiel van Leeuwen (mvl@exedo.nl)
* 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
@ -67,6 +69,8 @@
#include "mozIStorageStatement.h"
#include "mozIStorageConnection.h"
#include "mozStorageHelper.h"
#include "nsIPrivateBrowsingService.h"
#include "nsNetCID.h"
/******************************************************************************
* nsCookieService impl:
@ -409,13 +413,14 @@ nsCookieService::nsCookieService()
, mCookiesPermissions(BEHAVIOR_ACCEPT)
, mMaxNumberOfCookies(kMaxNumberOfCookies)
, mMaxCookiesPerHost(kMaxCookiesPerHost)
, mHostTable(&mDefaultHostTable)
{
}
nsresult
nsCookieService::Init()
{
if (!mHostTable.Init()) {
if (!mHostTable->Init()) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -442,6 +447,17 @@ nsCookieService::Init()
if (mObserverService) {
mObserverService->AddObserver(this, "profile-before-change", PR_TRUE);
mObserverService->AddObserver(this, "profile-do-change", PR_TRUE);
mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, PR_TRUE);
nsCOMPtr<nsIPrivateBrowsingService> pbs =
do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
if (pbs) {
PRBool inPrivateBrowsing = PR_FALSE;
pbs->GetPrivateBrowsingEnabled(&inPrivateBrowsing);
if (inPrivateBrowsing) {
Observe(nsnull, NS_PRIVATE_BROWSING_SWITCH_TOPIC, NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).get());
}
}
}
mPermissionService = do_GetService(NS_COOKIEPERMISSION_CONTRACTID);
@ -639,6 +655,31 @@ nsCookieService::Observe(nsISupports *aSubject,
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
if (prefBranch)
PrefChanged(prefBranch);
} else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) {
if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).Equals(aData)) {
// backup the existing in-memory DB
if (mPrivateHostTable.IsInitialized() || mPrivateHostTable.Init()) {
mHostTable = &mPrivateHostTable;
mCookieCount = mHostTable->Count();
NotifyChanged(nsnull, NS_LITERAL_STRING("cleared").get());
}
// close the connection to the on-disk DB
mStmtInsert = nsnull;
mStmtDelete = nsnull;
mStmtUpdate = nsnull;
mDBConn = nsnull;
// continue to use the in-memory DB
} else if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) {
// open the connection to the on-disk DB
InitDB();
// restore the in-memory DB as it was prior to private browsing
if (mPrivateHostTable.IsInitialized())
mPrivateHostTable.Clear();
mHostTable = &mDefaultHostTable;
mCookieCount = mHostTable->Count();
NotifyChanged(nsnull, NS_LITERAL_STRING("reload").get());
// continue to use both on-disk and in-memory DB
}
}
return NS_OK;
@ -831,7 +872,7 @@ nsCookieService::GetEnumerator(nsISimpleEnumerator **aEnumerator)
nsCOMArray<nsICookie> cookieList(mCookieCount);
nsGetEnumeratorData data(&cookieList, PR_Now() / PR_USEC_PER_SEC);
mHostTable.EnumerateEntries(COMArrayCallback, &data);
mHostTable->EnumerateEntries(COMArrayCallback, &data);
return NS_NewArrayEnumerator(aEnumerator, cookieList);
}
@ -1172,7 +1213,7 @@ nsCookieService::GetCookieInternal(nsIURI *aHostURI,
// begin hash lookup, walking up the subdomain levels.
// we use nextDot to force a lookup of the original host (without leading dot).
do {
nsCookieEntry *entry = mHostTable.GetEntry(currentDot);
nsCookieEntry *entry = mHostTable->GetEntry(currentDot);
cookie = entry ? entry->Head() : nsnull;
for (; cookie; cookie = cookie->Next()) {
// if the cookie is secure and the host scheme isn't, we can't send it
@ -2009,7 +2050,7 @@ nsCookieService::RemoveAllFromMemory()
{
// clearing the hashtable will call each nsCookieEntry's dtor,
// which releases all their respective children.
mHostTable.Clear();
mHostTable->Clear();
mCookieCount = 0;
}
@ -2035,7 +2076,7 @@ nsCookieService::RemoveExpiredCookies(PRInt64 aCurrentTime)
#ifdef PR_LOGGING
PRUint32 initialCookieCount = mCookieCount;
#endif
mHostTable.EnumerateEntries(removeExpiredCallback, &aCurrentTime);
mHostTable->EnumerateEntries(removeExpiredCallback, &aCurrentTime);
COOKIE_LOGSTRING(PR_LOG_DEBUG, ("RemoveExpiredCookies(): %ld purged; %ld remain", initialCookieCount - mCookieCount, mCookieCount));
}
@ -2075,7 +2116,7 @@ nsCookieService::CountCookiesFromHostInternal(const nsACString &aHost,
const char *currentDot = hostWithDot.get();
const char *nextDot = currentDot + 1;
do {
nsCookieEntry *entry = mHostTable.GetEntry(currentDot);
nsCookieEntry *entry = mHostTable->GetEntry(currentDot);
for (nsListIter iter(entry); iter.current; ++iter) {
// only count non-expired cookies
if (iter.current->Expiry() > aData.currentTime) {
@ -2119,7 +2160,7 @@ nsCookieService::FindCookie(const nsAFlatCString &aHost,
nsListIter &aIter,
PRInt64 aCurrentTime)
{
nsCookieEntry *entry = mHostTable.GetEntry(aHost.get());
nsCookieEntry *entry = mHostTable->GetEntry(aHost.get());
for (aIter = nsListIter(entry); aIter.current; ++aIter) {
if (aIter.current->Expiry() > aCurrentTime &&
aPath.Equals(aIter.current->Path()) &&
@ -2156,7 +2197,7 @@ nsCookieService::RemoveCookieFromList(nsListIter &aIter)
// we're removing the last element in the list - so just remove the entry
// from the hash. note that the entryclass' dtor will take care of
// releasing this last element for us!
mHostTable.RawRemoveEntry(aIter.entry);
mHostTable->RawRemoveEntry(aIter.entry);
aIter.current = nsnull;
} else {
@ -2211,7 +2252,7 @@ bindCookieParameters(mozIStorageStatement* aStmt, const nsCookie* aCookie)
PRBool
nsCookieService::AddCookieToList(nsCookie *aCookie, PRBool aWriteToDB)
{
nsCookieEntry *entry = mHostTable.PutEntry(aCookie->Host().get());
nsCookieEntry *entry = mHostTable->PutEntry(aCookie->Host().get());
if (!entry) {
NS_ERROR("can't insert element into a null entry!");
@ -2289,6 +2330,5 @@ findOldestCallback(nsCookieEntry *aEntry,
void
nsCookieService::FindOldestCookie(nsEnumerationData &aData)
{
mHostTable.EnumerateEntries(findOldestCallback, &aData);
mHostTable->EnumerateEntries(findOldestCallback, &aData);
}

Просмотреть файл

@ -22,6 +22,8 @@
* Contributor(s):
* Daniel Witte (dwitte@stanford.edu)
* Michiel van Leeuwen (mvl@exedo.nl)
* 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
@ -198,7 +200,9 @@ class nsCookieService : public nsICookieService
nsCOMPtr<nsIEffectiveTLDService> mTLDService;
// impl members
nsTHashtable<nsCookieEntry> mHostTable;
nsTHashtable<nsCookieEntry> *mHostTable;
nsTHashtable<nsCookieEntry> mDefaultHostTable;
nsTHashtable<nsCookieEntry> mPrivateHostTable;
PRUint32 mCookieCount;
// cached prefs

Просмотреть файл

@ -0,0 +1,198 @@
/* ***** 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 _CMSvc = null;
function get_CookieManager() {
if (_CMSvc)
return _CMSvc;
return _CMSvc = Components.classes["@mozilla.org/cookiemanager;1"].
getService(Components.interfaces.nsICookieManager2);
}
function is_cookie_available1(domain, path, name, value,
secure, httponly, session, expires) {
var cm = get_CookieManager();
var enumerator = cm.enumerator;
while (enumerator.hasMoreElements()) {
var cookie = enumerator.getNext().QueryInterface(Components.interfaces.nsICookie);
if (cookie.host == domain &&
cookie.path == path &&
cookie.name == name &&
cookie.value == value &&
cookie.isSecure == secure &&
cookie.expires == expires)
return true;
}
return false;
}
function is_cookie_available2(domain, path, name, value,
secure, httponly, session, expires) {
var cookie = {
name: name,
value: value,
isDomain: true,
host: domain,
path: path,
isSecure: secure,
expires: expires,
status: 0,
policy: 0,
isSession: session,
expiry: expires,
isHttpOnly: httponly,
QueryInterface: function(iid) {
var validIIDs = [Components.interfaces.nsISupports,
Components.interfaces.nsICookie,
Components.interfaces.nsICookie2];
for (var i = 0; i < validIIDs.length; ++i) {
if (iid == validIIDs[i])
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
};
var cm = get_CookieManager();
return cm.cookieExists(cookie);
}
var cc_observer = null;
function setup_cookie_changed_observer() {
cc_observer = {
gotCleared: false,
gotReloaded: false,
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) {
if (topic == "cookie-changed") {
if (!subject) {
if (data == "cleared")
this.gotCleared = true;
else if (data == "reload")
this.gotReloaded = true;
}
}
}
};
var os = Components.classes["@mozilla.org/observer-service;1"].
getService(Components.interfaces.nsIObserverService);
os.addObserver(cc_observer, "cookie-changed", false);
}
function run_test() {
var pb = get_PBSvc();
if (pb) { // Private Browsing might not be available
var cm = get_CookieManager();
do_check_neq(cm, null);
setup_cookie_changed_observer();
do_check_neq(cc_observer, null);
try {
// create Cookie-A
const time = (new Date("Jan 1, 2030")).getTime() / 1000;
cm.add("pbtest.example.com", "/", "C1", "V1", false, true, false, time);
// make sure Cookie-A is retrievable
do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
// enter private browsing mode
pb.privateBrowsingEnabled = true;
// make sure the "cleared" notification was fired
do_check_true(cc_observer.gotCleared);
// make sure Cookie-A is not retrievable
do_check_false(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_false(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
// create Cookie-B
const time2 = (new Date("Jan 2, 2030")).getTime() / 1000;
cm.add("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2);
// make sure Cookie-B is retrievable
do_check_true(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
do_check_true(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
// exit private browsing mode
pb.privateBrowsingEnabled = false;
// make sure the "reload" notification was fired
do_check_true(cc_observer.gotReloaded);
// make sure Cookie-B is not retrievable
do_check_false(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
do_check_false(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
// make sure Cookie-A is retrievable
do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
} catch (e) {
do_throw("Unexpected exception while testing cookies: " + e);
}
}
do_test_finished();
}