Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / base / src / nsPluginsDirOS2.cpp
blob31ddfff8a9be04b7a360c137f522bd745531b3a7
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the Mozilla OS/2 libraries.
16 * The Initial Developer of the Original Code is
17 * John Fairhurst, <john_fairhurst@iname.com>.
18 * Portions created by the Initial Developer are Copyright (C) 1998
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 // OS/2 plugin-loading code.
39 #define INCL_DOS
40 #define INCL_DOSERRORS
41 #include <os2.h>
43 #include "nsPluginsDir.h"
44 #include "prlink.h"
45 #include "plstr.h"
46 #include "prmem.h"
47 #include "prprf.h"
48 #include "nsPluginDefs.h"
50 #include "nsString.h"
52 /* Load a string stored as RCDATA in a resource segment */
53 /* Returned string needs to be PR_Free'd by caller */
54 static char *LoadRCDATAString( HMODULE hMod, ULONG resid)
56 APIRET rc;
57 ULONG ulSize = 0;
58 char *string = 0;
60 rc = DosQueryResourceSize( hMod, RT_RCDATA, resid, &ulSize);
62 if( rc == NO_ERROR)
64 char *readOnlyString = 0;
65 rc = DosGetResource( hMod, RT_RCDATA, resid, (void**) &readOnlyString);
67 /* allow for 0-termination if user hasn't got it right */
68 if( readOnlyString[ ulSize - 1] != '\0')
69 ulSize++;
71 if( rc == NO_ERROR)
73 /* copy string & zero-terminate */
74 string = (char*) PR_Malloc( ulSize);
75 memcpy( string, readOnlyString, ulSize - 1);
76 string[ ulSize - 1] = '\0';
78 DosFreeResource( readOnlyString);
82 return string;
85 /* Load a version string stored as RCDATA in a resource segment */
86 /* Returned string needs to be PR_Free'd by caller */
87 static char *LoadRCDATAVersion(HMODULE hMod, ULONG resid)
89 APIRET rc;
90 ULONG ulSize = 0;
91 char *string = 0;
93 rc = DosQueryResourceSize(hMod, RT_RCDATA, resid, &ulSize);
95 // version info is should be 8 chars
96 if (rc == NO_ERROR && ulSize == 8)
98 char *version = NULL;
99 rc = DosGetResource(hMod, RT_RCDATA, resid, (void**) &version);
101 if (rc == NO_ERROR)
103 string = PR_smprintf("%d.%d.%d.%d\n",
104 version[0], version[2], version[4], version[6]);
106 DosFreeResource(version);
110 return string;
113 static PRUint32 CalculateVariantCount(char* mimeTypes)
115 PRUint32 variants = 1;
117 if(mimeTypes == nsnull)
118 return 0;
120 char* index = mimeTypes;
121 while (*index)
123 if (*index == '|')
124 variants++;
126 ++index;
128 return variants;
131 static char** MakeStringArray(PRUint32 variants, char* data)
133 if((variants <= 0) || (data == nsnull))
134 return nsnull;
136 char ** array = (char **)PR_Calloc(variants, sizeof(char *));
137 if(array == nsnull)
138 return nsnull;
140 char * start = data;
141 for(PRUint32 i = 0; i < variants; i++)
143 char * p = PL_strchr(start, '|');
144 if(p != nsnull)
145 *p = 0;
147 array[i] = PL_strdup(start);
148 start = ++p;
150 return array;
153 static void FreeStringArray(PRUint32 variants, char ** array)
155 if((variants == 0) || (array == nsnull))
156 return;
158 for(PRUint32 i = 0; i < variants; i++)
160 if(array[i] != nsnull)
162 PL_strfree(array[i]);
163 array[i] = nsnull;
166 PR_Free(array);
169 // nsPluginsDir class
171 PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
173 nsCAutoString leaf;
174 if (NS_FAILED(file->GetNativeLeafName(leaf)))
175 return PR_FALSE;
177 const char *leafname = leaf.get();
179 if( nsnull != leafname)
181 int len = strlen( leafname);
182 if( len > 6 && // np*.dll
183 (0 == strnicmp( &(leafname[len - 4]), ".dll", 4)) &&
184 (0 == strnicmp( leafname, "np", 2)))
186 return PR_TRUE;
189 return PR_FALSE;
192 // nsPluginFile implementation
194 nsPluginFile::nsPluginFile(nsIFile* file)
195 : mPlugin(file)
198 nsPluginFile::~nsPluginFile()
201 // Loads the plugin into memory using NSPR's shared-library loading
202 nsresult nsPluginFile::LoadPlugin( PRLibrary *&outLibrary)
204 if (!mPlugin)
205 return NS_ERROR_NULL_POINTER;
207 nsCAutoString temp;
208 mPlugin->GetNativePath(temp);
210 outLibrary = PR_LoadLibrary(temp.get());
211 return outLibrary == nsnull ? NS_ERROR_FAILURE : NS_OK;
214 // Obtains all of the information currently available for this plugin.
215 nsresult nsPluginFile::GetPluginInfo( nsPluginInfo &info)
217 nsresult rc = NS_ERROR_FAILURE;
218 HMODULE hPlug = 0; // Need a HMODULE to query resource statements
219 char failure[ CCHMAXPATH] = "";
220 APIRET ret;
222 const char* path;
223 nsCAutoString temp;
224 mPlugin->GetNativePath(temp);
225 path = temp.get();
226 ret = DosLoadModule( failure, CCHMAXPATH, path, &hPlug);
227 info.fVersion = nsnull;
229 while( ret == NO_ERROR)
231 info.fName = LoadRCDATAString( hPlug, NS_INFO_ProductName);
233 info.fVersion = LoadRCDATAVersion( hPlug, NS_INFO_ProductVersion);
235 // get description (doesn't matter if it's missing)...
236 info.fDescription = LoadRCDATAString( hPlug, NS_INFO_FileDescription);
238 char * mimeType = LoadRCDATAString( hPlug, NS_INFO_MIMEType);
239 if( nsnull == mimeType) break;
241 char * mimeDescription = LoadRCDATAString( hPlug, NS_INFO_FileOpenName);
242 if( nsnull == mimeDescription) break;
244 char * extensions = LoadRCDATAString( hPlug, NS_INFO_FileExtents);
245 if( nsnull == extensions) break;
247 info.fVariantCount = CalculateVariantCount(mimeType);
249 info.fMimeTypeArray = MakeStringArray(info.fVariantCount, mimeType);
250 if( info.fMimeTypeArray == nsnull) break;
252 info.fMimeDescriptionArray = MakeStringArray(info.fVariantCount, mimeDescription);
253 if( nsnull == info.fMimeDescriptionArray) break;
255 info.fExtensionArray = MakeStringArray(info.fVariantCount, extensions);
256 if( nsnull == info.fExtensionArray) break;
258 info.fFileName = PL_strdup(path);
260 rc = NS_OK;
261 break;
264 if( 0 != hPlug)
265 DosFreeModule( hPlug);
267 return rc;
270 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
272 if(info.fName != nsnull)
273 PL_strfree(info.fName);
275 if(info.fFileName != nsnull)
276 PL_strfree(info.fFileName);
278 if(info.fVersion != nsnull)
279 PL_strfree(info.fVersion);
281 if(info.fDescription != nsnull)
282 PL_strfree(info.fDescription);
284 if(info.fMimeTypeArray != nsnull)
285 FreeStringArray(info.fVariantCount, info.fMimeTypeArray);
287 if(info.fMimeDescriptionArray != nsnull)
288 FreeStringArray(info.fVariantCount, info.fMimeDescriptionArray);
290 if(info.fExtensionArray != nsnull)
291 FreeStringArray(info.fVariantCount, info.fExtensionArray);
293 memset((void *)&info, 0, sizeof(info));
295 return NS_OK;