home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbBundle.js < prev    next >
Text File  |  2012-06-08  |  13KB  |  395 lines

  1. /**
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  27.  
  28. const SONGBIRD_BUNDLE_IID = Components.interfaces.sbIBundle;
  29.  
  30. const Cc = Components.classes;
  31. const Ci = Components.interfaces;
  32. const Cr = Components.results
  33. const Cu = Components.utils
  34.  
  35. function Bundle() {
  36.   this._datalisteners = new Array();
  37.   this._installlisteners = new Array();
  38.  
  39.   var obs = Components.classes["@mozilla.org/observer-service;1"]
  40.                       .getService(Components.interfaces.nsIObserverService);
  41.   obs.addObserver(this, "quit-application", false);
  42. }
  43.  
  44. Bundle.prototype.constructor = Bundle;
  45.  
  46. Bundle.prototype = {
  47.   classDescription: "Songbird Bundle Service Interface",
  48.   classID:          Components.ID("{ff29ec35-1294-42ae-a341-63d0303df969}"),
  49.   contractID:       "@songbirdnest.com/Songbird/Bundle;1",
  50.  
  51.   _bundleid: null,
  52.   _bundleURL: null,
  53.   _req: null,
  54.   _datalisteners: null,
  55.   _installlisteners: null,
  56.   _status: 0,
  57.   _extlist: null,
  58.   _browser: null,
  59.   _downloadListener: null,
  60.   _url: null,
  61.   _file: null,
  62.   _filename: null,
  63.   _needrestart: false,
  64.   _bundleversion: 0,
  65.   _simulate_lots_of_entries: false,
  66.   _init: false,
  67.   _onload: null,
  68.   _onerror: null,
  69.   _installresult: -1,
  70.   _timer: null,
  71.  
  72.   LOG: function(str) {
  73.     var consoleService = Components.classes['@mozilla.org/consoleservice;1']
  74.                             .getService(Components.interfaces.nsIConsoleService);
  75.     consoleService.logStringMessage(str);
  76.   },
  77.   
  78.   
  79.   get bundleId() {
  80.     return this._bundleid;
  81.   },
  82.  
  83.   set bundleId(aStringValue) {
  84.     // Make sure there is a string object to pass.
  85.     if (aStringValue == null)
  86.       aStringValue = "";
  87.     this._bundleid = aStringValue;
  88.   },
  89.  
  90.   set bundleURL(aBundleURL) {
  91.     // validate
  92.     if (aBundleURL == null)
  93.       aBundleURL = "";
  94.     this._bundleURL = aBundleURL;
  95.   },
  96.   
  97.   get bundleURL() {
  98.     return this._bundleURL;
  99.   },
  100.  
  101.   retrieveBundleData: function(aTimeout) {
  102.     // Get the bundle URL.
  103.     var url = this.formatBundleURL();
  104.     url += this._getRandomParameter();
  105.  
  106.     // Retrieve the bundle.
  107.     this.retrieveBundleDataCommon(url, true);
  108.  
  109.     // If specified, set up a callback to enforce request timeout
  110.     if (aTimeout > 0) {
  111.       this._timer = Components.classes["@mozilla.org/timer;1"]
  112.                               .createInstance(Components.interfaces.nsITimer);
  113.       this._timer.initWithCallback(this, aTimeout,
  114.                                    Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  115.     }
  116.   },
  117.  
  118.   retrieveLocalBundleData: function() {
  119.     this.retrieveBundleDataCommon(this._bundleURL, false);
  120.   },
  121.  
  122.   retrieveBundleDataCommon: function(aBundleURL, aAsync) {
  123.     if (this._init && this._req) {
  124.       this._req.abort();
  125.       var httpReq = this._req.QueryInterface(Components.interfaces.nsIJSXMLHttpRequest);
  126.       httpReq.removeEventListener("load", this._onload, false);
  127.       httpReq.removeEventListener("error", this._onerror, false);
  128.       this._req = null;
  129.       this._status = SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_DOWNLOADING;
  130.     }
  131.  
  132.     this._onload = {
  133.       _that: null,
  134.       handleEvent: function( event ) { this._that.onLoad(); }
  135.     }; this._onload._that = this;
  136.  
  137.     this._onerror = {
  138.       _that: null,
  139.       handleEvent: function( event ) { this._that.onError(); }
  140.     }; this._onerror._that = this;
  141.  
  142.     this._req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
  143.                           .createInstance(Components.interfaces.nsIXMLHttpRequest);
  144.     if (aAsync)
  145.       this._req.mozBackgroundRequest = true;
  146.     var httpReq = this._req.QueryInterface(Components.interfaces.nsIJSXMLHttpRequest);
  147.     httpReq.addEventListener("load", this._onload, false);
  148.     httpReq.addEventListener("error", this._onerror, false);
  149.  
  150.     // Get the bundle URL.
  151.     var url = this.formatBundleURL();
  152.  
  153.     this._req.open('GET', aBundleURL, aAsync);
  154.     this._req.send(null);
  155.     this._init = true;
  156.   },
  157.  
  158.   formatBundleURL: function() {
  159.     // XXXredfive - this will(may) change to the mozilla urlformatter when
  160.     //  bmo 430235 gets fixed.
  161.     // use the urlFormatter service to replace the %FOO% mumbo-jumbo
  162.     var urlFormatter = Cc["@songbirdnest.com/moz/sburlformatter;1"]
  163.                          .getService(Ci.sbIURLFormatter);
  164.     var pbag = Cc["@mozilla.org/hash-property-bag;1"]
  165.                  .createInstance(Ci.nsIWritablePropertyBag2);
  166.     var url = urlFormatter.formatURL(this._bundleURL, pbag);
  167.  
  168.     return url;
  169.   },
  170.  
  171.   get bundleDataDocument() {
  172.     return this._req ? this._req.responseXML : null;
  173.   },
  174.  
  175.   get bundleDataText() {
  176.     return this._req ? this._req.responseText : "";
  177.   },
  178.   
  179.   addBundleDataListener: function(aListener) {
  180.     this._datalisteners.push(aListener);
  181.   },
  182.   
  183.   removeBundleDataListener: function (aListener) {
  184.     var r = this.getDataListenerIndex(aListener);
  185.     if (r != -1) this._datalisteners.splice(r, 1);
  186.   },
  187.   
  188.   getNumDataListeners: function() {
  189.     return this._datalisteners.length;
  190.   },
  191.   
  192.   getDataListener: function(aIndex) {
  193.     return this._datalisteners[aIndex];
  194.   },
  195.  
  196.   getDataListenerIndex: function(aListener) {
  197.     return this._datalisteners.indexOf(aListener);
  198.   },
  199.  
  200.   addBundleInstallListener: function(aListener) {
  201.     this._installlisteners.push(aListener);
  202.   },
  203.   
  204.   removeBundleInstallListener: function (aListener) {
  205.     var r = this.getInstallListenerIndex(aListener);
  206.     if (r != -1) this._installlisteners.splice(r, 1);
  207.   },
  208.   
  209.   get installListenerCount() {
  210.     return this._installlisteners.length;
  211.   },
  212.   
  213.   getInstallListener: function(aIndex) {
  214.     return this._installlisteners[aIndex];
  215.   },
  216.   
  217.   getInstallaListenerIndex: function(aListener) {
  218.     for (var i = 0; i < this._installlisteners.length; i++) if (this._datalisteners[i] == aListener) return i;
  219.     return -1;
  220.   },
  221.   
  222.   get bundleDataStatus() {
  223.     return this._status;
  224.   },
  225.   
  226.   onLoad: function() {
  227.     this._status = SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS;
  228.     this.getExtensionList();
  229.     for (var i = 0; i < this._datalisteners.length; i++) this._datalisteners[i].onDownloadComplete(this);
  230.   },
  231.  
  232.   onError: function() {
  233.     this._status = SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_ERROR;
  234.     for (var i = 0; i < this._datalisteners.length; i++) this._datalisteners[i].onError(this);
  235.   },
  236.   
  237.   getDataNodes: function(bundledocument) {
  238.     if (!bundledocument) return null;
  239.     var datablocknodes = bundledocument.childNodes;
  240.     
  241.     for (var i = 0; i < datablocknodes.length; i++) {
  242.       if (datablocknodes[i].nodeType == Ci.nsIDOMNode.ELEMENT_NODE &&
  243.           datablocknodes[i].tagName == "SongbirdInstallBundle") {
  244.         this._bundleversion = datablocknodes[i].getAttribute("version")
  245. /*
  246.         // Sample code to generate some elements for testing.
  247.         for ( var j = 0; j < 10; j++ ) {
  248.           var testElement = bundledocument.createElement("XPI");
  249.           testElement.setAttribute("name", "TEST ELEMENT #" + (j+1) );
  250.           testElement.setAttribute("url", "");
  251.           datablocknodes[i].appendChild( testElement );
  252.         }
  253. */
  254.         return datablocknodes[i].childNodes;
  255.       }
  256.     }
  257.     return null;
  258.   },
  259.  
  260.   installFlaggedExtensions: function(aWindow) {
  261.     var windowWatcherService = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
  262.                             .getService(Components.interfaces.nsIWindowWatcher);
  263.                             
  264.     this._installresult = Components.interfaces.sbIBundle.BUNDLE_INSTALL_ERROR;
  265.     windowWatcherService.openWindow(aWindow,
  266.                                     "chrome://songbird/content/xul/setupProgress.xul",
  267.                                     "_blank",
  268.                                     "chrome,dialog=yes,centerscreen,alwaysRaised,close=no,modal",
  269.                                     this);
  270.     return this._installresult;
  271.   },
  272.   
  273.   setInstallResult: function(aResult) {
  274.     this._installresult = aResult;
  275.   },
  276.   
  277.   getExtensionList: function() {
  278.     this._extlist = new Array();
  279.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS) {
  280.       var bundledocument = this.bundleDataDocument;
  281.       if (bundledocument) {
  282.         var nodes = this.getDataNodes(bundledocument);
  283.         if (nodes) {
  284.           for (var i = 0; i < nodes.length; i++) {
  285.             if (nodes[i].nodeType == Ci.nsIDOMNode.ELEMENT_NODE &&
  286.                 nodes[i].tagName == "XPI") {
  287.               var inst = nodes[i].getAttribute("default");
  288.               this._extlist.push(Array(nodes[i], (inst=="true" || inst=="1")));
  289.             }
  290.           }
  291.         }
  292.       }
  293.     }
  294.   },
  295.   
  296.   get bundleExtensionCount() {
  297.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS) {
  298.       if (this._simulate_lots_of_entries) return this._extlist.length * 20;
  299.       return this._extlist.length;
  300.     }
  301.     return 0;
  302.   },
  303.  
  304.   removeExtension: function(aIndex) {
  305.     if (!this._extlist) return;
  306.     if (this._extlist.length != 0 && this._simulate_lots_of_entries)
  307.       aIndex = aIndex % this._extlist.length;
  308.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS && aIndex < this.bundleExtensionCount)
  309.       this._extlist.splice(aIndex, 1);
  310.   },
  311.  
  312.   getExtensionAttribute: function(aIndex, aAttributeName) {
  313.     if (!this._extlist) return "";
  314.     if (this._extlist.length != 0 && this._simulate_lots_of_entries) 
  315.       aIndex = aIndex % this._extlist.length;
  316.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS && aIndex < this.bundleExtensionCount) 
  317.       return this._extlist[aIndex][0].getAttribute(aAttributeName);
  318.     return "";
  319.   },
  320.       
  321.   getExtensionInstallFlag: function(aIndex) {
  322.     if (!this._extlist) return false;
  323.     if (this._extlist.length != 0 && this._simulate_lots_of_entries) 
  324.       aIndex = aIndex % this._extlist.length;
  325.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS && aIndex < this.bundleExtensionCount) 
  326.       return this._extlist[aIndex][1];
  327.     return false;
  328.   },
  329.   
  330.   setExtensionInstallFlag: function(aIndex, aInstallFlag) {
  331.     if (!this._extlist) return;
  332.     if (this._extlist.length != 0 && this._simulate_lots_of_entries) 
  333.       aIndex = aIndex % this._extlist.length;
  334.     if (this._status == SONGBIRD_BUNDLE_IID.BUNDLE_DATA_STATUS_SUCCESS && aIndex < this.bundleExtensionCount) 
  335.       this._extlist[aIndex][1] = aInstallFlag;
  336.   },
  337.   
  338.   _getRandomParameter: function() {
  339.     var aUUIDGenerator = (Components.classes["@mozilla.org/uuid-generator;1"]).createInstance();
  340.     aUUIDGenerator = aUUIDGenerator.QueryInterface(Components.interfaces.nsIUUIDGenerator);
  341.     var aUUID = aUUIDGenerator.generateUUID();
  342.     return "?randomguid=" + escape(aUUID);
  343.   },
  344.   
  345.   setNeedRestart: function(aRequired) {
  346.     this._needrestart = aRequired;
  347.   },
  348.  
  349.   get restartRequired() {
  350.     return this._needrestart;
  351.   },
  352.  
  353.   get bundleDataVersion() {
  354.     return this._bundleversion;
  355.   },
  356.   
  357.   // nsITimerCallback
  358.   notify: function(timer)
  359.   {
  360.     if(this._req.readyState != 4) { // 4 = COMPLETED
  361.       // abort() stops the http request so the normal event listeners are never
  362.       // called so we need to call onError() manually.
  363.       this._req.abort();
  364.       this.onError();
  365.     }
  366.     this._timer.cancel();
  367.     this._timer = null;
  368.   },
  369.  
  370.   // nsIObserver
  371.   observe: function(aSubject, aTopic, aData) {
  372.     if (aTopic == "quit-application") {
  373.       if (this._timer) {
  374.         this._timer.cancel();
  375.         this._timer = null;
  376.       }
  377.     }
  378.   },
  379.  
  380.   /**
  381.    * See nsISupports.idl
  382.    */
  383.   QueryInterface:
  384.     XPCOMUtils.generateQI([SONGBIRD_BUNDLE_IID,
  385.                            Components.interfaces.sbPIBundle,
  386.                            Components.interfaces.nsIWebProgressListener,
  387.                            Components.interfaces.nsISupportsWeakReference,
  388.                            Components.interfaces.nsIObserver])
  389. }; // Bundle.prototype
  390.  
  391. function NSGetModule(compMgr, fileSpec) {
  392.   return XPCOMUtils.generateModule([Bundle]);
  393. }
  394.  
  395.