home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 December / PCWorld_2005-12_cd.bin / komunikace / netscape / nsb-install-8-0.exe / components / optout.js < prev    next >
Text File  |  2005-09-26  |  9KB  |  300 lines

  1. /*
  2.  * Constants
  3.  */
  4. const OPTOUT_CONTRACTID = '@mozilla.org/optout;1';
  5. const OPTOUT_CID = Components.ID('{FF17BE30-85F8-11D9-9669-0800200C9A66}');
  6. const OPTOUT_IID = Components.interfaces.nsIOptOutService;
  7.  
  8. /*
  9.  * Class definitions
  10.  */
  11.  
  12. /* The nsOptOut class constructor. */
  13. function nsOptOut() {
  14.     // this.Init();
  15. }
  16.  
  17. /* the nsOptOut class def */
  18. nsOptOut.prototype = {
  19.     OPTOUT_NS : "http://home.netscape.com/NC-optout#",
  20.     OO_FILENAME : "optout.rdf",
  21.     OO_REMOTEURL : null,    
  22.     
  23.     RES_HOSTNAME : null,
  24.     
  25.     RDFService : null,
  26.     RDFDataSource : null,
  27.     RDFLoadObserver : null,
  28.     RemoteRDFLoadObserver : null,
  29.     
  30.     SiteList : null,
  31.     
  32.     MyInit : function () {
  33.         // dump("OPTOUT: MyInit()\n");
  34.         if (!this.RDFService) {
  35.             this.RDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  36.                                         .getService(Components.interfaces.nsIRDFService);    
  37.             this.RES_HOSTNAME = this.RDFService.GetResource(this.OPTOUT_NS+"hostname");                                            
  38.         }        
  39.         
  40.         if (!this.SiteList) {
  41.             this.SiteList = new Array();    
  42.         }
  43.                                         
  44.         if (!this.RDFLoadObserver) {
  45.             this.debug("Instantiating observer");
  46.             this.RDFLoadObserver = {
  47.                 onBeginLoad : function(sink){},
  48.                 onInterrupt : function(sink){},
  49.                 onResume : function(sink){},
  50.                 onError : function(sink,status,msg){},
  51.                 onEndLoad : function(sink) {
  52.                     RDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  53.                                            .getService(Components.interfaces.nsIRDFService);
  54.                     this.debug("onEndLoad()");
  55.                     sink.removeXMLSinkObserver(this);
  56.                     sink.QueryInterface(Components.interfaces.nsIRDFDataSource);
  57.                     this.debug('Loaded datasource: '+sink.URI);
  58.                     resources = sink.GetAllResources();
  59.                     while (resources.hasMoreElements()) {
  60.                         res = resources.getNext();
  61.                         res.QueryInterface(Components.interfaces.nsIRDFResource);
  62.                         this.parent.HandleResource(sink, res);
  63.                         RDFService.UnregisterResource(res);
  64.                     }
  65.                     RDFService.UnregisterDataSource(sink);
  66.                     // Now update from the server
  67.                     this.parent.RefreshRemoteComponents();
  68.                 },
  69.                 debug : function(msg) {
  70.                     this.parent.debug('RDFLoadObserver: '+msg);
  71.                 },
  72.  
  73.                 QueryInterface: function(aIID)
  74.                 { if (!aIID.equals(Components.interfaces.nsIObserver) &&
  75.                         !aIID.equals(Components.interfaces.nsISupports) &&
  76.                         !aIID.equals(Components.interfaces.nsIRDFXMLSinkObserver))
  77.                       throw Components.results.NS_ERROR_NO_INTERFACE;
  78.                     return this;
  79.                 }
  80.             };
  81.             this.RDFLoadObserver.parent = this;
  82.         }
  83.         
  84.         if (!this.RemoteRDFLoadObserver) {
  85.             this.debug("Instantiating other observer");
  86.             this.RemoteRDFLoadObserver = {
  87.                 bNewComponents : false,
  88.                 newList : null,
  89.                 onBeginLoad : function(sink){},
  90.                 onInterrupt : function(sink){},
  91.                 onResume : function(sink){},
  92.                 onError : function(sink,status,msg){},
  93.                 onEndLoad : function(sink) {
  94.                     RDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  95.                                            .getService(Components.interfaces.nsIRDFService);
  96.                     this.debug("onEndLoad()");
  97.                     sink.removeXMLSinkObserver(this);
  98.                     this.debug('Loaded datasource: '+sink.URI);
  99.                     var fileURI = this.parent.GetLocalFile();
  100.                     sink.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  101.                     sink.FlushTo(fileURI.spec);
  102.                     RDFService.UnregisterDataSource(sink);
  103.                 },
  104.                 
  105.                 debug : function(msg) {
  106.                     this.parent.debug('RemoteRDFLoadObserver: '+msg);
  107.                 },
  108.  
  109.                 QueryInterface: function(aIID)
  110.                 { if (!aIID.equals(Components.interfaces.nsIObserver) &&
  111.                         !aIID.equals(Components.interfaces.nsISupports) &&
  112.                         !aIID.equals(Components.interfaces.nsIRDFXMLSinkObserver))
  113.                       throw Components.results.NS_ERROR_NO_INTERFACE;
  114.                     return this;
  115.                 }
  116.             };
  117.             this.RemoteRDFLoadObserver.parent = this;        
  118.         }
  119.     },    
  120.  
  121.     RefreshRemoteComponents : function() {
  122.         var gPrefService = Components.classes["@mozilla.org/preferences-service;1"]
  123.                                      .getService(Components.interfaces.nsIPrefBranch);
  124.         this.debug("RefreshRemoteComponents()");
  125.         var user_guid = gPrefService.getCharPref("browser.info.guid");
  126.         var partnerId;
  127.         try {
  128.             partnerId = gPrefService.getCharPref("browser.partnerId");
  129.         } catch (ex) {
  130.             partnerId = 'chapera';
  131.         }
  132.         var REMOTE_RDF_URL = gPrefService.getCharPref("optout.URL");
  133.                                  // + "?guid=" + user_guid + "&partnerId=" + partnerId;
  134.         try {
  135.             this.RDFDataSource = this.RDFService.GetDataSource(REMOTE_RDF_URL);
  136.             this.RDFDataSource.QueryInterface(Components.interfaces.nsIRDFXMLSink);
  137.             this.RDFDataSource.addXMLSinkObserver(this.RemoteRDFLoadObserver);
  138.         } catch (ex) { }
  139.         this.debug("Finished RefreshRemoteComponents()");
  140.     },
  141.     
  142.     /**
  143.      * Grabs a local RDF file describing what SPUI components are
  144.      * currently available, and sets listener on the loading of that
  145.      * file.
  146.      **/
  147.     RefreshSiteList : function() {
  148.         this.debug("RefreshSiteList()");
  149.         this.MyInit();
  150.  
  151.         var fileURI = this.GetLocalFile();
  152.         try {
  153.             this.RDFDataSource = this.RDFService.GetDataSource(fileURI.spec);
  154.             this.RDFDataSource.QueryInterface(Components.interfaces.nsIRDFXMLSink);
  155.             this.RDFDataSource.addXMLSinkObserver(this.RDFLoadObserver);
  156.  
  157.         } catch (ex) {  
  158.             this.RefreshRemoteComponents(); 
  159.             }
  160.         this.debug("Finished RefreshSiteList()");
  161.     },
  162.     
  163.     GetLocalFile : function() {
  164.         var profileDir = Components.classes["@mozilla.org/file/directory_service;1"].
  165.             getService(Components.interfaces.nsIProperties).
  166.             get("ProfD", Components.interfaces.nsIFile);
  167.         profileDir.append(this.OO_FILENAME);
  168.         var ios = Components.classes["@mozilla.org/network/io-service;1"]
  169.             .getService(Components.interfaces.nsIIOService);
  170.         var fileURI = ios.newFileURI(profileDir);
  171.         return fileURI;
  172.     },
  173.     
  174.     
  175.     /**
  176.      * Parse the description for a particular resource
  177.      **/
  178.     HandleResource : function(datasource, res) {
  179.         this.debug('Found resource: '+res.Value);
  180.         var labels = datasource.ArcLabelsOut(res);
  181.         while (labels.hasMoreElements()) {
  182.             var label = labels.getNext();
  183.             if (label instanceof Components.interfaces.nsIRDFResource) {
  184.                 var dbg = '* '+label.Value;
  185.                 var target = datasource.GetTarget(res, label, true);
  186.                 if (target instanceof Components.interfaces.nsIRDFResource) {
  187.                     dbg += ' => '+target.Value;
  188.                 } else if (target instanceof Components.interfaces.nsIRDFLiteral) {
  189.                     dbg += ' => '+target.Value;
  190.                 } else {
  191.                     try {
  192.                         dbg += ' => '+target.Value;
  193.                     } catch (ex) { }
  194.                 }
  195.                 this.debug(dbg);
  196.             }
  197.         }
  198.         // See if this resource specifies an actual SPUI component
  199.         if (datasource.hasArcOut(res, this.RES_HOSTNAME))
  200.         {
  201.             this.debug('^** this is an optout site! **^');
  202.                 
  203.             var siteHostName = datasource.GetTarget(res, this.RES_HOSTNAME, true);
  204.             siteHostName.QueryInterface(Components.interfaces.nsIRDFLiteral);
  205.             this.SiteList.push(siteHostName.Value);
  206.         }
  207.     },
  208.     
  209.     isInList: function (aSiteName) {
  210.         this.debug("IsInList()");
  211.         this.MyInit();
  212.  
  213.         if (!this.SiteList) return false;
  214.         for (var i=0; i< this.SiteList.length; i++) {
  215.             this.debug("Checking against site : " + this.SiteList[i] + " \n");
  216.             if (this.siteMatch(this.SiteList[i],aSiteName))
  217.                 return true;    
  218.         }        
  219.         return false;
  220.     },
  221.     
  222.     siteMatch: function (aSiteName, bSiteName) {
  223.         if (bSiteName.indexOf(aSiteName) > -1)
  224.             return true;
  225.         return false;
  226.     },
  227.     
  228.     debug : function (aStr) {        
  229.         var gPrefService =
  230.             Components.classes["@mozilla.org/preferences-service;1"]
  231.                       .getService(Components.interfaces.nsIPrefBranch);
  232.         if (gPrefService.getPrefType("optout.debug") &&
  233.             gPrefService.getBoolPref("optout.debug"))
  234.         {
  235.             dump("OPTOUT: " + aStr + "\n");     
  236.         }        
  237.     },
  238.  
  239.     QueryInterface: function(iid) {
  240.         if (!iid.equals(Components.interfaces.nsISupports) &&
  241.                 !iid.equals(OPTOUT_IID))
  242.             throw Components.results.NS_ERROR_NO_INTERFACE;
  243.         return this;
  244.     }
  245. };
  246.  
  247. /*
  248.  * Objects
  249.  */
  250.  
  251. /* nsOptOut Module (for XPCOM registration) */
  252. var nsOptOutModule = {
  253.     registerSelf: function(compMgr, fileSpec, location, type) {
  254.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  255.         compMgr.registerFactoryLocation(OPTOUT_CID, 
  256.                                                                         "nsOptOut JS component", 
  257.                                                                         OPTOUT_CONTRACTID, 
  258.                                                                         fileSpec, 
  259.                                                                         location,
  260.                                                                         type);
  261.     },
  262.  
  263.     getClassObject: function(compMgr, cid, iid) {
  264.         if (!cid.equals(OPTOUT_CID))
  265.             throw Components.results.NS_ERROR_NO_INTERFACE;
  266.  
  267.         if (!iid.equals(Components.interfaces.nsIFactory))
  268.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  269.  
  270.         return nsOptOutFactory;
  271.     },
  272.  
  273.     canUnload: function(compMgr) {
  274.         return true;
  275.     }
  276. };
  277.  
  278. /* nsOptOut Class Factory */
  279. var nsOptOutFactory = {
  280.     createInstance: function(outer, iid) {
  281.         if (outer != null)
  282.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  283.         
  284.         if (!iid.equals(OPTOUT_IID) &&
  285.                 !iid.equals(Components.interfaces.nsISupports))
  286.                 throw Components.results.NS_ERROR_INVALID_ARG;
  287.  
  288.         return new nsOptOut();
  289.     }
  290. }
  291.  
  292. /*
  293.  * Functions
  294.  */
  295.  
  296. /* module initialisation */
  297. function NSGetModule(comMgr, fileSpec) {
  298.     return nsOptOutModule;
  299. }
  300.