Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / base / src / nsPluginsDirWin.cpp
blob3a1fc354b73bd78baff0f93abee9de968084125b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 nsPluginsDirWin.cpp
41 Windows implementation of the nsPluginsDir/nsPluginsFile classes.
43 by Alex Musil
46 #include "nsPluginsDir.h"
47 #include "prlink.h"
48 #include "plstr.h"
49 #include "prmem.h"
50 #include "prprf.h"
52 #include "windows.h"
53 #include "winbase.h"
55 #include "nsString.h"
57 ///////////////////////////////////////////////////////////////////////////
59 /* Local helper functions */
61 static char* GetKeyValue(char* verbuf, char* key)
63 char *buf = NULL;
64 UINT blen;
66 ::VerQueryValue(verbuf,
67 TEXT(key),
68 (void **)&buf, &blen);
70 if(buf != NULL)
72 #ifdef WINCE
73 // On windows CE, the verbuf is wide and the shunt
74 // layer can't do much about it. So, here we
75 // convert the wide string.
76 return PL_strdup(NS_ConvertUTF16toUTF8((PRUnichar*)buf).get());
77 #else
78 return PL_strdup(buf);
79 #endif
82 return nsnull;
85 static char* GetVersion(char* verbuf)
87 VS_FIXEDFILEINFO *fileInfo;
88 UINT fileInfoLen;
90 ::VerQueryValue(verbuf,
91 "\\",
92 (void **)&fileInfo, &fileInfoLen);
94 if (fileInfo != NULL)
96 return PR_smprintf("%ld.%ld.%ld.%ld",
97 HIWORD(fileInfo->dwFileVersionMS),
98 LOWORD(fileInfo->dwFileVersionMS),
99 HIWORD(fileInfo->dwFileVersionLS),
100 LOWORD(fileInfo->dwFileVersionLS));
103 return nsnull;
106 static PRUint32 CalculateVariantCount(char* mimeTypes)
108 PRUint32 variants = 1;
110 if(mimeTypes == NULL)
111 return 0;
113 char* index = mimeTypes;
114 while (*index)
116 if (*index == '|')
117 variants++;
119 ++index;
121 return variants;
124 static char** MakeStringArray(PRUint32 variants, char* data)
126 // The number of variants has been calculated based on the mime
127 // type array. Plugins are not explicitely required to match
128 // this number in two other arrays: file extention array and mime
129 // description array, and some of them actually don't.
130 // We should handle such situations gracefully
132 if((variants <= 0) || (data == NULL))
133 return NULL;
135 char ** array = (char **)PR_Calloc(variants, sizeof(char *));
136 if(array == NULL)
137 return NULL;
139 char * start = data;
141 for(PRUint32 i = 0; i < variants; i++)
143 char * p = PL_strchr(start, '|');
144 if(p != NULL)
145 *p = 0;
147 array[i] = PL_strdup(start);
149 if(p == NULL)
151 // nothing more to look for, fill everything left
152 // with empty strings and break
153 while(++i < variants)
154 array[i] = PL_strdup("");
156 break;
159 start = ++p;
161 return array;
164 static void FreeStringArray(PRUint32 variants, char ** array)
166 if((variants == 0) || (array == NULL))
167 return;
169 for(PRUint32 i = 0; i < variants; i++)
171 if(array[i] != NULL)
173 PL_strfree(array[i]);
174 array[i] = NULL;
177 PR_Free(array);
179 ///////////////////////////////////////////////////////////////////////////
181 /* nsPluginsDir implementation */
183 PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
185 PRBool ret = PR_FALSE;
186 nsCAutoString path;
187 if (NS_FAILED(file->GetNativePath(path)))
188 return PR_FALSE;
190 const char *pathname = path.get();
191 const char* filename;
192 char* extension;
193 PRUint32 len;
194 // this is most likely a path, so skip to the filename
195 filename = PL_strrchr(pathname, '\\');
196 if(filename)
197 ++filename;
198 else
199 filename = pathname;
201 len = PL_strlen(filename);
202 // the filename must be: "np*.dll"
203 extension = PL_strrchr(filename, '.');
204 if(extension)
205 ++extension;
207 if(len > 5)
209 if(!PL_strncasecmp(filename, "np", 2) && !PL_strcasecmp(extension, "dll"))
210 return PR_TRUE;
212 return ret;
215 ///////////////////////////////////////////////////////////////////////////
217 /* nsPluginFile implementation */
219 nsPluginFile::nsPluginFile(nsIFile* file)
220 : mPlugin(file)
222 // nada
225 nsPluginFile::~nsPluginFile()
227 // nada
231 * Loads the plugin into memory using NSPR's shared-library loading
232 * mechanism. Handles platform differences in loading shared libraries.
234 nsresult nsPluginFile::LoadPlugin(PRLibrary* &outLibrary)
236 // How can we convert to a full path names for using with NSPR?
237 if (!mPlugin)
238 return NS_ERROR_NULL_POINTER;
240 nsCAutoString temp;
241 mPlugin->GetNativePath(temp);
243 char* index;
244 char* pluginFolderPath = PL_strdup(temp.get());
246 index = PL_strrchr(pluginFolderPath, '\\');
247 *index = 0;
249 BOOL restoreOrigDir = FALSE;
250 char aOrigDir[MAX_PATH + 1];
251 DWORD dwCheck = ::GetCurrentDirectory(sizeof(aOrigDir), aOrigDir);
252 NS_ASSERTION(dwCheck <= MAX_PATH + 1, "Error in Loading plugin");
254 if (dwCheck <= MAX_PATH + 1)
256 restoreOrigDir = ::SetCurrentDirectory(pluginFolderPath);
257 NS_ASSERTION(restoreOrigDir, "Error in Loading plugin");
260 outLibrary = PR_LoadLibrary(temp.get());
262 if (restoreOrigDir)
264 BOOL bCheck = ::SetCurrentDirectory(aOrigDir);
265 NS_ASSERTION(bCheck, "Error in Loading plugin");
268 PL_strfree(pluginFolderPath);
270 return NS_OK;
274 * Obtains all of the information currently available for this plugin.
276 nsresult nsPluginFile::GetPluginInfo(nsPluginInfo& info)
278 nsresult res = NS_OK;
279 DWORD zerome, versionsize;
280 char* verbuf = nsnull;
282 const char* path;
284 if (!mPlugin)
285 return NS_ERROR_NULL_POINTER;
287 nsCAutoString temp;
288 mPlugin->GetNativePath(temp);
289 path = temp.get();
291 versionsize = ::GetFileVersionInfoSize((char*)path, &zerome);
292 if (versionsize > 0)
293 verbuf = (char *)PR_Malloc(versionsize);
294 if(!verbuf)
295 return NS_ERROR_OUT_OF_MEMORY;
297 if(::GetFileVersionInfo((char*)path, NULL, versionsize, verbuf))
299 info.fName = GetKeyValue(verbuf, "\\StringFileInfo\\040904E4\\ProductName");
300 info.fDescription = GetKeyValue(verbuf, "\\StringFileInfo\\040904E4\\FileDescription");
302 char *mimeType = GetKeyValue(verbuf, "\\StringFileInfo\\040904E4\\MIMEType");
303 char *mimeDescription = GetKeyValue(verbuf, "\\StringFileInfo\\040904E4\\FileOpenName");
304 char *extensions = GetKeyValue(verbuf, "\\StringFileInfo\\040904E4\\FileExtents");
306 info.fVariantCount = CalculateVariantCount(mimeType);
307 info.fMimeTypeArray = MakeStringArray(info.fVariantCount, mimeType);
308 info.fMimeDescriptionArray = MakeStringArray(info.fVariantCount, mimeDescription);
309 info.fExtensionArray = MakeStringArray(info.fVariantCount, extensions);
310 info.fFileName = PL_strdup(path);
311 info.fVersion = GetVersion(verbuf);
313 PL_strfree(mimeType);
314 PL_strfree(mimeDescription);
315 PL_strfree(extensions);
317 else
318 res = NS_ERROR_FAILURE;
321 PR_Free(verbuf);
323 return res;
326 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
328 if(info.fName != NULL)
329 PL_strfree(info.fName);
331 if(info.fDescription != NULL)
332 PL_strfree(info.fDescription);
334 if(info.fMimeTypeArray != NULL)
335 FreeStringArray(info.fVariantCount, info.fMimeTypeArray);
337 if(info.fMimeDescriptionArray != NULL)
338 FreeStringArray(info.fVariantCount, info.fMimeDescriptionArray);
340 if(info.fExtensionArray != NULL)
341 FreeStringArray(info.fVariantCount, info.fExtensionArray);
343 if(info.fFileName != NULL)
344 PL_strfree(info.fFileName);
346 if(info.fVersion != NULL)
347 PL_strfree(info.fVersion);
349 ZeroMemory((void *)&info, sizeof(info));
351 return NS_OK;