home *** CD-ROM | disk | FTP | other *** search
- var perfmon;
-
- function compareNum(a,b)
- {
- return a-b
- }
-
- // Returns the index (1-based) of hashkey in thishash
- function hash_value(thishash, hashkey) {
- for (var i=0; i<thishash.length; i++) {
- if (thishash[i] == hashkey) return (i+1); // Can't be zero-based
- }
- return 0;
- }
-
- if (!perfmon) perfmon = {
-
- DT_NS : "http://home.netscape.com/DT-rdf#",
- NC_NS : "http://home.netscape.com/NC-rdf#",
- WEB_NS : "http://home.netscape.com/WEB-rdf#",
- RDF_NS : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
- XUL_NS : "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
- NC_NS_CMD : null,
-
- // definition of the services frequently used for datattracking
- kRDFContractID : "@mozilla.org/rdf/rdf-service;1",
- kRDFSVCIID : Components.interfaces.nsIRDFService,
- kRDFRSCIID : Components.interfaces.nsIRDFResource,
- kRDFLITIID : Components.interfaces.nsIRDFLiteral,
- kRDFINTIID : Components.interfaces.nsIRDFInt,
- kRDFDSIID : Components.interfaces.nsIRDFDataSource,
- RDF : null,
-
- kRDFCContractID : "@mozilla.org/rdf/container;1",
- kRDFCIID : Components.interfaces.nsIRDFContainer,
- RDFC : null,
-
- kRDFCUContractID : "@mozilla.org/rdf/container-utils;1",
- kRDFCUIID : Components.interfaces.nsIRDFContainerUtils,
- RDFCU : null,
-
- kPREFContractID : "@mozilla.org/preferences-service;1",
- kPREFIID : Components.interfaces.nsIPrefService,
- PREF : null,
-
- kSOUNDContractID : "@mozilla.org/sound;1",
- kSOUNDIID : Components.interfaces.nsISound,
- SOUND : null,
-
- kWINDOWContractID : "@mozilla.org/appshell/window-mediator;1",
- kWINDOWIID : Components.interfaces.nsIWindowMediator,
- WINDOWSVC : null,
-
- kDSContractID : "@mozilla.org/widget/dragservice;1",
- kDSIID : Components.interfaces.nsIDragService,
- DS : null,
-
- kDTContractID : "@mozilla.org/browser/perfmon-service;1",
- kDTSVCIID : Components.interfaces.nsIPerfMonService,
- DTSVC : null,
- DTDS : null,
-
- UUID : null,
-
- thisDate : null,
- epochDays : null,
- timeToSend : null,
- minimumSendWindow : 10800000, //3 hours in milliseconds
- // minimumSendWindow : 3600000, // 1 hour for testing
-
- TIME_DAY : 86400000,
- TIME_HOUR : 3600000,
- TIME_MINUTE : 60000,
-
- setTodaysDate : function() {
- var todaysDate = new Date();
- var epochDate = new Date(2004, 05, 01); //EPOCH DATE, June 1
- var diffDate = new Date();
-
- var datediff = diffDate.setTime(Math.abs(todaysDate.getTime() - epochDate.getTime()));
- this.epochDays = Math.floor(datediff/86400000) //calculate days and convert to string
- this.thisDate = (todaysDate.getFullYear() + "-" + (todaysDate.getMonth() + 1) + "-" + todaysDate.getDate());
- },
-
- setTimeToSend : function() {
-
- var dateNow = new Date();
- var timeNow = dateNow.getTime();
- // Date.UTC(dateNow.getFullYear(), dateNow.getMonth(), dateNow.getHours(), dateNow.getMinutes(), dateNow.getSeconds());
- // debug ("Diff between getTime and UTC of a current Date object: " + dateNow.getTime() + ", " + timeNow + "\n");
-
- var MINTIME = 0;
- var MAXTIME = 0;
- // BLACKOUT time is midnight to 1 am PST (UTC -0700)
- // Next blackout window is...
- // JAVASCRIPT time is 0-based
- var UTCBLACKOUTMIN = 6;
-
- var blackoutDate = new Date();
- blackoutDate.setUTCHours(UTCBLACKOUTMIN);
- var BLACKOUTMIN = blackoutDate.getTime();
- var BLACKOUTMAX = BLACKOUTMIN + this.TIME_HOUR;
-
-
-
- MINTIME = Math.min(timeNow, BLACKOUTMAX);
-
- if (dateNow.getUTCHours() > UTCBLACKOUTMIN) // Already past the start of todays blackout
- {
- // Create tomorrow's blackout window
- BLACKOUTMIN += this.TIME_DAY;
- BLACKOUTMAX += this.TIME_DAY;
- }
-
- //this.debug("BLACKOUTMAX: " + BLACKOUTMAX + ", BLACKOUTMIN: " + BLACKOUTMIN);
-
-
- // MAXTIME is minimum of: start of next blackout period, or 10 pm localtime
- var tonightCutoff = new Date();
- tonightCutoff.setHours(21); // 10 pm
- var tonightUTC = tonightCutoff.getTime();
- if (timeNow > tonightUTC) // if it's already past 10 pm local, send whenever
- {
- MAXTIME = MINTIME + this.minimumSendWindow;
- } else {
- MAXTIME = tonightUTC;
- }
-
- // Make sure the selected time is not within the BLACKOUT window
-
- do {
- // this.debug("MINTIME: " + MINTIME + ", MAXTIME: " + MAXTIME);
- this.timeToSend = this.RanSpan(MINTIME, MAXTIME);
- // this.debug(this.timeToSend);
- } while (this.timeToSend < BLACKOUTMAX && this.timeToSend > BLACKOUTMIN)
-
- setTimeout("perfmon.sendTimer()", 60000);
-
- },
-
-
- initServices : function() {
-
- this.debug('initServices()');
- if (!this.DTDS) {
- this.NS_NS_CMD = this.NC_NS + "command?cmd=";
- this.RDF = Components.classes[this.kRDFContractID].getService(this.kRDFSVCIID);
- this.RDFC = Components.classes[this.kRDFCContractID].createInstance(this.kRDFCIID);
- this.RDFCU = Components.classes[this.kRDFCUContractID].getService(this.kRDFCUIID);
- this.PREF = Components.classes[this.kPREFContractID].getService(this.kPREFIID)
- .getBranch(null);
- this.SOUND = Components.classes[this.kSOUNDContractID].createInstance(this.kSOUNDIID);
- this.WINDOWSVC = Components.classes[this.kWINDOWContractID].getService(this.kWINDOWIID);
- this.DS = Components.classes[this.kDSContractID].getService(this.kDSIID);
- this.DTSVC = Components.classes[this.kDTContractID].getService(this.kDTSVCIID);
- this.DTDS = this.DTSVC.QueryInterface(this.kRDFDSIID);
-
- this.setTodaysDate();
- this.setTimeToSend();
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
-
- try {
- this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, 'browser_started');
- this.DTSVC.writePerfMon();
-
- this.UUID = "";
- if ((prefs.getPrefType("browser.info.guid") == prefs.PREF_STRING) &&
- (this.UUID = prefs.getCharPref("browser.info.guid"))) {
- // nothing
- } else {
- this.UUID = this.DTSVC.uuidCreate();
- prefs.setCharPref("browser.info.guid",this.UUID);
- }
- } catch (ex) {
- dump('perfmon.js: call to DTSVC.perfMonIncrementCommand() failed '
- + 'with exception:\n' + ex + '\n');
- this.DTDS = null;
- }
-
- // JMC - New datatracking functionality - June 15, 2005
- // Send real-time packet on startup
-
- // Should respect optout
-
- if ((prefs.getPrefType("browser.info.track_usage") == prefs.PREF_BOOL) &&
- !(prefs.getBoolPref("browser.info.track_usage"))) {
- return;
- }
-
- var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
- var startupString = "http://ns8-stats.netscape.com/?s" + versionNum + "=" + this.UUID;
- this.debug("Sending startup string: \n" + startupString + "\n\n");
- var result = this.DTSVC.sendHttpRequest(startupString);
- if (!result) {
- this.debug ("Failed sending startup string \n");
- }
- }
- },
-
- checkTimeToSend : function() {
-
- // this.debug("CheckTimeToSend\n");
- // If there's more than three days' data, force send
- if (this.countDaysPending() > 3 ) {
- return true;
- }
-
- // Otherwise, send if we're within 10 minutes of our send time
- var currentTime = new Date();
-
- currentTime = currentTime.getTime();
- // this.debug ("Current time is: " + currentTime + "\n");
- if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) < (this.TIME_MINUTE * 10)))
- {
- return true;
- }
-
- // If we're more than 10 minutes out, Select a new send time
- if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) > (this.TIME_MINUTE * 10)))
- {
- this.setTimeToSend();
- }
-
- return false;
- },
-
- trackData : function(commandObject) {
- this.debug("Tracking data for" + commandObject );
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
-
- if ((prefs.getPrefType("browser.info.track_usage") == prefs.PREF_BOOL) &&
- !(prefs.getBoolPref("browser.info.track_usage"))) {
- return;
- }
-
- var commandstring;
- if (commandObject.hasAttribute('perfmon'))
- {
- commandstring = commandObject.getAttribute('perfmon');
- } else if (commandObject.hasAttribute('command'))
- {
- commandstring = commandObject.getAttribute('command');
- } else if (commandObject.hasAttribute('id'))
- {
- commandstring = commandObject.getAttribute('id');
- }
- if (commandstring)
- {
- var rExp = /\s|:/gi
- commandstring = commandstring.replace(rExp, "_");
- // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
- if (!this.epochDays)
- this.setTodaysDate();
-
- this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, commandstring);
- this.DTSVC.writePerfMon();
- } else {
- this.debug ("Couldn't identify the command for perfmon.");
- }
-
- // this.debug ("Time to send is: " + this.timeToSend + "\n\n");
- },
-
- sendTimer : function() {
- if (this.checkTimeToSend())
- {
- if (this.sendPerfMon())
- {
- this.timeToSend += this.TIME_DAY; // Set sending time to tomorrow, same time
- this.dumpPrefs(); // FIXME
- } else {
- this.timeToSend += (this.TIME_MINUTE * 5); // Wait five minutes before trying again
- }
- }
-
- setTimeout("perfmon.sendTimer()", 60000);
- },
-
- countDaysPending : function() {
-
- var daysCount = 0;
-
- resources = this.DTDS.GetAllResources();
- while (resources.hasMoreElements()) {
- res = resources.getNext();
- res.QueryInterface(this.kRDFRSCIID);
- // this.debug('resource: '+res.Value);
- labels = this.DTDS.ArcLabelsOut(res);
- while (labels.hasMoreElements()) {
- label = labels.getNext();
- if (label instanceof this.kRDFRSCIID) {
- target = this.DTDS.GetTarget(res, label, true);
- if (target instanceof this.kRDFINTIID) {
- if (label.Value == this.DT_NS + 'epochDays') {
- daysCount++;
- }
- }
- }
- }
- }
- return daysCount;
- },
-
- sendPerfMon : function() {
- // Today is guaranteed to have a record
-
- var entries = new Array();
- var records = new Object();
- var dates = new Object();
-
- resources = this.DTDS.GetAllResources();
- while (resources.hasMoreElements()) {
- res = resources.getNext();
- res.QueryInterface(this.kRDFRSCIID);
- // this.debug('resource: '+res.Value);
- labels = this.DTDS.ArcLabelsOut(res);
- var recordstring = '';
- var epochNumber = 0;
-
- // Get Title String
- var titlestring;
- var itemRes = this.RDF.GetResource(this.DT_NS + 'title');
- var target = this.DTDS.GetTarget(res, itemRes, true);
- if (target) target = target.QueryInterface(kRDFLITIID);
- if (!target) {
- titlestring = '';
- } else {
- titlestring = target.Value;
- }
-
- while (labels.hasMoreElements()) {
- label = labels.getNext();
- if (label instanceof this.kRDFRSCIID) {
- target = this.DTDS.GetTarget(res, label, true);
- if (target instanceof this.kRDFINTIID) {
- dbg += ' => '+target.Value;
- if (label.Value == this.DT_NS + 'epochDays') {
- entries[entries.length] = target.Value;
- epochNumber = target.Value;
- } else {
- var hashvalue = hash_value(dt_hash, label.Value.substring(this.DT_NS.length));
- if (hashvalue)
- {
- recordstring += hashvalue + "|";
- } else {
- recordstring += label.Value.substring(this.DT_NS.length) + "|";
- }
- recordstring += target.Value + "|";
-
- // for each button
- // look up the hashtable
- // if not present, use the full name
-
- }
- }
- }
- }
- if (epochNumber) {
- records[epochNumber] = recordstring.substring(0,recordstring.length -1);
- dates[epochNumber] = titlestring;
- }
- }
-
- entries.sort(compareNum);
-
- if (entries[0] >= this.epochDays) {
- return true;
- }
-
- var sendstring;
- sendstring = entries[0] + "|";
- for (i=0; i < (entries.length); i++) {
- if (i > 0 && ((entries[i] - entries[i-1]) > 1)) { // Some null days here
- sendstring += "0|" + (entries[i] - entries[i-1] -1) + ":";
-
- }
- if (i != (entries.length - 1)) { // Skip todays' record
- sendstring += records[entries[i]] + ":";
- }
- }
-
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
- var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
- var testString = "http://ns8-stats.netscape.com/?stat" + versionNum + "=" + sendstring.substring(0,sendstring.length - 1) + "&guid=" + this.UUID;
- this.debug("Sending string: \n" + testString + "\n\n");
- var result = this.DTSVC.sendHttpRequest(testString);
- if (result) {
- // Delete old nodes
- this.debug("Deleting nodes...\n");
- for (i=0; i < (entries.length - 1); i++) {
- this.DTSVC.removeTrackedDate(dates[entries[i]]);
- }
- return true;
- } else {
- this.debug ("Failed: " + result + "\n");
- }
- return false;
- },
-
- debug : function(msg) {
- dump('**** -- perfmon.js: '+msg+'\n');
- },
-
-
- Random : function(N) {
- return Math.floor(N * (Math.random() % 1));
- },
-
- Randum : function(N) {
- return (N * (Math.random() % 1)) | 0;
- },
-
- RanSpan : function(MinV, MaxV) {
- return MinV + this.Random(MaxV - MinV + 1);
- },
-
- dumpRDF : function() {
- resources = this.DTDS.GetAllResources();
- while (resources.hasMoreElements()) {
- res = resources.getNext();
- res.QueryInterface(this.kRDFRSCIID);
- this.debug('resource: '+res.Value);
- var labels = this.DTDS.ArcLabelsOut(res);
- while (labels.hasMoreElements()) {
- var label = labels.getNext();
- if (label instanceof this.kRDFRSCIID) {
- dbg = ' * '+label.Value;
- var target = this.DTDS.GetTarget(res, label, true);
- if (target instanceof this.kRDFRSCIID) {
- dbg += ' => '+target.Value;
- } else if (target instanceof this.kRDFLITIID) {
- dbg += ' => '+target.Value;
- } else if (target instanceof this.kRDFINTIID) {
- dbg += ' => '+target.Value;
- } else {
- try {
- dbg += ' => '+target.Value;
- } catch (ex) { }
- }
- this.debug(dbg);
- }
- }
- }
- },
-
- echoPrefs : function() {
- return;
-
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
- var childcount = new Object;
- var thesePrefList = prefs.getChildList("", childcount);
- for (i=0; i < thesePrefList.length; i++) {
- this.debug(thesePrefList[i]);
- }
- },
-
- dumpPrefs : function() {
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
- var lastTimeSent = 0;
- if (prefs.getPrefType("browser.last_track_prefs_epoch") == prefs.PREF_INT) {
- lastTimeSent = prefs.getIntPref("browser.last_track_prefs_epoch");
- }
- if (((prefs.getPrefType("browser.info.track_prefs") == prefs.PREF_BOOL) &&
- !(prefs.getBoolPref("browser.info.track_prefs"))) ||
- (this.epochDays - lastTimeSent < 7)) {
- this.debug("sent recently... leaving.");
- return;
- }
-
- var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
- var thesePrefs = prefService.getBranch("");
- var defPrefs = prefService.getDefaultBranch("");
- var childcount = new Object;
- var thesePrefList = thesePrefs.getChildList("", childcount);
-
- var commandstring = this.epochDays + "|";
-
-
- for (i=0; i < thesePrefList.length; i++) {
- // this.debug("In Loop: " + i);
- var prefType = thesePrefs.getPrefType(thesePrefList[i]);
- var userPrefValue = 0;
- var defPrefValue = 0;
- switch (prefType) {
-
- case thesePrefs.PREF_STRING:
- userPrefValue = thesePrefs.getCharPref(thesePrefList[i]);
- try {
- defPrefValue = defPrefs.getCharPref(thesePrefList[i]);
- } catch (ex) {
- defPrefValue = "";
- }
- break;
- case thesePrefs.PREF_INT:
- userPrefValue = thesePrefs.getIntPref(thesePrefList[i]);
- try {
- defPrefValue = defPrefs.getIntPref(thesePrefList[i]);
- } catch (ex) {
- defPrefValue = 0;
- }
- break;
- case thesePrefs.PREF_BOOL:
- userPrefValue = thesePrefs.getBoolPref(thesePrefList[i]);
- try {
- defPrefValue = defPrefs.getBoolPref(thesePrefList[i]);
- } catch (ex) {
- defPrefValue = false;
- }
- break;
- }
- // (ccampbell): no longer using blacklist... just restricting prefs
- // to those in pref_hash instead.
- //var blacklist = hash_value(pref_bl, (thesePrefList[i]));
- if (userPrefValue != defPrefValue) {
- var hashvalue = hash_value(pref_hash, (thesePrefList[i]));
- if (hashvalue) {
- commandstring += hashvalue + "|" + userPrefValue + "|";
- }
- //else {
- // commandstring += thesePrefList[i] + "|" + userPrefValue + "|";
- //}
- // this.debug("Pref: " + thesePrefList[i] + ": " + userPrefValue);
- } else {
- // this.debug("default value");
- }
-
- }
-
- var rExp = /\s|:/gi
- commandstring = commandstring.replace(rExp, "_");
- // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
- if (!this.epochDays)
- this.setTodaysDate();
-
-
- var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
- var testString = "http://ns8-stats.netscape.com/?prefs" + versionNum + "=" + commandstring.substring(0,commandstring.length - 1) + "&guid=" + this.UUID;
- this.debug("Sending string: \n" + testString + "\n\n");
- var result = this.DTSVC.sendHttpRequest(testString);
- if (result) {
- prefs.setIntPref("browser.last_track_prefs_epoch", this.epochDays);
-
- return true;
- } else {
- this.debug ("Failed: " + result + "\n");
- }
- return false;
- }
- };
-