Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / plugin / base / src / nsPluginsDirBeOS.cpp
blob07650c076abb6de6df1bc42857dd7f647e0884e8
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):
23 * Alex Musil
24 * Makoto Hamanaka <VYA04230@nifty.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 nsPluginsDirBeOS.cpp
43 BeOS implementation of the nsPluginsDir/nsPluginsFile classes.
45 Based on nsPluginsDirUNIX.cpp r1.12 by Alex Musil
48 #include "nsPluginsDir.h"
49 #include "prlink.h"
50 #include "plstr.h"
51 #include "prmem.h"
52 #include "nsReadableUtils.h"
53 #include "nsString.h"
55 #include <File.h>
56 #include <AppFileInfo.h>
57 #include <Message.h>
58 #include <String.h>
60 //#define NS_PLUGIN_BEOS_DEBUG
62 /* Local helper functions */
64 static char* GetFileName(const char* pathname)
66 const char* filename = nsnull;
68 // this is most likely a path, so skip to the filename
69 filename = PL_strrchr(pathname, '/');
70 if(filename)
71 ++filename;
72 else
73 filename = pathname;
75 return PL_strdup(filename);
78 static nsresult GetMimeExtensions(const char *mimeType, char *extensions, int extLen)
80 // check variables
81 if (!mimeType || !extensions || extLen < 1) return NS_ERROR_FAILURE;
82 extensions[0] = '\0';
84 // make mime object
85 BMimeType mime(mimeType) ;
86 if (mime.InitCheck() != B_OK)
87 return NS_ERROR_FAILURE;
89 // get extensions : comma separated (if multiple extensions in a mime-type)
90 // ex) "jpg,jpeg"
91 BString extStr("");
92 BMessage extMsg;
93 mime.GetFileExtensions(&extMsg);
94 uint32 type;
95 int32 types_num;
96 if (extMsg.GetInfo("extensions", &type, &types_num) != B_OK
97 || type != B_STRING_TYPE || types_num == 0)
98 return NS_ERROR_FAILURE;
100 for (int i = 0 ; i < types_num ; i ++) {
101 const char *ext;
102 if (extMsg.FindString("extensions", i, &ext) != B_OK) {
103 break;
105 if (i > 0)
106 extStr.Append(",");
107 extStr.Append(ext);
109 PL_strncpyz(extensions, extStr.String(), extLen) ;
111 return NS_OK;
114 ///////////////////////////////////////////////////////////////////////////
116 /* nsPluginsDir implementation */
118 PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
120 return PR_TRUE;
123 ///////////////////////////////////////////////////////////////////////////
125 /* nsPluginFile implementation */
127 nsPluginFile::nsPluginFile(nsIFile* spec)
128 : mPlugin(spec)
130 // nada
133 nsPluginFile::~nsPluginFile()
135 // nada
139 * Loads the plugin into memory using NSPR's shared-library loading
140 * mechanism. Handles platform differences in loading shared libraries.
142 nsresult nsPluginFile::LoadPlugin(PRLibrary* &outLibrary)
144 nsCAutoString path;
145 nsresult rv = mPlugin->GetNativePath(path);
146 if (NS_OK != rv) {
147 return rv;
149 pLibrary = outLibrary = PR_LoadLibrary(path.get());
151 #ifdef NS_DEBUG
152 printf("LoadPlugin() %s returned %lx\n",path,(unsigned long)pLibrary);
153 #endif
155 return NS_OK;
158 typedef char* (*BeOS_Plugin_GetMIMEDescription)();
162 * Obtains all of the information currently available for this plugin.
164 nsresult nsPluginFile::GetPluginInfo(nsPluginInfo& info)
166 info.fVersion = nsnull;
167 nsCAutoString fpath;
168 nsresult rv = mPlugin->GetNativePath(fpath);
169 if (NS_OK != rv) {
170 return rv;
172 const char *path = fpath.get();
173 int i;
175 #ifdef NS_PLUGIN_BEOS_DEBUG
176 printf("nsPluginFile::GetPluginInfo() an attempt to load MIME String\n");
177 printf("path = <%s>\n", path);
178 #endif
180 // get supported mime types
181 BFile file(path, B_READ_ONLY);
182 if (file.InitCheck() != B_OK)
183 return NS_ERROR_FAILURE;
185 BAppFileInfo appinfo(&file);
186 if (appinfo.InitCheck() != B_OK)
187 return NS_ERROR_FAILURE;
189 BMessage msg;
190 if (appinfo.GetSupportedTypes(&msg) != B_OK)
191 return NS_ERROR_FAILURE;
193 uint32 type;
194 int32 types_num;
195 if (msg.GetInfo("types", &type, &types_num) != B_OK
196 || type != B_STRING_TYPE)
197 return NS_ERROR_FAILURE;
199 // set mime types to plugin info
200 info.fMimeTypeArray =(char **)PR_Malloc(types_num * sizeof(char *));
201 info.fMimeDescriptionArray =(char **)PR_Malloc(types_num * sizeof(char *));
202 info.fExtensionArray =(char **)PR_Malloc(types_num * sizeof(char *));
204 for (i = 0 ; i < types_num ; i ++) {
205 // get mime string
206 const char *mtype;
207 if (msg.FindString("types", i, &mtype) != B_OK) {
208 types_num = i;
209 break;
212 // get (short)description for the mime
213 char desc[B_MIME_TYPE_LENGTH+1] = "";
214 BMimeType mime(mtype) ;
215 if (mime.InitCheck() == B_OK)
216 mime.GetShortDescription(desc);
218 // get file extensions for the mime
219 char extensions[B_MIME_TYPE_LENGTH+1] = "";
220 GetMimeExtensions(mtype, extensions, B_MIME_TYPE_LENGTH+1);
222 #ifdef NS_PLUGIN_BEOS_DEBUG
223 printf(" mime = %30s | %10s | %15s |\n",
224 mtype, extensions, desc);
225 #endif
227 info.fMimeTypeArray[i] = PL_strdup( mtype ? mtype : (char *)"" ) ;
228 info.fMimeDescriptionArray[i] = PL_strdup( desc ) ;
229 info.fExtensionArray[i] = PL_strdup( extensions );
232 // get name and description of this plugin
233 version_info vinfo;
234 if (appinfo.GetVersionInfo(&vinfo, B_APP_VERSION_KIND) == B_OK
235 && *vinfo.short_info) {
236 // XXX convert UTF-8 2byte chars to 1 byte chars, to avoid string corruption
237 info.fName = ToNewCString(NS_ConvertUTF8toUTF16(vinfo.short_info));
238 info.fDescription = ToNewCString(NS_ConvertUTF8toUTF16(vinfo.long_info));
239 } else {
240 // use filename as its name
241 info.fName = GetFileName(path);
242 info.fDescription = PL_strdup("");
245 info.fVariantCount = types_num;
246 info.fFileName = PL_strdup(path);
249 #ifdef NS_PLUGIN_BEOS_DEBUG
250 printf("info.fFileName = %s\n", info.fFileName);
251 printf("info.fName = %s\n", info.fName);
252 printf("info.fDescription = %s\n", info.fDescription);
253 #endif
255 return NS_OK;
258 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
260 if (info.fName)
261 PL_strfree(info.fName);
263 if (info.fDescription)
264 PL_strfree(info.fDescription);
266 for (PRUint32 i = 0; i < info.fVariantCount; i++) {
267 if (info.fMimeTypeArray[i])
268 PL_strfree(info.fMimeTypeArray[i]);
270 if (info.fMimeDescriptionArray[i])
271 PL_strfree(info.fMimeDescriptionArray[i]);
273 if (info.fExtensionArray[i])
274 PL_strfree(info.fExtensionArray[i]);
277 PR_FREEIF(info.fMimeTypeArray);
278 PR_FREEIF(info.fMimeDescriptionArray);
279 PR_FREEIF(info.fExtensionArray);
281 if (info.fFileName)
282 PL_strfree(info.fFileName);
284 if (info.fVersion)
285 PL_strfree(info.fVersion);
287 return NS_OK;