[JAEGER] Merge from tracemonkey.
[mozilla-central.git] / modules / plugin / base / src / nsPluginsDirOS2.cpp
blob95ebfafcd5b1f5bf6ec5436d35bcabbdfba44f90
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 "npapi.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 rv = NS_ERROR_FAILURE;
218 HMODULE hPlug = 0; // Need a HMODULE to query resource statements
219 char failure[ CCHMAXPATH] = "";
220 APIRET ret;
222 nsCAutoString path;
223 if (NS_FAILED(rv = mPlugin->GetNativePath(path)))
224 return rv;
226 nsCAutoString fileName;
227 if (NS_FAILED(rv = mPlugin->GetNativeLeafName(fileName)))
228 return rv;
230 ret = DosLoadModule( failure, CCHMAXPATH, path.get(), &hPlug);
231 info.fVersion = nsnull;
233 while( ret == NO_ERROR)
235 info.fName = LoadRCDATAString( hPlug, NP_INFO_ProductName);
237 info.fVersion = LoadRCDATAVersion( hPlug, NP_INFO_ProductVersion);
239 // get description (doesn't matter if it's missing)...
240 info.fDescription = LoadRCDATAString( hPlug, NP_INFO_FileDescription);
242 char * mimeType = LoadRCDATAString( hPlug, NP_INFO_MIMEType);
243 if( nsnull == mimeType) break;
245 char * mimeDescription = LoadRCDATAString( hPlug, NP_INFO_FileOpenName);
246 if( nsnull == mimeDescription) break;
248 char * extensions = LoadRCDATAString( hPlug, NP_INFO_FileExtents);
249 if( nsnull == extensions) break;
251 info.fVariantCount = CalculateVariantCount(mimeType);
253 info.fMimeTypeArray = MakeStringArray(info.fVariantCount, mimeType);
254 if( info.fMimeTypeArray == nsnull) break;
256 info.fMimeDescriptionArray = MakeStringArray(info.fVariantCount, mimeDescription);
257 if( nsnull == info.fMimeDescriptionArray) break;
259 info.fExtensionArray = MakeStringArray(info.fVariantCount, extensions);
260 if( nsnull == info.fExtensionArray) break;
262 info.fFullPath = PL_strdup(path.get());
263 info.fFileName = PL_strdup(fileName.get());
265 rv = NS_OK;
266 break;
269 if( 0 != hPlug)
270 DosFreeModule( hPlug);
272 return rv;
275 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
277 if(info.fName != nsnull)
278 PL_strfree(info.fName);
280 if(info.fFullPath != nsnull)
281 PL_strfree(info.fFullPath);
283 if(info.fFileName != nsnull)
284 PL_strfree(info.fFileName);
286 if(info.fVersion != nsnull)
287 PL_strfree(info.fVersion);
289 if(info.fDescription != nsnull)
290 PL_strfree(info.fDescription);
292 if(info.fMimeTypeArray != nsnull)
293 FreeStringArray(info.fVariantCount, info.fMimeTypeArray);
295 if(info.fMimeDescriptionArray != nsnull)
296 FreeStringArray(info.fVariantCount, info.fMimeDescriptionArray);
298 if(info.fExtensionArray != nsnull)
299 FreeStringArray(info.fVariantCount, info.fExtensionArray);
301 memset((void *)&info, 0, sizeof(info));
303 return NS_OK;