michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsPluginsDirUtils_h___ michael@0: #define nsPluginsDirUtils_h___ michael@0: michael@0: #include "nsPluginsDir.h" michael@0: #include "nsTArray.h" michael@0: #include "prmem.h" michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Output format from NPP_GetMIMEDescription: "...mime type[;version]:[extension]:[desecription];..." michael@0: // The ambiguity of mime description could cause the browser fail to parse the MIME types michael@0: // correctly. michael@0: // E.g. "mime type::desecription;" // correct w/o ext michael@0: // "mime type:desecription;" // wrong w/o ext michael@0: // michael@0: static nsresult michael@0: ParsePluginMimeDescription(const char *mdesc, nsPluginInfo &info) michael@0: { michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: if (!mdesc || !*mdesc) michael@0: return rv; michael@0: michael@0: char *mdescDup = PL_strdup(mdesc); // make a dup of intput string we'll change it content michael@0: char anEmptyString[] = ""; michael@0: nsAutoTArray tmpMimeTypeArr; michael@0: char delimiters[] = {':',':',';'}; michael@0: int mimeTypeVariantCount = 0; michael@0: char *p = mdescDup; // make a dup of intput string we'll change it content michael@0: while(p) { michael@0: char *ptrMimeArray[] = {anEmptyString, anEmptyString, anEmptyString}; michael@0: michael@0: // It's easy to point out ptrMimeArray[0] to the string sounds like michael@0: // "Mime type is not specified, plugin will not function properly." michael@0: // and show this on "about:plugins" page, but we have to mark this particular michael@0: // mime type of given plugin as disable on "about:plugins" page, michael@0: // which feature is not implemented yet. michael@0: // So we'll ignore, without any warnings, an empty description strings, michael@0: // in other words, if after parsing ptrMimeArray[0] == anEmptyString is true. michael@0: // It is possible do not to registry a plugin at all if it returns michael@0: // an empty string on GetMIMEDescription() call, michael@0: // e.g. plugger returns "" if pluggerrc file is not found. michael@0: michael@0: char *s = p; michael@0: int i; michael@0: for (i = 0; i < (int) sizeof(delimiters) && (p = PL_strchr(s, delimiters[i])); i++) { michael@0: ptrMimeArray[i] = s; // save start ptr michael@0: *p++ = 0; // overwrite delimiter michael@0: s = p; // move forward michael@0: } michael@0: if (i == 2) michael@0: ptrMimeArray[i] = s; michael@0: // fill out the temp array michael@0: // the order is important, it should be the same in for loop below michael@0: if (ptrMimeArray[0] != anEmptyString) { michael@0: tmpMimeTypeArr.AppendElement(ptrMimeArray[0]); michael@0: tmpMimeTypeArr.AppendElement(ptrMimeArray[1]); michael@0: tmpMimeTypeArr.AppendElement(ptrMimeArray[2]); michael@0: mimeTypeVariantCount++; michael@0: } michael@0: } michael@0: michael@0: // fill out info structure michael@0: if (mimeTypeVariantCount) { michael@0: info.fVariantCount = mimeTypeVariantCount; michael@0: // we can do these 3 mallocs at once, later on code cleanup michael@0: info.fMimeTypeArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *)); michael@0: info.fMimeDescriptionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *)); michael@0: info.fExtensionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *)); michael@0: michael@0: int j,i; michael@0: for (j = i = 0; i < mimeTypeVariantCount; i++) { michael@0: // the order is important, do not change it michael@0: // we can get rid of PL_strdup here, later on code cleanup michael@0: info.fMimeTypeArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++)); michael@0: info.fExtensionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++)); michael@0: info.fMimeDescriptionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++)); michael@0: } michael@0: rv = NS_OK; michael@0: } michael@0: if (mdescDup) michael@0: PR_Free(mdescDup); michael@0: return rv; michael@0: } michael@0: michael@0: #endif /* nsPluginsDirUtils_h___ */