home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Multimedia / Songbird / Songbird_1.8.0-1800_windows-i686-msvc8.exe / components / sbLocalDatabaseMigrationHelper.js < prev    next >
Text File  |  2010-08-30  |  8KB  |  265 lines

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2010 POTI, Inc.
  7.  * http://www.songbirdnest.com
  8.  *
  9.  * This file may be licensed under the terms of of the
  10.  * GNU General Public License Version 2 (the ``GPL'').
  11.  *
  12.  * Software distributed under the License is distributed
  13.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  14.  * express or implied. See the GPL for the specific language
  15.  * governing rights and limitations.
  16.  *
  17.  * You should have received a copy of the GPL along with this
  18.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  19.  * or write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  *
  22.  *=END SONGBIRD GPL
  23.  */
  24.  
  25. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  26. Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
  27. Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
  28. Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
  29. Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
  30.  
  31. const Cc = Components.classes;
  32. const Ci = Components.interfaces;
  33. const Cr = Components.results;
  34.  
  35. const SBLDBCOMP = "@songbirdnest.com/Songbird/Library/LocalDatabase/";
  36.  
  37.  
  38. function sbLocalDatabaseMigrationHelper()
  39. {
  40.   SBJobUtils.JobBase.call(this);
  41.   this._migrationHandlers = {};
  42. }
  43.  
  44. //-----------------------------------------------------------------------------
  45. // sbLocalDatabaseMigration Implementation
  46. //-----------------------------------------------------------------------------
  47.  
  48. sbLocalDatabaseMigrationHelper.prototype = {
  49.   __proto__: SBJobUtils.JobBase.prototype,
  50.   
  51.   classDescription: "Songbird Local Database Library Migration",
  52.   classID:          Components.ID("{744f6217-4cb9-4929-8d1d-72492c1b8c83}"),
  53.   contractID:       SBLDBCOMP + "MigrationHelper;1",
  54.   implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
  55.  
  56.   // needs to be DOM_OBJECT to allow remoteAPI to access it.
  57.   flags: Ci.nsIClassInfo.DOM_OBJECT,
  58.  
  59.   _initialized: false,
  60.   
  61.   _securityMixin: null,
  62.   _initializedSCC: false,
  63.   
  64.   _publicWProps: [ "" ],
  65.   _publicRProps: [ "classinfo:classDescription",
  66.                    "classinfo:contractID",
  67.                    "classinfo:classID",
  68.                    "classinfo:implementationLanguage",
  69.                    "classinfo:flags" ],
  70.   _publicMethods: [ "internal:canMigrate",
  71.                     "internal:getLatestSchemaVersion",
  72.                     "internal:migrate"],
  73.   _publicInterfaces: [ Ci.nsISupports,
  74.                        Ci.nsIClassInfo,
  75.                        Ci.nsISecurityCheckedComponent,
  76.                        Ci.sbILocalDatabaseMigrationHelper,
  77.                        Ci.sbIJobProgress,
  78.                        Ci.sbIJobCancelable ],
  79.   
  80.   _latestSchemaVersion: 24,
  81.   _lowestFromSchemaVersion: Number.MAX_VALUE,
  82.   
  83.   _migrationHandlers:   null,
  84.  
  85.   //
  86.   // Local methods
  87.   //
  88.  
  89.   _init: function sbLDBM_init() {
  90.     this._getMigrationHandlerList();
  91.     this._initialized = true;
  92.   },
  93.  
  94.   _getMigrationHandlerList: function sbLDBM_getMigrationHandlerList() {
  95.     this._migrationHandlers = {};
  96.     
  97.     for(let contractID in Cc) {
  98.       if(contractID.indexOf(SBLDBCOMP + "Migration/Handler") == 0) {
  99.  
  100.         let migrationHandler = 
  101.           Cc[contractID].createInstance(Ci.sbILocalDatabaseMigrationHandler);
  102.         let migrationHandlerKey = migrationHandler.fromVersion + 
  103.                                   "," + 
  104.                                   migrationHandler.toVersion;
  105.         this._migrationHandlers[migrationHandlerKey] = migrationHandler;
  106.         
  107.         if(this._lowestFromSchemaVersion > migrationHandler.fromVersion) {
  108.           this._lowestFromSchemaVersion = migrationHandler.fromVersion;
  109.         }
  110.       }
  111.     }
  112.   },
  113.  
  114.   // 
  115.   // sbILocalDatabaseMigration
  116.   // 
  117.  
  118.   getLatestSchemaVersion: function sbLDBM_getLatestSchemaVersion() {
  119.     return this._latestSchemaVersion;
  120.   },
  121.  
  122.   canMigrate: function sbLDBM_canMigrate(aFromVersion, aToVersion) {
  123.     if(!this._initialized) {
  124.       this._init();
  125.     }
  126.     
  127.     var oldVersion = aFromVersion;
  128.     var newVersion = oldVersion + 1;
  129.     
  130.     for(let i = 0; i < aToVersion - aFromVersion; ++i) {
  131.       let key = oldVersion + "," + newVersion;
  132.       
  133.       if(!(key in this._migrationHandlers)) {
  134.         return false;
  135.       }
  136.       
  137.       oldVersion = newVersion;
  138.       newVersion += 1;
  139.     }
  140.     
  141.     return true;
  142.   },
  143.  
  144.   migrate: function sbLDBM_migrate(aFromVersion, aToVersion, aLibrary) {
  145.     if(!this._initialized) {
  146.       this._init();
  147.     }
  148.     
  149.     var oldVersion = aFromVersion;
  150.     var newVersion = oldVersion + 1;
  151.     
  152.     for(let i = 0; i < aToVersion - aFromVersion; ++i) {
  153.       let key = oldVersion + "," + newVersion;
  154.       
  155.       if(!(key in this._migrationHandlers)) {
  156.         dump("Failed to find migration handler " + key + "\n");
  157.         throw Cr.NS_ERROR_UNEXPECTED;
  158.       }
  159.       try {
  160.         this._migrationHandlers[key].migrate(aLibrary);
  161.       } catch (e) {
  162.         dump("Error in migrating " + oldVersion + " to " + newVersion + ":\n" +
  163.              e + "\n");
  164.         throw(e);
  165.       }
  166.       
  167.       oldVersion = newVersion;
  168.       newVersion += 1;
  169.     }
  170.     
  171.     return;
  172.   },
  173.  
  174.  
  175.   //
  176.   // nsISupports
  177.   //
  178.  
  179.   QueryInterface: XPCOMUtils.generateQI([
  180.     Ci.sbILocalDatabaseMigrationHelper,
  181.     Ci.sbIJobProgress,
  182.     Ci.sbIJobCancelable,
  183.     Ci.nsIClassInfo,
  184.     Ci.nsISecurityCheckedComponent,
  185.     Ci.sbISecurityAggregator
  186.   ]),
  187.  
  188.  
  189.   // 
  190.   // nsIClassInfo
  191.   //
  192.  
  193.   getInterfaces: function sbLDBM_getInterfaces(count) {
  194.     var interfaces = [Ci.sbILocalDatabaseMigrationHelper, 
  195.                       Ci.nsIClassInfo,
  196.                       Ci.sbIJobProgress,
  197.                       Ci.sbIJobCancelable,
  198.                       Ci.nsISecurityCheckedComponent,
  199.                       Ci.sbISecurityAggregator,
  200.                       Ci.nsISupports];
  201.     
  202.     count.value = interfaces.length;
  203.     
  204.     return interfaces;
  205.   },
  206.  
  207.   getHelperForLanguage: function sbLDBM_getHelperForLanguage(aLanguage) {
  208.     return null;
  209.   },
  210.  
  211.  
  212.   //
  213.   // nsISecurityCheckedComponent -- implemented by the security mixin
  214.   //
  215.  
  216.   _initSCC: function sbLDBM__initSCC() {
  217.     this._securityMixin = Cc["@songbirdnest.com/remoteapi/security-mixin;1"]
  218.                             .createInstance(Ci.nsISecurityCheckedComponent);
  219.  
  220.     // initialize the security mixin with the cleared methods and props
  221.     this._securityMixin
  222.              .init(this, this._publicInterfaces, this._publicInterfaces.length,
  223.                          this._publicMethods, this._publicMethods.length,
  224.                          this._publicRProps, this._publicRProps.length,
  225.                          this._publicWProps, this._publicWProps.length,
  226.                          false);
  227.  
  228.     this._initializedSCC = true;
  229.   },
  230.  
  231.   canCreateWrapper: function sbLDBM_canCreateWrapper(iid) {
  232.     if (! this._initializedSCC)
  233.       this._initSCC();
  234.     return this._securityMixin.canCreateWrapper(iid);
  235.   },
  236.  
  237.   canCallMethod: function sbLDBM_canCallMethod(iid, methodName) {
  238.     if (! this._initializedSCC)
  239.       this._initSCC();
  240.     return this._securityMixin.canCallMethod(iid, methodName);
  241.   },
  242.  
  243.   canGetProperty: function sbLDBM_canGetProperty(iid, propertyName) {
  244.     if (! this._initializedSCC)
  245.       this._initSCC();
  246.     return this._securityMixin.canGetProperty(iid, propertyName);
  247.   },
  248.  
  249.   canSetProperty: function sbLDBM_canSetProperty(iid, propertyName) {
  250.     if (! this._initializedSCC)
  251.       this._initSCC();
  252.     return this._securityMixin.canSetProperty(iid, propertyName);
  253.   }
  254. };
  255.  
  256. //
  257. // Module
  258. //
  259.  
  260. function NSGetModule(compMgr, fileSpec) {
  261.   return XPCOMUtils.generateModule([
  262.     sbLocalDatabaseMigrationHelper
  263.   ]);
  264. }
  265.