home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / perfmon / perfmon.js < prev    next >
Encoding:
Text File  |  2005-07-29  |  17.0 KB  |  550 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.             var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  161.                         
  162.             try {
  163.                 this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, 'browser_started');
  164.                 this.DTSVC.writePerfMon();            
  165.             
  166.                 this.UUID = "";
  167.                 if ((prefs.getPrefType("browser.info.guid") == prefs.PREF_STRING) &&
  168.                     (this.UUID = prefs.getCharPref("browser.info.guid"))) {
  169.                     // nothing        
  170.                 } else {
  171.                     this.UUID = this.DTSVC.uuidCreate();
  172.                     prefs.setCharPref("browser.info.guid",this.UUID);
  173.                 }
  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.             // JMC - New datatracking functionality - June 15, 2005
  181.             // Send real-time packet on startup
  182.             
  183.             // Should respect optout
  184.             
  185.             if ((prefs.getPrefType("browser.info.track_usage") == prefs.PREF_BOOL) &&
  186.                 !(prefs.getBoolPref("browser.info.track_usage"))) {
  187.                 return;        
  188.             }
  189.             
  190.             var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
  191.             var startupString = "http://ns8-stats.netscape.com/?s" + versionNum + "=" +  this.UUID;
  192.             this.debug("Sending startup string: \n" + startupString + "\n\n");
  193.             var result = this.DTSVC.sendHttpRequest(startupString);
  194.             if (!result) {
  195.                 this.debug ("Failed sending startup string \n");    
  196.             }                
  197.         }
  198.     },    
  199.     
  200.     checkTimeToSend : function() {
  201.         
  202.         // this.debug("CheckTimeToSend\n");
  203.         // If there's more than three days' data, force send
  204.         if (this.countDaysPending() > 3    ) {
  205.             return true;    
  206.         }
  207.         
  208.         // Otherwise, send if we're within 10 minutes of our send time
  209.         var currentTime = new Date();
  210.         
  211.         currentTime = currentTime.getTime();
  212.         // this.debug ("Current time is: " + currentTime + "\n");
  213.         if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) < (this.TIME_MINUTE * 10)))
  214.         {
  215.             return true;
  216.         }
  217.         
  218.         // If we're more than 10 minutes out, Select a new send time
  219.         if (currentTime > this.timeToSend && ((currentTime - this.timeToSend) > (this.TIME_MINUTE * 10)))
  220.         {
  221.             this.setTimeToSend();
  222.         }
  223.         
  224.         return false;        
  225.     },
  226.     
  227.     trackData : function(commandObject) {
  228.         this.debug("Tracking data for" + commandObject );
  229.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  230.         
  231.         if ((prefs.getPrefType("browser.info.track_usage") == prefs.PREF_BOOL) &&
  232.             !(prefs.getBoolPref("browser.info.track_usage"))) {
  233.             return;        
  234.         }
  235.         
  236.         var commandstring;
  237.         if (commandObject.hasAttribute('perfmon'))
  238.         {
  239.             commandstring = commandObject.getAttribute('perfmon');
  240.         } else if (commandObject.hasAttribute('command'))
  241.         {
  242.         commandstring = commandObject.getAttribute('command');
  243.         } else if (commandObject.hasAttribute('id'))
  244.         {
  245.         commandstring = commandObject.getAttribute('id');
  246.         }
  247.         if (commandstring)
  248.         {
  249.             var rExp = /\s|:/gi
  250.             commandstring = commandstring.replace(rExp, "_");    
  251.             // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
  252.             if (!this.epochDays)
  253.                 this.setTodaysDate();            
  254.             
  255.             this.DTSVC.perfMonIncrementCommand(this.thisDate, this.epochDays, commandstring);
  256.             this.DTSVC.writePerfMon();
  257.         } else {
  258.             this.debug ("Couldn't identify the command for perfmon.");
  259.         }
  260.         
  261.             // this.debug ("Time to send is: " + this.timeToSend + "\n\n");
  262.     },
  263.     
  264.     sendTimer : function() {
  265.         if (this.checkTimeToSend())
  266.         {
  267.             if (this.sendPerfMon())
  268.             {
  269.                 this.timeToSend += this.TIME_DAY; // Set sending time to tomorrow, same time
  270.                 this.dumpPrefs(); // FIXME
  271.             } else {
  272.                 this.timeToSend += (this.TIME_MINUTE * 5); // Wait five minutes before trying again
  273.             }
  274.         }
  275.         
  276.         setTimeout("perfmon.sendTimer()", 60000);
  277.     },
  278.     
  279.     countDaysPending : function() {
  280.         
  281.         var daysCount = 0;
  282.         
  283.         resources = this.DTDS.GetAllResources();
  284.         while (resources.hasMoreElements()) {
  285.             res = resources.getNext();
  286.             res.QueryInterface(this.kRDFRSCIID);
  287.             // this.debug('resource: '+res.Value);
  288.             labels = this.DTDS.ArcLabelsOut(res);
  289.             while (labels.hasMoreElements()) {
  290.                 label = labels.getNext();
  291.                 if (label instanceof this.kRDFRSCIID) {
  292.                     target = this.DTDS.GetTarget(res, label, true);
  293.                      if (target instanceof this.kRDFINTIID) {
  294.                         if (label.Value == this.DT_NS + 'epochDays') {
  295.                             daysCount++;
  296.                         }
  297.                     }
  298.                 }
  299.             }
  300.         }
  301.         return daysCount;
  302.     },
  303.     
  304.     sendPerfMon : function() {
  305.         // Today is guaranteed to have a record
  306.  
  307.         var entries = new Array();
  308.         var records = new Object();
  309.         var dates = new Object();
  310.  
  311.         resources = this.DTDS.GetAllResources();
  312.         while (resources.hasMoreElements()) {
  313.             res = resources.getNext();
  314.             res.QueryInterface(this.kRDFRSCIID);
  315.             // this.debug('resource: '+res.Value);
  316.             labels = this.DTDS.ArcLabelsOut(res);
  317.             var recordstring = '';
  318.             var epochNumber = 0;
  319.  
  320.             // Get Title String
  321.             var titlestring;
  322.             var itemRes = this.RDF.GetResource(this.DT_NS + 'title');
  323.             var target = this.DTDS.GetTarget(res, itemRes, true);
  324.             if (target) target = target.QueryInterface(kRDFLITIID);
  325.             if (!target) { 
  326.                 titlestring = ''; 
  327.             } else {
  328.                 titlestring = target.Value; 
  329.             }
  330.  
  331.             while (labels.hasMoreElements()) {
  332.                 label = labels.getNext();
  333.                 if (label instanceof this.kRDFRSCIID) {
  334.                     target = this.DTDS.GetTarget(res, label, true);
  335.                      if (target instanceof this.kRDFINTIID) {
  336.                         dbg += ' => '+target.Value;
  337.                         if (label.Value == this.DT_NS + 'epochDays') {
  338.                             entries[entries.length] = target.Value;
  339.                             epochNumber = target.Value;
  340.                         } else {    
  341.                             var hashvalue = hash_value(dt_hash, label.Value.substring(this.DT_NS.length));
  342.                             if (hashvalue)
  343.                             {
  344.                                 recordstring += hashvalue + "|";
  345.                             } else {        
  346.                                 recordstring += label.Value.substring(this.DT_NS.length) + "|";
  347.                             }        
  348.                             recordstring += target.Value + "|";
  349.                                              
  350.                             // for each button
  351.                                 // look up the hashtable
  352.                                 // if not present, use the full name
  353.         
  354.                         }
  355.                     }
  356.                 }
  357.             }
  358.             if (epochNumber) {
  359.                 records[epochNumber] = recordstring.substring(0,recordstring.length -1);            
  360.                 dates[epochNumber] = titlestring;
  361.             }        
  362.         }
  363.             
  364.         entries.sort(compareNum);
  365.         
  366.         if (entries[0] >= this.epochDays) {
  367.             return true;
  368.         }
  369.         
  370.         var sendstring;
  371.         sendstring = entries[0] + "|";        
  372.         for (i=0; i < (entries.length); i++) {
  373.             if (i > 0 && ((entries[i] - entries[i-1]) > 1)) { // Some null days here
  374.                 sendstring += "0|" + (entries[i] - entries[i-1] -1) + ":";
  375.                 
  376.             }            
  377.             if (i != (entries.length - 1)) { // Skip todays' record
  378.                 sendstring += records[entries[i]] + ":";
  379.             }
  380.         }
  381.         
  382.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  383.         var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
  384.         var testString = "http://ns8-stats.netscape.com/?stat" + versionNum + "=" + sendstring.substring(0,sendstring.length - 1) + "&guid=" + this.UUID;
  385.         this.debug("Sending string: \n" + testString + "\n\n");
  386.         var result = this.DTSVC.sendHttpRequest(testString);
  387.         if (result) {
  388.             // Delete old nodes
  389.             this.debug("Deleting nodes...\n");
  390.             for (i=0; i < (entries.length - 1); i++) {
  391.                 this.DTSVC.removeTrackedDate(dates[entries[i]]);
  392.             }            
  393.             return true;
  394.         } else {
  395.             this.debug ("Failed: " + result + "\n");    
  396.         }
  397.         return false;
  398.     },
  399.     
  400.     debug : function(msg) {
  401.         dump('**** -- perfmon.js: '+msg+'\n');
  402.     },
  403.  
  404.  
  405.     Random : function(N) {
  406.     return Math.floor(N * (Math.random() % 1));
  407.     },
  408.  
  409.     Randum : function(N) {
  410.     return (N * (Math.random() % 1)) | 0;
  411.     },
  412.  
  413.     RanSpan : function(MinV, MaxV) {
  414.     return MinV + this.Random(MaxV - MinV + 1);
  415.     },
  416.  
  417.     dumpRDF : function() {
  418.         resources = this.DTDS.GetAllResources();
  419.         while (resources.hasMoreElements()) {
  420.             res = resources.getNext();
  421.             res.QueryInterface(this.kRDFRSCIID);
  422.             this.debug('resource: '+res.Value);
  423.             var labels = this.DTDS.ArcLabelsOut(res);
  424.             while (labels.hasMoreElements()) {
  425.                 var label = labels.getNext();
  426.                 if (label instanceof this.kRDFRSCIID) {
  427.                     dbg = ' * '+label.Value;
  428.                     var target = this.DTDS.GetTarget(res, label, true);
  429.                     if (target instanceof this.kRDFRSCIID) {
  430.                         dbg += ' => '+target.Value;
  431.                     } else if (target instanceof this.kRDFLITIID) {
  432.                         dbg += ' => '+target.Value;
  433.                     } else if (target instanceof this.kRDFINTIID) {
  434.                         dbg += ' => '+target.Value;
  435.                     } else {
  436.                         try {
  437.                             dbg += ' => '+target.Value;
  438.                         } catch (ex) { }
  439.                     }
  440.                     this.debug(dbg);
  441.                 }
  442.             }
  443.         }
  444.     },
  445.     
  446.     echoPrefs : function() {
  447.         return;
  448.         
  449.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  450.         var childcount = new Object;
  451.         var thesePrefList = prefs.getChildList("", childcount);    
  452.         for (i=0; i < thesePrefList.length; i++) {
  453.             this.debug(thesePrefList[i]);    
  454.         }    
  455.     },
  456.     
  457.     dumpPrefs : function() {
  458.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  459.         var lastTimeSent = 0;
  460.         if (prefs.getPrefType("browser.last_track_prefs_epoch") == prefs.PREF_INT) {
  461.             lastTimeSent = prefs.getIntPref("browser.last_track_prefs_epoch");            
  462.         }
  463.         if (((prefs.getPrefType("browser.info.track_prefs") == prefs.PREF_BOOL) &&
  464.             !(prefs.getBoolPref("browser.info.track_prefs"))) ||
  465.             (this.epochDays - lastTimeSent < 7)) {
  466.                 this.debug("sent recently... leaving.");
  467.             return;        
  468.         }
  469.         
  470.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  471.         var thesePrefs = prefService.getBranch("");
  472.         var defPrefs = prefService.getDefaultBranch("");
  473.         var childcount = new Object;
  474.         var thesePrefList = thesePrefs.getChildList("", childcount);
  475.         
  476.         var commandstring = this.epochDays + "|";    
  477.         
  478.         
  479.         for (i=0; i < thesePrefList.length; i++) {
  480.             // this.debug("In Loop: " + i);
  481.             var prefType = thesePrefs.getPrefType(thesePrefList[i]);
  482.             var userPrefValue = 0;
  483.             var defPrefValue = 0;
  484.             switch (prefType) {
  485.                 
  486.                 case thesePrefs.PREF_STRING:
  487.                     userPrefValue = thesePrefs.getCharPref(thesePrefList[i]);
  488.                     try {
  489.                         defPrefValue = defPrefs.getCharPref(thesePrefList[i]);
  490.                     } catch (ex) {
  491.                         defPrefValue = "";    
  492.                     }
  493.                 break;
  494.                  case thesePrefs.PREF_INT:
  495.                      userPrefValue = thesePrefs.getIntPref(thesePrefList[i]);
  496.                     try {
  497.                         defPrefValue = defPrefs.getIntPref(thesePrefList[i]);
  498.                     } catch (ex) {
  499.                         defPrefValue = 0;    
  500.                     }
  501.                  break;
  502.                  case thesePrefs.PREF_BOOL:
  503.                      userPrefValue = thesePrefs.getBoolPref(thesePrefList[i]);
  504.                     try {
  505.                         defPrefValue = defPrefs.getBoolPref(thesePrefList[i]);
  506.                     } catch (ex) {
  507.                         defPrefValue = false;    
  508.                     }
  509.                  break;
  510.             }
  511.             // (ccampbell): no longer using blacklist... just restricting prefs
  512.             // to those in pref_hash instead.
  513.             //var blacklist = hash_value(pref_bl, (thesePrefList[i]));              
  514.             if (userPrefValue != defPrefValue) {
  515.                 var hashvalue = hash_value(pref_hash, (thesePrefList[i]));
  516.                 if (hashvalue) {
  517.                     commandstring += hashvalue + "|" + userPrefValue + "|";
  518.                 }
  519.                 //else {
  520.                 //    commandstring += thesePrefList[i] + "|" + userPrefValue + "|";
  521.                 //}
  522.                 // this.debug("Pref: " + thesePrefList[i] + ": " + userPrefValue);
  523.             } else {
  524.                 // this.debug("default value");    
  525.             }
  526.     
  527.         }
  528.         
  529.         var rExp = /\s|:/gi
  530.             commandstring = commandstring.replace(rExp, "_");    
  531.             // this.debug ("Tracking data for :" + commandstring + "\n\n\n");
  532.             if (!this.epochDays)
  533.                 this.setTodaysDate();            
  534.         
  535.         
  536.         var versionNum = prefs.getCharPref('app.version').replace(/\./g, "_");
  537.         var testString = "http://ns8-stats.netscape.com/?prefs" + versionNum + "=" + commandstring.substring(0,commandstring.length - 1) + "&guid=" + this.UUID;
  538.         this.debug("Sending string: \n" + testString + "\n\n");
  539.         var result = this.DTSVC.sendHttpRequest(testString);
  540.         if (result) {
  541.             prefs.setIntPref("browser.last_track_prefs_epoch", this.epochDays);
  542.                         
  543.             return true;
  544.         } else {
  545.             this.debug ("Failed: " + result + "\n");    
  546.         }
  547.         return false;    
  548.     }
  549. };
  550.