1 11 package org.eclipse.swt.browser; 12 13 import org.eclipse.swt.internal.C; 14 import org.eclipse.swt.internal.mozilla.*; 15 16 class AppFileLocProvider { 17 XPCOMObject supports; 18 XPCOMObject directoryServiceProvider; 19 XPCOMObject directoryServiceProvider2; 20 int refCount = 0; 21 String mozillaPath, profilePath; 22 String [] pluginDirs; 23 24 static final String SEPARATOR_OS = System.getProperty ("file.separator"); static final String CHROME_DIR = "chrome"; static final String COMPONENTS_DIR = "components"; static final String HISTORY_FILE = "history.dat"; static final String LOCALSTORE_FILE = "localstore.rdf"; static final String MIMETYPES_FILE = "mimeTypes.rdf"; static final String PLUGINS_DIR = "plugins"; static final String USER_PLUGINS_DIR = ".mozilla" + SEPARATOR_OS + "plugins"; static final String PREFERENCES_FILE = "prefs.js"; 34 public AppFileLocProvider (String path) { 35 mozillaPath = path + SEPARATOR_OS; 36 createCOMInterfaces (); 37 } 38 39 int AddRef () { 40 refCount++; 41 return refCount; 42 } 43 44 void createCOMInterfaces () { 45 46 supports = new XPCOMObject (new int[] {2, 0, 0}) { 47 public int method0 (int [] args) {return queryInterface (args[0], args[1]);} 48 public int method1 (int [] args) {return AddRef ();} 49 public int method2 (int [] args) {return Release ();} 50 }; 51 52 directoryServiceProvider = new XPCOMObject (new int[] {2, 0, 0, 3}) { 53 public int method0 (int [] args) {return queryInterface (args[0], args[1]);} 54 public int method1 (int [] args) {return AddRef ();} 55 public int method2 (int [] args) {return Release ();} 56 public int method3 (int [] args) {return getFile (args[0], args[1], args[2]);} 57 }; 58 59 directoryServiceProvider2 = new XPCOMObject (new int[] {2, 0, 0, 3, 2}) { 60 public int method0 (int [] args) {return queryInterface (args[0], args[1]);} 61 public int method1 (int [] args) {return AddRef ();} 62 public int method2 (int [] args) {return Release ();} 63 public int method3 (int [] args) {return getFile (args[0], args[1], args[2]);} 64 public int method4 (int [] args) {return getFiles (args[0], args[1]);} 65 }; 66 } 67 68 void disposeCOMInterfaces () { 69 if (supports != null) { 70 supports.dispose (); 71 supports = null; 72 } 73 if (directoryServiceProvider != null) { 74 directoryServiceProvider.dispose (); 75 directoryServiceProvider = null; 76 } 77 if (directoryServiceProvider2 != null) { 78 directoryServiceProvider2.dispose (); 79 directoryServiceProvider2 = null; 80 } 81 } 82 83 int getAddress () { 84 return directoryServiceProvider.getAddress (); 85 } 86 87 int queryInterface (int riid, int ppvObject) { 88 if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE; 89 nsID guid = new nsID (); 90 XPCOM.memmove (guid, riid, nsID.sizeof); 91 92 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { 93 XPCOM.memmove (ppvObject, new int [] {supports.getAddress ()}, C.PTR_SIZEOF); 94 AddRef (); 95 return XPCOM.NS_OK; 96 } 97 if (guid.Equals (nsIDirectoryServiceProvider.NS_IDIRECTORYSERVICEPROVIDER_IID)) { 98 XPCOM.memmove (ppvObject, new int [] {directoryServiceProvider.getAddress ()}, C.PTR_SIZEOF); 99 AddRef (); 100 return XPCOM.NS_OK; 101 } 102 if (guid.Equals (nsIDirectoryServiceProvider2.NS_IDIRECTORYSERVICEPROVIDER2_IID)) { 103 XPCOM.memmove (ppvObject, new int [] {directoryServiceProvider2.getAddress ()}, C.PTR_SIZEOF); 104 AddRef (); 105 return XPCOM.NS_OK; 106 } 107 108 XPCOM.memmove (ppvObject, new int [] {0}, C.PTR_SIZEOF); 109 return XPCOM.NS_ERROR_NO_INTERFACE; 110 } 111 112 int Release () { 113 refCount--; 114 if (refCount == 0) disposeCOMInterfaces (); 115 return refCount; 116 } 117 118 void setProfilePath (String path) { 119 profilePath = path; 120 } 121 122 123 124 int getFiles (int prop, int _retval) { 125 int size = XPCOM.strlen (prop); 126 byte[] bytes = new byte[size]; 127 XPCOM.memmove (bytes, prop, size); 128 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes)); 129 String [] propertyValues = null; 130 131 if (propertyName.equals (XPCOM.NS_APP_PLUGINS_DIR_LIST)) { 132 if (pluginDirs == null) { 133 int index = 0; 134 135 int ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_PLUGIN_PATH, true)); 136 if (ptr != 0) { 137 int length = C.strlen (ptr); 138 byte[] buffer = new byte[length]; 139 C.memmove (buffer, ptr, length); 140 String value = new String (MozillaDelegate.mbcsToWcs (null, buffer)); 141 if (value.length () > 0) { 142 pluginDirs = new String [3]; 143 pluginDirs[index++] = value; 144 } 145 } 146 if (pluginDirs == null) { 147 pluginDirs = new String [2]; 148 } 149 150 151 pluginDirs[index++] = mozillaPath + PLUGINS_DIR; 152 153 154 pluginDirs[index++] = System.getProperty("user.home") + SEPARATOR_OS + USER_PLUGINS_DIR; 155 } 156 propertyValues = pluginDirs; 157 } 158 159 XPCOM.memmove(_retval, new int [] {0}, C.PTR_SIZEOF); 160 if (propertyValues != null) { 161 int [] result = new int [1]; 162 nsISupports[] files = new nsISupports [propertyValues.length]; 163 for (int i = 0; i < propertyValues.length; i++) { 164 nsEmbedString pathString = new nsEmbedString (propertyValues[i]); 165 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), true, result); 166 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 167 if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); 168 pathString.dispose (); 169 170 nsILocalFile localFile = new nsILocalFile (result[0]); 171 result[0] = 0; 172 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); 173 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 174 if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); 175 localFile.Release (); 176 177 nsIFile file = new nsIFile (result[0]); 178 result[0] = 0; 179 files[i] = file; 180 } 181 182 SimpleEnumerator enumerator = new SimpleEnumerator (files); 183 enumerator.AddRef (); 184 185 XPCOM.memmove (_retval, new int [] {enumerator.getAddress ()}, C.PTR_SIZEOF); 186 return XPCOM.NS_OK; 187 } 188 189 return XPCOM.NS_ERROR_FAILURE; 190 } 191 192 193 194 int getFile(int prop, int persistent, int _retval) { 195 int size = XPCOM.strlen (prop); 196 byte[] bytes = new byte[size]; 197 XPCOM.memmove (bytes, prop, size); 198 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes)); 199 String propertyValue = null; 200 201 if (propertyName.equals (XPCOM.NS_APP_HISTORY_50_FILE)) { 202 propertyValue = profilePath + HISTORY_FILE; 203 } else if (propertyName.equals (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) { 204 propertyValue = profilePath + MIMETYPES_FILE; 205 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_FILE)) { 206 propertyValue = profilePath + PREFERENCES_FILE; 207 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_DIR)) { 208 propertyValue = profilePath; 209 } else if (propertyName.equals (XPCOM.NS_APP_USER_CHROME_DIR)) { 210 propertyValue = profilePath + CHROME_DIR; 211 } else if (propertyName.equals (XPCOM.NS_APP_USER_PROFILE_50_DIR)) { 212 propertyValue = profilePath; 213 } else if (propertyName.equals (XPCOM.NS_APP_LOCALSTORE_50_FILE)) { 214 propertyValue = profilePath + LOCALSTORE_FILE; 215 } else if (propertyName.equals (XPCOM.NS_APP_CACHE_PARENT_DIR)) { 216 propertyValue = profilePath; 217 } else if (propertyName.equals (XPCOM.NS_OS_HOME_DIR)) { 218 propertyValue = System.getProperty("user.home"); } else if (propertyName.equals (XPCOM.NS_OS_TEMP_DIR)) { 220 propertyValue = System.getProperty("java.io.tmpdir"); } else if (propertyName.equals (XPCOM.NS_GRE_DIR)) { 222 propertyValue = mozillaPath; 223 } else if (propertyName.equals (XPCOM.NS_GRE_COMPONENT_DIR)) { 224 propertyValue = mozillaPath + COMPONENTS_DIR; 225 } else if (propertyName.equals (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) { 226 propertyValue = mozillaPath; 227 } else if (propertyName.equals (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) { 228 propertyValue = mozillaPath; 229 } else if (propertyName.equals (XPCOM.NS_XPCOM_COMPONENT_DIR)) { 230 propertyValue = mozillaPath + COMPONENTS_DIR; 231 } else if (propertyName.equals (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) { 232 propertyValue = mozillaPath; 233 } 234 235 XPCOM.memmove (persistent, new int[] {1}, 4); 236 XPCOM.memmove (_retval, new int [] {0}, C.PTR_SIZEOF); 237 if (propertyValue != null && propertyValue.length () > 0) { 238 int [] result = new int [1]; 239 nsEmbedString pathString = new nsEmbedString (propertyValue); 240 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), true, result); 241 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 242 if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); 243 pathString.dispose (); 244 245 nsILocalFile localFile = new nsILocalFile (result [0]); 246 result[0] = 0; 247 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); 248 if (rc != XPCOM.NS_OK) Mozilla.error (rc); 249 if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); 250 251 XPCOM.memmove (_retval, new int [] {result[0]}, C.PTR_SIZEOF); 252 localFile.Release (); 253 return XPCOM.NS_OK; 254 } 255 256 return XPCOM.NS_ERROR_FAILURE; 257 } 258 } 259 | Popular Tags |