LoaderContext and ApplicationDomain changes with Adobe AIR?

前端 未结 2 1664
失恋的感觉
失恋的感觉 2021-02-06 06:52

I\'m currently experimenting with loading external SWF files from both an standard AS3 application, and an AIR application. It seems that the AIR application doesn\'t act the sa

2条回答
  •  一整个雨季
    2021-02-06 07:50

    Got it.

    The issue was caused by a different behaviour in the SecurityDomain system within an AIR application. When a SWF file is loaded within an AIR application, it always depend to a different sandbox. Thus, AIR creates a new SecurityDomain for this SWF.

    Since a SecurityDomain is a group of one or more ApplicationDomains, this behaviour forced the creation of a new ApplicationDomain (within the new SecurityDomain), ignoring the specified one (which belong to the 'main' SecurityDomain).

    There is a workaround using URLLoader. When loaded from bytecode (using Loader.loadBytes), a SWF is loaded within the same SecurityDomain. This is why you have to put the allowLoadBytesCodeExecution to true, since it can be unsafe. So loading the SWF indirectly, first though an URLLoader, and then with Loader.loadBytes, solve this issue.

    Here's the snippet :

    package {
        import flash.display.Loader;
        import flash.display.LoaderInfo;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.net.URLLoader;
        import flash.net.URLLoaderDataFormat;
        import flash.net.URLRequest;
        import flash.system.ApplicationDomain;
        import flash.system.LoaderContext;
        import flash.utils.ByteArray;
    
        public class Invoker extends Sprite
        {
            public function Invoker()
            {
                var uldr : URLLoader = new URLLoader();
                uldr.dataFormat = URLLoaderDataFormat.BINARY;
                uldr.addEventListener(Event.COMPLETE, onBytesComplete);
    
                uldr.load(new URLRequest("otherSwf.swf"));
            }
    
            private function onBytesComplete(e : Event) : void
            {
                var bytes : ByteArray = (e.target as URLLoader).data;
    
                var ldr : Loader = new Loader();
                ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onChildComplete);
    
                var ldrC : LoaderContext = new LoaderContext();
    
                // This property was for AIR 1.0.
                //ldrC.allowLoadBytesCodeExecution = true;
    
                // Since AIR 2.0, it's allowCodeImport.
                ldrC.allowCodeImport = true;
    
                ldr.loadBytes(bytes, ldrC);
            }
    
            private function onChildComplete(e : Event) : void
            {
                var c1ad : ApplicationDomain = (e.target as LoaderInfo).applicationDomain;
                var inad : ApplicationDomain = ApplicationDomain.currentDomain;
    
                trace("Child One parentDomain : " + c1ad.parentDomain);
                trace("Invoker parentDomain   : " + inad.parentDomain);
    
                trace("Child One has Invoker  : " + c1ad.hasDefinition("Invoker"));
                trace("Invoker has Invoker    : " + inad.hasDefinition("Invoker"));
            }
        }
    }
    

    Hope this helps.

提交回复
热议问题