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
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.
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 ***** */
43 BeOS implementation of the nsPluginsDir/nsPluginsFile classes.
45 Based on nsPluginsDirUNIX.cpp r1.12 by Alex Musil
48 #include "nsPluginsDir.h"
52 #include "nsReadableUtils.h"
56 #include <AppFileInfo.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
, '/');
75 return PL_strdup(filename
);
78 static nsresult
GetMimeExtensions(const char *mimeType
, char *extensions
, int extLen
)
81 if (!mimeType
|| !extensions
|| extLen
< 1) return NS_ERROR_FAILURE
;
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)
93 mime
.GetFileExtensions(&extMsg
);
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
++) {
102 if (extMsg
.FindString("extensions", i
, &ext
) != B_OK
) {
109 PL_strncpyz(extensions
, extStr
.String(), extLen
) ;
114 ///////////////////////////////////////////////////////////////////////////
116 /* nsPluginsDir implementation */
118 PRBool
nsPluginsDir::IsPluginFile(nsIFile
* file
)
123 ///////////////////////////////////////////////////////////////////////////
125 /* nsPluginFile implementation */
127 nsPluginFile::nsPluginFile(nsIFile
* spec
)
133 nsPluginFile::~nsPluginFile()
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
)
145 nsresult rv
= mPlugin
->GetNativePath(path
);
149 pLibrary
= outLibrary
= PR_LoadLibrary(path
.get());
152 printf("LoadPlugin() %s returned %lx\n",path
,(unsigned long)pLibrary
);
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
;
168 nsresult rv
= mPlugin
->GetNativePath(fpath
);
172 const char *path
= fpath
.get();
175 #ifdef NS_PLUGIN_BEOS_DEBUG
176 printf("nsPluginFile::GetPluginInfo() an attempt to load MIME String\n");
177 printf("path = <%s>\n", path
);
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
;
190 if (appinfo
.GetSupportedTypes(&msg
) != B_OK
)
191 return NS_ERROR_FAILURE
;
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
++) {
207 if (msg
.FindString("types", i
, &mtype
) != B_OK
) {
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
);
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
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
));
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
);
258 nsresult
nsPluginFile::FreePluginInfo(nsPluginInfo
& info
)
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
);
282 PL_strfree(info
.fFileName
);
285 PL_strfree(info
.fVersion
);