Flash-depth understanding of the sandbox - Application Domains_ under

2011-01-12  来源:本站原创  分类:Flash  人气:109 

Same Domain: Runtime Shared Libraries same domain: the runtime shared libraries

The definition of the new domain added to an existing application that might be most useful application domain. Because the inheritance can only be defined within the parent domain pairs share, and merge into the same application defined domain, you can use this field for all SWF sharing, including parent and child.

Flash-depth understanding of the sandbox - Application Domains_ under

Parent application domain, including the definition of the sub-SWF

Runtime Shared Libraries (RSLs) is the use of this mechanism. RSLs can be loaded at run time independent of the code base. By RSLs, other SWF which can be shared without the need to compile the code to itself, thus eliminating the redundancy and reduce the volume of documentation, but also make the code easier to maintain. We loaded in the main application domain RSL, which can be shared throughout the process definition.

RSLs need to do before using the preparation. First of all, ActionScript compiler released SWF file needs to know what the definition of the time need not be compiled.

Native Flash Player API defines do not need to compile. While each SWF needs to use the definition of native (Array, XML, Sprite, etc.), but these definitions exist only in Flash Player executable file, do not and will not be compiled into the SWF file. Playerglobal.swc compiler uses a special called SWC (pre-compiled SWF library) to identify the original definition. It contains the original definition of interfaces, including the name and data type definitions and so on. Through which the compiler to compile SWF, and will not compile these definitions into the final SWF.

The compiler can also refer to the same as other similar playerglobal.swc SWC library. These libraries as "external" library, which contains the definition is used to compile, will not be included into the SWF inside.

Here in the editing tools are not discussed in detail how the library link in the settings. Different versions of the editor to set a little different, specific methods, please refer to Flash document.

Although we SWCs to compile SWF, but in fact they themselves SWF files, and other similar to the loaded SWF. Library during compile time, while generating the SWF and SWC files. SWF used to run the load, while the SWC in the external libraries used at compile time.

Flash-depth understanding of the sandbox - Application Domains_ under

Compiler uses SWCs shared library, SWF loading shared libraries at run time

Another preparatory work need to write code. When using external libraries, published SWF does not contain the definition of the library. If you try to run the Flash Player code, which will produce the verification error, the SWF is basically paralyzed.

Flash Player will use in class when the first check its definition. If the application domain does not include the definition, then the validation error will be generated.

In fact the lack of definition of the error generated in two ways. Checksum error is among the worst of two, said the class does not work for catastrophic failure. The other is the reference error when a data type is not available, but cited the case occurred. Although the lack of reference to the definition can also cause errors, but this error only when the class has been verified within the normal process of interrupting code execution.

var instance:DoesNotExist;
// VerifyError: Error #1014: Class DoesNotExist could not be found.
//  When the Flash Player when the validation class that contains the definition validation error has occurred
var instance:Object = new DoesNotExist();
// ReferenceError: Error #1065: Variable DoesNotExist is not defined.
//  When code execution to occur when this line reference errors

The main difference is that the validation error and the class definition, whereas the reference error associated with the code execution. The code within the class to perform, you must first by check. Instance the example above, the object is declared as Object types, you can check via the normal (just when will encounter in the implementation of reference error).

Note: Strict Mode Note: strict mode

External library is defined without reference to the SWF in its compilation of a method. Another method is to turn off strict mode, which will greatly relax the inspection of variables. For class use, you can reference a class that does not exist and will not cause the compiler error. Does not exist you can not directly be used as variable type class (this will generate in the run-time error checking), but you can be like the above "reference error" to refer to as the example. In non-strict mode, the compiler may not detect errors that might occur, it is usually not recommended to use this mode.

The SWF file uses the RSLs must ensure that the first loaded up RSLs, in order to use these external definitions. We should begin before the main application with a pre-loader to load the RSLs.

The following shows a SWF loaded outside the class that contains Doughnut RSL example. Although the SWF directly reference this class, but it is compiled in an external library, and by the way SWC reference. RSL first use in the Doughnut class is loaded before coming, so will not cause validation errors.

Doughnut.as (compiled as doughnutLibrary.swc and doughnutLibrary.swf):

package {
        import flash.display.Sprite;

        public class Doughnut extends Sprite {
                public function Doughnut(){

                        // draw a doughnut shape
                        graphics.beginFill(0xFF99AA);
                        graphics.drawCircle(0, 0, 50);
                        graphics.drawCircle(0, 0, 25);
                }
        }
}

ShapesMain.as (Shapes.swf the main class):

package {
        import flash.display.Sprite;

        public class ShapesMain extends Sprite {
                public function ShapesMain(){

                        //  Although there is no compiled into Shapes.swf ,
                        //  But we doughnutLibrary.swc the external library
                        //  You can gain access to the Doughnut class reference
                        var donut:Doughnut = new Doughnut();
                        donut.x = 100;
                        donut.y = 100;
                        addChild(donut);
                }
        }
}

