home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / air / update / core / UpdaterHSM.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  19.6 KB  |  538 lines

  1. package air.update.core
  2. {
  3.    import air.update.descriptors.ApplicationDescriptor;
  4.    import air.update.descriptors.UpdateDescriptor;
  5.    import air.update.events.DownloadErrorEvent;
  6.    import air.update.events.StatusFileUpdateErrorEvent;
  7.    import air.update.events.StatusFileUpdateEvent;
  8.    import air.update.events.StatusUpdateErrorEvent;
  9.    import air.update.events.StatusUpdateEvent;
  10.    import air.update.events.UpdateEvent;
  11.    import air.update.logging.Logger;
  12.    import air.update.net.FileDownloader;
  13.    import air.update.states.HSM;
  14.    import air.update.states.HSMEvent;
  15.    import air.update.states.UpdateState;
  16.    import air.update.utils.Constants;
  17.    import air.update.utils.FileUtils;
  18.    import air.update.utils.VersionUtils;
  19.    import flash.events.ErrorEvent;
  20.    import flash.events.Event;
  21.    import flash.events.IOErrorEvent;
  22.    import flash.events.ProgressEvent;
  23.    import flash.filesystem.File;
  24.    import flash.net.URLRequest;
  25.    
  26.    public class UpdaterHSM extends HSM
  27.    {
  28.       private static var logger:Logger = Logger.getLogger("air.update.core.UpdaterHSM");
  29.       
  30.       public static const EVENT_CHECK:String = "updater.check";
  31.       
  32.       public static const EVENT_DOWNLOAD:String = "updater.download";
  33.       
  34.       public static const EVENT_INSTALL:String = "updater.install";
  35.       
  36.       public static const EVENT_INSTALL_TRIGGER:String = "install.trigger";
  37.       
  38.       public static const EVENT_FILE_INSTALL_TRIGGER:String = "file_install.trigger";
  39.       
  40.       public static const EVENT_STATE_CLEAR_TRIGGER:String = "state_clear.trigger";
  41.       
  42.       public static const EVENT_ASYNC:String = "check.async";
  43.       
  44.       public static const EVENT_FILE:String = "check.file";
  45.       
  46.       public static const EVENT_VERIFIED:String = "check.verified";
  47.       
  48.       private var descriptorURL:URLRequest;
  49.       
  50.       private var unpackager:AIRUnpackager;
  51.       
  52.       private var descriptorFile:File;
  53.       
  54.       private var _descriptor:UpdateDescriptor;
  55.       
  56.       private var requestedFile:File;
  57.       
  58.       private var requestedURL:String;
  59.       
  60.       private var downloader:FileDownloader;
  61.       
  62.       private var updateFile:File;
  63.       
  64.       private var updateURL:URLRequest;
  65.       
  66.       private var _applicationDescriptor:ApplicationDescriptor;
  67.       
  68.       private var _configuration:UpdaterConfiguration;
  69.       
  70.       public function UpdaterHSM()
  71.       {
  72.          super(this.stateReady);
  73.       }
  74.       
  75.       protected function stateInitialized(param1:Event) : void
  76.       {
  77.          logger.finest("stateInitialized: " + param1.type);
  78.          switch(param1.type)
  79.          {
  80.             case HSMEvent.ENTER:
  81.                transition(this.stateReady);
  82.          }
  83.       }
  84.       
  85.       protected function stateDownloading(param1:Event) : void
  86.       {
  87.          if(param1.type != ProgressEvent.PROGRESS)
  88.          {
  89.             logger.finest("stateDownloading: " + param1.type);
  90.          }
  91.          switch(param1.type)
  92.          {
  93.             case HSMEvent.ENTER:
  94.                this.downloader = new FileDownloader(this.updateURL,this.updateFile);
  95.                this.downloader.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR,dispatch);
  96.                this.downloader.addEventListener(UpdateEvent.DOWNLOAD_START,dispatch);
  97.                this.downloader.addEventListener(ProgressEvent.PROGRESS,dispatch);
  98.                this.downloader.addEventListener(UpdateEvent.DOWNLOAD_COMPLETE,dispatch);
  99.                this.downloader.download();
  100.                break;
  101.             case UpdateEvent.DOWNLOAD_START:
  102.                dispatchEvent(param1.clone());
  103.                break;
  104.             case ProgressEvent.PROGRESS:
  105.                dispatchEvent(param1.clone());
  106.                break;
  107.             case DownloadErrorEvent.DOWNLOAD_ERROR:
  108.                dispatchEvent(param1.clone());
  109.                transition(this.stateErrored);
  110.                break;
  111.             case UpdateEvent.DOWNLOAD_COMPLETE:
  112.                this.downloader = null;
  113.                transition(this.stateDownloaded);
  114.          }
  115.       }
  116.       
  117.       public function getUpdateState() : int
  118.       {
  119.          var _loc1_:int = -1;
  120.          switch(stateHSM)
  121.          {
  122.             case this.stateInitialized:
  123.                _loc1_ = UpdateState.READY;
  124.                break;
  125.             case this.stateBeforeChecking:
  126.                _loc1_ = UpdateState.BEFORE_CHECKING;
  127.                break;
  128.             case this.stateChecking:
  129.                _loc1_ = UpdateState.CHECKING;
  130.                break;
  131.             case this.stateAvailable:
  132.             case this.stateAvailableFile:
  133.                _loc1_ = UpdateState.AVAILABLE;
  134.                break;
  135.             case this.stateDownloading:
  136.                _loc1_ = UpdateState.DOWNLOADING;
  137.                break;
  138.             case this.stateDownloaded:
  139.                _loc1_ = UpdateState.DOWNLOADED;
  140.                break;
  141.             case this.stateInstalling:
  142.                _loc1_ = UpdateState.INSTALLING;
  143.                break;
  144.             case this.statePendingInstall:
  145.                _loc1_ = UpdateState.PENDING_INSTALLING;
  146.                break;
  147.             case this.stateReady:
  148.                _loc1_ = UpdateState.READY;
  149.          }
  150.          return _loc1_;
  151.       }
  152.       
  153.       private function fileUnpackaged() : void
  154.       {
  155.          var xml:XML = null;
  156.          logger.finer("Parsing file descriptor");
  157.          try
  158.          {
  159.             xml = this.unpackager.descriptorXML;
  160.             this._applicationDescriptor = new ApplicationDescriptor(xml);
  161.             this._applicationDescriptor.validate();
  162.             if(VersionUtils.getApplicationID() != this._applicationDescriptor.id)
  163.             {
  164.                throw new Error("Different applicationID",Constants.ERROR_DIFFERENT_APPLICATION_ID);
  165.             }
  166.             if(!this.isNewerVersion(VersionUtils.getApplicationVersion(),this._applicationDescriptor.version))
  167.             {
  168.                dispatchEvent(new StatusFileUpdateEvent(StatusFileUpdateEvent.FILE_UPDATE_STATUS,false,false,false,this._applicationDescriptor.version,this.requestedFile.nativePath));
  169.                transition(this.stateReady);
  170.                return;
  171.             }
  172.             transition(this.stateAvailableFile);
  173.          }
  174.          catch(e:Error)
  175.          {
  176.             logger.fine("Error validating file descriptor: " + e);
  177.             dispatchEvent(new StatusFileUpdateErrorEvent(StatusFileUpdateErrorEvent.FILE_UPDATE_ERROR,false,false,e.message,e.errorID));
  178.             transition(stateErrored);
  179.          }
  180.       }
  181.       
  182.       private function isNewerVersion(param1:String, param2:String) : Boolean
  183.       {
  184.          if(this.configuration)
  185.          {
  186.             return this.configuration.isNewerVersionFunction(param1,param2);
  187.          }
  188.          return VersionUtils.isNewerVersion(param1,param2);
  189.       }
  190.       
  191.       protected function stateDownloaded(param1:Event) : void
  192.       {
  193.          var descriptor:ApplicationDescriptor = null;
  194.          var event:Event = param1;
  195.          logger.finest("stateDownloaded: " + event.type);
  196.          switch(event.type)
  197.          {
  198.             case HSMEvent.ENTER:
  199.                this.unpackager = new AIRUnpackager();
  200.                this.unpackager.addEventListener(Event.COMPLETE,dispatch);
  201.                this.unpackager.addEventListener(ErrorEvent.ERROR,dispatch);
  202.                this.unpackager.addEventListener(IOErrorEvent.IO_ERROR,dispatch);
  203.                this.unpackager.unpackageAsync(this.updateFile.url);
  204.                break;
  205.             case ErrorEvent.ERROR:
  206.             case IOErrorEvent.IO_ERROR:
  207.                this.unpackager.cancel();
  208.                this.unpackager = null;
  209.                dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,"",Constants.ERROR_AIR_UNPACKAGING,ErrorEvent(event).errorID));
  210.                transition(this.stateErrored);
  211.                break;
  212.             case Event.COMPLETE:
  213.                this.unpackager.cancel();
  214.                descriptor = new ApplicationDescriptor(this.unpackager.descriptorXML);
  215.                try
  216.                {
  217.                   descriptor.validate();
  218.                }
  219.                catch(e:Error)
  220.                {
  221.                   unpackager = null;
  222.                   dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,"",Constants.ERROR_VALIDATE,e.errorID));
  223.                   transition(stateErrored);
  224.                   return;
  225.                }
  226.                if(descriptor.id != VersionUtils.getApplicationID())
  227.                {
  228.                   dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,"Different applicationID",Constants.ERROR_VALIDATE,Constants.ERROR_DIFFERENT_APPLICATION_ID));
  229.                   transition(this.stateErrored);
  230.                   return;
  231.                }
  232.                if(this._descriptor.version != descriptor.version)
  233.                {
  234.                   dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,"Version mismatch",Constants.ERROR_VALIDATE,Constants.ERROR_VERSION_MISMATCH));
  235.                   transition(this.stateErrored);
  236.                   return;
  237.                }
  238.                if(!this.isNewerVersion(VersionUtils.getApplicationVersion(),descriptor.version))
  239.                {
  240.                   dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,"Not a newer version",Constants.ERROR_VALIDATE,Constants.ERROR_NOT_NEW_VERSION));
  241.                   transition(this.stateErrored);
  242.                   return;
  243.                }
  244.                dispatch(new Event(EVENT_VERIFIED));
  245.                break;
  246.             case EVENT_VERIFIED:
  247.                if(dispatchEvent(new UpdateEvent(UpdateEvent.DOWNLOAD_COMPLETE,false,true)))
  248.                {
  249.                   transition(this.stateInstalling);
  250.                   return;
  251.                }
  252.                break;
  253.             case EVENT_INSTALL:
  254.                transition(this.stateInstalling);
  255.          }
  256.       }
  257.       
  258.       protected function stateReady(param1:Event) : void
  259.       {
  260.          logger.finest("stateReady: " + param1.type);
  261.          switch(param1.type)
  262.          {
  263.             case HSMEvent.ENTER:
  264.                break;
  265.             case EVENT_ASYNC:
  266.                this.updateURL = new URLRequest(this.requestedURL);
  267.                this.updateFile = FileUtils.getLocalDescriptorFile();
  268.                transitionAsync(this.stateBeforeChecking);
  269.                break;
  270.             case EVENT_FILE:
  271.                transitionAsync(this.stateUnpackaging);
  272.          }
  273.       }
  274.       
  275.       protected function stateChecking(param1:Event) : void
  276.       {
  277.          logger.finest("stateChecking: " + param1.type);
  278.          switch(param1.type)
  279.          {
  280.             case HSMEvent.ENTER:
  281.                this.downloader = new FileDownloader(this.updateURL,this.updateFile);
  282.                this.downloader.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR,dispatch);
  283.                this.downloader.addEventListener(ProgressEvent.PROGRESS,dispatch);
  284.                this.downloader.addEventListener(UpdateEvent.DOWNLOAD_COMPLETE,dispatch);
  285.                this.downloader.download();
  286.                break;
  287.             case UpdateEvent.DOWNLOAD_START:
  288.             case ProgressEvent.PROGRESS:
  289.                break;
  290.             case DownloadErrorEvent.DOWNLOAD_ERROR:
  291.                dispatchEvent(new StatusUpdateErrorEvent(StatusUpdateErrorEvent.UPDATE_ERROR,false,false,DownloadErrorEvent(param1).text,DownloadErrorEvent(param1).errorID,DownloadErrorEvent(param1).subErrorID));
  292.                transition(this.stateErrored);
  293.                break;
  294.             case UpdateEvent.DOWNLOAD_COMPLETE:
  295.                this.downloader = null;
  296.                this.descriptorDownloaded();
  297.          }
  298.       }
  299.       
  300.       private function startTimer(param1:Number = -1) : void
  301.       {
  302.       }
  303.       
  304.       public function installFile(param1:File) : void
  305.       {
  306.          this.requestedFile = param1;
  307.          dispatch(new Event(EVENT_FILE));
  308.       }
  309.       
  310.       protected function stateAvailable(param1:Event) : void
  311.       {
  312.          logger.finest("stateAvailable: " + param1.type);
  313.          switch(param1.type)
  314.          {
  315.             case HSMEvent.ENTER:
  316.                if(dispatchEvent(new StatusUpdateEvent(StatusUpdateEvent.UPDATE_STATUS,false,true,true,this.descriptor.version,this.descriptor.description)))
  317.                {
  318.                   transition(this.stateDownloading);
  319.                   return;
  320.                }
  321.                break;
  322.             case EVENT_DOWNLOAD:
  323.                transition(this.stateDownloading);
  324.          }
  325.       }
  326.       
  327.       protected function stateUnpackaging(param1:Event) : void
  328.       {
  329.          logger.finest("stateUnpackaging: " + param1.type);
  330.          switch(param1.type)
  331.          {
  332.             case HSMEvent.ENTER:
  333.                this.unpackager = new AIRUnpackager();
  334.                this.unpackager.addEventListener(Event.COMPLETE,dispatch);
  335.                this.unpackager.addEventListener(ErrorEvent.ERROR,dispatch);
  336.                this.unpackager.addEventListener(IOErrorEvent.IO_ERROR,dispatch);
  337.                this.unpackager.unpackageAsync(this.requestedFile.url);
  338.                break;
  339.             case Event.COMPLETE:
  340.                this.unpackager.cancel();
  341.                this.fileUnpackaged();
  342.                this.unpackager = null;
  343.                break;
  344.             case ErrorEvent.ERROR:
  345.             case IOErrorEvent.IO_ERROR:
  346.                this.unpackager.cancel();
  347.                this.unpackager = null;
  348.                dispatchEvent(new StatusFileUpdateErrorEvent(StatusFileUpdateErrorEvent.FILE_UPDATE_ERROR,false,false,"",ErrorEvent(param1).errorID));
  349.                transition(this.stateErrored);
  350.          }
  351.       }
  352.       
  353.       private function installUpdate() : void
  354.       {
  355.          logger.finest("Installing update");
  356.          dispatchEvent(new Event(EVENT_INSTALL_TRIGGER));
  357.       }
  358.       
  359.       protected function stateCancelled(param1:Event) : void
  360.       {
  361.          logger.finest("stateCancelled: " + param1.type);
  362.          switch(param1.type)
  363.          {
  364.             case HSMEvent.ENTER:
  365.                dispatchEvent(new Event(EVENT_STATE_CLEAR_TRIGGER));
  366.                if(this.downloader)
  367.                {
  368.                   this.downloader.cancel();
  369.                   this.downloader = null;
  370.                }
  371.                transition(this.stateReady);
  372.          }
  373.       }
  374.       
  375.       public function checkAsync(param1:String) : void
  376.       {
  377.          this.requestedURL = param1;
  378.          dispatch(new Event(EVENT_ASYNC));
  379.       }
  380.       
  381.       protected function stateErrored(param1:Event) : void
  382.       {
  383.          logger.finest("stateErrored: " + param1.type);
  384.          switch(param1.type)
  385.          {
  386.             case HSMEvent.ENTER:
  387.                dispatchEvent(new Event(EVENT_STATE_CLEAR_TRIGGER));
  388.                if(this.downloader)
  389.                {
  390.                   this.downloader.cancel();
  391.                   this.downloader = null;
  392.                }
  393.                transition(this.stateReady);
  394.          }
  395.       }
  396.       
  397.       protected function stateInstalling(param1:Event) : void
  398.       {
  399.          logger.finest("stateInstalling: " + param1.type);
  400.          switch(param1.type)
  401.          {
  402.             case HSMEvent.ENTER:
  403.                if(!dispatchEvent(new UpdateEvent(UpdateEvent.BEFORE_INSTALL,false,true)))
  404.                {
  405.                   transition(this.statePendingInstall);
  406.                   return;
  407.                }
  408.                this.installUpdate();
  409.                break;
  410.          }
  411.       }
  412.       
  413.       protected function stateAvailableFile(param1:Event) : void
  414.       {
  415.          logger.finest("stateAvailableFile: " + param1.type + " file: " + this.requestedFile.url);
  416.          switch(param1.type)
  417.          {
  418.             case HSMEvent.ENTER:
  419.                if(dispatchEvent(new StatusFileUpdateEvent(StatusFileUpdateEvent.FILE_UPDATE_STATUS,false,true,true,this._applicationDescriptor.version,this.requestedFile.nativePath)))
  420.                {
  421.                   transition(this.stateInstallingFile);
  422.                   return;
  423.                }
  424.                break;
  425.             case EVENT_INSTALL:
  426.                transition(this.stateInstallingFile);
  427.          }
  428.       }
  429.       
  430.       public function get applicationDescriptor() : ApplicationDescriptor
  431.       {
  432.          return this._applicationDescriptor;
  433.       }
  434.       
  435.       private function installFileUpdate() : void
  436.       {
  437.          logger.finest("Installing file update");
  438.          dispatchEvent(new Event(EVENT_FILE_INSTALL_TRIGGER));
  439.       }
  440.       
  441.       public function set configuration(param1:UpdaterConfiguration) : void
  442.       {
  443.          this._configuration = param1;
  444.       }
  445.       
  446.       public function get airFile() : File
  447.       {
  448.          return this.requestedFile;
  449.       }
  450.       
  451.       public function get descriptor() : UpdateDescriptor
  452.       {
  453.          return this._descriptor;
  454.       }
  455.       
  456.       protected function stateBeforeChecking(param1:Event) : void
  457.       {
  458.          var _loc2_:UpdateEvent = null;
  459.          logger.finest("stateBeforeChecking: " + param1.type);
  460.          switch(param1.type)
  461.          {
  462.             case HSMEvent.ENTER:
  463.                _loc2_ = new UpdateEvent(UpdateEvent.CHECK_FOR_UPDATE,false,true);
  464.                dispatchEvent(_loc2_);
  465.                if(!_loc2_.isDefaultPrevented())
  466.                {
  467.                   transition(this.stateChecking);
  468.                   return;
  469.                }
  470.                logger.finer("CheckForUpdate cancelled");
  471.                break;
  472.             case EVENT_CHECK:
  473.                transition(this.stateChecking);
  474.          }
  475.       }
  476.       
  477.       protected function stateInstallingFile(param1:Event) : void
  478.       {
  479.          logger.finest("stateInstallingFile: " + param1.type);
  480.          switch(param1.type)
  481.          {
  482.             case HSMEvent.ENTER:
  483.                this.installFileUpdate();
  484.          }
  485.       }
  486.       
  487.       protected function statePendingInstall(param1:Event) : void
  488.       {
  489.          logger.finest("statePendingInstall: " + param1.type);
  490.          switch(param1.type)
  491.          {
  492.             case HSMEvent.ENTER:
  493.                return;
  494.             default:
  495.                return;
  496.          }
  497.       }
  498.       
  499.       public function get configuration() : UpdaterConfiguration
  500.       {
  501.          return this._configuration;
  502.       }
  503.       
  504.       public function cancel() : void
  505.       {
  506.          transition(this.stateCancelled);
  507.       }
  508.       
  509.       private function descriptorDownloaded() : void
  510.       {
  511.          var xml:XML = null;
  512.          logger.finer("Parsing descriptor");
  513.          try
  514.          {
  515.             xml = FileUtils.readXMLFromFile(this.updateFile);
  516.             this._descriptor = new UpdateDescriptor(xml);
  517.             this._descriptor.validate();
  518.             if(!this.isNewerVersion(VersionUtils.getApplicationVersion(),this._descriptor.version))
  519.             {
  520.                dispatchEvent(new StatusUpdateEvent(StatusUpdateEvent.UPDATE_STATUS));
  521.                transition(this.stateReady);
  522.                return;
  523.             }
  524.             this.updateFile = FileUtils.getLocalUpdateFile();
  525.             this.updateURL = new URLRequest(this.descriptor.url);
  526.             transition(this.stateAvailable);
  527.          }
  528.          catch(e:Error)
  529.          {
  530.             logger.fine("Error loading/validating downloaded descriptor: " + e);
  531.             dispatchEvent(new StatusUpdateErrorEvent(StatusUpdateErrorEvent.UPDATE_ERROR,false,false,e.message,e.errorID));
  532.             transition(stateErrored);
  533.          }
  534.       }
  535.    }
  536. }
  537.  
  538.