13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Programming HTML and JavaScript in AIR<br />

var interface = {};<br />

interface.calculatePrice = function(){<br />

return ".45 c<strong>en</strong>ts";<br />

}<br />

interface.storeID = "abc"<br />

window.childSandboxBridge = interface;<br />

If this child cont<strong>en</strong>t was loaded into an iframe assigned an id of “child”, you could access the interface from par<strong>en</strong>t<br />

cont<strong>en</strong>t by reading the childSandboxBridge property of the frame:<br />

var childInterface = docum<strong>en</strong>t.getElem<strong>en</strong>tById("child").cont<strong>en</strong>tWindow.childSandboxBridge;<br />

air.trace(childInterface.calculatePrice()); //traces ".45 c<strong>en</strong>ts"<br />

air.trace(childInterface.storeID)); //traces "abc"<br />

Establishing a par<strong>en</strong>t sandbox bridge<br />

Adobe AIR 1.0 and later<br />

The par<strong>en</strong>tSandboxBridge property allows the par<strong>en</strong>t docum<strong>en</strong>t to expose an interface to cont<strong>en</strong>t in a child<br />

docum<strong>en</strong>t. To expose an interface, the par<strong>en</strong>t docum<strong>en</strong>t sets the par<strong>en</strong>tSandbox property of the child docum<strong>en</strong>t to a<br />

function or object defined in the par<strong>en</strong>t docum<strong>en</strong>t. You can th<strong>en</strong> access the object or function from cont<strong>en</strong>t in the<br />

child. The following example shows how a script running in a par<strong>en</strong>t frame can expose an object containing a function<br />

to a child docum<strong>en</strong>t:<br />

var interface = {};<br />

interface.save = function(text){<br />

var saveFile = air.File("app-storage:/save.txt");<br />

//write text to file<br />

}<br />

docum<strong>en</strong>t.getElem<strong>en</strong>tById("child").cont<strong>en</strong>tWindow.par<strong>en</strong>tSandboxBridge = interface;<br />

Using this interface, cont<strong>en</strong>t in the child frame could save text to a file named save.txt, but would not have any other<br />

access to the file system. The child cont<strong>en</strong>t could call the save function as follows:<br />

var textToSave = "A string.";<br />

window.par<strong>en</strong>tSandboxBridge.save(textToSave);<br />

Application cont<strong>en</strong>t should expose the narrowest interface possible to other sandboxes. Non-application cont<strong>en</strong>t<br />

should be considered inher<strong>en</strong>tly untrustworthy since it may be subject to accid<strong>en</strong>tal or malicious code injection. You<br />

must put appropriate safeguards in place to prev<strong>en</strong>t misuse of the interface you expose through the par<strong>en</strong>t sandbox<br />

bridge.<br />

Accessing a par<strong>en</strong>t sandbox bridge during page loading<br />

Adobe AIR 1.0 and later<br />

In order for a script in a child docum<strong>en</strong>t to access a par<strong>en</strong>t sandbox bridge, the bridge must be set up before the script<br />

is run. Window, frame and iframe objects dispatch a dominitialize ev<strong>en</strong>t wh<strong>en</strong> a new page DOM has be<strong>en</strong> created,<br />

but before any scripts have be<strong>en</strong> parsed, or DOM elem<strong>en</strong>ts added. You can use the dominitialize ev<strong>en</strong>t to establish<br />

the bridge early <strong>en</strong>ough in the page construction sequ<strong>en</strong>ce that all scripts in the child docum<strong>en</strong>t can access it.<br />

The following example illustrates how to create a par<strong>en</strong>t sandbox bridge in response to the dominitialize ev<strong>en</strong>t<br />

dispatched from the child frame:<br />

Last updated 6/6/2012<br />

998

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!