Back to index

lightning-sunbird  0.9+nobinonly
nsPluginsDirDarwin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Patrick C. Beard <beard@netscape.com>
00024  *   Josh Aas <josh@mozillafoundation.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041   nsPluginsDirDarwin.cpp
00042   
00043   Mac OS X implementation of the nsPluginsDir/nsPluginsFile classes.
00044   
00045   by Patrick C. Beard.
00046  */
00047 
00048 #include "prlink.h"
00049 #include "prnetdb.h"
00050 
00051 #include "nsPluginsDir.h"
00052 #include "ns4xPlugin.h"
00053 #include "nsPluginsDirUtils.h"
00054 
00055 #include "nsILocalFileMac.h"
00056 #include <Carbon/Carbon.h>
00057 #include <string.h>
00058 #include <stdio.h>
00059 #include <unistd.h>
00060 #include <fcntl.h>
00061 #include <mach-o/loader.h>
00062 #include <mach-o/fat.h>
00063 
00064 #include <CFURL.h>
00065 #include <CFBundle.h>
00066 #include <CFString.h>
00067 #include <CodeFragments.h>
00068 
00069 typedef NS_4XPLUGIN_CALLBACK(const char *, NP_GETMIMEDESCRIPTION) ();
00070 typedef NS_4XPLUGIN_CALLBACK(OSErr, BP_GETSUPPORTEDMIMETYPES) (BPSupportedMIMETypes *mimeInfo, UInt32 flags);
00071 
00072 
00073 /*
00074 ** Returns a CFBundleRef if the FSSpec refers to a Mac OS X bundle directory.
00075 ** The caller is responsible for calling CFRelease() to deallocate.
00076 */
00077 static CFBundleRef getPluginBundle(const char* path)
00078 {
00079     CFBundleRef bundle = NULL;
00080     CFStringRef pathRef = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
00081     if (pathRef) {
00082         CFURLRef bundleURL = CFURLCreateWithFileSystemPath(NULL, pathRef, kCFURLPOSIXPathStyle, true);
00083         if (bundleURL != NULL) {
00084             bundle = CFBundleCreate(NULL, bundleURL);
00085             CFRelease(bundleURL);
00086         }
00087         CFRelease(pathRef);
00088     }
00089     return bundle;
00090 }
00091 
00092 static OSErr toFSSpec(nsIFile* file, FSSpec& outSpec)
00093 {
00094     nsCOMPtr<nsILocalFileMac> lfm = do_QueryInterface(file);
00095     if (!lfm)
00096         return -1;
00097     FSSpec foo;
00098     lfm->GetFSSpec(&foo);
00099     outSpec = foo;
00100 
00101     return NS_OK;
00102 }
00103 
00104 static nsresult toCFURLRef(nsIFile* file, CFURLRef& outURL)
00105 {
00106   nsCOMPtr<nsILocalFileMac> lfm = do_QueryInterface(file);
00107   if (!lfm)
00108     return NS_ERROR_FAILURE;
00109   CFURLRef url;
00110   nsresult rv = lfm->GetCFURL(&url);
00111   if (NS_SUCCEEDED(rv))
00112     outURL = url;
00113   
00114   return rv;
00115 }
00116 
00117 
00118 // Opens the resource fork for the plugin
00119 // Also checks if the plugin is a CFBundle and opens gets the correct resource
00120 static short OpenPluginResourceFork(nsIFile *pluginFile)
00121 {
00122     FSSpec spec;
00123     OSErr err = toFSSpec(pluginFile, spec);
00124     Boolean targetIsFolder, wasAliased;
00125     err = ::ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased);
00126     short refNum = ::FSpOpenResFile(&spec, fsRdPerm);
00127     if (refNum < 0) {
00128         nsCString path;
00129         pluginFile->GetNativePath(path);
00130         CFBundleRef bundle = getPluginBundle(path.get());
00131         if (bundle) {
00132             refNum = CFBundleOpenBundleResourceMap(bundle);
00133             CFRelease(bundle);
00134         }
00135     }
00136     
00137     return refNum;
00138 }
00139 
00140 // function to test whether or not this is a loadable plugin
00141 static PRBool IsLoadablePlugin(CFURLRef aURL)
00142 {
00143   if (!aURL)
00144     return PR_FALSE;
00145   
00146   PRBool isLoadable = PR_FALSE;
00147   char path[PATH_MAX];
00148   if (CFURLGetFileSystemRepresentation(aURL, TRUE, (UInt8*)path, sizeof(path))) {
00149     UInt32 magic;
00150     int f = open(path, O_RDONLY);
00151     if (f != -1) {
00152       // Mach-O headers use the byte ordering of the architecture on which
00153       // they run, so test against the magic number in the byte order
00154       // we're compiling for. Fat headers are always big-endian, so swap
00155       // them to host before comparing to host representation of the magic
00156       if (read(f, &magic, sizeof(magic)) == sizeof(magic)) {
00157         if ((magic == MH_MAGIC) || (PR_ntohl(magic) == FAT_MAGIC))
00158           isLoadable = PR_TRUE;
00159 #ifdef __POWERPC__
00160         // if we're on ppc, we can use CFM plugins
00161         if (isLoadable == PR_FALSE) {
00162           UInt32 magic2;
00163           if (read(f, &magic2, sizeof(magic2)) == sizeof(magic2)) {
00164             UInt32 cfm_header1 = 0x4A6F7921; // 'Joy!'
00165             UInt32 cfm_header2 = 0x70656666; // 'peff'
00166             if (cfm_header1 == magic && cfm_header2 == magic2)
00167               isLoadable = PR_TRUE;
00168           }
00169         }
00170 #endif
00171       }
00172       close(f);
00173     }
00174   }
00175   return isLoadable;
00176 }
00177 
00178 PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
00179 {
00180   CFURLRef pluginURL = NULL;
00181   if (NS_FAILED(toCFURLRef(file, pluginURL)))
00182     return PR_FALSE;
00183   
00184   PRBool isPluginFile = PR_FALSE;
00185   
00186   CFBundleRef pluginBundle = CFBundleCreate(kCFAllocatorDefault, pluginURL);
00187   if (pluginBundle) {
00188     UInt32 packageType, packageCreator;
00189     CFBundleGetPackageInfo(pluginBundle, &packageType, &packageCreator);
00190     if (packageType == 'BRPL' || packageType == 'IEPL' || packageType == 'NSPL') {
00191       CFURLRef executableURL = CFBundleCopyExecutableURL(pluginBundle);
00192       if (executableURL) {
00193         isPluginFile = IsLoadablePlugin(executableURL);
00194         CFRelease(executableURL);
00195       }
00196     }
00197   
00198     // some safari plugins that we can't use don't have resource forks 
00199     short refNum;
00200     if (isPluginFile) {
00201       refNum = OpenPluginResourceFork(file);
00202       if (refNum < 0) {
00203         isPluginFile = PR_FALSE;
00204       } else {
00205         ::CloseResFile(refNum); 
00206       }
00207     }
00208   
00209     CFRelease(pluginBundle);
00210   }
00211   else {
00212     LSItemInfoRecord info;
00213     if (LSCopyItemInfoForURL(pluginURL, kLSRequestTypeCreator, &info) == noErr) {
00214       if ((info.filetype == 'shlb' && info.creator == 'MOSS') ||
00215           info.filetype == 'NSPL' ||
00216           info.filetype == 'BRPL' ||
00217           info.filetype == 'IEPL') {
00218         isPluginFile = IsLoadablePlugin(pluginURL);
00219       }
00220     }
00221   }
00222   
00223   CFRelease(pluginURL);
00224   return isPluginFile;
00225 }
00226 
00227 nsPluginFile::nsPluginFile(nsIFile *spec)
00228     : mPlugin(spec)
00229 {
00230 }
00231 
00232 nsPluginFile::~nsPluginFile() {}
00233 
00238 nsresult nsPluginFile::LoadPlugin(PRLibrary* &outLibrary)
00239 {
00240     const char* path;
00241 
00242     if (!mPlugin)
00243         return NS_ERROR_NULL_POINTER;
00244 
00245     nsCAutoString temp;
00246     mPlugin->GetNativeLeafName(temp);
00247     /*
00248      * Don't load the VDP fake plugin, to avoid tripping a bad bug in OS X
00249      * 10.5.3 (see bug 436575).
00250      */
00251     if (!strcmp(temp.get(), "VerifiedDownloadPlugin.plugin")) {
00252         NS_WARNING("Preventing load of VerifiedDownloadPlugin.plugin (see bug 436575)");
00253         return NS_ERROR_FAILURE;
00254     }
00255 
00256     mPlugin->GetNativePath(temp);
00257     path = temp.get();
00258 
00259     outLibrary = PR_LoadLibrary(path);
00260     pLibrary = outLibrary;
00261     if (!outLibrary) {
00262         return NS_ERROR_FAILURE;
00263     }
00264 #ifdef DEBUG
00265     printf("[loaded plugin %s]\n", path);
00266 #endif
00267     return NS_OK;
00268 }
00269 
00270 static char* p2cstrdup(StringPtr pstr)
00271 {
00272     int len = pstr[0];
00273     char* cstr = new char[len + 1];
00274     if (cstr != NULL) {
00275         ::BlockMoveData(pstr + 1, cstr, len);
00276         cstr[len] = '\0';
00277     }
00278     return cstr;
00279 }
00280 
00281 static char* GetNextPluginStringFromHandle(Handle h, short *index)
00282 {
00283   char *ret = p2cstrdup((unsigned char*)(*h + *index));
00284   *index += (ret ? PL_strlen(ret) : 0) + 1;
00285   return ret;
00286 }
00287 
00288 static char* GetPluginString(short id, short index)
00289 {
00290     Str255 str;
00291     ::GetIndString(str, id, index);
00292     return p2cstrdup(str);
00293 }
00294 
00295 short nsPluginFile::OpenPluginResource()
00296 {
00297     return OpenPluginResourceFork(mPlugin);
00298 }
00299 
00303 nsresult nsPluginFile::GetPluginInfo(nsPluginInfo& info)
00304 {
00305     // clear out the info, except for the first field.
00306     memset(&info.fName, 0, sizeof(info) - sizeof(PRUint32));
00307 
00308     // need to open the plugin's resource file and read some resources.
00309     short refNum = OpenPluginResource();
00310 
00311     if (refNum >= 0) {
00312         if (info.fPluginInfoSize >= sizeof(nsPluginInfo)) {
00313             // 'STR#', 126, 2 => plugin name.
00314             info.fName = GetPluginString(126, 2);
00315       
00316             // 'STR#', 126, 1 => plugin description.
00317             info.fDescription = GetPluginString(126, 1);
00318 
00319             nsCString path;
00320             mPlugin->GetNativePath(path);
00321       
00322             FSSpec spec;
00323             toFSSpec(mPlugin, spec);
00324             info.fFileName = p2cstrdup(spec.name);
00325 
00326             info.fFullPath = PL_strdup(path.get());
00327             CFBundleRef bundle = getPluginBundle(path.get());
00328             if (bundle) {
00329                 info.fBundle = PR_TRUE;
00330                 CFRelease(bundle);
00331             } else
00332                 info.fBundle = PR_FALSE;
00333 
00334       // It's possible that our plugin has 2 special extra entry points that'll give us more
00335       // mime type info. Quicktime does this to get around the need of having admin rights
00336       // to change mime info in the resource fork. We need to use this info instead of the
00337       // resource. See bug 113464.
00338       BPSupportedMIMETypes mi = {kBPSupportedMIMETypesStructVers_1, NULL, NULL};
00339       if (pLibrary) {
00340 
00341         // First, check for NP_GetMIMEDescription
00342         NP_GETMIMEDESCRIPTION pfnGetMimeDesc = 
00343           (NP_GETMIMEDESCRIPTION)PR_FindSymbol(pLibrary, NP_GETMIMEDESCRIPTION_NAME); 
00344         if (pfnGetMimeDesc) {
00345           nsresult rv = ParsePluginMimeDescription(pfnGetMimeDesc(), info);
00346           if (NS_SUCCEEDED(rv)) {    // if we could parse the mime types from NP_GetMIMEDescription,
00347             ::CloseResFile(refNum);  // we've got what we need, close the resource, we're done
00348             return rv;
00349           }
00350         }
00351 
00352         // Next check for mime info from BP_GetSupportedMIMETypes
00353         BP_GETSUPPORTEDMIMETYPES pfnMime = 
00354           (BP_GETSUPPORTEDMIMETYPES)PR_FindSymbol(pLibrary, "BP_GetSupportedMIMETypes");
00355         if (pfnMime && noErr == pfnMime(&mi, 0) && mi.typeStrings) {        
00356           info.fVariantCount = (**(short**)mi.typeStrings) / 2;
00357           ::HLock(mi.typeStrings);
00358           if (mi.infoStrings)  // it's possible some plugins have infoStrings missing
00359             ::HLock(mi.infoStrings);
00360         }
00361       }
00362       
00363       // Last, we couldn't get info from an extra entry point for some reason, 
00364       // Lets get info from normal resources
00365       if (!info.fVariantCount) {
00366         mi.typeStrings = ::Get1Resource('STR#', 128);
00367         if (mi.typeStrings) {
00368           info.fVariantCount = (**(short**)mi.typeStrings) / 2;
00369           ::DetachResource(mi.typeStrings);
00370           ::HLock(mi.typeStrings);
00371         } else {
00372           // Don't add this plugin because no mime types could be found
00373           ::CloseResFile(refNum);
00374           return NS_ERROR_FAILURE;
00375         }
00376 
00377         mi.infoStrings = ::Get1Resource('STR#', 127);
00378         if (mi.infoStrings) {
00379           ::DetachResource(mi.infoStrings);
00380           ::HLock(mi.infoStrings);
00381         }
00382       }
00383 
00384       // fill-in rest of info struct
00385       int variantCount = info.fVariantCount;
00386       info.fMimeTypeArray      = new char*[variantCount];
00387       info.fExtensionArray     = new char*[variantCount];
00388       if (mi.infoStrings)
00389         info.fMimeDescriptionArray = new char*[variantCount];
00390 
00391       short mimeIndex = 2, descriptionIndex = 2;
00392             for (int i = 0; i < variantCount; i++) {
00393         info.fMimeTypeArray[i]          = GetNextPluginStringFromHandle(mi.typeStrings, &mimeIndex);
00394         info.fExtensionArray[i]         = GetNextPluginStringFromHandle(mi.typeStrings, &mimeIndex);
00395         if (mi.infoStrings)
00396           info.fMimeDescriptionArray[i] = GetNextPluginStringFromHandle(mi.infoStrings, &descriptionIndex);
00397       }
00398 
00399       ::HUnlock(mi.typeStrings);
00400       ::DisposeHandle(mi.typeStrings);
00401       if (mi.infoStrings) {
00402         ::HUnlock(mi.infoStrings);      
00403         ::DisposeHandle(mi.infoStrings);
00404             }
00405         }
00406     
00407         ::CloseResFile(refNum);
00408     }
00409     return NS_OK;
00410 }
00411 
00412 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
00413 {
00414     if (info.fPluginInfoSize <= sizeof(nsPluginInfo)) 
00415         {
00416             delete[] info.fName;
00417             delete[] info.fDescription;
00418             int variantCount = info.fVariantCount;
00419             for (int i = 0; i < variantCount; i++) 
00420                 {
00421                     delete[] info.fMimeTypeArray[i];
00422                     delete[] info.fExtensionArray[i];
00423                     delete[] info.fMimeDescriptionArray[i];
00424                 }
00425             delete[] info.fMimeTypeArray;
00426             delete[] info.fMimeDescriptionArray;
00427             delete[] info.fExtensionArray;
00428             delete[] info.fFileName;
00429             delete[] info.fFullPath;
00430         }
00431     return NS_OK;
00432 }