Back to index

lightning-sunbird  0.9+nobinonly
plugload.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 
00040 DWORD GetPluginsDir(char * path, DWORD maxsize)
00041 {
00042   if(!path)
00043     return 0;
00044 
00045   path[0] = '\0';
00046 
00047 #ifdef XP_WIN
00048 
00049   DWORD res = GetModuleFileName(NULL, path, maxsize);
00050   if(res == 0)
00051     return 0;
00052 
00053   if(path[strlen(path) - 1] == '\\')
00054     path[lstrlen(path) - 1] = '\0';
00055 
00056   char *p = strrchr(path, '\\');
00057 
00058   if(p)
00059     *p = '\0';
00060 
00061   strcat(path, "\\plugins");
00062 
00063 #endif
00064 
00065 #ifdef XP_UNIX
00066   // Implement UNIX version
00067 #endif
00068 
00069 #ifdef XP_MAC
00070   // Implement Mac version
00071 #endif
00072 
00073   res = strlen(path);
00074   return res;
00075 }
00076 
00077 XP_HLIB LoadRealPlugin(char * mimetype)
00078 {
00079   if(!mimetype || !strlen(mimetype))
00080     return NULL;
00081 
00082 #ifdef XP_WIN
00083 
00084   BOOL bDone = FALSE;
00085   WIN32_FIND_DATA ffdataStruct;
00086 
00087   char szPath[_MAX_PATH];
00088   char szFileName[_MAX_PATH];
00089 
00090   GetPluginsDir(szPath, _MAX_PATH);
00091 
00092   strcpy(szFileName, szPath);
00093   strcat(szFileName, "\\00*");
00094 
00095   HANDLE handle = FindFirstFile(szFileName, &ffdataStruct);
00096   if(handle == INVALID_HANDLE_VALUE) 
00097   {
00098     FindClose(handle);
00099     return NULL;
00100   }
00101 
00102   DWORD versize = 0L;
00103   DWORD zero = 0L;
00104   char * verbuf = NULL;
00105 
00106   do
00107   {
00108     strcpy(szFileName, szPath);
00109     strcat(szFileName, "\\");
00110     strcat(szFileName, ffdataStruct.cFileName);
00111     if(!(ffdataStruct. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
00112     {
00113       versize = GetFileVersionInfoSize(szFileName, &zero);
00114            if (versize > 0)
00115                   verbuf = new char[versize];
00116       else 
00117         continue;
00118 
00119       if(!verbuf)
00120                   continue;
00121 
00122       GetFileVersionInfo(szFileName, NULL, versize, verbuf);
00123 
00124       char *mimetypes = NULL;
00125       UINT len = 0;
00126 
00127       if(!VerQueryValue(verbuf, "\\StringFileInfo\\040904E4\\MIMEType", (void **)&mimetypes, &len)
00128          || !mimetypes || !len)
00129       {
00130         delete [] verbuf;
00131         continue;
00132       }
00133 
00134       // browse through a string of mimetypes
00135       mimetypes[len] = '\0';
00136       char * type = mimetypes;
00137 
00138       BOOL more = TRUE;
00139       while(more)
00140       {
00141         char * p = strchr(type, '|');
00142         if(p)
00143           *p = '\0';
00144         else
00145           more = FALSE;
00146 
00147         if(0 == stricmp(mimetype, type))
00148         {
00149           // this is it!
00150           delete [] verbuf;
00151           FindClose(handle);
00152           HINSTANCE hLib = LoadLibrary(szFileName);
00153           return hLib;
00154         }
00155 
00156         type = p;
00157         type++;
00158       }
00159 
00160       delete [] verbuf;
00161     }
00162 
00163   } while(FindNextFile(handle, &ffdataStruct));
00164 
00165   FindClose(handle);
00166 
00167 #endif
00168 
00169 #ifdef XP_UNIX
00170   // Implement UNIX version
00171 #endif
00172 
00173 #ifdef XP_MAC
00174   // Implement Mac version
00175 #endif
00176 
00177   return NULL;
00178 }
00179 
00180 void UnloadRealPlugin(XP_HLIB hLib)
00181 {
00182 #ifdef XP_WIN
00183   if(!hLib)
00184     FreeLibrary(hLib);
00185 #endif
00186 
00187 #ifdef XP_UNIX
00188   // Implement UNIX version
00189 #endif
00190 
00191 #ifdef XP_MAC
00192   // Implement Mac version
00193 #endif
00194 }