Changes to the MRJ Plugin Carbon for the Java Embedding Plugin

A) Changes related to the Java Embedding Plugin

1. Added calls to Java Embedding Plugin routines (all of whose names
   start with "JEP") to MRJSession.cpp and (especially) MRJContext.cp.
   Added supporting code to MRJContext.cp and MRJContext.h.  Added the
   API header file JavaEmbeddingPlugin.h.

2. Added JavaEmbeddingPlugin.cpp, which allows support for the Java
   Embedding Plugin API to be conditional:  Support is enabled only if
   JavaEmbeddingPlugin.bundle can be found.  Otherwise the MRJ Plugin
   Carbon falls back to using Apple's Java Embedding Framework.

3. Added MIME types for Java 1.4.X and 1.5 to the MRJ Plugin Carbon's
   STR# resources (in the Strings.rsrc file).

4. Changed various resource strings to make the copyright and version
   information consistent and to mention the Java Embedding Plugin.

5. Removed the JSObject class's "final" modifier (in JSObject.java in
   js/src/liveconnect/classes/netscape/javascript).  This allows
   apple.applet.JSObject (present in Java 1.4.2 through Update 2) or
   sun.plugin.javascript.webkit.JSObject (present in later versions)
   to override it -- which in turn allows the Java Embedding Plugin to
   load libAppletsInCocoa.jnilib (or JavaPluginCocoa in Update 1 and
   later), which avoids various UnsatisfiedLinkError messages.

6. Changed how the JVM is started and applets are created, to better
   accomodate the asynchronicity of these procedures.  The changes
   were much bigger for Java 1.4.X than for Java 1.3.1.  For more
   information see CreateJVM(), OpenSessionJNI(), InitSecurityJNI()
   and afterInitAWT() in MRJSession.cpp, appletCreatedCallback() and
   loadApplet() in MRJContext.cp, and AppletView's loadAWTLibraries
   and initWithParams methods in JEP's AppletView.m.

7. Now detects whether or not the HTML that invoked an applet
   specified its "width" and "height" using absolute numbers of pixels
   or percentages of the space available.  In the latter case (on Java
   1.4.X and higher) the Java Embedding Plugin will resize the applet
   "natively".  See MRJContext::fixAttributes(), JEPController
   fixAttributes and AppletFrame.resizeApplet() for more information.

8. Tightened up the code that implements security settings, and
   altered it to accomodate the different needs of the Java 1.4.X and
   Java 1.3.1 JVMs.  See MRJSession.java for more information.

9. Added a JEPDoLiveConnectProxy() call to the Java Embedding Plugin
   API to make it possible to do JavaScript-to-Java LiveConnect on
   Tiger.  The Mozilla-family browsers always do JavaScript on the
   main thread.  But for Tiger compatibility, JavaScript-to-Java calls
   that can trigger Java drawing need to run on a secondary thread.
   This is handled by JEPDoLiveConnectProxy() in the JEP's
   Controller.m and by LiveConnect.java.  The new API also ensures (on
   all Java 1.4.X and 1.5.X versions) that an autorelease pool is in
   place during JavaScript-to-Java LiveConnect.

10. Added support for getting and setting cookies (via the
   nsICookieStorage interface) in Mozilla.org browsers.

   See MRJGetCookie() and MRJSetCookie() in MRJPlugin.cpp.

11. Added support for getting a JSObject for a particular
   MRJPluginInstance without using the Java-to-JavaScript
   JSObject.getWindow() method.  This is needed to support automatic
   proxy configuration.

   For more information see MRJGetJSObject() and the
   GetWindowAltMessage class in LiveConnectNativeMethods.cpp.

B) Unrelated Bug Fixes or Changes

1. Disabled all the "SupportsMixin" code.

   As best I can tell this code is supposed to make it easier to use
   XPCOM's "QueryInterface" functionality (see
   http://www.geocrawler.com/archives/3/137/1999/2/0/1048574/, which
   is the only reference I've been able to find outside the source
   code itself).  But in at least one case, it causes a crash by (in
   effect) making QueryInterface() return the wrong answer!  (The
   browser queries for the nsIJVMPluginInstance interface on an
   MRJPluginInstance object, with the intention of calling its
   GetJavaObject() method.  Instead it gets the nsIPluginInstance
   interface and ends up calling the Initialize() method ... which of
   course leads to a crash.)

   I didn't feel like spending huge amounts of time fixing the
   SupportsMixin code (which is very cryptic) and then testing it
   comprehensively.  So I just disabled all of it (which only took a
   couple of hours).

