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.
4
5#ifndef NET_BASE_MIME_UTIL_H__
6#define NET_BASE_MIME_UTIL_H__
7
8#include <string>
9#include <vector>
10
11#include "base/files/file_path.h"
12#include "net/base/net_export.h"
13
14namespace net {
15
16// Get the mime type (if any) that is associated with the given file extension.
17// Returns true if a corresponding mime type exists.
18NET_EXPORT bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
19                                         std::string* mime_type);
20
21// Get the mime type (if any) that is associated with the given file extension.
22// Returns true if a corresponding mime type exists. In this method,
23// the search for a mime type is constrained to a limited set of
24// types known to the net library, the OS/registry is not consulted.
25NET_EXPORT bool GetWellKnownMimeTypeFromExtension(
26    const base::FilePath::StringType& ext,
27    std::string* mime_type);
28
29// Get the mime type (if any) that is associated with the given file.  Returns
30// true if a corresponding mime type exists.
31NET_EXPORT bool GetMimeTypeFromFile(const base::FilePath& file_path,
32                                    std::string* mime_type);
33
34// Get the preferred extension (if any) associated with the given mime type.
35// Returns true if a corresponding file extension exists.  The extension is
36// returned without a prefixed dot, ex "html".
37NET_EXPORT bool GetPreferredExtensionForMimeType(
38    const std::string& mime_type,
39    base::FilePath::StringType* extension);
40
41// Check to see if a particular MIME type is in our list.
42NET_EXPORT bool IsSupportedImageMimeType(const std::string& mime_type);
43NET_EXPORT bool IsSupportedMediaMimeType(const std::string& mime_type);
44NET_EXPORT bool IsSupportedNonImageMimeType(const std::string& mime_type);
45NET_EXPORT bool IsUnsupportedTextMimeType(const std::string& mime_type);
46NET_EXPORT bool IsSupportedJavascriptMimeType(const std::string& mime_type);
47NET_EXPORT bool IsSupportedCertificateMimeType(const std::string& mime_type);
48
49// Convenience function.
50NET_EXPORT bool IsSupportedMimeType(const std::string& mime_type);
51
52// Returns true if this the mime_type_pattern matches a given mime-type.
53// Checks for absolute matching and wildcards.  mime-types should be in
54// lower case.
55NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern,
56                                const std::string& mime_type);
57
58// Returns true if the |type_string| is a correctly-formed mime type specifier.
59// Allows strings of the form x/y[;params], where "x" is a legal mime type name.
60// Also allows wildcard types -- "x/*", "*/*", and "*".
61NET_EXPORT bool IsMimeType(const std::string& type_string);
62
63// Returns true if and only if all codecs are supported, false otherwise.
64NET_EXPORT bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs);
65
66// Parses a codec string, populating |codecs_out| with the prefix of each codec
67// in the string |codecs_in|. For example, passed "aaa.b.c,dd.eee", if
68// |strip| == true |codecs_out| will contain {"aaa", "dd"}, if |strip| == false
69// |codecs_out| will contain {"aaa.b.c", "dd.eee"}.
70// See http://www.ietf.org/rfc/rfc4281.txt.
71NET_EXPORT void ParseCodecString(const std::string& codecs,
72                                 std::vector<std::string>* codecs_out,
73                                 bool strip);
74
75// Check to see if a particular MIME type is in our list which only supports a
76// certain subset of codecs.
77NET_EXPORT bool IsStrictMediaMimeType(const std::string& mime_type);
78
79// Check to see if a particular MIME type is in our list which only supports a
80// certain subset of codecs. Returns true if and only if all codecs are
81// supported for that specific MIME type, false otherwise. If this returns
82// false you will still need to check if the media MIME tpyes and codecs are
83// supported.
84NET_EXPORT bool IsSupportedStrictMediaMimeType(
85    const std::string& mime_type,
86    const std::vector<std::string>& codecs);
87
88// Get the extensions associated with the given mime type. This should be passed
89// in lower case. There could be multiple extensions for a given mime type, like
90// "html,htm" for "text/html", or "txt,text,html,..." for "text/*".
91// Note that we do not erase the existing elements in the the provided vector.
92// Instead, we append the result to it.
93NET_EXPORT void GetExtensionsForMimeType(
94    const std::string& mime_type,
95    std::vector<base::FilePath::StringType>* extensions);
96
97// Test only method that removes proprietary media types and codecs from the
98// list of supported MIME types and codecs. These types and codecs must be
99// removed to ensure consistent layout test results across all Chromium
100// variations.
101NET_EXPORT void RemoveProprietaryMediaTypesAndCodecsForTests();
102
103// Returns the IANA media type contained in |mime_type|, or an empty
104// string if |mime_type| does not specifify a known media type.
105// Supported media types are defined at:
106// http://www.iana.org/assignments/media-types/index.html
107NET_EXPORT const std::string GetIANAMediaType(const std::string& mime_type);
108
109// A list of supported certificate-related mime types.
110enum CertificateMimeType {
111#define CERTIFICATE_MIME_TYPE(name, value) CERTIFICATE_MIME_TYPE_ ## name = value,
112#include "net/base/mime_util_certificate_type_list.h"
113#undef CERTIFICATE_MIME_TYPE
114};
115
116NET_EXPORT CertificateMimeType GetCertificateMimeTypeForMimeType(
117    const std::string& mime_type);
118
119// Prepares one value as part of a multi-part upload request.
120NET_EXPORT void AddMultipartValueForUpload(const std::string& value_name,
121                                           const std::string& value,
122                                           const std::string& mime_boundary,
123                                           const std::string& content_type,
124                                           std::string* post_data);
125
126// Adds the final delimiter to a multi-part upload request.
127NET_EXPORT void AddMultipartFinalDelimiterForUpload(
128    const std::string& mime_boundary,
129    std::string* post_data);
130
131}  // namespace net
132
133#endif  // NET_BASE_MIME_UTIL_H__
134