Shapes.swf (RSL loader):

var rslLoader:Loader = new Loader();
rslLoader.contentLoaderInfo.addEventListener(Event.INIT, rslInit);

//  As defined in the RSL is loaded into the current application domain
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;

var url:String = "doughnutLibrary.swf";
rslLoader.load(new URLRequest(url), context);

function rslInit(event:Event):void {
        //  Only when after importing the definition of the current application domain RSL
        //  We can use the Doughnut defined by ShapesMain Class checksum
        addChild(new ShapesMain());
}

In this case, Shapes.swf is the main program, when RSL is loaded to instantiate the main class ShapesMain. If you do not import the RSL, as defined in the time to create ShapesMain instance because the application domain will not find the corresponding class validation error occurred.

Note: Flex the RSL

Discussed here is the bottom of the method should not be used for Flex development. Flex framework RSLs have their own set of processing mechanisms, and more about the RSL in Flex application, refer to the Flex Runtime Shared Libraries (Flex 4) .

Getting Definitions Dynamically defined dynamic access

We can use Application.getDefinition method to get the definition not in the application domain, or covered by the definition of the parent domain. This method returns the application domain and the definition of any reference to the parent domain. In the current application domain getDefinition method using the same effect as a global function getDefinitionByName .

We can also SWF's LoaderInfo.applicationDomain to get outside in the ApplicationDomain.currentDomain application domain. In the following example we load a SWF using Loader file, then load the application domain that extract com.example.Box class definition.

try {
        var domain:ApplicationDomain = loader.contentLoaderInfo.applicationDomain;
        var boxClass:Class = domain.getDefinition("com.example.Box") as Class;
        var boxInstance:Object = new boxClass();
}catch(err:Error){
        trace(err.message);
}

The above example contains two knowledge points. First of all, getDefinition the return value is explicitly converted to Class type, because getDefinition default return type is Object, it may represent a class type other than the addition to the type (function, namespace, interface). Second, the operation should be placed try-catch function of the body, because if the definition of failure will throw getDefinition find an error. Or you can use before using getDefinition ApplicationDomain.hasDefinition to detect whether the find a definition of success.

Obtained by dynamic approach to the definition, rather than those in the current application domain (and the successor within the program) is defined, it can not be used as the variable type. Like the RSL, as in the application domain can not find the class definition when the error in the checksum. So the example above boxInstance variable declared as type Object type instead of Box, Box class definition because the application domain does not exist.

Same-definition Collisions same definition of conflict

Some may occur when the definition of reference to match your application to another definition of cross-domain situation. This will generate wrong type cast as follows:

TypeError: Error #1034: Type Coercion failed: cannot convert
        com.example::[email protected] to com.example.MyClass.

You can see the definition of different memory space with the @ symbol to distinguish. Although their internal code may be exactly the same (or different), but there are different because of their application domain (or security domain), the so they are two different definitions.

Only as Object, as defined in the native Flash Player can be located in different domains (even across security domains) of the definition of associate. In fact, most of the time to declare a variable type of cross-domain when they are needed Object type.

Although we can use this generic type Object to resolve the conflict error defined, in fact, we should arrange the location of the application domain to eliminate this mismatch.

Conclusion Summary

This tutorial contains a lot of information. Discussed the first part of what is safe domain, and how it affects the content from different domains. Flash Player security sandbox in this mechanisms to protect user data. Flash developers should be aware of and rational use of such restrictions.

The second part discusses the application domain - one for the security sandbox sandbox defined by type of ActionScript. Application domain level mechanism provides a different definition of SWF direct method of sharing and reuse.

In the security domain and application domain concepts, there are many pitfalls. Hopefully this tutorial will help you be prepared for this. You should not only understand their mode of operation, but also know how to apply them to achieve the effect you want.

Translator's Note:

Through the translation of this article, I truly understand that the translation work difficult. While the usual look at the speed of information in English pretty fast, but the Chinese need to spend more repeat several times the time. In this tutorial translation, text translation, I did not use aids, completely by hand to play, while understanding the intention of the translation again. Sense than simply read through the harvest is still some more to come.

This article describes the knowledge is very important, especially from the AS2 developers time to grow up is very easy to fall into the trap that some of the text. This part of the knowledge to fully grasp the module architecture design, management, memory and other aspects of great help. Here to once again thank the original author senocular bring us such a wonderful tutorial.