2. Fixed a crash bug in GetLiveConnectProxy() in CSecureEnv.cpp --
   until I added a test to prevent it, the code could (and often did)
   dereference a null pointer.

3. Fixed a couple of Java class specifications in JNI code in
   CSecureEnv.cpp -- they used '.' for the path separator, but it
   should be '/'.

4. Added a PkgInfo file:  This makes OS X recognize the MRJ Plugin
   Carbon as a plugin.

5. Got rid of a block of code in MRJContext.cp's
   synchronizeVisibility() method which was causing applets to display
   22 pixels higher than they should display -- whether using the Java
   Embedding Plugin or the Java Embedding Framework.

6. Changed the MRJSession::getCurrentEnv() method (in plugin/oji/
   MRJCarbon/plugin/Source/MRJSession.cpp) to always return (and if
   necessary create) a "JNI environment" usable in the current thread.

7. Changed the getCurrentClassLoader() method of the
   StubSecurityManager class (in JNIUtils.java) to get it to work
   properly when we're running on the main thread with AllPermission.
   The previous (unfixed) version caused JSObject.getWindow() to
   always return null, which in turn disabled the Java-to-JavaScript
   side of LiveConnect.

8. Changed the ToGlobalRef(), Wrap_JSObject() and Unwrap_JSObject()
   routines in LiveConnectNativeMethods.cpp and the GetVersion(),
   FatalError(), DeleteGlobalRef() and GetJavaVM() routines in
   CSecureEnv.cpp, to ensure that their 'env' parameters always point
   to a "native" JNIEnv structure (instead of sometimes to a
   ProxyJNIEnv class).  Before this change, LiveConnect methods other
   than JSObject.getWindow() would sometimes crash mysteriously.

9. Optimized Java-to-JavaScript calls -- speeding them up considerably
   and avoiding certain kinds of hangs.  Previously, making a
   Java-to-JavaScript call entailed posting a message to a different
   thread, which would in turn post an encapsulated message to the
   "JavaScript thread" (i.e. the main thread).  Now, if we're
   currently running on the main thread I just execute the message
   directly (this avoids the hang that would occur if we waited (on
   the main thread) for the message to be executed (on the main
   thread)).  And if we're currently running on a secondary thread, I
   just post the message to the main thread -- using either
   nsIJVMThreadManager::PostEvent() (on Java 1.3.1) or
   JEPRunOnMainThread() on more recent Java versions.
   JEPRunOnMainThread() is much more efficient than
   nsIJVMThreadManager::PostEvent(), and less likely to cause hangs.
   For more information see sendMessage() in
   LiveConnectNativeMethods.cpp.

10. Tightened up the code that detects when an applet has been or is
   being loaded or destroyed.  For more information do a
   case-insensitive search on "appletLoading", "isDestroying" and
   "isValidInstance" in MRJContext.cp and MRJPlugin.cpp.

