简体   繁体   English

LoaderContext和ApplicationDomain是否随Adobe AIR发生变化?

[英]LoaderContext and ApplicationDomain changes with Adobe AIR?

I'm currently experimenting with loading external SWF files from both an standard AS3 application, and an AIR application. 我目前正在尝试从标准AS3应用程序和AIR应用程序加载外部SWF文件。 It seems that the AIR application doesn't act the same way a standard SWF run by the Flash Player does. 似乎AIR应用程序的行为与Flash Player运行的标准SWF的行为方式不同。

According to the documentation , the applicationDomain property of LoaderContext is usable in an AIR application too, but it just seems to be not working. 根据文档LoaderContextapplicationDomain属性也可以在AIR应用程序中使用,但它似乎无法正常工作。

I have the following code : 我有以下代码:

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

Compiling this code as an SWF file and launching it with the Flash Player does this output, which seems right : 将此代码编译为SWF文件并使用Flash Player启动它会执行此输出,这似乎是正确的:

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

But the same code as an AIR application does a different output : 但是与AIR应用程序相同的代码会执行不同的输出:

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

According to the documentation, the first output (using a SWF with Flash Player, and not an AIR application) is the right one. 根据文档,第一个输出(使用SWF与Flash Player,而不是AIR应用程序)是正确的。 Also, playing around with this snippet and changing the application domain to others possible configurations (like new ApplicationDomain(null) , or ApplicationDomain.currentDomain ) does exaclty what the documentation says with the SWF, but does not change the output of the AIR application. 此外,使用此代码段并将应用程序域更改为其他可能的配置(如new ApplicationDomain(null)ApplicationDomain.currentDomain )确实可以说明文档对SWF所说的内容,但不会更改AIR应用程序的输出。

Any clue why AIR is simply ignoring the application domain passed to the loader context ? 任何线索为什么AIR只是忽略传递给加载器上下文的应用程序域? Any documentation about this particular issue ? 有关此特定问题的任何文档?

Thank you very much. 非常感谢你。

Got it. 得到它了。

The issue was caused by a different behaviour in the SecurityDomain system within an AIR application. 该问题是由AIR应用程序中的SecurityDomain系统中的不同行为引起的。 When a SWF file is loaded within an AIR application, it always depend to a different sandbox. 在AIR应用程序中加载SWF文件时,它始终依赖于不同的沙箱。 Thus, AIR creates a new SecurityDomain for this SWF. 因此,AIR为此SWF创建一个新的SecurityDomain

Since a SecurityDomain is a group of one or more ApplicationDomain s, this behaviour forced the creation of a new ApplicationDomain (within the new SecurityDomain ), ignoring the specified one (which belong to the 'main' SecurityDomain ). 由于SecurityDomain是一个或多个ApplicationDomain的组,因此此行为强制创建新的ApplicationDomain (在新的SecurityDomain ),忽略指定的(属于' SecurityDomain )。

There is a workaround using URLLoader . 使用URLLoader有一种解决方法。 When loaded from bytecode (using Loader.loadBytes ), a SWF is loaded within the same SecurityDomain . 从字节码加载(使用Loader.loadBytes )时,SWF将加载到同一个SecurityDomain This is why you have to put the allowLoadBytesCodeExecution to true, since it can be unsafe. 这就是为什么你必须将allowLoadBytesCodeExecution为true,因为它可能是不安全的。 So loading the SWF indirectly, first though an URLLoader , and then with Loader.loadBytes , solve this issue. 因此,首先通过URLLoader ,然后使用Loader.loadBytes间接加载SWF,解决了这个问题。

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. 希望这可以帮助。

that's a good one, thanx :) 这是一个很好的,比:)

Just one more detail: allowLoadBytesCodeExecution is now a legacy property, it was defined in AIR 1.0. 还有一个细节: allowLoadBytesCodeExecution现在是一个遗留属性,它是在AIR 1.0中定义的。 From AIR 2.0 on use allowCodeImport instead. 从AIR 2.0开始使用allowCodeImport

ciao, PG ciao,PG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM