home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2012 April / ME_04_2012.iso / Video-Tutorial / iPhoto / media / player.swf / scripts / mx / modules / ModuleManager.as < prev    next >
Encoding:
Text File  |  2011-11-11  |  16.5 KB  |  583 lines

  1. package mx.modules
  2. {
  3.    import mx.core.IFlexModuleFactory;
  4.    import mx.core.mx_internal;
  5.    
  6.    use namespace mx_internal;
  7.    
  8.    public class ModuleManager
  9.    {
  10.       mx_internal static const VERSION:String = "4.5.0.20967";
  11.       
  12.       public function ModuleManager()
  13.       {
  14.          super();
  15.       }
  16.       
  17.       public static function getModule(param1:String) : IModuleInfo
  18.       {
  19.          return getSingleton().getModule(param1);
  20.       }
  21.       
  22.       public static function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  23.       {
  24.          return getSingleton().getAssociatedFactory(param1);
  25.       }
  26.       
  27.       private static function getSingleton() : Object
  28.       {
  29.          if(!ModuleManagerGlobals.managerSingleton)
  30.          {
  31.             ModuleManagerGlobals.managerSingleton = new ModuleManagerImpl();
  32.          }
  33.          return ModuleManagerGlobals.managerSingleton;
  34.       }
  35.    }
  36. }
  37.  
  38. import flash.display.Loader;
  39. import flash.events.ErrorEvent;
  40. import flash.events.Event;
  41. import flash.events.EventDispatcher;
  42. import flash.events.IOErrorEvent;
  43. import flash.events.ProgressEvent;
  44. import flash.events.SecurityErrorEvent;
  45. import flash.net.URLRequest;
  46. import flash.system.ApplicationDomain;
  47. import flash.system.LoaderContext;
  48. import flash.system.Security;
  49. import flash.system.SecurityDomain;
  50. import flash.utils.ByteArray;
  51. import flash.utils.Dictionary;
  52. import flash.utils.getQualifiedClassName;
  53. import mx.core.IFlexModuleFactory;
  54. import mx.events.ModuleEvent;
  55. import mx.events.Request;
  56.  
  57. class ModuleManagerImpl extends EventDispatcher
  58. {
  59.    private var moduleDictionary:Dictionary = new Dictionary(true);
  60.    
  61.    public function ModuleManagerImpl()
  62.    {
  63.       super();
  64.    }
  65.    
  66.    public function getAssociatedFactory(param1:Object) : IFlexModuleFactory
  67.    {
  68.       var _loc3_:Object = null;
  69.       var _loc4_:ModuleInfo = null;
  70.       var _loc5_:ApplicationDomain = null;
  71.       var _loc6_:Class = null;
  72.       var _loc2_:String = getQualifiedClassName(param1);
  73.       for(_loc3_ in this.moduleDictionary)
  74.       {
  75.          _loc4_ = _loc3_ as ModuleInfo;
  76.          if(_loc4_.ready)
  77.          {
  78.             _loc5_ = _loc4_.applicationDomain;
  79.             if(_loc5_.hasDefinition(_loc2_))
  80.             {
  81.                _loc6_ = Class(_loc5_.getDefinition(_loc2_));
  82.                if((Boolean(_loc6_)) && param1 is _loc6_)
  83.                {
  84.                   return _loc4_.factory;
  85.                }
  86.             }
  87.          }
  88.       }
  89.       return null;
  90.    }
  91.    
  92.    public function getModule(param1:String) : IModuleInfo
  93.    {
  94.       var _loc3_:Object = null;
  95.       var _loc4_:ModuleInfo = null;
  96.       var _loc2_:ModuleInfo = null;
  97.       for(_loc3_ in this.moduleDictionary)
  98.       {
  99.          _loc4_ = _loc3_ as ModuleInfo;
  100.          if(this.moduleDictionary[_loc4_] == param1)
  101.          {
  102.             _loc2_ = _loc4_;
  103.             break;
  104.          }
  105.       }
  106.       if(!_loc2_)
  107.       {
  108.          _loc2_ = new ModuleInfo(param1);
  109.          this.moduleDictionary[_loc2_] = param1;
  110.       }
  111.       return new ModuleInfoProxy(_loc2_);
  112.    }
  113. }
  114.  
  115. class ModuleInfo extends EventDispatcher
  116. {
  117.    private var factoryInfo:FactoryInfo;
  118.    
  119.    private var loader:Loader;
  120.    
  121.    private var numReferences:int = 0;
  122.    
  123.    private var parentModuleFactory:IFlexModuleFactory;
  124.    
  125.    private var _error:Boolean = false;
  126.    
  127.    private var _loaded:Boolean = false;
  128.    
  129.    private var _ready:Boolean = false;
  130.    
  131.    private var _setup:Boolean = false;
  132.    
  133.    private var _url:String;
  134.    
  135.    public function ModuleInfo(param1:String)
  136.    {
  137.       super();
  138.       this._url = param1;
  139.    }
  140.    
  141.    public function get applicationDomain() : ApplicationDomain
  142.    {
  143.       return !!this.factoryInfo ? this.factoryInfo.applicationDomain : null;
  144.    }
  145.    
  146.    public function get error() : Boolean
  147.    {
  148.       return this._error;
  149.    }
  150.    
  151.    public function get factory() : IFlexModuleFactory
  152.    {
  153.       return !!this.factoryInfo ? this.factoryInfo.factory : null;
  154.    }
  155.    
  156.    public function get loaded() : Boolean
  157.    {
  158.       return this._loaded;
  159.    }
  160.    
  161.    public function get ready() : Boolean
  162.    {
  163.       return this._ready;
  164.    }
  165.    
  166.    public function get setup() : Boolean
  167.    {
  168.       return this._setup;
  169.    }
  170.    
  171.    public function get size() : int
  172.    {
  173.       return !!this.factoryInfo ? int(this.factoryInfo.bytesTotal) : 0;
  174.    }
  175.    
  176.    public function get url() : String
  177.    {
  178.       return this._url;
  179.    }
  180.    
  181.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null, param3:ByteArray = null, param4:IFlexModuleFactory = null) : void
  182.    {
  183.       if(this._loaded)
  184.       {
  185.          return;
  186.       }
  187.       this._loaded = true;
  188.       this.parentModuleFactory = param4;
  189.       if(param3)
  190.       {
  191.          this.loadBytes(param1,param3);
  192.          return;
  193.       }
  194.       if(this._url.indexOf("published://") == 0)
  195.       {
  196.          return;
  197.       }
  198.       var _loc5_:URLRequest = new URLRequest(this._url);
  199.       var _loc6_:LoaderContext = new LoaderContext();
  200.       _loc6_.applicationDomain = !!param1 ? param1 : new ApplicationDomain(ApplicationDomain.currentDomain);
  201.       _loc6_.securityDomain = param2;
  202.       if(param2 == null && Security.sandboxType == Security.REMOTE)
  203.       {
  204.          _loc6_.securityDomain = SecurityDomain.currentDomain;
  205.       }
  206.       this.loader = new Loader();
  207.       this.loader.contentLoaderInfo.addEventListener(Event.INIT,this.initHandler);
  208.       this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.completeHandler);
  209.       this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,this.progressHandler);
  210.       this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.errorHandler);
  211.       this.loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.errorHandler);
  212.       this.loader.load(_loc5_,_loc6_);
  213.    }
  214.    
  215.    private function loadBytes(param1:ApplicationDomain, param2:ByteArray) : void
  216.    {
  217.       var _loc3_:LoaderContext = new LoaderContext();
  218.       _loc3_.applicationDomain = !!param1 ? param1 : new ApplicationDomain(ApplicationDomain.currentDomain);
  219.       if("allowLoadBytesCodeExecution" in _loc3_)
  220.       {
  221.          _loc3_["allowLoadBytesCodeExecution"] = true;
  222.       }
  223.       this.loader = new Loader();
  224.       this.loader.contentLoaderInfo.addEventListener(Event.INIT,this.initHandler);
  225.       this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.completeHandler);
  226.       this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.errorHandler);
  227.       this.loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.errorHandler);
  228.       this.loader.loadBytes(param2,_loc3_);
  229.    }
  230.    
  231.    public function resurrect() : void
  232.    {
  233.       if(!this._ready)
  234.       {
  235.          return;
  236.       }
  237.       if(!this.factoryInfo)
  238.       {
  239.          if(this._loaded)
  240.          {
  241.             dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  242.          }
  243.          this.loader = null;
  244.          this._loaded = false;
  245.          this._setup = false;
  246.          this._ready = false;
  247.          this._error = false;
  248.       }
  249.    }
  250.    
  251.    public function release() : void
  252.    {
  253.       if(!this._ready)
  254.       {
  255.          this.unload();
  256.       }
  257.    }
  258.    
  259.    private function clearLoader() : void
  260.    {
  261.       if(this.loader)
  262.       {
  263.          if(this.loader.contentLoaderInfo)
  264.          {
  265.             this.loader.contentLoaderInfo.removeEventListener(Event.INIT,this.initHandler);
  266.             this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.completeHandler);
  267.             this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,this.progressHandler);
  268.             this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.errorHandler);
  269.             this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.errorHandler);
  270.          }
  271.          try
  272.          {
  273.             if(this.loader.content)
  274.             {
  275.                this.loader.content.removeEventListener("ready",this.readyHandler);
  276.                this.loader.content.removeEventListener("error",this.moduleErrorHandler);
  277.             }
  278.          }
  279.          catch(error:Error)
  280.          {
  281.          }
  282.          if(this._loaded)
  283.          {
  284.             try
  285.             {
  286.                this.loader.close();
  287.             }
  288.             catch(error:Error)
  289.             {
  290.             }
  291.          }
  292.          try
  293.          {
  294.             this.loader.unload();
  295.          }
  296.          catch(error:Error)
  297.          {
  298.          }
  299.          this.loader = null;
  300.       }
  301.    }
  302.    
  303.    public function unload() : void
  304.    {
  305.       this.clearLoader();
  306.       if(this._loaded)
  307.       {
  308.          dispatchEvent(new ModuleEvent(ModuleEvent.UNLOAD));
  309.       }
  310.       this.factoryInfo = null;
  311.       this.parentModuleFactory = null;
  312.       this._loaded = false;
  313.       this._setup = false;
  314.       this._ready = false;
  315.       this._error = false;
  316.    }
  317.    
  318.    public function publish(param1:IFlexModuleFactory) : void
  319.    {
  320.       if(this.factoryInfo)
  321.       {
  322.          return;
  323.       }
  324.       if(this._url.indexOf("published://") != 0)
  325.       {
  326.          return;
  327.       }
  328.       this.factoryInfo = new FactoryInfo();
  329.       this.factoryInfo.factory = param1;
  330.       this._loaded = true;
  331.       this._setup = true;
  332.       this._ready = true;
  333.       this._error = false;
  334.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  335.       dispatchEvent(new ModuleEvent(ModuleEvent.PROGRESS));
  336.       dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  337.    }
  338.    
  339.    public function addReference() : void
  340.    {
  341.       ++this.numReferences;
  342.    }
  343.    
  344.    public function removeReference() : void
  345.    {
  346.       --this.numReferences;
  347.       if(this.numReferences == 0)
  348.       {
  349.          this.release();
  350.       }
  351.    }
  352.    
  353.    public function initHandler(param1:Event) : void
  354.    {
  355.       var _loc2_:ModuleEvent = null;
  356.       this.factoryInfo = new FactoryInfo();
  357.       try
  358.       {
  359.          this.factoryInfo.factory = this.loader.content as IFlexModuleFactory;
  360.       }
  361.       catch(error:Error)
  362.       {
  363.       }
  364.       if(!this.factoryInfo.factory)
  365.       {
  366.          _loc2_ = new ModuleEvent(ModuleEvent.ERROR,param1.bubbles,param1.cancelable);
  367.          _loc2_.bytesLoaded = 0;
  368.          _loc2_.bytesTotal = 0;
  369.          _loc2_.errorText = "SWF is not a loadable module";
  370.          dispatchEvent(_loc2_);
  371.          return;
  372.       }
  373.       this.loader.content.addEventListener("ready",this.readyHandler);
  374.       this.loader.content.addEventListener("error",this.moduleErrorHandler);
  375.       this.loader.content.addEventListener(Request.GET_PARENT_FLEX_MODULE_FACTORY_REQUEST,this.getFlexModuleFactoryRequestHandler,false,0,true);
  376.       try
  377.       {
  378.          this.factoryInfo.applicationDomain = this.loader.contentLoaderInfo.applicationDomain;
  379.       }
  380.       catch(error:Error)
  381.       {
  382.       }
  383.       this._setup = true;
  384.       dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  385.    }
  386.    
  387.    public function progressHandler(param1:ProgressEvent) : void
  388.    {
  389.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  390.       _loc2_.bytesLoaded = param1.bytesLoaded;
  391.       _loc2_.bytesTotal = param1.bytesTotal;
  392.       dispatchEvent(_loc2_);
  393.    }
  394.    
  395.    public function completeHandler(param1:Event) : void
  396.    {
  397.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  398.       _loc2_.bytesLoaded = this.loader.contentLoaderInfo.bytesLoaded;
  399.       _loc2_.bytesTotal = this.loader.contentLoaderInfo.bytesTotal;
  400.       dispatchEvent(_loc2_);
  401.    }
  402.    
  403.    public function errorHandler(param1:ErrorEvent) : void
  404.    {
  405.       this._error = true;
  406.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.ERROR,param1.bubbles,param1.cancelable);
  407.       _loc2_.bytesLoaded = 0;
  408.       _loc2_.bytesTotal = 0;
  409.       _loc2_.errorText = param1.text;
  410.       dispatchEvent(_loc2_);
  411.    }
  412.    
  413.    public function getFlexModuleFactoryRequestHandler(param1:Request) : void
  414.    {
  415.       param1.value = this.parentModuleFactory;
  416.    }
  417.    
  418.    public function readyHandler(param1:Event) : void
  419.    {
  420.       this._ready = true;
  421.       this.factoryInfo.bytesTotal = this.loader.contentLoaderInfo.bytesTotal;
  422.       var _loc2_:ModuleEvent = new ModuleEvent(ModuleEvent.READY);
  423.       _loc2_.bytesLoaded = this.loader.contentLoaderInfo.bytesLoaded;
  424.       _loc2_.bytesTotal = this.loader.contentLoaderInfo.bytesTotal;
  425.       this.clearLoader();
  426.       dispatchEvent(_loc2_);
  427.    }
  428.    
  429.    public function moduleErrorHandler(param1:Event) : void
  430.    {
  431.       var _loc2_:ModuleEvent = null;
  432.       this._ready = true;
  433.       this.factoryInfo.bytesTotal = this.loader.contentLoaderInfo.bytesTotal;
  434.       this.clearLoader();
  435.       if(param1 is ModuleEvent)
  436.       {
  437.          _loc2_ = ModuleEvent(param1);
  438.       }
  439.       else
  440.       {
  441.          _loc2_ = new ModuleEvent(ModuleEvent.ERROR);
  442.       }
  443.       dispatchEvent(_loc2_);
  444.    }
  445. }
  446.  
  447. class FactoryInfo
  448. {
  449.    public var factory:IFlexModuleFactory;
  450.    
  451.    public var applicationDomain:ApplicationDomain;
  452.    
  453.    public var bytesTotal:int = 0;
  454.    
  455.    public function FactoryInfo()
  456.    {
  457.       super();
  458.    }
  459. }
  460.  
  461. class ModuleInfoProxy extends EventDispatcher implements IModuleInfo
  462. {
  463.    private var info:ModuleInfo;
  464.    
  465.    private var referenced:Boolean = false;
  466.    
  467.    private var _data:Object;
  468.    
  469.    public function ModuleInfoProxy(param1:ModuleInfo)
  470.    {
  471.       super();
  472.       this.info = param1;
  473.       param1.addEventListener(ModuleEvent.SETUP,this.moduleEventHandler,false,0,true);
  474.       param1.addEventListener(ModuleEvent.PROGRESS,this.moduleEventHandler,false,0,true);
  475.       param1.addEventListener(ModuleEvent.READY,this.moduleEventHandler,false,0,true);
  476.       param1.addEventListener(ModuleEvent.ERROR,this.moduleEventHandler,false,0,true);
  477.       param1.addEventListener(ModuleEvent.UNLOAD,this.moduleEventHandler,false,0,true);
  478.    }
  479.    
  480.    public function get data() : Object
  481.    {
  482.       return this._data;
  483.    }
  484.    
  485.    public function set data(param1:Object) : void
  486.    {
  487.       this._data = param1;
  488.    }
  489.    
  490.    public function get error() : Boolean
  491.    {
  492.       return this.info.error;
  493.    }
  494.    
  495.    public function get factory() : IFlexModuleFactory
  496.    {
  497.       return this.info.factory;
  498.    }
  499.    
  500.    public function get loaded() : Boolean
  501.    {
  502.       return this.info.loaded;
  503.    }
  504.    
  505.    public function get ready() : Boolean
  506.    {
  507.       return this.info.ready;
  508.    }
  509.    
  510.    public function get setup() : Boolean
  511.    {
  512.       return this.info.setup;
  513.    }
  514.    
  515.    public function get url() : String
  516.    {
  517.       return this.info.url;
  518.    }
  519.    
  520.    public function publish(param1:IFlexModuleFactory) : void
  521.    {
  522.       this.info.publish(param1);
  523.    }
  524.    
  525.    public function load(param1:ApplicationDomain = null, param2:SecurityDomain = null, param3:ByteArray = null, param4:IFlexModuleFactory = null) : void
  526.    {
  527.       var _loc5_:ModuleEvent = null;
  528.       this.info.resurrect();
  529.       if(!this.referenced)
  530.       {
  531.          this.info.addReference();
  532.          this.referenced = true;
  533.       }
  534.       if(this.info.error)
  535.       {
  536.          dispatchEvent(new ModuleEvent(ModuleEvent.ERROR));
  537.       }
  538.       else if(this.info.loaded)
  539.       {
  540.          if(this.info.setup)
  541.          {
  542.             dispatchEvent(new ModuleEvent(ModuleEvent.SETUP));
  543.             if(this.info.ready)
  544.             {
  545.                _loc5_ = new ModuleEvent(ModuleEvent.PROGRESS);
  546.                _loc5_.bytesLoaded = this.info.size;
  547.                _loc5_.bytesTotal = this.info.size;
  548.                dispatchEvent(_loc5_);
  549.                dispatchEvent(new ModuleEvent(ModuleEvent.READY));
  550.             }
  551.          }
  552.       }
  553.       else
  554.       {
  555.          this.info.load(param1,param2,param3,param4);
  556.       }
  557.    }
  558.    
  559.    public function release() : void
  560.    {
  561.       if(this.referenced)
  562.       {
  563.          this.info.removeReference();
  564.          this.referenced = false;
  565.       }
  566.    }
  567.    
  568.    public function unload() : void
  569.    {
  570.       this.info.unload();
  571.       this.info.removeEventListener(ModuleEvent.SETUP,this.moduleEventHandler);
  572.       this.info.removeEventListener(ModuleEvent.PROGRESS,this.moduleEventHandler);
  573.       this.info.removeEventListener(ModuleEvent.READY,this.moduleEventHandler);
  574.       this.info.removeEventListener(ModuleEvent.ERROR,this.moduleEventHandler);
  575.       this.info.removeEventListener(ModuleEvent.UNLOAD,this.moduleEventHandler);
  576.    }
  577.    
  578.    private function moduleEventHandler(param1:ModuleEvent) : void
  579.    {
  580.       dispatchEvent(param1);
  581.    }
  582. }
  583.