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 IsSupportedMediaMimeType(const std::string
& mime_type
);
56 // Returns true if this the mime_type_pattern matches a given mime-type.
57 // Checks for absolute matching and wildcards. MIME types are case insensitive.
58 NET_EXPORT
bool MatchesMimeType(const std::string
& mime_type_pattern
,
59 const std::string
& mime_type
);
61 // Returns true if the |type_string| is a correctly-formed mime type specifier
62 // with no parameter, i.e. string that matches the following ABNF (see the
63 // definition of content ABNF in RFC2045 and media-type ABNF httpbis p2
68 // If |top_level_type| is non-NULL, sets it to parsed top-level type string.
69 // If |subtype| is non-NULL, sets it to parsed subtype string.
70 NET_EXPORT
bool ParseMimeTypeWithoutParameter(const std::string
& type_string
,
71 std::string
* top_level_type
,
72 std::string
* subtype
);
74 // Returns true if the |type_string| is a top-level type of any media type
75 // registered with IANA media types registry at
76 // http://www.iana.org/assignments/media-types/media-types.xhtml or an
77 // experimental type (type with x- prefix).
79 // This method doesn't check that the input conforms to token ABNF, so if input
80 // is experimental type strings, you need to check check that before using
82 NET_EXPORT
bool IsValidTopLevelMimeType(const std::string
& type_string
);
84 // Returns true if and only if all codecs are supported, false otherwise.
85 NET_EXPORT
bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
);
87 // Parses a codec string, populating |codecs_out| with the prefix of each codec
88 // in the string |codecs_in|. For example, passed "aaa.b.c,dd.eee", if
89 // |strip| == true |codecs_out| will contain {"aaa", "dd"}, if |strip| == false
90 // |codecs_out| will contain {"aaa.b.c", "dd.eee"}.
91 // See http://www.ietf.org/rfc/rfc4281.txt.
92 NET_EXPORT
void ParseCodecString(const std::string
& codecs
,
93 std::vector
<std::string
>* codecs_out
,
96 // Check to see if a particular MIME type is in our list which only supports a
97 // certain subset of codecs.
98 NET_EXPORT
bool IsStrictMediaMimeType(const std::string
& mime_type
);
100 // Indicates that the MIME type and (possible codec string) are supported by the
101 // underlying platform.
103 // The underlying platform is known not to support the given MIME type and
104 // codec combination.
107 // The underlying platform is known to support the given MIME type and codec
111 // The underlying platform is unsure whether the given MIME type and codec
112 // combination can be rendered or not before actually trying to play it.
116 // Checks the |mime_type| and |codecs| against the MIME types known to support
117 // only a particular subset of codecs.
118 // * Returns IsSupported if the |mime_type| is supported and all the codecs
119 // within the |codecs| are supported for the |mime_type|.
120 // * Returns MayBeSupported if the |mime_type| is supported and is known to
121 // support only a subset of codecs, but |codecs| was empty. Also returned if
122 // all the codecs in |codecs| are supported, but additional codec parameters
123 // were supplied (such as profile) for which the support cannot be decided.
124 // * Returns IsNotSupported if either the |mime_type| is not supported or the
125 // |mime_type| is supported but at least one of the codecs within |codecs| is
126 // not supported for the |mime_type|.
127 NET_EXPORT SupportsType
IsSupportedStrictMediaMimeType(
128 const std::string
& mime_type
,
129 const std::vector
<std::string
>& codecs
);
131 // Get the extensions associated with the given mime type. There could be
132 // multiple extensions for a given mime type, like "html,htm" for "text/html",
133 // or "txt,text,html,..." for "text/*".
134 // Note that we do not erase the existing elements in the the provided vector.
135 // Instead, we append the result to it.
136 NET_EXPORT
void GetExtensionsForMimeType(
137 const std::string
& mime_type
,
138 std::vector
<base::FilePath::StringType
>* extensions
);
140 // Test only method that removes proprietary media types and codecs from the
141 // list of supported MIME types and codecs. These types and codecs must be
142 // removed to ensure consistent layout test results across all Chromium
144 NET_EXPORT
void RemoveProprietaryMediaTypesAndCodecsForTests();
146 // A list of supported certificate-related mime types.
148 // A Java counterpart will be generated for this enum.
149 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
150 enum CertificateMimeType
{
151 CERTIFICATE_MIME_TYPE_UNKNOWN
,
152 CERTIFICATE_MIME_TYPE_X509_USER_CERT
,
153 CERTIFICATE_MIME_TYPE_X509_CA_CERT
,
154 CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
,
157 // Prepares one value as part of a multi-part upload request.
158 NET_EXPORT
void AddMultipartValueForUpload(const std::string
& value_name
,
159 const std::string
& value
,
160 const std::string
& mime_boundary
,
161 const std::string
& content_type
,
162 std::string
* post_data
);
164 // Adds the final delimiter to a multi-part upload request.
165 NET_EXPORT
void AddMultipartFinalDelimiterForUpload(
166 const std::string
& mime_boundary
,
167 std::string
* post_data
);
171 #endif // NET_BASE_MIME_UTIL_H__