Index: extensions/java/xpcom/nsJavaInterfaces.cpp
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/Attic/nsJavaInterfaces.cpp,v
retrieving revision 1.26.2.5
diff -u -6 -p -r1.26.2.5 nsJavaInterfaces.cpp
--- extensions/java/xpcom/nsJavaInterfaces.cpp	18 Jan 2007 19:36:12 -0000	1.26.2.5
+++ extensions/java/xpcom/nsJavaInterfaces.cpp	21 Mar 2007 22:03:26 -0000
@@ -427,6 +427,70 @@ MOZILLA_NATIVE(getNativeHandleFromAWT) (
 #else
   NS_WARNING("getNativeHandleFromAWT JNI method not implemented");
 #endif
 
   return handle;
 }
+
+extern "C" NS_EXPORT jlong JNICALL
+JXUTILS_NATIVE(wrapJavaObject) (JNIEnv* env, jobject, jobject aJavaObject,
+                                jstring aIID)
+{
+  nsresult rv;
+  nsISupports* xpcomObject = nsnull;
+
+  if (!aJavaObject || !aIID) {
+    rv = NS_ERROR_NULL_POINTER;
+  } else {
+    const char* str = env->GetStringUTFChars(aIID, nsnull);
+    if (!str) {
+      rv = NS_ERROR_OUT_OF_MEMORY;
+    } else {
+      nsID iid;
+      if (iid.Parse(str)) {
+        rv = GetNewOrUsedXPCOMObject(env, aJavaObject, iid, &xpcomObject);
+      } else {
+        rv = NS_ERROR_INVALID_ARG;
+      }
+
+      env->ReleaseStringUTFChars(aIID, str);
+    }
+  }
+
+  if (NS_FAILED(rv)) {
+    ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
+  }
+  return NS_REINTERPRET_CAST(jlong, xpcomObject);
+}
+
+extern "C" NS_EXPORT jobject JNICALL
+JXUTILS_NATIVE(wrapXPCOMObject) (JNIEnv* env, jobject, jlong aXPCOMObject,
+                                 jstring aIID)
+{
+  nsresult rv;
+  jobject javaObject = nsnull;
+  nsISupports* xpcomObject = NS_REINTERPRET_CAST(nsISupports*, aXPCOMObject);
+
+  if (!xpcomObject || !aIID) {
+    rv = NS_ERROR_NULL_POINTER;
+  } else {
+    const char* str = env->GetStringUTFChars(aIID, nsnull);
+    if (!str) {
+      rv = NS_ERROR_OUT_OF_MEMORY;
+    } else {
+      nsID iid;
+      if (iid.Parse(str)) {
+        // XXX Should we be passing something other than NULL for aObjectLoader?
+        rv = GetNewOrUsedJavaObject(env, xpcomObject, iid, nsnull, &javaObject);
+      } else {
+        rv = NS_ERROR_INVALID_ARG;
+      }
+
+      env->ReleaseStringUTFChars(aIID, str);
+    }
+  }
+
+  if (NS_FAILED(rv)) {
+    ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
+  }
+  return javaObject;
+}
Index: extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp,v
retrieving revision 1.3.4.8
diff -u -6 -p -r1.3.4.8 nsJavaXPCOMGlue.cpp
--- extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp	1 Feb 2007 22:31:07 -0000	1.3.4.8
+++ extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp	21 Mar 2007 22:03:26 -0000
@@ -81,16 +81,18 @@ enum {
   kFunc_GetServiceManager,
   kFunc_NewLocalFile,
   kFunc_CallXPCOMMethod,
   kFunc_FinalizeProxy,
   kFunc_IsSameXPCOMObject,
   kFunc_ReleaseProfileLock,
-  kFunc_GetNativeHandleFromAWT
+  kFunc_GetNativeHandleFromAWT,
+  kFunc_WrapJavaObject,
+  kFunc_WrapXPCOMObject
 };
 
