Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsPluginsDirUtils_h___
7 #define nsPluginsDirUtils_h___
9 #include "nsPluginsDir.h"
10 #include "nsTArray.h"
11 #include "prmem.h"
13 ///////////////////////////////////////////////////////////////////////////////
14 // Output format from NPP_GetMIMEDescription: "...mime type[;version]:[extension]:[desecription];..."
15 // The ambiguity of mime description could cause the browser fail to parse the MIME types
16 // correctly.
17 // E.g. "mime type::desecription;" // correct w/o ext
18 // "mime type:desecription;" // wrong w/o ext
19 //
20 static nsresult
21 ParsePluginMimeDescription(const char *mdesc, nsPluginInfo &info)
22 {
23 nsresult rv = NS_ERROR_FAILURE;
24 if (!mdesc || !*mdesc)
25 return rv;
27 char *mdescDup = PL_strdup(mdesc); // make a dup of intput string we'll change it content
28 char anEmptyString[] = "";
29 nsAutoTArray<char*, 8> tmpMimeTypeArr;
30 char delimiters[] = {':',':',';'};
31 int mimeTypeVariantCount = 0;
32 char *p = mdescDup; // make a dup of intput string we'll change it content
33 while(p) {
34 char *ptrMimeArray[] = {anEmptyString, anEmptyString, anEmptyString};
36 // It's easy to point out ptrMimeArray[0] to the string sounds like
37 // "Mime type is not specified, plugin will not function properly."
38 // and show this on "about:plugins" page, but we have to mark this particular
39 // mime type of given plugin as disable on "about:plugins" page,
40 // which feature is not implemented yet.
41 // So we'll ignore, without any warnings, an empty description strings,
42 // in other words, if after parsing ptrMimeArray[0] == anEmptyString is true.
43 // It is possible do not to registry a plugin at all if it returns
44 // an empty string on GetMIMEDescription() call,
45 // e.g. plugger returns "" if pluggerrc file is not found.
47 char *s = p;
48 int i;
49 for (i = 0; i < (int) sizeof(delimiters) && (p = PL_strchr(s, delimiters[i])); i++) {
50 ptrMimeArray[i] = s; // save start ptr
51 *p++ = 0; // overwrite delimiter
52 s = p; // move forward
53 }
54 if (i == 2)
55 ptrMimeArray[i] = s;
56 // fill out the temp array
57 // the order is important, it should be the same in for loop below
58 if (ptrMimeArray[0] != anEmptyString) {
59 tmpMimeTypeArr.AppendElement(ptrMimeArray[0]);
60 tmpMimeTypeArr.AppendElement(ptrMimeArray[1]);
61 tmpMimeTypeArr.AppendElement(ptrMimeArray[2]);
62 mimeTypeVariantCount++;
63 }
64 }
66 // fill out info structure
67 if (mimeTypeVariantCount) {
68 info.fVariantCount = mimeTypeVariantCount;
69 // we can do these 3 mallocs at once, later on code cleanup
70 info.fMimeTypeArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
71 info.fMimeDescriptionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
72 info.fExtensionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
74 int j,i;
75 for (j = i = 0; i < mimeTypeVariantCount; i++) {
76 // the order is important, do not change it
77 // we can get rid of PL_strdup here, later on code cleanup
78 info.fMimeTypeArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
79 info.fExtensionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
80 info.fMimeDescriptionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
81 }
82 rv = NS_OK;
83 }
84 if (mdescDup)
85 PR_Free(mdescDup);
86 return rv;
87 }
89 #endif /* nsPluginsDirUtils_h___ */