Actionscript 3 在空中安全沙箱中执行远程SWF
我尝试下载一个外部SWF并在空中安全沙箱中运行它 这是空气应用程序的代码:Actionscript 3 在空中安全沙箱中执行远程SWF,actionscript-3,security,air,flash-builder,sandbox,Actionscript 3,Security,Air,Flash Builder,Sandbox,我尝试下载一个外部SWF并在空中安全沙箱中运行它 这是空气应用程序的代码: public class Downloader extends Sprite { private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf"); private var _main:NativeWindow; public function Downloa
public class Downloader extends Sprite
{
private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf");
private var _main:NativeWindow;
public function Downloader()
{
var loader:URLLoader = new URLLoader(REMOTE_FILE);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, downloadComplete);
}
private function downloadComplete(e:Event):void{
var ba:ByteArray = e.target.data;
var stream:FileStream = new FileStream();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba);
stream.close();
loadAndRunSwf();
}
private function loadAndRunSwf(){
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
////obsolete?
//var context:LoaderContext = new LoaderContext();
//context.allowLoadBytesCodeExecution = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context*/);
this._main.stage.addChild(loader);
this._main.activate();
}
}
downloadable.swf的代码:
public class Downloadable extends Sprite
{
private var _btn:Button = new Button();
private var _baseFolder:File = new File("app-storage:/");
public function downloadable_test()
{
this.addChild(_btn);
_btn.label = "access Harddisk";
...
}
}
所以现在,如果我运行Downloader,它将下载swf并尝试运行它,但我会在Downloadeable中得到一个异常
private var _baseFolder:File = new File("app-storage:/");
错误:
SecurityError: file
at runtime::SecurityManager$/checkPrivilegeForCaller()
那么-我需要做什么来防止此类安全错误呢?我希望我的远程SWF被视为本地代码,运行在与AIR代码相同的安全沙箱中。我不确定Android,但对于常规web播放器,您需要为加载程序上下文的SecurityDomain指定SecurityDomain.currentDomain,以便加载的代码在权限方面与加载程序相等。另外请注意,由于无法解释的原因,如果在从PC上的文件系统加载时使用SecurityDomain,Flash Player会投诉
无论多么复杂,Flash播放器的安全性往往是默默无闻的安全性。。。因此,如果它不能按照您的编码方式工作,请尝试Loader.loadBytes()“变通方法”。我遇到了与Mat类似的问题。使用SecurityDomain不起作用=我得到如下错误:SecurityError:error#2142:安全沙盒冲突:本地SWF文件无法使用LoaderContext.SecurityDomain属性。app:/game.swf试图加载app storage:/levels/00_intro.swf。你说得对。。。在到处乱写代码之后,Loader.loadBytes()似乎可以正常工作。谢谢。虽然看起来这个把戏只在电脑上起作用。。不在Android上:/抱歉。。我收回我最后的评论。。这是另一个Android问题(与桌面版本不兼容)。现在我可以确认它在Android上也能工作。
function loadAndRunSwf()
{
var context:LoaderContext=new LoaderContext(false);
context.allowCodeImport=true;
var ba:ByteArray=new ByteArray();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var fileStream:FileStream=new FileStream();
var loader:Loader = new Loader();
fileStream.open(file,"read");
fileStream.readBytes(ba);
ba.position=0;
loader.loadBytes(ba,context);
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
this._main.stage.addChild(loader);
this._main.activate();
}