-#define JX_NUM_FUNCS 16
+#define JX_NUM_FUNCS 18
 
 
 // Get path string from java.io.File object.
 jstring
 GetJavaFilePath(JNIEnv* env, jobject aFile)
 {
@@ -163,12 +165,16 @@ LoadXULMethods(JNIEnv* env, jobject aXPC
     { "_Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject@16",
             (NSFuncPtr*) &aFunctions[kFunc_IsSameXPCOMObject] },
     { "_Java_org_mozilla_xpcom_ProfileLock_release@16",
             (NSFuncPtr*) &aFunctions[kFunc_ReleaseProfileLock] },
     { "_Java_org_mozilla_xpcom_internal_MozillaImpl_getNativeHandleFromAWT@12",
             (NSFuncPtr*) &aFunctions[kFunc_GetNativeHandleFromAWT] },
+    { "_Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapJavaObject@16",
+            (NSFuncPtr*) &aFunctions[kFunc_WrapJavaObject] },
+    { "_Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapXPCOMObject@20",
+            (NSFuncPtr*) &aFunctions[kFunc_WrapXPCOMObject] },
     { nsnull, nsnull }
   };
 #else
   nsDynamicFunctionLoad funcs[] = {
     { "Java_org_mozilla_xpcom_internal_MozillaImpl_initialize",
             (NSFuncPtr*) &aFunctions[kFunc_Initialize] },
@@ -199,12 +205,16 @@ LoadXULMethods(JNIEnv* env, jobject aXPC
     { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject",
             (NSFuncPtr*) &aFunctions[kFunc_IsSameXPCOMObject] },
     { "Java_org_mozilla_xpcom_ProfileLock_release",
             (NSFuncPtr*) &aFunctions[kFunc_ReleaseProfileLock] },
     { "Java_org_mozilla_xpcom_internal_MozillaImpl_getNativeHandleFromAWT",
             (NSFuncPtr*) &aFunctions[kFunc_GetNativeHandleFromAWT] },
+    { "Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapJavaObject",
+            (NSFuncPtr*) &aFunctions[kFunc_WrapJavaObject] },
+    { "Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_wrapXPCOMObject",
+            (NSFuncPtr*) &aFunctions[kFunc_WrapXPCOMObject] },
     { nsnull, nsnull }
   };
 #endif
 
   rv = XPCOMGlueLoadXULFunctions(funcs);
   if (NS_FAILED(rv))
@@ -300,17 +310,24 @@ RegisterNativeMethods(JNIEnv* env, void*
     { "finalizeProxyNative", "(Ljava/lang/Object;)V",
       (void*) aFunctions[kFunc_FinalizeProxy] },
     { "isSameXPCOMObject", "(Ljava/lang/Object;Ljava/lang/Object;)Z",
       (void*) aFunctions[kFunc_IsSameXPCOMObject] }
   };
 
-   JNINativeMethod lockProxy_methods[] = {
+  JNINativeMethod lockProxy_methods[] = {
     { "releaseNative", "(J)V",
       (void*) aFunctions[kFunc_ReleaseProfileLock] }
   };
 
+  JNINativeMethod util_methods[] = {
+    { "wrapJavaObject", "(Ljava/lang/Object;Ljava/lang/String;)J",
+      (void*) aFunctions[kFunc_WrapJavaObject] },
+    { "wrapXPCOMObject", "(JLjava/lang/String;)Ljava/lang/Object;",
+      (void*) aFunctions[kFunc_WrapXPCOMObject] }
+  };
+
   jint rc = -1;
   jclass clazz = env->FindClass("org/mozilla/xpcom/internal/MozillaImpl");
   if (clazz) {
     rc = env->RegisterNatives(clazz, mozilla_methods,
                           sizeof(mozilla_methods) / sizeof(mozilla_methods[0]));
   }
@@ -345,12 +362,20 @@ RegisterNativeMethods(JNIEnv* env, void*
   if (clazz) {
     rc = env->RegisterNatives(clazz, lockProxy_methods,
                       sizeof(lockProxy_methods) / sizeof(lockProxy_methods[0]));
   }
   NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);
 
+  rc = -1;
+  clazz = env->FindClass("org/mozilla/xpcom/internal/JavaXPCOMMethods");
+  if (clazz) {
+    rc = env->RegisterNatives(clazz, util_methods,
+                              sizeof(util_methods) / sizeof(util_methods[0]));
+  }
+  NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE);
+
   return NS_OK;
 }
 
 // Load the JavaXPCOM methods from the XUL shared library, and registers them
 // as Java native methods.
 extern "C" JX_EXPORT void JNICALL
Index: extensions/java/xpcom/interfaces/IJavaXPCOMUtils.java
===================================================================
RCS file: extensions/java/xpcom/interfaces/IJavaXPCOMUtils.java
diff -N extensions/java/xpcom/interfaces/IJavaXPCOMUtils.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ extensions/java/xpcom/interfaces/IJavaXPCOMUtils.java	6 Feb 2007 22:21:11 -0000
@@ -0,0 +1,59 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Java XPCOM Bindings.
+ *
+ * The Initial Developer of the Original Code is IBM Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2007
+ * IBM Corporation. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Javier Pedemonte (jhpedemonte@gmail.com)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+package org.mozilla.xpcom;
+
+public interface IJavaXPCOMUtils {
+
+	/**
+	 * Returns a pointer to a C++ proxy object for the given Java object.
+	 * 
+	 * @param aJavaObject   Java object to encapsulate in C++ proxy
+	 * @param aIID          interface ID for requested proxy
+	 * @return  C pointer (as long) of new proxy
+	 */
+	long wrapJavaObject(Object aJavaObject, String aIID);
+
+	/**
+	 * Returns a Java proxy for the given C++ XPCOM object
+	 * 
+	 * @param aXPCOMObject  C++ XPCOM object to encapsulate in Java proxy
+	 * @param aIID          interface ID for requested proxy
+	 * @return  new Proxy
+	 */
+	Object wrapXPCOMObject(long aXPCOMObject, String aIID);
+
+}
Index: extensions/java/xpcom/interfaces/Makefile.in
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/interfaces/Makefile.in,v
retrieving revision 1.5.4.11
diff -u -6 -p -r1.5.4.11 Makefile.in
--- extensions/java/xpcom/interfaces/Makefile.in	18 Oct 2006 20:37:46 -0000	1.5.4.11
+++ extensions/java/xpcom/interfaces/Makefile.in	21 Mar 2007 22:03:26 -0000
@@ -47,12 +47,13 @@ JARFILE_SRC = $(patsubst %.jar,%-src.jar
 JAVA_SRCS = \
 		$(srcdir)/Mozilla.java \
 		$(srcdir)/GREVersionRange.java \
 		$(srcdir)/IMozilla.java \
 		$(srcdir)/IGRE.java \
 		$(srcdir)/IXPCOM.java \
+		$(srcdir)/IJavaXPCOMUtils.java \
 		$(srcdir)/IAppFileLocProvider.java \
 		$(srcdir)/INIParser.java \
 		$(srcdir)/VersionComparator.java \
 		$(srcdir)/ProfileLock.java \
 		$(srcdir)/../XPCOMException.java \
 		$(srcdir)/XPCOMInitializationException.java \
Index: extensions/java/xpcom/interfaces/Mozilla.java
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/interfaces/Attic/Mozilla.java,v
retrieving revision 1.3.4.7
diff -u -6 -p -r1.3.4.7 Mozilla.java
--- extensions/java/xpcom/interfaces/Mozilla.java	1 Feb 2007 22:31:08 -0000	1.3.4.7
+++ extensions/java/xpcom/interfaces/Mozilla.java	21 Mar 2007 22:03:26 -0000
@@ -82,23 +82,23 @@ import org.mozilla.interfaces.nsISupport
  *    // handle exception
  * }
  * </pre>
  * 
  * @see http://www.mozilla.org/projects/embedding/GRE.html
  */
-public class Mozilla implements IMozilla, IGRE, IXPCOM, IXPCOMError {
+public class Mozilla implements IMozilla, IGRE, IXPCOM, IJavaXPCOMUtils,
+IXPCOMError {
 
   private static Mozilla mozillaInstance = new Mozilla();
 
   private static final String JAVAXPCOM_JAR = "javaxpcom.jar";
 
   private IMozilla mozilla = null;
-
   private IGRE gre = null;
-
   private IXPCOM xpcom = null;
+  private IJavaXPCOMUtils jxutils = null;
 
   /**
    * @return
    */
   public static Mozilla getInstance() {
     return mozillaInstance;
@@ -652,12 +652,17 @@ public class Mozilla implements IMozilla
           true, loader);
       gre = (IGRE) greClass.newInstance();
 
       Class xpcomClass = Class.forName("org.mozilla.xpcom.internal.XPCOMImpl",
                                        true, loader);
       xpcom = (IXPCOM) xpcomClass.newInstance();
+
+      Class javaXPCOMClass =
+    	  Class.forName("org.mozilla.xpcom.internal.JavaXPCOMMethods",
+    			  true, loader);
+      jxutils  = (IJavaXPCOMUtils) javaXPCOMClass.newInstance();
     } catch (Exception e) {
       throw new XPCOMInitializationException("Could not load " +
           "org.mozilla.xpcom.internal.* classes", e);
     }
     
     mozilla.initialize(aLibXULDirectory);
@@ -1038,7 +1043,25 @@ public class Mozilla implements IMozilla
     } catch (NullPointerException e) {
       throw new XPCOMInitializationException("Must call " +
           "Mozilla.getInstance().initialize() before using this method", e);
     }
   }
 
