comparison dwt/browser/AppFileLocProvider.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2003, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.browser.AppFileLocProvider;
12
13 import dwt.dwthelper.utils;
14
15 import java.util.Vector;
16
17 import dwt.internal.C;
18 import dwt.internal.Compatibility;
19 import dwt.internal.mozilla.XPCOM;
20 import dwt.internal.mozilla.XPCOMObject;
21 import dwt.internal.mozilla.nsEmbedString;
22 import dwt.internal.mozilla.nsID;
23 import dwt.internal.mozilla.nsIDirectoryServiceProvider;
24 import dwt.internal.mozilla.nsIDirectoryServiceProvider2;
25 import dwt.internal.mozilla.nsIFile;
26 import dwt.internal.mozilla.nsILocalFile;
27 import dwt.internal.mozilla.nsISupports;
28
29 class AppFileLocProvider {
30 XPCOMObject supports;
31 XPCOMObject directoryServiceProvider;
32 XPCOMObject directoryServiceProvider2;
33 int refCount = 0;
34 String mozillaPath, profilePath;
35 String[] pluginDirs;
36 bool isXULRunner;
37
38 static final String SEPARATOR_OS = System.getProperty ("file.separator"); //$NON-NLS-1$
39 static final String CHROME_DIR = "chrome"; //$NON-NLS-1$
40 static final String COMPONENTS_DIR = "components"; //$NON-NLS-1$
41 static final String HISTORY_FILE = "history.dat"; //$NON-NLS-1$
42 static final String LOCALSTORE_FILE = "localstore.rdf"; //$NON-NLS-1$
43 static final String MIMETYPES_FILE = "mimeTypes.rdf"; //$NON-NLS-1$
44 static final String PLUGINS_DIR = "plugins"; //$NON-NLS-1$
45 static final String USER_PLUGINS_DIR = ".mozilla" + SEPARATOR_OS + "plugins"; //$NON-NLS-1$ //$NON-NLS-2$
46 static final String PREFERENCES_FILE = "prefs.js"; //$NON-NLS-1$
47
48 AppFileLocProvider (String path) {
49 mozillaPath = path + SEPARATOR_OS;
50 createCOMInterfaces ();
51 }
52
53 int AddRef () {
54 refCount++;
55 return refCount;
56 }
57
58 void createCOMInterfaces () {
59 /* Create each of the interfaces that this object implements */
60 supports = new XPCOMObject (new int[] {2, 0, 0}) {
61 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
62 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
63 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
64 };
65
66 directoryServiceProvider = new XPCOMObject (new int[] {2, 0, 0, 3}) {
67 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
68 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
69 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
70 public int /*long*/ method3 (int /*long*/[] args) {return getFile (args[0], args[1], args[2]);}
71 };
72
73 directoryServiceProvider2 = new XPCOMObject (new int[] {2, 0, 0, 3, 2}) {
74 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
75 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
76 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
77 public int /*long*/ method3 (int /*long*/[] args) {return getFile (args[0], args[1], args[2]);}
78 public int /*long*/ method4 (int /*long*/[] args) {return getFiles (args[0], args[1]);}
79 };
80 }
81
82 void disposeCOMInterfaces () {
83 if (supports !is null) {
84 supports.dispose ();
85 supports = null;
86 }
87 if (directoryServiceProvider !is null) {
88 directoryServiceProvider.dispose ();
89 directoryServiceProvider = null;
90 }
91 if (directoryServiceProvider2 !is null) {
92 directoryServiceProvider2.dispose ();
93 directoryServiceProvider2 = null;
94 }
95 }
96
97 int /*long*/ getAddress () {
98 return directoryServiceProvider.getAddress ();
99 }
100
101 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
102 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
103 nsID guid = new nsID ();
104 XPCOM.memmove (guid, riid, nsID.sizeof);
105
106 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
107 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
108 AddRef ();
109 return XPCOM.NS_OK;
110 }
111 if (guid.Equals (nsIDirectoryServiceProvider.NS_IDIRECTORYSERVICEPROVIDER_IID)) {
112 XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider.getAddress ()}, C.PTR_SIZEOF);
113 AddRef ();
114 return XPCOM.NS_OK;
115 }
116 if (guid.Equals (nsIDirectoryServiceProvider2.NS_IDIRECTORYSERVICEPROVIDER2_IID)) {
117 XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider2.getAddress ()}, C.PTR_SIZEOF);
118 AddRef ();
119 return XPCOM.NS_OK;
120 }
121
122 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
123 return XPCOM.NS_ERROR_NO_INTERFACE;
124 }
125
126 int Release () {
127 refCount--;
128 if (refCount is 0) disposeCOMInterfaces ();
129 return refCount;
130 }
131
132 void setProfilePath (String path) {
133 profilePath = path;
134 if (!Compatibility.fileExists (path, "")) { //$NON-NLS-1$
135 int /*long*/[] result = new int /*long*/[1];
136 nsEmbedString pathString = new nsEmbedString (path);
137 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
138 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
139 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
140 pathString.dispose ();
141
142 nsILocalFile file = new nsILocalFile (result [0]);
143 rc = file.Create (nsILocalFile.DIRECTORY_TYPE, 0700);
144 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
145 file.Release ();
146 }
147 }
148
149 /* nsIDirectoryServiceProvider2 */
150
151 int getFiles (int /*long*/ prop, int /*long*/ _retval) {
152 int size = XPCOM.strlen (prop);
153 byte[] bytes = new byte[size];
154 XPCOM.memmove (bytes, prop, size);
155 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes));
156 String[] propertyValues = null;
157
158 if (propertyName.equals (XPCOM.NS_APP_PLUGINS_DIR_LIST)) {
159 if (pluginDirs is null) {
160 int index = 0;
161 /* set the first value(s) to the MOZ_PLUGIN_PATH environment variable value if it's defined */
162 int /*long*/ ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_PLUGIN_PATH, true));
163 if (ptr !is 0) {
164 int length = C.strlen (ptr);
165 byte[] buffer = new byte[length];
166 C.memmove (buffer, ptr, length);
167 String value = new String (MozillaDelegate.mbcsToWcs (null, buffer));
168 if (value.length () > 0) {
169 String separator = System.getProperty ("path.separator"); // $NON-NLS-1$
170 Vector segments = new Vector ();
171 int start, end = -1;
172 do {
173 start = end + 1;
174 end = value.indexOf (separator, start);
175 String segment;
176 if (end is -1) {
177 segment = value.substring (start);
178 } else {
179 segment = value.substring (start, end);
180 }
181 if (segment.length () > 0) segments.addElement (segment);
182 } while (end !is -1);
183 int segmentsSize = segments.size ();
184 pluginDirs = new String [segmentsSize + 2];
185 for (index = 0; index < segmentsSize; index++) {
186 pluginDirs[index] = (String)segments.elementAt (index);
187 }
188 }
189 }
190 if (pluginDirs is null) {
191 pluginDirs = new String[2];
192 }
193
194 /* set the next value to the GRE path + "plugins" */
195 pluginDirs[index++] = mozillaPath + PLUGINS_DIR;
196
197 /* set the next value to the home directory + "/.mozilla/plugins" */
198 pluginDirs[index++] = System.getProperty("user.home") + SEPARATOR_OS + USER_PLUGINS_DIR;
199 }
200 propertyValues = pluginDirs;
201 }
202
203 XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
204 if (propertyValues !is null) {
205 int /*long*/[] result = new int /*long*/[1];
206 nsISupports[] files = new nsISupports [propertyValues.length];
207 int index = 0;
208 for (int i = 0; i < propertyValues.length; i++) {
209 nsEmbedString pathString = new nsEmbedString (propertyValues[i]);
210 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
211 if (rc !is XPCOM.NS_ERROR_FILE_UNRECOGNIZED_PATH) {
212 /* value appears to be a valid pathname */
213 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
214 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
215
216 nsILocalFile localFile = new nsILocalFile (result[0]);
217 result[0] = 0;
218 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result);
219 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
220 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
221 localFile.Release ();
222
223 nsIFile file = new nsIFile (result[0]);
224 files[index++] = file;
225 }
226 pathString.dispose ();
227 result[0] = 0;
228 }
229
230 if (index < propertyValues.length) {
231 /* there were some invalid values so remove the trailing empty array slots */
232 nsISupports[] temp = new nsISupports [index];
233 System.arraycopy (files, 0, temp, 0, index);
234 files = temp;
235 }
236
237 SimpleEnumerator enumerator = new SimpleEnumerator (files);
238 enumerator.AddRef ();
239 XPCOM.memmove (_retval, new int /*long*/[] {enumerator.getAddress ()}, C.PTR_SIZEOF);
240 return XPCOM.NS_OK;
241 }
242
243 return XPCOM.NS_ERROR_FAILURE;
244 }
245
246 /* nsIDirectoryServiceProvider implementation */
247
248 int getFile(int /*long*/ prop, int /*long*/ persistent, int /*long*/ _retval) {
249 int size = XPCOM.strlen (prop);
250 byte[] bytes = new byte[size];
251 XPCOM.memmove (bytes, prop, size);
252 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes));
253 String propertyValue = null;
254
255 if (propertyName.equals (XPCOM.NS_APP_HISTORY_50_FILE)) {
256 propertyValue = profilePath + HISTORY_FILE;
257 } else if (propertyName.equals (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) {
258 propertyValue = profilePath + MIMETYPES_FILE;
259 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_FILE)) {
260 propertyValue = profilePath + PREFERENCES_FILE;
261 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_DIR)) {
262 propertyValue = profilePath;
263 } else if (propertyName.equals (XPCOM.NS_APP_USER_CHROME_DIR)) {
264 propertyValue = profilePath + CHROME_DIR;
265 } else if (propertyName.equals (XPCOM.NS_APP_USER_PROFILE_50_DIR)) {
266 propertyValue = profilePath;
267 } else if (propertyName.equals (XPCOM.NS_APP_LOCALSTORE_50_FILE)) {
268 propertyValue = profilePath + LOCALSTORE_FILE;
269 } else if (propertyName.equals (XPCOM.NS_APP_CACHE_PARENT_DIR)) {
270 propertyValue = profilePath;
271 } else if (propertyName.equals (XPCOM.NS_OS_HOME_DIR)) {
272 propertyValue = System.getProperty("user.home"); //$NON-NLS-1$
273 } else if (propertyName.equals (XPCOM.NS_OS_TEMP_DIR)) {
274 propertyValue = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
275 } else if (propertyName.equals (XPCOM.NS_GRE_DIR)) {
276 propertyValue = mozillaPath;
277 } else if (propertyName.equals (XPCOM.NS_GRE_COMPONENT_DIR)) {
278 propertyValue = mozillaPath + COMPONENTS_DIR;
279 } else if (propertyName.equals (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) {
280 propertyValue = mozillaPath;
281 } else if (propertyName.equals (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) {
282 propertyValue = mozillaPath;
283 } else if (propertyName.equals (XPCOM.NS_XPCOM_COMPONENT_DIR)) {
284 propertyValue = mozillaPath + COMPONENTS_DIR;
285 } else if (propertyName.equals (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) {
286 propertyValue = mozillaPath;
287 } else if (propertyName.equals (XPCOM.NS_APP_PREF_DEFAULTS_50_DIR)) {
288 /*
289 * Answering a value for this property causes problems in Mozilla versions
290 * < 1.7. Unfortunately this property is queried early enough in the
291 * Browser creation process that the Mozilla version being used is not
292 * yet determined. However it is known if XULRunner is being used or not.
293 *
294 * For now answer a value for this property iff XULRunner is the GRE.
295 * If the range of Mozilla versions supported by the Browser is changed
296 * in the future to be >= 1.7 then this value can always be answered.
297 */
298 if (isXULRunner) propertyValue = profilePath;
299 }
300
301 XPCOM.memmove (persistent, new int[] {1}, 4); /* PRBool */
302 XPCOM.memmove (_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
303 if (propertyValue !is null && propertyValue.length () > 0) {
304 int /*long*/[] result = new int /*long*/[1];
305 nsEmbedString pathString = new nsEmbedString (propertyValue);
306 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
307 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
308 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
309 pathString.dispose ();
310
311 nsILocalFile localFile = new nsILocalFile (result [0]);
312 result[0] = 0;
313 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result);
314 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
315 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
316
317 XPCOM.memmove (_retval, new int /*long*/[] {result[0]}, C.PTR_SIZEOF);
318 localFile.Release ();
319 return XPCOM.NS_OK;
320 }
321
322 return XPCOM.NS_ERROR_FAILURE;
323 }
324 }