+ private static final JSObject.JSFunction loadArchive = new JSObject.JSFunction() {
+ public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+ if (!ThreadMessage.suspendThread()) return null;
+
+ try {
+ if (args == null || args.length < 1 || args[0] == null) return null;
+ URL u = new URL(args[0].toString());
+
+ Function callback = null;
+ if (args.length == 2 && args[1] != null && args[1] instanceof Function) callback = (Function)args[1];
+
+ if (!u.getFile().endsWith(".xwar")) {
+ if (Log.on) Log.log(this, "Error: archive names must end with .xwar: " + u.getFile());
+ throw new JavaScriptException("Error: archive names must end with .xwar: " + u.getFile());
+ }
+
+ if (u.getProtocol().equals("http")) {
+ HTTP http = new HTTP(u.toString());
+ if (Main.originAddr == null) {
+ try {
+ Main.originAddr = InetAddress.getByName(u.getHost());
+ } catch (UnknownHostException e) {
+ if (Log.on) Log.log(this, "couldn't resolve " + u.getHost() + "; proceeding without permissions");
+ Main.originAddr = InetAddress.getByName("0.0.0.0");
+ }
+ } else {
+ Main.originAddr = InetAddress.getByName("0.0.0.0");
+ }
+ Resources.loadArchive(http.getInputStream(), (int)http.getContentLength(), callback);
+
+ } else if (u.getProtocol().equals("file")) {
+ if (Main.originAddr != null) {
+ if (Log.on) Log.log(this, "scripts downloaded from the network may not load xwars from the local filesystem");
+ throw new JavaScriptException("scripts downloaded from the network may not load xwars from the local filesystem");
+ }
+ Resources.loadArchive(new FileInputStream(u.getFile()), (int)new File(u.getFile()).length(), callback);
+
+ } else {
+ if (Log.on) Log.log(this, "unknown protocol \"" + u.getProtocol() + "\"");
+ throw new JavaScriptException("unknown protocol \"" + u.getProtocol() + "\"");
+ }
+
+ } catch (MalformedURLException me) {
+ if (Log.on) Log.log(this, "Malformed URL: " + args[0]);
+ if (Log.on) Log.log(this, me);
+ throw new JavaScriptException(me.toString());
+
+ } catch (IOException ioe) {
+ if (Log.on) Log.log(this, "IOException while loading archive:");
+ if (Log.on) Log.log(this, ioe);
+ throw new JavaScriptException(ioe.toString());
+
+ } finally {
+ ThreadMessage.resumeThread();
+
+ }
+ return null;
+ }
+ };
+
+ private static final JSObject.JSFunction prefetchImage = new JSObject.JSFunction() {
+ public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+ if (args == null || args.length < 1 || args[0] == null) return null;
+ Box.getImage(args[0].toString(), args.length > 1 && args[1] instanceof Function ? (Function)args[1] : null);
+ return null;
+ }
+ };
+