+	public long wrapJavaObject(Object aJavaObject, String aIID) {
+		try {
+			return jxutils.wrapJavaObject(aJavaObject, aIID);
+		} catch (NullPointerException e) {
+			throw new XPCOMInitializationException("Must call " +
+				"Mozilla.getInstance().initialize() before using this method", e);
+		}
+	}
+	
+	public Object wrapXPCOMObject(long aXPCOMObject, String aIID) {
+		try {
+			return jxutils.wrapXPCOMObject(aXPCOMObject, aIID);
+		} catch (NullPointerException e) {
+			throw new XPCOMInitializationException("Must call " +
+				"Mozilla.getInstance().initialize() before using this method", e);
+		}
+	}
+
 }
Index: extensions/java/xpcom/src/JavaXPCOMMethods.java
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/src/Attic/JavaXPCOMMethods.java,v
retrieving revision 1.1.2.4
diff -u -6 -p -r1.1.2.4 JavaXPCOMMethods.java
--- extensions/java/xpcom/src/JavaXPCOMMethods.java	3 Oct 2006 20:02:15 -0000	1.1.2.4
+++ extensions/java/xpcom/src/JavaXPCOMMethods.java	21 Mar 2007 22:03:26 -0000
@@ -35,14 +35,16 @@
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.xpcom.internal;
 
 import java.io.File;
 
