1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_BASE_MIME_UTIL_H__
6 #define NET_BASE_MIME_UTIL_H__
8 // This file defines MIME utility functions. All of them assume the MIME type
9 // to be of the format specified by rfc2045. According to it, MIME types are
10 // case strongly insensitive except parameter values, which may or may not be
13 // These utilities perform a *case-sensitive* matching for parameter values,
14 // which may produce some false negatives. Except that, matching is
17 // All constants in mime_util.cc must be written in lower case, except parameter
18 // values, which can be any case.
23 #include "base/files/file_path.h"
24 #include "net/base/net_export.h"
28 // Get the mime type (if any) that is associated with the given file extension.
29 // Returns true if a corresponding mime type exists.
30 NET_EXPORT
bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
31 std::string
* mime_type
);
33 // Get the mime type (if any) that is associated with the given file extension.
34 // Returns true if a corresponding mime type exists. In this method,
35 // the search for a mime type is constrained to a limited set of
36 // types known to the net library, the OS/registry is not consulted.
37 NET_EXPORT
bool GetWellKnownMimeTypeFromExtension(
38 const base::FilePath::StringType
& ext
,
39 std::string
* mime_type
);
41 // Get the mime type (if any) that is associated with the given file. Returns
42 // true if a corresponding mime type exists.
43 NET_EXPORT
bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
44 std::string
* mime_type
);
46 // Get the preferred extension (if any) associated with the given mime type.
47 // Returns true if a corresponding file extension exists. The extension is
48 // returned without a prefixed dot, ex "html".
49 NET_EXPORT
bool GetPreferredExtensionForMimeType(
50 const std::string
& mime_type
,
51 base::FilePath::StringType
* extension
);
53 // Check to see if a particular MIME type is in our list.
54 NET_EXPORT
bool IsSupportedImageMimeType(const std::string
& mime_type
);
55 NET_EXPORT
bool IsSupportedMediaMimeType(const std::string
& mime_type
);
56 NET_EXPORT
bool IsSupportedNonImageMimeType(const std::string
& mime_type
);
57 NET_EXPORT
bool IsUnsupportedTextMimeType(const std::string
& mime_type
);
58 NET_EXPORT
bool IsSupportedJavascriptMimeType(const std::string
& mime_type
);
59 NET_EXPORT
bool IsSupportedCertificateMimeType(const std::string
& mime_type
);
61 // Convenience function.
62 NET_EXPORT
bool IsSupportedMimeType(const std::string
& mime_type
);
64 // Returns true if this the mime_type_pattern matches a given mime-type.
65 // Checks for absolute matching and wildcards. MIME types are case insensitive.
66 NET_EXPORT
bool MatchesMimeType(const std::string
& mime_type_pattern
,
67 const std::string
& mime_type
);
69 // Returns true if the |type_string| is a correctly-formed mime type specifier
70 // with no parameter, i.e. string that matches the following ABNF (see the
71 // definition of content ABNF in RFC2045 and media-type ABNF httpbis p2
76 // If |top_level_type| is non-NULL, sets it to parsed top-level type string.
77 // If |subtype| is non-NULL, sets it to parsed subtype string.
78 NET_EXPORT
bool ParseMimeTypeWithoutParameter(const std::string
& type_string
,
79 std::string
* top_level_type
,
80 std::string
* subtype
);
82 // Returns true if the |type_string| is a top-level type of any media type
83 // registered with IANA media types registry at
84 // http://www.iana.org/assignments/media-types/media-types.xhtml or an
85 // experimental type (type with x- prefix).
87 // This method doesn't check that the input conforms to token ABNF, so if input
88 // is experimental type strings, you need to check check that before using
90 NET_EXPORT
bool IsValidTopLevelMimeType(const std::string
& type_string
);
92 // Returns true if and only if all codecs are supported, false otherwise.
93 NET_EXPORT
bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
);
95 // Parses a codec string, populating |codecs_out| with the prefix of each codec
96 // in the string |codecs_in|. For example, passed "aaa.b.c,dd.eee", if
97 // |strip| == true |codecs_out| will contain {"aaa", "dd"}, if |strip| == false
98 // |codecs_out| will contain {"aaa.b.c", "dd.eee"}.
99 // See http://www.ietf.org/rfc/rfc4281.txt.
100 NET_EXPORT
void ParseCodecString(const std::string
& codecs
,
101 std::vector
<std::string
>* codecs_out
,
104 // Check to see if a particular MIME type is in our list which only supports a
105 // certain subset of codecs.
106 NET_EXPORT
bool IsStrictMediaMimeType(const std::string
& mime_type
);
108 // Indicates that the MIME type and (possible codec string) are supported by the
109 // underlying platform.
111 // The underlying platform is known not to support the given MIME type and
112 // codec combination.
115 // The underlying platform is known to support the given MIME type and codec
119 // The underlying platform is unsure whether the given MIME type and codec
120 // combination can be rendered or not before actually trying to play it.
124 // Checks the |mime_type| and |codecs| against the MIME types known to support
125 // only a particular subset of codecs.
126 // * Returns IsSupported if the |mime_type| is supported and all the codecs
127 // within the |codecs| are supported for the |mime_type|.
128 // * Returns MayBeSupported if the |mime_type| is supported and is known to
129 // support only a subset of codecs, but |codecs| was empty. Also returned if
130 // all the codecs in |codecs| are supported, but additional codec parameters
131 // were supplied (such as profile) for which the support cannot be decided.
132 // * Returns IsNotSupported if either the |mime_type| is not supported or the
133 // |mime_type| is supported but at least one of the codecs within |codecs| is
134 // not supported for the |mime_type|.
135 NET_EXPORT SupportsType
IsSupportedStrictMediaMimeType(
136 const std::string
& mime_type
,
137 const std::vector
<std::string
>& codecs
);
139 // Get the extensions associated with the given mime type. There could be
140 // multiple extensions for a given mime type, like "html,htm" for "text/html",
141 // or "txt,text,html,..." for "text/*".
142 // Note that we do not erase the existing elements in the the provided vector.
143 // Instead, we append the result to it.
144 NET_EXPORT
void GetExtensionsForMimeType(
145 const std::string
& mime_type
,
146 std::vector
<base::FilePath::StringType
>* extensions
);
148 // Test only method that removes proprietary media types and codecs from the
149 // list of supported MIME types and codecs. These types and codecs must be
150 // removed to ensure consistent layout test results across all Chromium
152 NET_EXPORT
void RemoveProprietaryMediaTypesAndCodecsForTests();
154 // A list of supported certificate-related mime types.
156 // A Java counterpart will be generated for this enum.
157 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
158 enum CertificateMimeType
{
159 CERTIFICATE_MIME_TYPE_UNKNOWN
,
160 CERTIFICATE_MIME_TYPE_X509_USER_CERT
,
161 CERTIFICATE_MIME_TYPE_X509_CA_CERT
,
162 CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
,
165 NET_EXPORT CertificateMimeType
GetCertificateMimeTypeForMimeType(
166 const std::string
& mime_type
);
168 // Prepares one value as part of a multi-part upload request.
169 NET_EXPORT
void AddMultipartValueForUpload(const std::string
& value_name
,
170 const std::string
& value
,
171 const std::string
& mime_boundary
,
172 const std::string
& content_type
,
173 std::string
* post_data
);
175 // Adds the final delimiter to a multi-part upload request.
176 NET_EXPORT
void AddMultipartFinalDelimiterForUpload(
177 const std::string
& mime_boundary
,
178 std::string
* post_data
);
182 #endif // NET_BASE_MIME_UTIL_H__