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