Back to index

lightning-sunbird  0.9+nobinonly
nsAppFileLocProviderProxy.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Java XPCOM Bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Javier Pedemonte (jhpedemonte@gmail.com)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsAppFileLocProviderProxy.h"
00039 #include "nsJavaXPCOMBindingUtils.h"
00040 #include "nsILocalFile.h"
00041 #include "nsISimpleEnumerator.h"
00042 
00043 
00044 nsAppFileLocProviderProxy::nsAppFileLocProviderProxy(jobject aJavaObject)
00045 {
00046   mJavaLocProvider = GetJNIEnv()->NewGlobalRef(aJavaObject);
00047 }
00048 
00049 nsAppFileLocProviderProxy::~nsAppFileLocProviderProxy()
00050 {
00051   GetJNIEnv()->DeleteGlobalRef(mJavaLocProvider);
00052 }
00053 
00054 NS_IMPL_ISUPPORTS2(nsAppFileLocProviderProxy,
00055                    nsIDirectoryServiceProvider,
00056                    nsIDirectoryServiceProvider2)
00057 
00058 
00059 // nsIDirectoryServiceProvider
00060 
00061 NS_IMETHODIMP
00062 nsAppFileLocProviderProxy::GetFile(const char* aProp, PRBool* aIsPersistant,
00063                                    nsIFile** aResult)
00064 {
00065   // Setup params for calling Java function
00066   JNIEnv* env = GetJNIEnv();
00067   jstring prop = env->NewStringUTF(aProp);
00068   if (!prop)
00069     return NS_ERROR_OUT_OF_MEMORY;
00070   jbooleanArray persistant = env->NewBooleanArray(1);
00071   if (!persistant)
00072     return NS_ERROR_OUT_OF_MEMORY;
00073 
00074   // Create method ID
00075   jmethodID mid = nsnull;
00076   jclass clazz = env->GetObjectClass(mJavaLocProvider);
00077   if (clazz) {
00078     mid = env->GetMethodID(clazz, "getFile",
00079                            "(Ljava/lang/String;[Z)Ljava/io/File;");
00080   }
00081   if (!mid)
00082     return NS_ERROR_FAILURE;
00083 
00084   // Call Java function
00085   jobject javaFile = nsnull;
00086   javaFile = env->CallObjectMethod(mJavaLocProvider, mid, prop, persistant);
00087   if (javaFile == nsnull || env->ExceptionCheck())
00088     return NS_ERROR_FAILURE;
00089 
00090   // Set boolean output value
00091   env->GetBooleanArrayRegion(persistant, 0, 1, (jboolean*) aIsPersistant);
00092 
00093   // Set nsIFile result value
00094   nsCOMPtr<nsILocalFile> localFile;
00095   nsresult rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile));
00096   if (NS_SUCCEEDED(rv)) {
00097     return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult);
00098   }
00099 
00100   return rv;
00101 }
00102 
00103 
00104 // nsIDirectoryServiceProvider2
00105 
00106 class DirectoryEnumerator : public nsISimpleEnumerator
00107 {
00108 public:
00109   NS_DECL_ISUPPORTS
00110 
00111   DirectoryEnumerator(jobjectArray aJavaFileArray)
00112     : mIndex(0)
00113   {
00114     JNIEnv* env = GetJNIEnv();
00115     mJavaFileArray = NS_STATIC_CAST(jobjectArray,
00116                                     env->NewGlobalRef(aJavaFileArray));
00117     mArraySize = env->GetArrayLength(aJavaFileArray);
00118   }
00119 
00120   ~DirectoryEnumerator()
00121   {
00122     GetJNIEnv()->DeleteGlobalRef(mJavaFileArray);
00123   }
00124 
00125   NS_IMETHOD HasMoreElements(PRBool* aResult)
00126   {
00127     if (!mJavaFileArray) {
00128       *aResult = PR_FALSE;
00129     } else {
00130       *aResult = (mIndex < mArraySize);
00131     }
00132     return NS_OK;
00133   }
00134 
00135   NS_IMETHOD GetNext(nsISupports** aResult)
00136   {
00137     nsresult rv = NS_ERROR_FAILURE;
00138 
00139     JNIEnv* env = GetJNIEnv();
00140     jobject javaFile = env->GetObjectArrayElement(mJavaFileArray, mIndex++);
00141     if (javaFile) {
00142       nsCOMPtr<nsILocalFile> localFile;
00143       rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile));
00144       env->DeleteLocalRef(javaFile);
00145 
00146       if (NS_SUCCEEDED(rv)) {
00147         return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult);
00148       }
00149     }
00150 
00151     env->ExceptionClear();
00152     return NS_ERROR_FAILURE;
00153   }
00154 
00155 private:
00156   jobjectArray  mJavaFileArray;
00157   PRUint32      mArraySize;
00158   PRUint32      mIndex;
00159 };
00160 
00161 NS_IMPL_ISUPPORTS1(DirectoryEnumerator, nsISimpleEnumerator)
00162 
00163 NS_IMETHODIMP
00164 nsAppFileLocProviderProxy::GetFiles(const char* aProp,
00165                                     nsISimpleEnumerator** aResult)
00166 {
00167   nsresult rv = NS_OK;
00168 
00169   // Setup params for calling Java function
00170   JNIEnv* env = GetJNIEnv();
00171   jstring prop = env->NewStringUTF(aProp);
00172   if (!prop)
00173     rv = NS_ERROR_OUT_OF_MEMORY;
00174 
00175   // Create method ID
00176   jmethodID mid = nsnull;
00177   if (NS_SUCCEEDED(rv)) {
00178     jclass clazz = env->GetObjectClass(mJavaLocProvider);
00179     if (clazz) {
00180       mid = env->GetMethodID(clazz, "getFiles",
00181                              "(Ljava/lang/String;)[Ljava/io/File;");
00182       env->DeleteLocalRef(clazz);
00183     }
00184     if (!mid)
00185       rv = NS_ERROR_FAILURE;
00186   }
00187 
00188   // Call Java function
00189   jobject javaFileArray = nsnull;
00190   if (NS_SUCCEEDED(rv)) {
00191     javaFileArray = env->CallObjectMethod(mJavaLocProvider, mid, prop);
00192 
00193     // Handle any exception thrown by Java method.
00194     jthrowable exp = env->ExceptionOccurred();
00195     if (exp) {
00196 #ifdef DEBUG
00197       env->ExceptionDescribe();
00198 #endif
00199 
00200       // If the exception is an instance of XPCOMException, then get the
00201       // nsresult from the exception instance.  Else, default to
00202       // NS_ERROR_FAILURE.
00203       if (env->IsInstanceOf(exp, xpcomExceptionClass)) {
00204         jfieldID fid;
00205         fid = env->GetFieldID(xpcomExceptionClass, "errorcode", "J");
00206         if (fid) {
00207           rv = env->GetLongField(exp, fid);
00208         } else {
00209           rv = NS_ERROR_FAILURE;
00210         }
00211         NS_ASSERTION(fid, "Couldn't get 'errorcode' field of XPCOMException");
00212       } else {
00213         rv = NS_ERROR_FAILURE;
00214       }
00215     } else {
00216       // No exception thrown.  Check the result.
00217       if (javaFileArray == nsnull) {
00218         rv = NS_ERROR_FAILURE;
00219       }
00220     }
00221   }
00222 
00223   if (NS_SUCCEEDED(rv)) {
00224     // Parse return value
00225     *aResult = new DirectoryEnumerator(NS_STATIC_CAST(jobjectArray,
00226                                                       javaFileArray));
00227     NS_ADDREF(*aResult);
00228     return NS_OK;
00229   }
00230 
00231   // Handle error conditions
00232   *aResult = nsnull;
00233   env->ExceptionClear();
00234   return rv;
00235 }
00236 
00237 
00239 
00240 nsresult
00241 NS_NewAppFileLocProviderProxy(jobject aJavaLocProvider,
00242                               nsIDirectoryServiceProvider** aResult)
00243 {
00244   nsAppFileLocProviderProxy* provider =
00245             new nsAppFileLocProviderProxy(aJavaLocProvider);
00246   if (provider == nsnull)
00247     return NS_ERROR_OUT_OF_MEMORY;
00248   NS_ADDREF(provider);
00249 
00250   *aResult = provider;
00251   return NS_OK;
00252 }
00253