Created a cookie tracker in trackers.js, and a test for it in tests/unit/test_cookie_store.js.

This commit is contained in:
jonathandicarlo@jonathan-dicarlos-macbook-pro.local 2008-05-27 09:44:26 -07:00
parent ecd321f34b
commit c8b1c91b51
3 changed files with 83 additions and 2 deletions

View File

@ -1029,6 +1029,13 @@ CookieEngine.prototype = {
if (!this.__store)
this.__store = new CookieStore();
return this.__store;
},
__tracker: null,
get _tracker() {
if (!this.__tracker)
this.__tracker = new CookieTracker();
return this.__tracker;
}
};
CookieEngine.prototype.__proto__ = new Engine();
@ -1111,4 +1118,4 @@ FormEngine.prototype = {
return this.__tracker;
}
};
FormEngine.prototype.__proto__ = new Engine();
FormEngine.prototype.__proto__ = new Engine();

View File

@ -35,7 +35,7 @@
* ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Tracker', 'BookmarksTracker', 'HistoryTracker',
'FormsTracker'];
'FormsTracker', 'CookieTracker'];
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -197,6 +197,41 @@ HistoryTracker.prototype = {
}
HistoryTracker.prototype.__proto__ = new Tracker();
function CookieTracker() {
this._init();
}
CookieTracker.prototype = {
_logName: "CookieTracker",
_init: function CT__init() {
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
this._score = 0;
/* cookieService can't register observers, but what we CAN do is
register a general observer with the global observerService
to watch for the 'cookie-changed' message. */
let observerService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
observerService.addObserver( this, 'cookie-changed', false );
},
// implement observe method to satisfy nsIObserver interface
observe: function ( aSubject, aTopic, aData ) {
/* This gets called when any cookie is added, changed, or removed.
aData will contain a string "added", "changed", etc. to tell us which,
but for now we can treat them all the same. aSubject is the new
cookie object itself. */
var newCookie = aSubject.QueryInterface( Ci.nsICookie2 );
if ( newCookie ) {
if ( !newCookie.isSession ) {
/* Any modification to a persistent cookie is worth
10 points out of 100. Ignore session cookies. */
this._score += 10;
}
}
}
}
CookieTracker.prototype.__proto__ = new Tracker();
function FormsTracker() {
this._init();
}

View File

@ -83,6 +83,44 @@ FakeCookieManager.prototype = {
}
};
function sub_test_cookie_tracker() {
Components.utils.import("resource://weave/trackers.js");
var ct = new CookieTracker();
// gonna have to use the real cookie manager here...
var cookieManager = Cc["@mozilla.org/cookiemanager;1"].
getService(Ci.nsICookieManager2);
var d = new Date();
d.setDate( d.getDate() + 1 );
cookieManager.add( "www.evilbrainjono.net",
"/blog/",
"comments",
"on",
false,
true,
false,
d.getTime() );
cookieManager.add( "www.evilbrainjono.net",
"/comic/",
"lang",
"jp",
false,
true,
false,
d.getTime() );
cookieManager.add( "www.evilbrainjono.net",
"/blog/",
"comments",
"off",
false,
true,
true, // session
d.getTime() );
// score is 10 per cookie changed, but we should be ignoring the
// session cookie, so we should be at 20 now.
do_check_eq( ct.score, 20 );
};
function run_test() {
/* Set a persistent cookie and a non-persistent cookie
@ -110,4 +148,5 @@ function run_test() {
var jsonGuids = [ guid for ( guid in json ) ];
do_check_eq( jsonGuids.length, 1 );
do_check_eq( jsonGuids[0], "evilbrainjono.net:/:login" );
sub_test_cookie_tracker();
}