xref: /aosp_15_r20/external/cronet/net/base/platform_mime_util_linux.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 #include "net/base/platform_mime_util.h"
6 
7 #include <string>
8 
9 #include "build/build_config.h"
10 #include "build/chromeos_buildflags.h"
11 
12 #if BUILDFLAG(IS_ANDROID)
13 #include "net/android/network_library.h"
14 #else
15 #include "base/nix/mime_util_xdg.h"
16 #endif
17 
18 namespace net {
19 
20 #if BUILDFLAG(IS_ANDROID)
GetPlatformMimeTypeFromExtension(const base::FilePath::StringType & ext,std::string * result) const21 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
22     const base::FilePath::StringType& ext,
23     std::string* result) const {
24   return android::GetMimeTypeFromExtension(ext, result);
25 }
26 #else
27 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
28     const base::FilePath::StringType& ext,
29     std::string* result) const {
30   base::FilePath dummy_path("foo." + ext);
31   std::string out = base::nix::GetFileMimeType(dummy_path);
32 
33   // GetFileMimeType likes to return application/octet-stream
34   // for everything it doesn't know - ignore that.
35   if (out == "application/octet-stream" || out.empty())
36     return false;
37 
38   // GetFileMimeType returns image/x-ico because that's what's in the XDG
39   // mime database. That database is the merger of the Gnome and KDE mime
40   // databases. Apparently someone working on KDE in 2001 decided .ico
41   // resolves to image/x-ico, whereas the rest of the world uses image/x-icon.
42   // FWIW, image/vnd.microsoft.icon is the official IANA assignment.
43   if (out == "image/x-ico")
44     out = "image/x-icon";
45 
46   *result = out;
47   return true;
48 }
49 
50 #endif  // BUILDFLAG(IS_ANDROID)
51 
GetPlatformPreferredExtensionForMimeType(const std::string & mime_type,base::FilePath::StringType * ext) const52 bool PlatformMimeUtil::GetPlatformPreferredExtensionForMimeType(
53     const std::string& mime_type,
54     base::FilePath::StringType* ext) const {
55   // xdg_mime doesn't provide an API to get extension from a MIME type, so we
56   // rely on the mappings hardcoded in mime_util.cc .
57   return false;
58 }
59 
GetPlatformExtensionsForMimeType(const std::string & mime_type,std::unordered_set<base::FilePath::StringType> * extensions) const60 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
61     const std::string& mime_type,
62     std::unordered_set<base::FilePath::StringType>* extensions) const {
63   // xdg_mime doesn't provide an API to get extension from a MIME type, so we
64   // rely on the mappings hardcoded in mime_util.cc .
65 }
66 
67 }  // namespace net
68