xref: /aosp_15_r20/external/cronet/base/nix/mime_util_xdg.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_NIX_MIME_UTIL_XDG_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_NIX_MIME_UTIL_XDG_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <map>
11*6777b538SAndroid Build Coastguard Worker #include <string>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
14*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker namespace base {
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker class FilePath;
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker namespace nix {
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // Mime type with weight.
23*6777b538SAndroid Build Coastguard Worker struct WeightedMime {
24*6777b538SAndroid Build Coastguard Worker   std::string mime_type;
25*6777b538SAndroid Build Coastguard Worker   uint8_t weight;
26*6777b538SAndroid Build Coastguard Worker };
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker // Map of file extension to weighted mime type.
29*6777b538SAndroid Build Coastguard Worker using MimeTypeMap = std::map<std::string, WeightedMime>;
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker // Parses a file at `file_path` which should be in the same format as the
32*6777b538SAndroid Build Coastguard Worker // /usr/share/mime/mime.cache file on Linux.
33*6777b538SAndroid Build Coastguard Worker // https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html#idm46058238280640
34*6777b538SAndroid Build Coastguard Worker // `out_mime_types` will be populated with keys that are a file extension and a
35*6777b538SAndroid Build Coastguard Worker // value that is a MIME type if a higher weighted mime is found that currently
36*6777b538SAndroid Build Coastguard Worker // exists. Returns true if there was a valid list parsed from the file and false
37*6777b538SAndroid Build Coastguard Worker // otherwise.
38*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool ParseMimeTypes(const FilePath& file_path,
39*6777b538SAndroid Build Coastguard Worker                                 MimeTypeMap& out_mime_types);
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker // Gets the mime type for a file at |filepath|.
42*6777b538SAndroid Build Coastguard Worker //
43*6777b538SAndroid Build Coastguard Worker // The mime type is calculated based only on the file name of |filepath|.  In
44*6777b538SAndroid Build Coastguard Worker // particular |filepath| will not be touched on disk and |filepath| doesn't even
45*6777b538SAndroid Build Coastguard Worker // have to exist.  This means that the function does not work for directories
46*6777b538SAndroid Build Coastguard Worker // (i.e. |filepath| is assumed to be a path to a file).
47*6777b538SAndroid Build Coastguard Worker //
48*6777b538SAndroid Build Coastguard Worker // Note that this function might need to read from disk the mime-types data
49*6777b538SAndroid Build Coastguard Worker // provided by the OS.  Therefore this function should not be called from
50*6777b538SAndroid Build Coastguard Worker // threads that disallow blocking.
51*6777b538SAndroid Build Coastguard Worker //
52*6777b538SAndroid Build Coastguard Worker // If the mime type is unknown, this will return application/octet-stream.
53*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::string GetFileMimeType(const FilePath& filepath);
54*6777b538SAndroid Build Coastguard Worker 
55*6777b538SAndroid Build Coastguard Worker }  // namespace nix
56*6777b538SAndroid Build Coastguard Worker }  // namespace base
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker #endif  // BASE_NIX_MIME_UTIL_XDG_H_
59