dom/plugins/base/nsPluginsDirUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/base/nsPluginsDirUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsPluginsDirUtils_h___
    1.10 +#define nsPluginsDirUtils_h___
    1.11 +
    1.12 +#include "nsPluginsDir.h"
    1.13 +#include "nsTArray.h"
    1.14 +#include "prmem.h"
    1.15 +
    1.16 +///////////////////////////////////////////////////////////////////////////////
    1.17 +// Output format from NPP_GetMIMEDescription: "...mime type[;version]:[extension]:[desecription];..."
    1.18 +// The ambiguity of mime description could cause the browser fail to parse the MIME types 
    1.19 +// correctly.
    1.20 +// E.g. "mime type::desecription;" // correct w/o ext 
    1.21 +//      "mime type:desecription;"  // wrong w/o ext 
    1.22 +//
    1.23 +static nsresult
    1.24 +ParsePluginMimeDescription(const char *mdesc, nsPluginInfo &info)
    1.25 +{
    1.26 +    nsresult rv = NS_ERROR_FAILURE;
    1.27 +    if (!mdesc || !*mdesc)
    1.28 +       return rv;
    1.29 +
    1.30 +    char *mdescDup = PL_strdup(mdesc); // make a dup of intput string we'll change it content
    1.31 +    char anEmptyString[] = "";
    1.32 +    nsAutoTArray<char*, 8> tmpMimeTypeArr;
    1.33 +    char delimiters[] = {':',':',';'};
    1.34 +    int mimeTypeVariantCount = 0;
    1.35 +    char *p = mdescDup; // make a dup of intput string we'll change it content
    1.36 +    while(p) {
    1.37 +        char *ptrMimeArray[] = {anEmptyString, anEmptyString, anEmptyString};
    1.38 +
    1.39 +        // It's easy to point out ptrMimeArray[0] to the string sounds like
    1.40 +        // "Mime type is not specified, plugin will not function properly."
    1.41 +        // and show this on "about:plugins" page, but we have to mark this particular
    1.42 +        // mime type of given plugin as disable on "about:plugins" page,
    1.43 +        // which feature is not implemented yet.
    1.44 +        // So we'll ignore, without any warnings, an empty description strings, 
    1.45 +        // in other words, if after parsing ptrMimeArray[0] == anEmptyString is true.
    1.46 +        // It is possible do not to registry a plugin at all if it returns
    1.47 +        // an empty string on GetMIMEDescription() call,
    1.48 +        // e.g. plugger returns "" if pluggerrc file is not found.
    1.49 +
    1.50 +        char *s = p;
    1.51 +        int i;
    1.52 +        for (i = 0; i < (int) sizeof(delimiters) && (p = PL_strchr(s, delimiters[i])); i++) {
    1.53 +            ptrMimeArray[i] = s; // save start ptr
    1.54 +            *p++ = 0; // overwrite delimiter
    1.55 +            s = p; // move forward
    1.56 +        }
    1.57 +        if (i == 2)
    1.58 +           ptrMimeArray[i] = s;
    1.59 +        // fill out the temp array 
    1.60 +        // the order is important, it should be the same in for loop below 
    1.61 +        if (ptrMimeArray[0] != anEmptyString) {
    1.62 +            tmpMimeTypeArr.AppendElement(ptrMimeArray[0]);
    1.63 +            tmpMimeTypeArr.AppendElement(ptrMimeArray[1]);
    1.64 +            tmpMimeTypeArr.AppendElement(ptrMimeArray[2]);
    1.65 +            mimeTypeVariantCount++;
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    // fill out info structure
    1.70 +    if (mimeTypeVariantCount) {
    1.71 +        info.fVariantCount         = mimeTypeVariantCount;
    1.72 +        // we can do these 3 mallocs at once, later on code cleanup
    1.73 +        info.fMimeTypeArray        = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
    1.74 +        info.fMimeDescriptionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
    1.75 +        info.fExtensionArray       = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
    1.76 +
    1.77 +        int j,i;
    1.78 +        for (j = i = 0; i < mimeTypeVariantCount; i++) {
    1.79 +            // the order is important, do not change it
    1.80 +           // we can get rid of PL_strdup here, later on code cleanup
    1.81 +            info.fMimeTypeArray[i]        =  PL_strdup(tmpMimeTypeArr.ElementAt(j++));
    1.82 +            info.fExtensionArray[i]       =  PL_strdup(tmpMimeTypeArr.ElementAt(j++));
    1.83 +            info.fMimeDescriptionArray[i] =  PL_strdup(tmpMimeTypeArr.ElementAt(j++));
    1.84 +        }
    1.85 +        rv = NS_OK;
    1.86 +    }
    1.87 +    if (mdescDup)
    1.88 +        PR_Free(mdescDup);
    1.89 +    return rv;
    1.90 +}
    1.91 +
    1.92 +#endif /* nsPluginsDirUtils_h___ */

mercurial