相关文章
  • Flash-depth understanding of the sandbox - Application Domains_ under 2011-01-12

    Same Domain: Runtime Shared Libraries same domain: the runtime shared libraries The definition of the new domain added to an existing application that might be most useful application domain. Because the inheritance can only be defined within the par

  • Flash-depth understanding of the sandbox - Application Domains_ on 2011-01-12

    Application domain Application Domains Application Domains Application Domain Placement location of the application domain Application Domain Inheritance inheritance application domain Child Domains: Definition Versioning sub-domains: the definition

  • [Transfer] Tutorial: Flash-depth understanding of the sandbox - Application Domains 2010-12-08

    [Url] http://kevincao.com/2010/11/application-domains/ [/ Url] Application domain Application Domains Application Domains Application Domain Placement location of the application domain Application Domain Inheritance inheritance application domain Ch

  • Flash-depth understanding of the sandbox - Application Domains 2010-12-08

    Application domain Application Domains Application Domains Application Domain Placement location of the application domain Application Domain Inheritance inheritance application domain Child Domains: Definition Versioning sub-domains: the definition

  • Flash-depth understanding of the sandbox - Security Domains_ on 2011-01-12

    Today, i have time to senocular on the on the security domain and application domain of the tutorial a good read again. That foreigners are professional people: the content is very organized and complete, illustrated, for example, is also very practi

  • Flash-depth understanding of the sandbox - Security Domains_ continued under 2011-01-12

    In the following example to see how you can quilt SWF scene access, but can not call stage.addEventListener method. http://first.example.com/parent.swf: var loader:Loader = new Loader(); addChild(loader); var url:String = "http://<samp>second&l

  • Flash-depth understanding of the sandbox - Security Domains_ under 2011-01-12

    SWF Communication Without Trust in non-trusted communication between the SWF In some cases there may be other sources with less reliable domain SWF communication, you do not want to completely trust the domain, open all the authorization. SharedEvent

  • Depth understanding of the principles of database log system 2010-12-30

    Depth understanding of the principles of database log system A: transaction system 1 affairs working model Transaction must meet the atomicity, the package or the whole operation or do not do all. Transaction management system needs to do two things,

  • Tutorial: Flash-depth understanding of the sandbox - Security Domains 2010-12-08

    [Url] http://kevincao.com/2010/11/security-domains/ [/ Url] Today, i have time to senocular on the security domain on the application domain and read it again a good tutorial. That foreigners are professional people: the content is very organized and

  • Flash-depth understanding of the sandbox - Security Domains 2010-12-08

    Today, i have time to senocular on the on the security domain and application domain of the tutorial a good read again. That foreigners are professional people: the content is very organized and complete, illustrated, for example, is also very practi

  • Depth understanding of Javascript closure (closure) 2009-03-06

    Recently, online access to a lot of Javascript closure (closure) of relevant information and writing are very academic and professional. For beginners to understand let alone closure, and even the language describing it is very difficult to understan

  • Eclipse plug-in development - in depth understanding of the menu (Menu) functions and extension points 2010-09-18

    Original from: Rain Maple Technical Tutorials Network http://www.fengfly.com Original URL: http://www.fengfly.com/plus/view-179398-1.html The menu is a variety of software and development platform will provide the necessary functionality, Eclipse is

  • Flash's socket in the practical application of problems in need of attention 2010-02-23

    Author: Mr. bottle transferred from www.54np.com Several versions of the AS API, Flash's socket found more and more problems. Perhaps long ago it was concluded, but I am a novice, it is not often read this article, senior experience, it encountered a

  • Depth understanding of the eval function in javascript 2009-03-01

    http://wanyij.blog.51cto.com/46570/43794 In this paper, the discovery of an appropriate title is not so easy, huh, huh, so in this note under the first two purposes of this article: (1) introduction of the eval function in javascript usage (2) how to

  • Depth understanding of demand, resulting in project failure 2009-10-12

    The needs of all categories of personnel to different interpretations: Demand for the failure of participants in the project than the weight of factors: Do not know the destination of the voyage, there is no wind! !

  • Depth understanding of the Statement and PreparedStatement 2011-05-07

    First, instead of using a PreparedStatement object Statement JDBC driver optimization is based on what functions to use Select Statement or PreparedStatement depends on how you use them for SQL statement is executed only once is the best choice State

  • Application Domains and Security Domains 2010-12-02

    Two very good articles on the Application Domain and Serurity Domains articles, in-depth analysis of flash sandbox mechanism. Tutorial: Flash-depth understanding of the sandbox - Application Domains Tutorial: Flash-depth understanding of the sandbox

  • [Transfer] in-depth understanding of Flash Player to redraw 2010-09-26

    http://flashteam.tencent.com/post/60/ depth understanding of flash-player redraw / comment-page-1 / # comment-172 SWF Flash Player will refresh the contents of the frame rate speed to the content needs to be changed, and this refresh process, we ofte

  • Translation: Understanding Flash Player 9 garbage collection, please correct me 2010-04-29

    Keywords: flash player Original Understanding garbage collection in Flash Player 9 Translation Currently I do research ActionScript 3.0, its ability to make me very excited. It brings a lot of native execution speed possible (the original sentence Th

  • Flash Player 9 on the Unload secret 2010-04-21

    Flash Player 9 on the Unload secret AS3 2009-08-01 12:29:19 read 134 comments 0 font size: small medium and large Flash Player 9 has a hidden secret, not even try to hide this BUG. But it still does not know by the public, as you can see Flash Player