1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Java XPCOM Bindings.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * IBM Corporation.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2005
|
---|
19 | * IBM Corporation. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Javier Pedemonte (jhpedemonte@gmail.com)
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nsAppFileLocProviderProxy.h"
|
---|
39 | #include "nsJavaXPCOMBindingUtils.h"
|
---|
40 | #include "nsILocalFile.h"
|
---|
41 | #include "nsISimpleEnumerator.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | nsAppFileLocProviderProxy::nsAppFileLocProviderProxy(jobject aJavaObject)
|
---|
45 | {
|
---|
46 | mJavaLocProvider = GetJNIEnv()->NewGlobalRef(aJavaObject);
|
---|
47 | }
|
---|
48 |
|
---|
49 | nsAppFileLocProviderProxy::~nsAppFileLocProviderProxy()
|
---|
50 | {
|
---|
51 | GetJNIEnv()->DeleteGlobalRef(mJavaLocProvider);
|
---|
52 | }
|
---|
53 |
|
---|
54 | NS_IMPL_ISUPPORTS2(nsAppFileLocProviderProxy,
|
---|
55 | nsIDirectoryServiceProvider,
|
---|
56 | nsIDirectoryServiceProvider2)
|
---|
57 |
|
---|
58 |
|
---|
59 | // nsIDirectoryServiceProvider
|
---|
60 |
|
---|
61 | NS_IMETHODIMP
|
---|
62 | nsAppFileLocProviderProxy::GetFile(const char* aProp, PRBool* aIsPersistant,
|
---|
63 | nsIFile** aResult)
|
---|
64 | {
|
---|
65 | // Setup params for calling Java function
|
---|
66 | JNIEnv* env = GetJNIEnv();
|
---|
67 | jstring prop = env->NewStringUTF(aProp);
|
---|
68 | if (!prop)
|
---|
69 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
70 | jbooleanArray persistant = env->NewBooleanArray(1);
|
---|
71 | if (!persistant)
|
---|
72 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
73 |
|
---|
74 | // Create method ID
|
---|
75 | jmethodID mid = nsnull;
|
---|
76 | jclass clazz = env->GetObjectClass(mJavaLocProvider);
|
---|
77 | if (clazz) {
|
---|
78 | mid = env->GetMethodID(clazz, "getFile",
|
---|
79 | "(Ljava/lang/String;[Z)Ljava/io/File;");
|
---|
80 | }
|
---|
81 | if (!mid)
|
---|
82 | return NS_ERROR_FAILURE;
|
---|
83 |
|
---|
84 | // Call Java function
|
---|
85 | jobject javaFile = nsnull;
|
---|
86 | javaFile = env->CallObjectMethod(mJavaLocProvider, mid, prop, persistant);
|
---|
87 | if (javaFile == nsnull || env->ExceptionCheck())
|
---|
88 | return NS_ERROR_FAILURE;
|
---|
89 |
|
---|
90 | // Set boolean output value
|
---|
91 | env->GetBooleanArrayRegion(persistant, 0, 1, (jboolean*) aIsPersistant);
|
---|
92 |
|
---|
93 | // Set nsIFile result value
|
---|
94 | nsCOMPtr<nsILocalFile> localFile;
|
---|
95 | nsresult rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile));
|
---|
96 | if (NS_SUCCEEDED(rv)) {
|
---|
97 | return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return rv;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | // nsIDirectoryServiceProvider2
|
---|
105 |
|
---|
106 | class DirectoryEnumerator : public nsISimpleEnumerator
|
---|
107 | {
|
---|
108 | public:
|
---|
109 | NS_DECL_ISUPPORTS
|
---|
110 |
|
---|
111 | DirectoryEnumerator(jobjectArray aJavaFileArray)
|
---|
112 | : mIndex(0)
|
---|
113 | {
|
---|
114 | JNIEnv* env = GetJNIEnv();
|
---|
115 | mJavaFileArray = static_cast<jobjectArray>
|
---|
116 | (env->NewGlobalRef(aJavaFileArray));
|
---|
117 | mArraySize = env->GetArrayLength(aJavaFileArray);
|
---|
118 | }
|
---|
119 |
|
---|
120 | ~DirectoryEnumerator()
|
---|
121 | {
|
---|
122 | GetJNIEnv()->DeleteGlobalRef(mJavaFileArray);
|
---|
123 | }
|
---|
124 |
|
---|
125 | NS_IMETHOD HasMoreElements(PRBool* aResult)
|
---|
126 | {
|
---|
127 | if (!mJavaFileArray) {
|
---|
128 | *aResult = PR_FALSE;
|
---|
129 | } else {
|
---|
130 | *aResult = (mIndex < mArraySize);
|
---|
131 | }
|
---|
132 | return NS_OK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | NS_IMETHOD GetNext(nsISupports** aResult)
|
---|
136 | {
|
---|
137 | nsresult rv = NS_ERROR_FAILURE;
|
---|
138 |
|
---|
139 | JNIEnv* env = GetJNIEnv();
|
---|
140 | jobject javaFile = env->GetObjectArrayElement(mJavaFileArray, mIndex++);
|
---|
141 | if (javaFile) {
|
---|
142 | nsCOMPtr<nsILocalFile> localFile;
|
---|
143 | rv = File_to_nsILocalFile(env, javaFile, getter_AddRefs(localFile));
|
---|
144 | env->DeleteLocalRef(javaFile);
|
---|
145 |
|
---|
146 | if (NS_SUCCEEDED(rv)) {
|
---|
147 | return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aResult);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | env->ExceptionClear();
|
---|
152 | return NS_ERROR_FAILURE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | private:
|
---|
156 | jobjectArray mJavaFileArray;
|
---|
157 | PRUint32 mArraySize;
|
---|
158 | PRUint32 mIndex;
|
---|
159 | };
|
---|
160 |
|
---|
161 | NS_IMPL_ISUPPORTS1(DirectoryEnumerator, nsISimpleEnumerator)
|
---|
162 |
|
---|
163 | NS_IMETHODIMP
|
---|
164 | nsAppFileLocProviderProxy::GetFiles(const char* aProp,
|
---|
165 | nsISimpleEnumerator** aResult)
|
---|
166 | {
|
---|
167 | nsresult rv = NS_OK;
|
---|
168 |
|
---|
169 | // Setup params for calling Java function
|
---|
170 | JNIEnv* env = GetJNIEnv();
|
---|
171 | jstring prop = env->NewStringUTF(aProp);
|
---|
172 | if (!prop)
|
---|
173 | rv = NS_ERROR_OUT_OF_MEMORY;
|
---|
174 |
|
---|
175 | // Create method ID
|
---|
176 | jmethodID mid = nsnull;
|
---|
177 | if (NS_SUCCEEDED(rv)) {
|
---|
178 | jclass clazz = env->GetObjectClass(mJavaLocProvider);
|
---|
179 | if (clazz) {
|
---|
180 | mid = env->GetMethodID(clazz, "getFiles",
|
---|
181 | "(Ljava/lang/String;)[Ljava/io/File;");
|
---|
182 | env->DeleteLocalRef(clazz);
|
---|
183 | }
|
---|
184 | if (!mid)
|
---|
185 | rv = NS_ERROR_FAILURE;
|
---|
186 | }
|
---|
187 |
|
---|
188 | // Call Java function
|
---|
189 | jobject javaFileArray = nsnull;
|
---|
190 | if (NS_SUCCEEDED(rv)) {
|
---|
191 | javaFileArray = env->CallObjectMethod(mJavaLocProvider, mid, prop);
|
---|
192 |
|
---|
193 | // Handle any exception thrown by Java method.
|
---|
194 | jthrowable exp = env->ExceptionOccurred();
|
---|
195 | if (exp) {
|
---|
196 | #ifdef DEBUG
|
---|
197 | env->ExceptionDescribe();
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | // If the exception is an instance of XPCOMException, then get the
|
---|
201 | // nsresult from the exception instance. Else, default to
|
---|
202 | // NS_ERROR_FAILURE.
|
---|
203 | if (env->IsInstanceOf(exp, xpcomExceptionClass)) {
|
---|
204 | jfieldID fid;
|
---|
205 | fid = env->GetFieldID(xpcomExceptionClass, "errorcode", "J");
|
---|
206 | if (fid) {
|
---|
207 | rv = env->GetLongField(exp, fid);
|
---|
208 | } else {
|
---|
209 | rv = NS_ERROR_FAILURE;
|
---|
210 | }
|
---|
211 | NS_ASSERTION(fid, "Couldn't get 'errorcode' field of XPCOMException");
|
---|
212 | } else {
|
---|
213 | rv = NS_ERROR_FAILURE;
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | // No exception thrown. Check the result.
|
---|
217 | if (javaFileArray == nsnull) {
|
---|
218 | rv = NS_ERROR_FAILURE;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (NS_SUCCEEDED(rv)) {
|
---|
224 | // Parse return value
|
---|
225 | *aResult = new DirectoryEnumerator(static_cast<jobjectArray>
|
---|
226 | (javaFileArray));
|
---|
227 | NS_ADDREF(*aResult);
|
---|
228 | return NS_OK;
|
---|
229 | }
|
---|
230 |
|
---|
231 | // Handle error conditions
|
---|
232 | *aResult = nsnull;
|
---|
233 | env->ExceptionClear();
|
---|
234 | return rv;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | ////////////////////////////////////////////////////////////////////////////////
|
---|
239 |
|
---|
240 | nsresult
|
---|
241 | NS_NewAppFileLocProviderProxy(jobject aJavaLocProvider,
|
---|
242 | nsIDirectoryServiceProvider** aResult)
|
---|
243 | {
|
---|
244 | nsAppFileLocProviderProxy* provider =
|
---|
245 | new nsAppFileLocProviderProxy(aJavaLocProvider);
|
---|
246 | if (provider == nsnull)
|
---|
247 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
248 | NS_ADDREF(provider);
|
---|
249 |
|
---|
250 | *aResult = provider;
|
---|
251 | return NS_OK;
|
---|
252 | }
|
---|
253 |
|
---|