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
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.
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.
40 #define INCL_DOSERRORS
43 #include "nsPluginsDir.h"
48 #include "nsPluginDefs.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
)
60 rc
= DosQueryResourceSize( hMod
, RT_RCDATA
, resid
, &ulSize
);
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')
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
);
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
)
93 rc
= DosQueryResourceSize(hMod
, RT_RCDATA
, resid
, &ulSize
);
95 // version info is should be 8 chars
96 if (rc
== NO_ERROR
&& ulSize
== 8)
99 rc
= DosGetResource(hMod
, RT_RCDATA
, resid
, (void**) &version
);
103 string
= PR_smprintf("%d.%d.%d.%d\n",
104 version
[0], version
[2], version
[4], version
[6]);
106 DosFreeResource(version
);
113 static PRUint32
CalculateVariantCount(char* mimeTypes
)
115 PRUint32 variants
= 1;
117 if(mimeTypes
== nsnull
)
120 char* index
= mimeTypes
;
131 static char** MakeStringArray(PRUint32 variants
, char* data
)
133 if((variants
<= 0) || (data
== nsnull
))
136 char ** array
= (char **)PR_Calloc(variants
, sizeof(char *));
141 for(PRUint32 i
= 0; i
< variants
; i
++)
143 char * p
= PL_strchr(start
, '|');
147 array
[i
] = PL_strdup(start
);
153 static void FreeStringArray(PRUint32 variants
, char ** array
)
155 if((variants
== 0) || (array
== nsnull
))
158 for(PRUint32 i
= 0; i
< variants
; i
++)
160 if(array
[i
] != nsnull
)
162 PL_strfree(array
[i
]);
169 // nsPluginsDir class
171 PRBool
nsPluginsDir::IsPluginFile(nsIFile
* file
)
174 if (NS_FAILED(file
->GetNativeLeafName(leaf
)))
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)))
192 // nsPluginFile implementation
194 nsPluginFile::nsPluginFile(nsIFile
* file
)
198 nsPluginFile::~nsPluginFile()
201 // Loads the plugin into memory using NSPR's shared-library loading
202 nsresult
nsPluginFile::LoadPlugin( PRLibrary
*&outLibrary
)
205 return NS_ERROR_NULL_POINTER
;
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
] = "";
224 mPlugin
->GetNativePath(temp
);
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
);
265 DosFreeModule( hPlug
);
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
));