代码之家  ›  专栏  ›  技术社区  ›  Tyn

LoaderContext和ApplicationDomain随AdobeAIR的变化?

  •  8
  • Tyn  · 技术社区  · 17 年前

    我目前正在试验从标准AS3应用程序和AIR应用程序加载外部SWF文件。AIR应用程序的运行方式似乎与Flash播放器运行的标准SWF不同。

    根据 documentation applicationDomain 性质 LoaderContext 在AIR应用程序中也可用,但它似乎不起作用。

    package {
        import flash.display.Loader;
        import flash.display.LoaderInfo;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.net.URLRequest;
        import flash.system.ApplicationDomain;
        import flash.system.LoaderContext;
    
        public class Invoker extends Sprite
        {
            private var _ldr : Loader;
    
            public function Invoker()
            {
                _ldr = new Loader();
                _ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onChildOneComplete);
    
                var ldrC : LoaderContext = new LoaderContext(false,
                    new ApplicationDomain(ApplicationDomain.currentDomain)
                );
    
                _ldr.load(new URLRequest("otherSwf.swf"), ldrC);
            }
    
            private function onChildOneComplete(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"));
            }
        }
    }
    

    将此代码编译为SWF文件,并使用Flash Player启动它,会产生以下输出,这似乎是正确的:

    Child One parentDomain : [object ApplicationDomain]
    Invoker parentDomain   : null
    Child One has Invoker  : true
    Invoker has Invoker    : true
    

    但与AIR应用程序相同的代码具有不同的输出:

    Child One parentDomain : null
    Invoker parentDomain   : null
    Child One has Invoker  : false
    Invoker has Invoker    : true
    

    根据文档,第一个输出(使用带Flash Player的SWF,而不是AIR应用程序)是正确的。此外,还可以使用此代码段并将应用程序域更改为其他可能的配置(如 new ApplicationDomain(null) 或 ApplicationDomain.currentDomain )对SWF的说明进行了验证,但并未改变空气应用程序的输出。

    为什么AIR只是忽略传递给加载程序上下文的应用程序域?有关于这个问题的文件吗?

    非常感谢你。

    2 回复  |  直到 17 年前
        1
  •  14
  •   Tyn    13 年前

    知道了。

    这一问题是由该地区的一种不同行为引起的 SecurityDomain 安全域 对于这个SWF。

    自从 安全域 是由一个或多个成员组成的组 ApplicationDomain s、 这种行为迫使创建了一个新的 应用领域 (在新的 安全域 安全域 ).

    有一个解决方法是使用 URLLoader Loader.loadBytes 安全域 . 这就是为什么你必须把 allowLoadBytesCodeExecution 这是真的,因为它可能是不安全的。因此,首先通过 ,然后是 ,解决这个问题。

    以下是片段:

    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"));
            }
        }
    }
    

    希望这有帮助。

        2
  •  1
  •   pigiuz    13 年前

    这是个好主意,thanx:)

    还有一个细节: allowLoadBytesCodeExecution 现在是一个遗留属性,它是在AIR 1.0中定义的。从AIR 2.0开始使用 allowCodeImport

    再见, PG

    推荐文章