+import org.mozilla.xpcom.IJavaXPCOMUtils;
 
-public class JavaXPCOMMethods {
+
+public class JavaXPCOMMethods implements IJavaXPCOMUtils {
 
   public static void registerJavaXPCOMMethods(File aLibXULDirectory) {
     // load JNI library
     String path = "";
     if (aLibXULDirectory != null) {
       path = aLibXULDirectory + File.separator;
@@ -82,8 +84,12 @@ public class JavaXPCOMMethods {
       }
     } catch (ClassNotFoundException e) {
       return null;
     }
   }
 
+  public native long wrapJavaObject(Object aJavaObject, String aIID);
+
+  public native Object wrapXPCOMObject(long aXPCOMObject, String aIID);
+
 }
 
Index: extensions/java/xpcom/src/dlldeps-javaxpcom.cpp
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/src/dlldeps-javaxpcom.cpp,v
retrieving revision 1.2.4.4
diff -u -6 -p -r1.2.4.4 dlldeps-javaxpcom.cpp
--- extensions/java/xpcom/src/dlldeps-javaxpcom.cpp	18 Jan 2007 19:36:13 -0000	1.2.4.4
+++ extensions/java/xpcom/src/dlldeps-javaxpcom.cpp	21 Mar 2007 22:03:26 -0000
@@ -67,8 +67,12 @@ void XXXNeverCalled_javaxpcom()
 
   JAVAPROXY_NATIVE(isSameXPCOMObject) (nsnull, nsnull, nsnull, nsnull);
 
   LOCKPROXY_NATIVE(release) (nsnull, nsnull, nsnull);
 
   MOZILLA_NATIVE(getNativeHandleFromAWT) (nsnull, nsnull, nsnull);
+
+  JXUTILS_NATIVE(wrapJavaObject) (nsnull, nsnull, nsnull, nsnull);
+
+  JXUTILS_NATIVE(wrapXPCOMObject) (nsnull, nsnull, nsnull, nsnull);
 }
 
Index: extensions/java/xpcom/src/nsJavaInterfaces.h
===================================================================
RCS file: /cvsroot/mozilla/extensions/java/xpcom/src/nsJavaInterfaces.h,v
retrieving revision 1.2.4.4
diff -u -6 -p -r1.2.4.4 nsJavaInterfaces.h
--- extensions/java/xpcom/src/nsJavaInterfaces.h	18 Jan 2007 19:36:13 -0000	1.2.4.4
+++ extensions/java/xpcom/src/nsJavaInterfaces.h	21 Mar 2007 22:03:27 -0000
@@ -43,12 +43,14 @@
 #define MOZILLA_NATIVE(func) Java_org_mozilla_xpcom_internal_MozillaImpl_##func
 #define GRE_NATIVE(func) Java_org_mozilla_xpcom_internal_GREImpl_##func
 #define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_internal_XPCOMImpl_##func
 #define JAVAPROXY_NATIVE(func) \
           Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_##func
 #define LOCKPROXY_NATIVE(func) Java_org_mozilla_xpcom_ProfileLock_##func
+#define JXUTILS_NATIVE(func) \
+          Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_##func
 
 
 extern "C" NS_EXPORT void JNICALL
 MOZILLA_NATIVE(initialize) (JNIEnv* env, jobject);
 
 extern "C" NS_EXPORT void JNICALL
@@ -98,7 +100,15 @@ JAVAPROXY_NATIVE(isSameXPCOMObject) (JNI
 extern "C" NS_EXPORT void JNICALL
 LOCKPROXY_NATIVE(release) (JNIEnv *env, jclass that, jlong aLockObject);
 
 extern "C" NS_EXPORT jlong JNICALL
 MOZILLA_NATIVE(getNativeHandleFromAWT) (JNIEnv* env, jobject, jobject widget);
 
+extern "C" NS_EXPORT jlong JNICALL
+JXUTILS_NATIVE(wrapJavaObject) (JNIEnv* env, jobject, jobject aJavaObject,
+                                jstring aIID);
+
+extern "C" NS_EXPORT jobject JNICALL
+JXUTILS_NATIVE(wrapXPCOMObject) (JNIEnv* env, jobject, jlong aXPCOMObject,
+                                 jstring aIID);
+
 #endif // _nsJavaInterfaces_h_
