home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2005 August / PCADVD_121.iso / internet / nsb-setup.exe / chrome / browser.jar / content / browser / perfmon / perfmon.js < prev    next >
Encoding:
Text File  |  2005-05-19  |  16.2 KB  |  532 lines

  1. var perfmon;
  2.  
  3. function compareNum(a,b)
  4. {
  5.     return a-b
  6. }
  7.  
  8. // Returns the index (1-based) of hashkey in thishash
  9. function hash_value(thishash, hashkey) {
  10.     for (var i=0; i<thishash.length; i++) {
  11.         if (thishash[i] == hashkey) return (i+1); // Can't be zero-based
  12.     }    
  13.     return 0;
  14. }
  15.  
  16. if (!perfmon) perfmon = {
  17.  
  18.     DT_NS : "http://home.netscape.com/DT-rdf#",
  19.     NC_NS : "http://home.netscape.com/NC-rdf#",
  20.     WEB_NS : "http://home.netscape.com/WEB-rdf#",
  21.     RDF_NS : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  22.     XUL_NS : "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  23.     NC_NS_CMD : null,
  24.  
  25.     // definition of the services frequently used for datattracking
  26.     kRDFContractID : "@mozilla.org/rdf/rdf-service;1",
  27.     kRDFSVCIID : Components.interfaces.nsIRDFService,
  28.     kRDFRSCIID : Components.interfaces.nsIRDFResource,
  29.     kRDFLITIID : Components.interfaces.nsIRDFLiteral,
  30.     kRDFINTIID : Components.interfaces.nsIRDFInt,
  31.     kRDFDSIID : Components.interfaces.nsIRDFDataSource,
  32.     RDF : null,
  33.  
  34.     kRDFCContractID : "@mozilla.org/rdf/container;1",
  35.     kRDFCIID :  Components.interfaces.nsIRDFContainer,
  36.     RDFC : null,
  37.  
  38.     kRDFCUContractID : "@mozilla.org/rdf/container-utils;1",
  39.     kRDFCUIID : Components.interfaces.nsIRDFContainerUtils,
  40.     RDFCU : null,
  41.  
  42.     kPREFContractID : "@mozilla.org/preferences-service;1",
  43.     kPREFIID : Components.interfaces.nsIPrefService,
  44.     PREF : null,
  45.  
  46.     kSOUNDContractID : "@mozilla.org/sound;1",
  47.     kSOUNDIID : Components.interfaces.nsISound,
  48.     SOUND : null,
  49.  
  50.     kWINDOWContractID : "@mozilla.org/appshell/window-mediator;1",
  51.     kWINDOWIID : Components.interfaces.nsIWindowMediator,
  52.     WINDOWSVC : null,
  53.  
  54.     kDSContractID : "@mozilla.org/widget/dragservice;1",
  55.     kDSIID : Components.interfaces.nsIDragService,
  56.     DS : null,
  57.  
  58.     kDTContractID : "@mozilla.org/browser/perfmon-service;1",
  59.     kDTSVCIID : Components.interfaces.nsIPerfMonService,
  60.     DTSVC : null,
  61.     DTDS : null,
  62.  
  63.     UUID : null,
  64.  
  65.     thisDate : null,
  66.     epochDays : null,
  67.     timeToSend : null,
  68.     minimumSendWindow : 10800000,   //3 hours in milliseconds
  69.     // minimumSendWindow : 3600000, // 1 hour for testing
  70.     
  71.     TIME_DAY : 86400000,
  72.     TIME_HOUR : 3600000,
  73.     TIME_MINUTE : 60000,
  74.  
  75.     setTodaysDate : function() {
  76.         var todaysDate = new Date();
  77.         var epochDate = new Date(2004, 05, 01); //EPOCH DATE, June 1
  78.         var diffDate = new Date();
  79.         
  80.         var datediff = diffDate.setTime(Math.abs(todaysDate.getTime() - epochDate.getTime()));
  81.         this.epochDays = Math.floor(datediff/86400000)    //calculate days and convert to string
  82.         this.thisDate = (todaysDate.getFullYear() + "-" + (todaysDate.getMonth() + 1) + "-" + todaysDate.getDate());
  83.     },
  84.  
  85.     setTimeToSend : function() {
  86.         
  87.         var dateNow = new Date();
  88.         var timeNow = dateNow.getTime();
  89.         // Date.UTC(dateNow.getFullYear(), dateNow.getMonth(), dateNow.getHours(), dateNow.getMinutes(), dateNow.getSeconds());
  90.         // debug ("Diff between getTime and UTC of a current Date object: " + dateNow.getTime() + ", " + timeNow + "\n");
  91.         
  92.         var MINTIME = 0;
  93.         var MAXTIME = 0;
  94.         // BLACKOUT time is midnight to 1 am PST (UTC -0700)
  95.         // Next blackout window is...
  96.         // JAVASCRIPT time is 0-based
  97.         var UTCBLACKOUTMIN = 6;
  98.         
  99.         var blackoutDate = new Date();
  100.         blackoutDate.setUTCHours(UTCBLACKOUTMIN);
  101.         var BLACKOUTMIN = blackoutDate.getTime();
  102.         var BLACKOUTMAX = BLACKOUTMIN + this.TIME_HOUR;
  103.         
  104.     
  105.         
  106.         MINTIME = Math.min(timeNow, BLACKOUTMAX);
  107.             
  108.         if (dateNow.getUTCHours() > UTCBLACKOUTMIN) // Already past the start of todays blackout
  109.         {
  110.             // Create tomorrow's blackout window
  111.             BLACKOUTMIN += this.TIME_DAY;
  112.             BLACKOUTMAX += this.TIME_DAY;
  113.         }        
  114.         
  115.         //this.debug("BLACKOUTMAX: " + BLACKOUTMAX + ", BLACKOUTMIN: " + BLACKOUTMIN);
  116.         
  117.         
  118.         // MAXTIME is minimum of:  start of next blackout period, or 10 pm localtime
  119.         var tonightCutoff = new Date();
  120.         tonightCutoff.setHours(21); // 10 pm
  121.         var tonightUTC = tonightCutoff.getTime();
  122.         if (timeNow > tonightUTC) // if it's already past 10 pm local, send whenever
  123.         {
  124.             MAXTIME = MINTIME + this.minimumSendWindow;
  125.         } else {
  126.             MAXTIME = tonightUTC;
  127.         }
  128.         
  129.         // Make sure the selected time is not within the BLACKOUT window
  130.         
  131.         do {
  132.         //    this.debug("MINTIME: " + MINTIME + ", MAXTIME: " + MAXTIME);
  133.             this.timeToSend = this.RanSpan(MINTIME, MAXTIME);
  134.             // this.debug(this.timeToSend);
  135.         } while (this.timeToSend < BLACKOUTMAX && this.timeToSend > BLACKOUTMIN)
  136.         
  137.         setTimeout("perfmon.sendTimer()", 60000);
  138.         
  139.     },
  140.     
  141.  
  142.     initServices : function() {
  143.         
  144.         this.debug('initServices()');
  145.         if (!this.DTDS) {
  146.             this.NS_NS_CMD = this.NC_NS + "command?cmd=";
  147.             this.RDF       = Components.classes[this.kRDFContractID].getService(this.kRDFSVCIID);
  148.             this.RDFC      = Components.classes[this.kRDFCContractID].createInstance(this.kRDFCIID);
  149.             this.RDFCU     = Components.classes[this.kRDFCUContractID].getService(this.kRDFCUIID);
  150.             this.PREF      = Components.classes[this.kPREFContractID].getService(this.kPREFIID)
  151.                                                                      .getBranch(null);
  152.             this.SOUND     = Components.classes[this.kSOUNDContractID].createInstance(this.kSOUNDIID);
  153.             this.WINDOWSVC = Components.classes[this.kWINDOWContractID].getService(this.kWINDOWIID);
  154.             this.DS        = Components.classes[this.kDSContractID].getService(this.kDSIID);
  155.             this.DTSVC     = Components.classes[this.kDTContractID].getService(this.kDTSVCIID);
  156.             this.DTDS      = this.DTSVC.QueryInterface(this.kRDFDSIID);
  157.                         
  158.             this.setTodaysDate();    
  159.             this.setTimeToSend();
  160.                         
  161.             try {
  162.                 this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, 'browser_started');
  163.                 this.DTSVC.writePerfMon();
  164.             
  165.             
  166.                 var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  167.                 this.UUID = "";
  168.                 if ((prefs.getPrefType("browser.info.guid") == prefs.PREF_STRING) &&
  169.                     (this.UUID = prefs.getCharPref("browser.info.guid"))) {
  170.                     return;        
  171.                 }
  172.                 this.UUID = this.DTSVC.uuidCreate();
  173.                 prefs.setCharPref("browser.info.guid",this.UUID);
  174.             } catch (ex) {
  175.                 dump('perfmon.js: call to DTSVC.perfMonIncrementCommand() failed '
  176.                     + 'with exception:\n' + ex + '\n');
  177.                 this.DTDS = null;
  178.             }
  179.             
  180.         }
  181.     },    
  182.     
  183.     checkTimeToSend : function() {
  184.         
  185.         // this.debug("CheckTimeToSend\n");
  186.         // If there's more than three days' data, force send
  187.         if (this.countDaysPending() > 3    ) {
  188.             return true;    
  189.         }
  190.         
  191.         // Otherwise, send if we're within 10 minutes of our send time
  192.         var currentTime = new Date();
  193.         
  194.         currentTime = currentTime.getTime();
  195.         // this.debug ("Current time is: " + currentTime + "\n");
  196.         if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) < (this.TIME_MINUTE * 10)))
  197.         {
  198.             return true;
  199.         }
  200.         
  201.         // If we're more than 10 minutes out, Select a new send time
  202.         if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) > (this.TIME_MINUTE * 10)))
  203.         {
  204.             this.setTimeToSend();
  205.         }
  206.         
  207.         return false;        
  208.     },
  209.     
  210.     trackData : function(commandObject) {
  211.         this.debug("Tracking data for" + commandObject );
  212.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  213.         
  214.         if ((prefs.getPrefType("browser.info.track_usage") == prefs.PREF_BOOL) &&
  215.             !(prefs.getBoolPref("browser.info.track_usage"))) {
  216.             return;        
  217.         }
  218.         
  219.         var commandstring;
  220.         if (commandObject.hasAttribute('perfmon'))
  221.         {
  222.             commandstring = commandObject.getAttribute('perfmon');
  223.         } else if (commandObject.hasAttribute('command'))
  224.         {
  225.         commandstring = commandObject.getAttribute('command');
  226.         } else if (commandObject.hasAttribute('id'))
  227.         {
  228.         commandstring = commandObject.getAttribute('id');
  229.         }
  230.         if (commandstring)
  231.         {
  232.             var rExp = /\s|:/gi
  233.             commandstring = commandstring.replace(rExp, "_");    
  234.             // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
  235.             if (!this.epochDays)
  236.                 this.setTodaysDate();            
  237.             
  238.             this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, commandstring);
  239.             this.DTSVC.writePerfMon();
  240.         } else {
  241.             this.debug ("Couldn't identify the command for perfmon.");
  242.         }
  243.         
  244.             // this.debug ("Time to send is: " + this.timeToSend + "\n\n");
  245.     },
  246.     
  247.     sendTimer : function() {
  248.         if (this.checkTimeToSend())
  249.         {
  250.             if (this.sendPerfMon())
  251.             {
  252.                 this.timeToSend += this.TIME_DAY; // Set sending time to tomorrow, same time
  253.                 this.dumpPrefs(); // FIXME
  254.             } else {
  255.                 this.timeToSend += (this.TIME_MINUTE * 5); // Wait five minutes before trying again
  256.             }
  257.         }
  258.         
  259.         setTimeout("perfmon.sendTimer()", 60000);
  260.     },
  261.     
  262.     countDaysPending : function() {
  263.         
  264.         var daysCount = 0;
  265.         
  266.         resources = this.DTDS.GetAllResources();
  267.         while (resources.hasMoreElements()) {
  268.             res = resources.getNext();
  269.             res.QueryInterface(this.kRDFRSCIID);
  270.             // this.debug('resource: '+res.Value);
  271.             labels = this.DTDS.ArcLabelsOut(res);
  272.             while (labels.hasMoreElements()) {
  273.                 label = labels.getNext();
  274.                 if (label instanceof this.kRDFRSCIID) {
  275.                     target = this.DTDS.GetTarget(res, label, true);
  276.                      if (target instanceof this.kRDFINTIID) {
  277.                         if (label.Value == this.DT_NS + 'epochDays') {
  278.                             daysCount++;
  279.                         }
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.         return daysCount;
  285.     },
  286.     
  287.     sendPerfMon : function() {
  288.         // Today is guaranteed to have a record
  289.  
  290.         var entries = new Array();
  291.         var records = new Object();
  292.         var dates = new Object();
  293.  
  294.         resources = this.DTDS.GetAllResources();
  295.         while (resources.hasMoreElements()) {
  296.             res = resources.getNext();
  297.             res.QueryInterface(this.kRDFRSCIID);
  298.             // this.debug('resource: '+res.Value);
  299.             labels = this.DTDS.ArcLabelsOut(res);
  300.             var recordstring = '';
  301.             var epochNumber = 0;
  302.  
  303.             // Get Title String
  304.             var titlestring;
  305.             var itemRes = this.RDF.GetResource(this.DT_NS + 'title');
  306.             var target = this.DTDS.GetTarget(res, itemRes, true);
  307.             if (target) target = target.QueryInterface(kRDFLITIID);
  308.             if (!target) { 
  309.                 titlestring = ''; 
  310.             } else {
  311.                 titlestring = target.Value; 
  312.             }
  313.  
  314.             while (labels.hasMoreElements()) {
  315.                 label = labels.getNext();
  316.                 if (label instanceof this.kRDFRSCIID) {
  317.                     target = this.DTDS.GetTarget(res, label, true);
  318.                      if (target instanceof this.kRDFINTIID) {
  319.                         dbg += ' => '+target.Value;
  320.                         if (label.Value == this.DT_NS + 'epochDays') {
  321.                             entries[entries.length] = target.Value;
  322.                             epochNumber = target.Value;
  323.                         } else {    
  324.                             var hashvalue = hash_value(dt_hash, label.Value.substring(this.DT_NS.length));
  325.                             if (hashvalue)
  326.                             {
  327.                                 recordstring += hashvalue + "|";
  328.                             } else {        
  329.                                 recordstring += label.Value.substring(this.DT_NS.length) + "|";
  330.                             }        
  331.                             recordstring += target.Value + "|";
  332.                                              
  333.                             // for each button
  334.                                 // look up the hashtable
  335.                                 // if not present, use the full name
  336.         
  337.                         }
  338.                     }
  339.                 }
  340.             }
  341.             if (epochNumber) {
  342.                 records[epochNumber] = recordstring.substring(0,recordstring.length -1);            
  343.                 dates[epochNumber] = titlestring;
  344.             }        
  345.         }
  346.             
  347.         entries.sort(compareNum);
  348.         
  349.         if (entries[0] >= this.epochDays) {
  350.             return true;
  351.         }
  352.         
  353.         var sendstring;
  354.         sendstring = entries[0] + "|";        
  355.         for (i=0; i < (entries.length); i++) {
  356.             if (i > 0 && ((entries[i] - entries[i-1]) > 1)) { // Some null days here
  357.                 sendstring += "0|" + (entries[i] - entries[i-1] -1) + ":";
  358.                 
  359.             }            
  360.             if (i != (entries.length - 1)) { // Skip todays' record
  361.                 sendstring += records[entries[i]] + ":";
  362.             }
  363.         }
  364.         
  365.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  366.         var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
  367.         var testString = "http://ns8-stats.netscape.com/?stat" + versionNum + "=" + sendstring.substring(0,sendstring.length - 1) + "&guid=" + this.UUID;
  368.         this.debug("Sending string: \n" + testString + "\n\n");
  369.         var result = this.DTSVC.sendHttpRequest(testString);
  370.         if (result) {
  371.             // Delete old nodes
  372.             this.debug("Deleting nodes...\n");
  373.             for (i=0; i < (entries.length - 1); i++) {
  374.                 this.DTSVC.removeTrackedDate(dates[entries[i]]);
  375.             }            
  376.             return true;
  377.         } else {
  378.             this.debug ("Failed: " + result + "\n");    
  379.         }
  380.         return false;
  381.     },
  382.     
  383.     debug : function(msg) {
  384.         dump('**** -- perfmon.js: '+msg+'\n');
  385.     },
  386.  
  387.  
  388.     Random : function(N) {
  389.     return Math.floor(N * (Math.random() % 1));
  390.     },
  391.  
  392.     Randum : function(N) {
  393.     return (N * (Math.random() % 1)) | 0;
  394.     },
  395.  
  396.     RanSpan : function(MinV, MaxV) {
  397.     return MinV + this.Random(MaxV - MinV + 1);
  398.     },
  399.  
  400.     dumpRDF : function() {
  401.         resources = this.DTDS.GetAllResources();
  402.         while (resources.hasMoreElements()) {
  403.             res = resources.getNext();
  404.             res.QueryInterface(this.kRDFRSCIID);
  405.             this.debug('resource: '+res.Value);
  406.             var labels = this.DTDS.ArcLabelsOut(res);
  407.             while (labels.hasMoreElements()) {
  408.                 var label = labels.getNext();
  409.                 if (label instanceof this.kRDFRSCIID) {
  410.                     dbg = ' * '+label.Value;
  411.                     var target = this.DTDS.GetTarget(res, label, true);
  412.                     if (target instanceof this.kRDFRSCIID) {
  413.                         dbg += ' => '+target.Value;
  414.                     } else if (target instanceof this.kRDFLITIID) {
  415.                         dbg += ' => '+target.Value;
  416.                     } else if (target instanceof this.kRDFINTIID) {
  417.                         dbg += ' => '+target.Value;
  418.                     } else {
  419.                         try {
  420.                             dbg += ' => '+target.Value;
  421.                         } catch (ex) { }
  422.                     }
  423.                     this.debug(dbg);
  424.                 }
  425.             }
  426.         }
  427.     },
  428.     
  429.     echoPrefs : function() {
  430.         return;
  431.         
  432.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  433.         var childcount = new Object;
  434.         var thesePrefList = prefs.getChildList("", childcount);    
  435.         for (i=0; i < thesePrefList.length; i++) {
  436.             this.debug(thesePrefList[i]);    
  437.         }    
  438.     },
  439.     
  440.     dumpPrefs : function() {
  441.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  442.         var lastTimeSent = 0;
  443.         if (prefs.getPrefType("browser.last_track_prefs_epoch") == prefs.PREF_INT) {
  444.             lastTimeSent = prefs.getIntPref("browser.last_track_prefs_epoch");            
  445.         }
  446.         if (((prefs.getPrefType("browser.info.track_prefs") == prefs.PREF_BOOL) &&
  447.             !(prefs.getBoolPref("browser.info.track_prefs"))) ||
  448.             (this.epochDays - lastTimeSent < 7)) {
  449.                 this.debug("sent recently... leaving.");
  450.             return;        
  451.         }
  452.         
  453.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  454.         var thesePrefs = prefService.getBranch("");
  455.         var defPrefs = prefService.getDefaultBranch("");
  456.         var childcount = new Object;
  457.         var thesePrefList = thesePrefs.getChildList("", childcount);
  458.         
  459.         var commandstring = this.epochDays + "|";    
  460.         
  461.         
  462.         for (i=0; i < thesePrefList.length; i++) {
  463.             // this.debug("In Loop: " + i);
  464.             var prefType = thesePrefs.getPrefType(thesePrefList[i]);
  465.             var userPrefValue = 0;
  466.             var defPrefValue = 0;
  467.             switch (prefType) {
  468.                 
  469.                 case thesePrefs.PREF_STRING:
  470.                     userPrefValue = thesePrefs.getCharPref(thesePrefList[i]);
  471.                     try {
  472.                         defPrefValue = defPrefs.getCharPref(thesePrefList[i]);
  473.                     } catch (ex) {
  474.                         defPrefValue = "";    
  475.                     }
  476.                 break;
  477.                  case thesePrefs.PREF_INT:
  478.                      userPrefValue = thesePrefs.getIntPref(thesePrefList[i]);
  479.                     try {
  480.                         defPrefValue = defPrefs.getIntPref(thesePrefList[i]);
  481.                     } catch (ex) {
  482.                         defPrefValue = 0;    
  483.                     }
  484.                  break;
  485.                  case thesePrefs.PREF_BOOL:
  486.                      userPrefValue = thesePrefs.getBoolPref(thesePrefList[i]);
  487.                     try {
  488.                         defPrefValue = defPrefs.getBoolPref(thesePrefList[i]);
  489.                     } catch (ex) {
  490.                         defPrefValue = false;    
  491.                     }
  492.                  break;
  493.             }
  494.             // (ccampbell): no longer using blacklist... just restricting prefs
  495.             // to those in pref_hash instead.
  496.             //var blacklist = hash_value(pref_bl, (thesePrefList[i]));              
  497.             if (userPrefValue != defPrefValue) {
  498.                 var hashvalue = hash_value(pref_hash, (thesePrefList[i]));
  499.                 if (hashvalue) {
  500.                     commandstring += hashvalue + "|" + userPrefValue + "|";
  501.                 }
  502.                 //else {
  503.                 //    commandstring += thesePrefList[i] + "|" + userPrefValue + "|";
  504.                 //}
  505.                 // this.debug("Pref: " + thesePrefList[i] + ": " + userPrefValue);
  506.             } else {
  507.                 // this.debug("default value");    
  508.             }
  509.     
  510.         }
  511.         
  512.         var rExp = /\s|:/gi
  513.             commandstring = commandstring.replace(rExp, "_");    
  514.             // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
  515.             if (!this.epochDays)
  516.                 this.setTodaysDate();            
  517.         
  518.         
  519.         var testString = "http://ns8-stats.netscape.com/?prefs=" + commandstring.substring(0,commandstring.length - 1) + "&guid=" + this.UUID;
  520.         this.debug("Sending string: \n" + testString + "\n\n");
  521.         var result = this.DTSVC.sendHttpRequest(testString);
  522.         if (result) {
  523.             prefs.setIntPref("browser.last_track_prefs_epoch", this.epochDays);
  524.                         
  525.             return true;
  526.         } else {
  527.             this.debug ("Failed: " + result + "\n");    
  528.         }
  529.         return false;    
  530.     }
  531. };
  532.