home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.applet.AudioClip;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.Window;
- import java.io.PrintStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import netscape.net.URLStreamHandlerFactory;
- import sun.awt.image.URLImageSource;
-
- class MozillaAppletContext implements AppletContext {
- int frameMWContext;
- Mapping appletFrames;
- Hashtable imageCache;
- Hashtable audioClipCache;
- URL documentURL;
- Integer contextID;
- static Console console;
- static int totalApplets;
- static int trimThreshold;
- static boolean noisyTrimming;
- static Mapping appletContexts = new Mapping();
- static int debug;
-
- MozillaAppletContext(Integer contextID) {
- this.contextID = contextID;
- this.appletFrames = new Mapping();
- this.imageCache = new Hashtable();
- this.audioClipCache = new Hashtable();
- }
-
- static void indent(PrintStream out, int i) {
- while(true) {
- --i;
- if (i < 0) {
- return;
- }
-
- out.print(" ");
- }
- }
-
- void dumpState(PrintStream out, int i) {
- indent(out, i);
- out.println("MozillaAppletContext #frames=" + this.appletFrames.size() + " #images=" + this.imageCache.size() + " #audioClips=" + this.audioClipCache.size() + " url=" + this.documentURL);
- int size = this.appletFrames.size();
-
- for(int j = 0; j < size; ++j) {
- EmbeddedAppletFrame eaf = (EmbeddedAppletFrame)this.appletFrames.elementAt(j);
- eaf.dumpState(out, i + 1);
- }
-
- if (this.imageCache.size() > 0) {
- indent(out, i + 1);
- out.println("image cache:");
- Enumeration e = this.imageCache.keys();
-
- while(e.hasMoreElements()) {
- URL u = (URL)e.nextElement();
- indent(out, i + 2);
- out.println(u.toString());
- }
- }
-
- if (this.audioClipCache.size() > 0) {
- indent(out, i + 1);
- out.println("audio clip cache:");
- Enumeration e = this.audioClipCache.keys();
-
- while(e.hasMoreElements()) {
- URL u = (URL)e.nextElement();
- indent(out, i + 2);
- out.println(u.toString());
- }
- }
-
- }
-
- void destroy() {
- if (debug > 0) {
- System.err.println("# destroy acx=" + this + " frameMWContext=" + this.frameMWContext);
- }
-
- this.destroyApplets();
- this.frameMWContext = 0;
- this.appletFrames = null;
- this.imageCache = null;
- this.audioClipCache = null;
- appletContexts.remove(this.contextID);
- }
-
- public AudioClip getAudioClip(URL url) {
- AudioClip clip = lookupAudioClip(this, url);
- return clip;
- }
-
- public Image getImage(URL url) {
- Image img = lookupImage(this, url);
- return img;
- }
-
- public Applet getApplet(String name) {
- int size = this.appletFrames.size();
-
- for(int i = 0; i < size; ++i) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.elementAt(i);
- if (name.equals(frame.applet.getParameter("name"))) {
- return frame.applet;
- }
- }
-
- return null;
- }
-
- public Enumeration getApplets() {
- int size = this.appletFrames.size();
- Vector v = new Vector(size);
-
- for(int i = 0; i < size; ++i) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.elementAt(i);
- v.addElement(frame.applet);
- }
-
- return v.elements();
- }
-
- public void showDocument(URL url) {
- this.showDocument(url, "_top");
- }
-
- public void showDocument(URL url, String target) {
- this.pShowDocument(url.toExternalForm(), (String)null, target);
- }
-
- public void showStatus(String status) {
- this.pShowStatus(status);
- }
-
- public void mochaOnLoad(int result) {
- this.pMochaOnLoad(result);
- }
-
- private native void pShowDocument(String var1, String var2, String var3);
-
- private native void pShowStatus(String var1);
-
- private native void pMochaOnLoad(int var1);
-
- static synchronized AudioClip lookupAudioClip(MozillaAppletContext incx, URL url) {
- int size = appletContexts.size();
-
- for(int i = 0; i < size; ++i) {
- MozillaAppletContext acx = (MozillaAppletContext)appletContexts.elementAt(i);
- Object clip = acx.audioClipCache.get(url);
- if (clip != null) {
- return (AudioClip)clip;
- }
- }
-
- AudioClip clip = new AppletAudioClip(url);
- incx.audioClipCache.put(url, clip);
- if (debug > 0) {
- System.err.println("# New audio clip: " + url);
- }
-
- return clip;
- }
-
- static synchronized Image lookupImage(MozillaAppletContext incx, URL url) {
- int size = appletContexts.size();
-
- for(int i = 0; i < size; ++i) {
- MozillaAppletContext acx = (MozillaAppletContext)appletContexts.elementAt(i);
- Object image = acx.imageCache.get(url);
- if (image != null) {
- return (Image)image;
- }
- }
-
- Image image;
- try {
- URLImageSource source = new URLImageSource(url);
- image = Toolkit.getDefaultToolkit().createImage(source);
- incx.imageCache.put(url, image);
- if (debug > 0) {
- System.err.println("# New image: " + url);
- }
- } catch (Exception var6) {
- image = null;
- }
-
- return image;
- }
-
- synchronized void initApplet(int appletID, String docURL, Hashtable params) {
- if (debug > 0) {
- System.err.println("# initApplet: appletID=" + appletID);
- }
-
- try {
- this.documentURL = new URL(docURL);
- } catch (MalformedURLException var17) {
- if (debug > 0) {
- System.err.println("# Malformed documentURL: " + docURL);
- }
-
- this.mochaOnLoad(-1);
- return;
- }
-
- String codebase = (String)params.get("codebase");
- if (codebase == null) {
- int tail = docURL.lastIndexOf(47) + 1;
- codebase = docURL.substring(0, tail);
- } else {
- if (!codebase.endsWith("/")) {
- codebase = codebase + "/";
- }
-
- try {
- URL u = new URL(this.documentURL, codebase);
- codebase = u.toString();
- int tail = codebase.lastIndexOf(47) + 1;
- if (tail != codebase.length() - 1) {
- codebase = codebase.substring(0, tail);
- }
- } catch (MalformedURLException var16) {
- }
-
- if (!docURL.startsWith("file:") && codebase.startsWith("file:")) {
- throw new AppletSecurityException("AppletContext: Can't use file:// URL in CODEBASE spec", codebase);
- }
- }
-
- params.put("codebase", codebase);
- URL codebaseURL = this.documentURL;
-
- try {
- codebaseURL = new URL(codebase);
- } catch (MalformedURLException var15) {
- }
-
- String archive = (String)params.get("archive");
- if (archive == null) {
- int tail = docURL.lastIndexOf(47) + 1;
- archive = docURL.substring(0, tail);
- } else {
- try {
- int commaIndex = archive.indexOf(44);
- if (commaIndex != -1) {
- archive = archive.substring(0, commaIndex);
- }
-
- URL u = new URL(codebaseURL, archive);
- archive = u.toString();
- } catch (MalformedURLException var14) {
- }
- }
-
- params.put("archive", archive);
- URL archiveURL = codebaseURL;
-
- try {
- archiveURL = new URL(archive);
- } catch (MalformedURLException var13) {
- }
-
- Integer frameKey = new Integer(appletID);
- EmbeddedAppletFrame frame = new EmbeddedAppletFrame(this.documentURL, codebaseURL, archiveURL, params, this, frameKey);
- this.appletFrames.put(frameKey, frame);
- ++totalApplets;
- if (debug > 0) {
- System.err.println("# total applets=" + totalApplets);
- }
-
- if (debug > 0) {
- String p = "";
-
- Object key;
- for(Enumeration e = params.keys(); e.hasMoreElements(); p = p + (String)key + "=" + (String)params.get(key) + " ") {
- key = e.nextElement();
- }
-
- System.err.println("# New applet: " + appletID + " at " + codebaseURL + " " + p);
- }
-
- frame.start();
- ((Window)frame).pack();
- ((Window)frame).show();
- frame.sendEvent(1);
- frame.sendEvent(2);
- }
-
- EmbeddedAppletFrame getAppletFrame(Integer appletID) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.get(appletID);
- if (frame == null && debug > 0) {
- System.err.println("# Warning: AppletFrame not found for appletID " + appletID);
- }
-
- return frame;
- }
-
- synchronized void startApplet(int appletID) {
- if (debug > 0) {
- System.err.println("# startApplet: appletID=" + appletID);
- }
-
- EmbeddedAppletFrame frame = this.getAppletFrame(new Integer(appletID));
- if (frame == null) {
- System.err.println("---> applet must have been pruned: " + appletID + " on " + this.documentURL);
- } else {
- frame.inHistory = false;
- frame.sendEvent(3);
- }
- }
-
- synchronized void stopApplet(int appletID) {
- if (debug > 0) {
- System.err.println("# stopApplet: appletID=" + appletID);
- }
-
- EmbeddedAppletFrame frame = this.getAppletFrame(new Integer(appletID));
- if (frame != null) {
- frame.inHistory = true;
- frame.sendEvent(4);
- trimApplets();
- }
- }
-
- synchronized void destroyApplet(Integer appletID) {
- if (noisyTrimming && debug > 0) {
- System.err.println("# destroyApplet: appletID=" + appletID);
- }
-
- EmbeddedAppletFrame frame = this.getAppletFrame(appletID);
- if (frame != null) {
- --totalApplets;
- if (noisyTrimming && debug > 0) {
- System.err.println("# total applets=" + totalApplets);
- }
-
- if (!noisyTrimming) {
- frame.noisy = false;
- }
-
- this.appletFrames.remove(appletID);
- ThreadGroup group = frame.handler.getThreadGroup();
- synchronized(group){}
-
- try {
- frame.sendEvent(4);
- frame.sendEvent(5);
- frame.sendEvent(0);
-
- try {
- group.wait(1000L);
- } catch (InterruptedException var11) {
- }
-
- if (group.activeCount() > 0) {
- SecurityManager.setScopePermission();
- group.stop();
- SecurityManager.resetScopePermission();
-
- try {
- group.wait(10000L);
- } catch (InterruptedException var10) {
- }
- }
-
- try {
- SecurityManager.setScopePermission();
- group.destroy();
- SecurityManager.resetScopePermission();
- } catch (Exception var9) {
- }
- } catch (Throwable var12) {
- throw var12;
- }
-
- }
- }
-
- synchronized void iconifyApplets() {
- if (debug > 0) {
- System.err.println("# iconifyApplets");
- }
-
- int size = this.appletFrames.size();
-
- for(int i = 0; i < size; ++i) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.elementAt(i);
- if (debug > 0) {
- System.err.println("# iconifyApplets: stopping appletID " + frame.pData);
- }
-
- frame.sendEvent(4);
- }
-
- }
-
- synchronized void uniconifyApplets() {
- if (debug > 0) {
- System.err.println("# uniconifyApplets");
- }
-
- int size = this.appletFrames.size();
-
- for(int i = 0; i < size; ++i) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)this.appletFrames.elementAt(i);
- if (debug > 0) {
- System.err.println("# uniconifyApplets: starting appletID " + frame.pData);
- }
-
- frame.sendEvent(3);
- }
-
- }
-
- synchronized Object reflectApplet(int appletID) {
- if (debug > 0) {
- System.err.println("# reflectApplet: appletID=" + appletID);
- }
-
- EmbeddedAppletFrame frame = this.getAppletFrame(new Integer(appletID));
- return frame == null ? null : frame.applet;
- }
-
- synchronized void destroyApplets() {
- if (debug > 0) {
- System.err.println("# destroyApplets");
- }
-
- int size = this.appletFrames.size();
-
- for(int i = size - 1; i >= 0; --i) {
- Integer frame = (Integer)this.appletFrames.keyAt(i);
- this.destroyApplet(frame);
- }
-
- }
-
- static void init() {
- console = new Console();
- System.setProperties(new AppletProperties());
- URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory());
- System.setSecurityManager(new AppletSecurity());
- }
-
- static void setConsoleState(int newstate) {
- setConsoleState0(newstate);
- }
-
- static native void setConsoleState0(int var0);
-
- static void showConsole() {
- console.show();
- }
-
- static void hideConsole() {
- console.hide();
- }
-
- static int trimApplets(int numberToTrim, boolean trimOnlyStoppedApplets) {
- int numberTrimmed = trimApplets1(numberToTrim, true);
- if (numberTrimmed < numberToTrim && !trimOnlyStoppedApplets) {
- numberTrimmed += trimApplets1(numberToTrim - numberTrimmed, false);
- }
-
- return numberTrimmed;
- }
-
- static int trimApplets1(int numberToTrim, boolean trimOnlyStoppedApplets) {
- int numberTrimmed;
- for(numberTrimmed = 0; numberToTrim-- > 0; ++numberTrimmed) {
- long oldestTime = -1L;
- EmbeddedAppletFrame oldestFrame = null;
- MozillaAppletContext contextOfOldest = null;
- int size = appletContexts.size();
-
- for(int i = 0; i < size; ++i) {
- MozillaAppletContext acx = (MozillaAppletContext)appletContexts.elementAt(i);
- int size2 = acx.appletFrames.size();
-
- for(int j = 0; j < size2; ++j) {
- EmbeddedAppletFrame frame = (EmbeddedAppletFrame)acx.appletFrames.elementAt(j);
- if ((!trimOnlyStoppedApplets || frame.inHistory) && (oldestTime == -1L || frame.timestamp < oldestTime)) {
- oldestTime = frame.timestamp;
- oldestFrame = frame;
- contextOfOldest = acx;
- }
- }
- }
-
- if (oldestFrame == null) {
- if (noisyTrimming && debug > 0) {
- System.err.println("# No stopped applets to prune.");
- }
-
- return numberTrimmed;
- }
-
- try {
- if (noisyTrimming) {
- Applet applet = oldestFrame.applet;
- String name = null;
- if (applet != null) {
- name = applet.getParameter("name");
- }
-
- if (name == null) {
- name = applet.getClass().getName().toString();
- }
-
- System.err.println("# Pruning applet " + name + " from " + contextOfOldest.documentURL + " to save memory.");
- if (debug > 0) {
- System.err.println("# Pruning appletID=" + oldestFrame.pData + " contextID=" + contextOfOldest.contextID + " applet=" + oldestFrame.applet);
- }
- }
- } catch (Throwable var14) {
- }
-
- try {
- contextOfOldest.destroyApplet(oldestFrame.appletID);
- } catch (Throwable var13) {
- }
- }
-
- return numberTrimmed;
- }
-
- static void trimApplets() {
- trimApplets(totalApplets - trimThreshold, true);
- }
-
- static MozillaAppletContext getAppletContext(int contextID) {
- Integer key = new Integer(contextID);
- MozillaAppletContext context = (MozillaAppletContext)appletContexts.get(key);
- if (context == null && debug > 0) {
- System.err.println("# Warning: applet context not found for contextID " + contextID);
- }
-
- return context;
- }
-
- static MozillaAppletContext ensureAppletContext(int contextID) {
- Integer key = new Integer(contextID);
- MozillaAppletContext context = (MozillaAppletContext)appletContexts.get(key);
- if (context == null) {
- context = new MozillaAppletContext(key);
- appletContexts.put(key, context);
- }
-
- return context;
- }
-
- static void initApplet(int parentContext, int frameContext, int contextID, int appletID, String[] argv) {
- Hashtable params = new Hashtable();
-
- for(int i = 1; i < argv.length; ++i) {
- String str = argv[i];
- int j = str.indexOf(61);
- if (j >= 0) {
- params.put(str.substring(0, j).toLowerCase(), str.substring(j + 1));
- }
- }
-
- String dbg = (String)params.get("debug");
- if (dbg != null) {
- try {
- debug = Integer.parseInt(dbg);
- } catch (Exception var9) {
- }
- }
-
- if (debug > 0) {
- System.err.println("# initApplet: contextID=" + contextID + " appletID=" + appletID + " parentContext=" + parentContext + " frameContext=" + frameContext);
- }
-
- trimApplets();
- MozillaAppletContext acx = ensureAppletContext(contextID);
- acx.frameMWContext = frameContext;
- acx.initApplet(appletID, argv[0], params);
- }
-
- static void startApplet(int contextID, int appletID, int newFrameMWContext) {
- if (debug > 0) {
- System.err.println("# startApplet: contextID=" + contextID + " appletID=" + appletID + " newFrameMWContext=" + newFrameMWContext);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.frameMWContext = newFrameMWContext;
- acx.startApplet(appletID);
- }
- }
-
- static void stopApplet(int contextID, int appletID) {
- if (debug > 0) {
- System.err.println("# stopApplet: contextID=" + contextID + " appletID=" + appletID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.frameMWContext = 0;
- acx.stopApplet(appletID);
- trimApplets();
- }
- }
-
- static void destroyApplet(int contextID, int appletID) {
- if (debug > 0) {
- System.err.println("# destroyApplet: contextID=" + contextID + " appletID=" + appletID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.destroyApplet(new Integer(appletID));
- if (acx.appletFrames.isEmpty()) {
- if (debug > 0) {
- System.err.println("# destroyApplet: destroying context for contextID " + contextID);
- }
-
- acx.destroy();
- }
-
- }
- }
-
- static Object reflectApplet(int contextID, int appletID) {
- if (debug > 0) {
- System.err.println("# reflectApplet: contextID=" + contextID + " appletID=" + appletID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- return acx == null ? null : acx.reflectApplet(appletID);
- }
-
- static void iconifyApplets(int contextID) {
- if (debug > 0) {
- System.err.println("# iconifyApplets: contextID=" + contextID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.iconifyApplets();
- }
- }
-
- static void uniconifyApplets(int contextID) {
- if (debug > 0) {
- System.err.println("# uniconifyApplets: contextID=" + contextID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.uniconifyApplets();
- }
- }
-
- static void destroyApplets(int contextID) {
- if (debug > 0) {
- System.err.println("# destroyApplets: contextID=" + contextID);
- }
-
- MozillaAppletContext acx = getAppletContext(contextID);
- if (acx != null) {
- acx.destroy();
- }
- }
-
- static void destroyAll() {
- if (debug > 0) {
- System.err.println("# destroyAll");
- }
-
- int size = appletContexts.size();
-
- for(int i = size - 1; i >= 0; --i) {
- MozillaAppletContext acx = (MozillaAppletContext)appletContexts.elementAt(i);
- acx.destroy();
- }
-
- }
-
- static void dumpState(PrintStream out) {
- int size = appletContexts.size();
-
- for(int i = 0; i < size; ++i) {
- MozillaAppletContext acx = (MozillaAppletContext)appletContexts.elementAt(i);
- acx.dumpState(out, 0);
- }
-
- }
- }
-