11. Ensured that all Java-to-Javascript LiveConnect methods (members
   of the JSObject class) wait, if need be, until the JavaScript
   methods they invoke are finished, and that the current plugin
   instance stays in existence while they're waiting.

   Some of these methods can return values.  But when they're not
   called on the main thread, their JavaScript counterparts are
   invoked, asynchronously, on a different thread (the "JavaScript
   thread", which is the main thread).  The original MRJ Plugin's
   Java-to-Javascript calls make no provision to wait until the
   JavaScript methods they've invoked have returned.  In most cases
   these methods do return quickly enough, and there isn't any
   trouble.  But sometimes they don't -- particularly when a
   JavaScript method displays an alert box (in which case it doesn't
   return until the user dismisses the box).  This leads to crashes
   and other wierdness.

   Version 0.8.3 of the MRJ Plugin JEP included a partial fix for this
   problem which used a new call in the Embedding Plugin API --
   JEPRunJRunnableOnMainThread().  But this "fix" didn't address the
   real problem (which at that point I didn't understand), and I have
   now removed it.  The MRJ Plugin JEP no longer has any "extraneous"
   dependencies on JavaEmbeddingPlugin.bundle.

   As of JEP 0.8.7, the Java-to-JavaScript methods that return values
   can safely be made to time out after 2 seconds -- this is now the
   default.

   For more information see the JavaMessage class in
   JavaMessageQueue.h and the code for all the Java-to-JavaScript
   methods that return values in LiveConnectNativeMethods.cpp (an
   example is Java_netscape_javascript_JSObject_getWindow()).

12. Added a fix for the "Sun Java Plugin Arbitrary Package Access
   Vulnerability" (which effects JavaScript-to-Java LiveConnect), as
   reported at the following URLs:

   http://securityfocus.com/archive/1/382072/2004-11-23/2004-11-29/0
   http://securityfocus.com/archive/1/381940/2004-11-16/2004-11-22/0

   The fix works for both Java 1.4.X (what you get when you use the
   MRJ Plugin JEP with JavaEmbeddingPlugin.bundle) and Java 1.3.1
   (what you get when you use the MRJ Plugin JEP by itself).  Earlier
   versions (prior to 0.8.8) of the Java Embedding Plugin are
   vulnerable, in both configurations.

   As of version 0.8.8 the Java Embedding Plugin now passes the test
   for this vulnerability at the following URLs, in both
   configurations:

   http://bcheck.scanit.be/bcheck/
   http://bcheck.scanit.be/bcheck/choosetests.php

   You might think that it's Apple's responsibility to fix this.  But
   none of Apple's own products are vulnerable -- either because (like
   the Java Applet.plugin and earlier versions of Safari) they don't
   support LiveConnect at all, or because (like more recent versions
   of Safari) their LiveConnect implementations have somehow avoided
   the problem.  So I suspect that Apple may never release versions of
   Java 1.4.X or Java 1.3.1 to address it.

   Interestingly, the bcheck test crashes the current version of
   Safari (the one that's included in Apple's 2004-12-02 security
   update).  But I wrote a simpler test of my own (a "hello world"
   applet plus the proof-of-concept JavaScript code given in Jouko
   Pynnonen's advisory), which doesn't crash Safari and does (I think)
   show that it's not vulnerable.

   For more information see checkClassAccess() in JNIUtils.java and
   Java_java_lang_Class_forName0_redirect() and InitSecurityJNI() in
   MRJSession.cpp.

13. Added code to fix a design flaw in the original MRJ Plugin -- it
   sometimes fails to elevate a signed applet's privileges when the
   applet's code is called via LiveConnect.  This helps when
   connecting to http://hushmail.com/.  For more information see
   fixProtectionDomains() in JNIUtils.java.

14. Fixed a bug in MRJContext::setWindow() that occasionally causes it
   to crash (with an access violation) when an applet is loaded --
   sometimes loadApplet() takes so long that mPluginWindow is invalid
   by the time it returns.

15. Fixed a (minor) security hole in all of Apple's Java versions
   (from 1.3.1 to 1.5.0), and in previous versions of the MRJPlugin
   and MRJPlugin JEP, that allows an applet "Frame"'s methods (to
   which access is normally forbidden) to be called from JavaScript.
   For more information search on "getAppletFrame" in MRJSession.cpp.

16. Added code to work around what I call the "spurious updates"
   bug(s) in the Mozilla family of browsers.  All of these browsers
   trigger large numbers of unnecessary screen updates (which trigger
   calls to MRJContext::drawApplet()) and make many redundant calls to
   SetWindow().  Camino even triggers one screen update for every
   keystroke while the focus is in an applet.  Unless we do something
   about it, this causes applets to flash annoyingly, sometimes
   several times per second.

   Some of the workarounds are in the MRJ Plugin JEP, in drawApplet(),
   setWindow(), checkUpdateNeeded() and checkPaintNeeded() in
   MRJContext.cp.  But, as of JEP 0.9.3, even more are in
   JavaEmbeddingPlugin.bundle.  See item #1 in that version's
   Changes.txt.

17. Fixed a bug in the MRJPlugin that is triggered by more than one
   applet on the same page doing Java-to-JavaScript LiveConnect -- the
   MRJPlugin is sometimes mistaken about which applet a given call
   comes from.  This caused some of the problems reported in bug
   1086807 (at the following URL):

   http://sourceforge.net/tracker/index.php?func=detail&aid=1086807&group_id=107955&atid=649116

   For more information see GetAnInstance(JNIEnv *) and
   GetAppletInstance(JNIEnv *, jobject) in
   LiveConnectNativeMethods.cpp, and getApplet() in JSObject.java.

18. Worked around a bug/design-flaw in Firefox 1.0RC1 (and later
   versions on the "Aviary" branch) and Mozilla 1.7.5 (and later
   versions on the "Mozilla 1.7" branch) which caused the MRJ Plugin
   to crash when attempting JavaScript-to-Java LiveConnect.  For more
   information see sendMessageToJava() in CSecureEnv.cpp.

19. Fixed a major security hole in JavaScript-to-Java LiveConnect
   triggered by recent changes to the nsIScriptSecurityManager
   interface on the Mozilla "trunk".  The hole effects all nightlies,
   alphas and betas of Mozilla-family browsers issued since
   2004-04-25, but not any of the released versions.  So, for example,
   Firefox 1.0.X, Mozilla 1.7.X and Camino 0.8.X aren't vulnerable.

   In browsers effected by this hole, potentially all security checks
   are disabled when doing JavaScript-to-Java LiveConnect.  So, for
   example, my fix for the ""Sun Java Plugin Arbitrary Package Access
   Vulnerability" (item #B12 above) doesn't work on any of the
   vulnerable browsers on JEP versions earlier than 0.9.2 (the first
   version to include this fix).

   For more information see sendMessageToJava() in CSecureEnv.cpp and
   https://bugzilla.mozilla.org/show_bug.cgi?id=293973.  Though some
   of these changes took place quite a while ago, I was unaware until
   very recently of those on the "trunk" -- because JavaScript-to-Java
   LiveConnect appeared to continue to work, though its security was
   broken.

   Part of my fix for this problem is code that will cause
   JavaScript-to-Java LiveConnect to fail if/when Mozilla.org makes
   further changes to the nsIScriptSecurityManager interface that the
   MRJ Plugin JEP is not yet aware of.

20. Fixed a problem with Java-to-JavaScript LiveConnect that caused
   hangs when several calls are made in succession to JSObject.call()
   or JSObject.eval() that trigger the display of JavaScript alert()
   messages.  For more information see the CallMessage and EvalMessage
   classes in LiveConnectNativeMethods.cpp and other code nearby.

21. Made changes to allow building the MRJ Plugin JEP inside more
   recent Mozilla-family source distros than Mozilla 1.6.  For more
   information see particularly the (conditional) definitions of
   nsPluginThreadPtr and nsPluginThread in MRJPlugin.h (together with
   the preceeding comment), and the conditional definition of
   NS_COMPONENTMANAGER_CID in LiveConnectNativeMethods.cpp.

   The MRJ Plugin JEP can now (as of JEP 0.9.3) be built inside a
   built-from-source copy of any Mozilla-family browser from Mozilla
   1.6 on, except for source trees pulled from the trunk between
   2004-04-02 and 2004-05-13.  The comment above the definition of
   nsPluginThreadPtr in MRJPlugin.h shows why this exclusion is
   necessary.

22. The Java Embedding Framework is now loaded dynamically.  And the
   MRJ Plugin JEP refuses to instantiate itself if neither the Java
   Embedding Framework nor the Java Embedding Plugin are present.
   This is needed because the Java Embedding Framework will disappear
   (along with Java 1.3.1) in the transition from PPC to Intel.

   For more information see JavaEmbeddingFramework.cpp and
   NSGetFactory() in MRJPlugin.cpp.

23. Changed the resource files in the plugin/oji/MRJCarbon/plugin/
   Resources/ directory from binary files (with names ending in
   *.rsrc) to text files (with names ending in *.r).  This makes them
   much easier to edit.

