1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsPluginsDirUtils_h___
39 : #define nsPluginsDirUtils_h___
40 :
41 : #include "nsPluginsDir.h"
42 : #include "nsTArray.h"
43 : #include "prmem.h"
44 :
45 : ///////////////////////////////////////////////////////////////////////////////
46 : // Output format from NPP_GetMIMEDescription: "...mime type[;version]:[extension]:[desecription];..."
47 : // The ambiguity of mime description could cause the browser fail to parse the MIME types
48 : // correctly.
49 : // E.g. "mime type::desecription;" // correct w/o ext
50 : // "mime type:desecription;" // wrong w/o ext
51 : //
52 : static nsresult
53 173 : ParsePluginMimeDescription(const char *mdesc, nsPluginInfo &info)
54 : {
55 173 : nsresult rv = NS_ERROR_FAILURE;
56 173 : if (!mdesc || !*mdesc)
57 0 : return rv;
58 :
59 173 : char *mdescDup = PL_strdup(mdesc); // make a dup of intput string we'll change it content
60 173 : char anEmptyString[] = "";
61 346 : nsAutoTArray<char*, 8> tmpMimeTypeArr;
62 173 : char delimiters[] = {':',':',';'};
63 173 : int mimeTypeVariantCount = 0;
64 173 : char *p = mdescDup; // make a dup of intput string we'll change it content
65 519 : while(p) {
66 173 : char *ptrMimeArray[] = {anEmptyString, anEmptyString, anEmptyString};
67 :
68 : // It's easy to point out ptrMimeArray[0] to the string sounds like
69 : // "Mime type is not specified, plugin will not function properly."
70 : // and show this on "about:plugins" page, but we have to mark this particular
71 : // mime type of given plugin as disable on "about:plugins" page,
72 : // which feature is not implemented yet.
73 : // So we'll ignore, without any warnings, an empty description strings,
74 : // in other words, if after parsing ptrMimeArray[0] == anEmptyString is true.
75 : // It is possible do not to registry a plugin at all if it returns
76 : // an empty string on GetMIMEDescription() call,
77 : // e.g. plugger returns "" if pluggerrc file is not found.
78 :
79 173 : char *s = p;
80 : int i;
81 519 : for (i = 0; i < (int) sizeof(delimiters) && (p = PL_strchr(s, delimiters[i])); i++) {
82 346 : ptrMimeArray[i] = s; // save start ptr
83 346 : *p++ = 0; // overwrite delimiter
84 346 : s = p; // move forward
85 : }
86 173 : if (i == 2)
87 173 : ptrMimeArray[i] = s;
88 : // fill out the temp array
89 : // the order is important, it should be the same in for loop below
90 173 : if (ptrMimeArray[0] != anEmptyString) {
91 173 : tmpMimeTypeArr.AppendElement(ptrMimeArray[0]);
92 173 : tmpMimeTypeArr.AppendElement(ptrMimeArray[1]);
93 173 : tmpMimeTypeArr.AppendElement(ptrMimeArray[2]);
94 173 : mimeTypeVariantCount++;
95 : }
96 : }
97 :
98 : // fill out info structure
99 173 : if (mimeTypeVariantCount) {
100 173 : info.fVariantCount = mimeTypeVariantCount;
101 : // we can do these 3 mallocs at once, later on code cleanup
102 173 : info.fMimeTypeArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
103 173 : info.fMimeDescriptionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
104 173 : info.fExtensionArray = (char **)PR_Malloc(mimeTypeVariantCount * sizeof(char *));
105 :
106 : int j,i;
107 346 : for (j = i = 0; i < mimeTypeVariantCount; i++) {
108 : // the order is important, do not change it
109 : // we can get rid of PL_strdup here, later on code cleanup
110 173 : info.fMimeTypeArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
111 173 : info.fExtensionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
112 173 : info.fMimeDescriptionArray[i] = PL_strdup(tmpMimeTypeArr.ElementAt(j++));
113 : }
114 173 : rv = NS_OK;
115 : }
116 173 : if (mdescDup)
117 173 : PR_Free(mdescDup);
118 173 : return rv;
119 : }
120 :
121 : #endif /* nsPluginsDirUtils_h___ */
|