Back to index

lightning-sunbird  0.9+nobinonly
nsPluginsDirUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 #ifndef nsPluginsDirUtils_h___
00039 #define nsPluginsDirUtils_h___
00040 
00041 #include "nsPluginsDir.h"
00042 #include "nsVoidArray.h"
00043 #include "prmem.h"
00044 
00046 // Ouput format from NPP_GetMIMEDescription: "...mime type[;version]:[extension]:[desecription];..."
00047 // The ambiguity of mime description could cause the browser fail to parse the MIME types 
00048 // correctly.
00049 // E.g. "mime type::desecription;" // correct w/o ext 
00050 //      "mime type:desecription;"  // wrong w/o ext 
00051 //
00052 static nsresult
00053 ParsePluginMimeDescription(const char *mdesc, nsPluginInfo &info)
00054 {
00055     nsresult rv = NS_ERROR_FAILURE;
00056     if (!mdesc || !*mdesc)
00057        return rv;
00058 
00059     char *mdescDup = PL_strdup(mdesc); // make a dup of intput string we'll change it content
00060     char anEmptyString[] = "";
00061     nsAutoVoidArray tmpMimeTypeArr;
00062     char delimiters[] = {':',':',';'};
00063     int mimeTypeVariantCount = 0;
00064     char *p = mdescDup; // make a dup of intput string we'll change it content
00065     while(p) {
00066         char *ptrMimeArray[] = {anEmptyString, anEmptyString, anEmptyString};
00067 
00068         // It's easy to point out ptrMimeArray[0] to the string sounds like
00069         // "Mime type is not specified, plugin will not function properly."
00070         // and show this on "about:plugins" page, but we have to mark this particular
00071         // mime type of given plugin as disable on "about:plugins" page,
00072         // which feature is not implemented yet.
00073         // So we'll ignore, without any warnings, an empty description strings, 
00074         // in other words, if after parsing ptrMimeArray[0] == anEmptyString is true.
00075         // It is possible do not to registry a plugin at all if it returns
00076         // an empty string on GetMIMEDescription() call,
00077         // e.g. plugger returns "" if pluggerrc file is not found.
00078 
00079         char *s = p;
00080         int i;
00081         for (i = 0; i < (int) sizeof(delimiters) && (p = PL_strchr(s, delimiters[i])); i++) {
00082             ptrMimeArray[i] = s; // save start ptr
00083             *p++ = 0; // overwrite delimiter
00084             s = p; // move forward
00085         }
00086         if (i == 2)
00087            ptrMimeArray[i] = s;
00088         // fill out the temp array 
00089         // the order is important, it should be the same in for loop below 
00090         if (ptrMimeArray[0] != anEmptyString) {
00091             tmpMimeTypeArr.AppendElement((void*) ptrMimeArray[0]);
00092             tmpMimeTypeArr.AppendElement((void*) ptrMimeArray[1]);
00093             tmpMimeTypeArr.AppendElement((void*) ptrMimeArray[2]);
00094             mimeTypeVariantCount++;
00095         }
00096     }
00097 
00098     // fill out info structure
00099     if (mimeTypeVariantCount) {
00100         info.fVariantCount         = mimeTypeVariantCount;
00101         // we can do these 3 mallocs at ones, later on code cleanup
00102         info.fMimeTypeArray        = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
00103         info.fMimeDescriptionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
00104         info.fExtensionArray       = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
00105 
00106         int j,i;
00107         for (j = i = 0; i < mimeTypeVariantCount; i++) {
00108             // the order is important, do not change it
00109            // we can get rid of PL_strdup here, later on code cleanup
00110             info.fMimeTypeArray[i]        =  PL_strdup((char*) tmpMimeTypeArr.ElementAt(j++));
00111             info.fExtensionArray[i]       =  PL_strdup((char*) tmpMimeTypeArr.ElementAt(j++));
00112             info.fMimeDescriptionArray[i] =  PL_strdup((char*) tmpMimeTypeArr.ElementAt(j++));
00113         }
00114         rv = NS_OK;
00115     }
00116     if (mdescDup)
00117         PR_Free(mdescDup);
00118     return rv;
00119 }
00120 
00121 #endif /* nsPluginsDirUtils_h___ */