Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 31de2b5 | 2011-02-25 02:47:06 | [diff] [blame] | 5 | #include "net/base/platform_mime_util.h" |
| 6 | |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 9 | #include "build/build_config.h" |
Yuta Hijikata | 101ed2a | 2020-11-18 07:50:39 | [diff] [blame] | 10 | #include "build/chromeos_buildflags.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 11 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 12 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 13 | #include "net/android/network_library.h" |
Joel Hockey | b86535f3 | 2022-09-12 21:20:35 | [diff] [blame] | 14 | #elif BUILDFLAG(IS_CHROMEOS) |
Joel Hockey | b4a0c8b9 | 2022-08-30 04:33:02 | [diff] [blame] | 15 | #include "third_party/xdg_shared_mime_info/mime_cache.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 16 | #else |
[email protected] | e5285892 | 2011-11-11 20:41:21 | [diff] [blame] | 17 | #include "base/nix/mime_util_xdg.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 18 | #endif |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 19 | |
| 20 | namespace net { |
| 21 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 22 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 23 | bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 24 | const base::FilePath::StringType& ext, |
| 25 | std::string* result) const { |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 26 | return android::GetMimeTypeFromExtension(ext, result); |
| 27 | } |
Joel Hockey | b86535f3 | 2022-09-12 21:20:35 | [diff] [blame] | 28 | #elif BUILDFLAG(IS_CHROMEOS) |
yawano | c829736 | 2015-05-19 05:47:59 | [diff] [blame] | 29 | bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( |
| 30 | const base::FilePath::StringType& ext, |
| 31 | std::string* result) const { |
Joel Hockey | b4a0c8b9 | 2022-08-30 04:33:02 | [diff] [blame] | 32 | return xdg_shared_mime_info::GetMimeCacheTypeFromExtension(ext, result); |
yawano | c829736 | 2015-05-19 05:47:59 | [diff] [blame] | 33 | } |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 34 | #else |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 35 | bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 36 | const base::FilePath::StringType& ext, |
| 37 | std::string* result) const { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 38 | base::FilePath dummy_path("foo." + ext); |
[email protected] | e5285892 | 2011-11-11 20:41:21 | [diff] [blame] | 39 | std::string out = base::nix::GetFileMimeType(dummy_path); |
[email protected] | 449c5e76 | 2009-05-16 06:54:06 | [diff] [blame] | 40 | |
[email protected] | bff1ddf8 | 2009-05-16 04:30:05 | [diff] [blame] | 41 | // GetFileMimeType likes to return application/octet-stream |
| 42 | // for everything it doesn't know - ignore that. |
[email protected] | dcefa30 | 2009-05-20 00:24:39 | [diff] [blame] | 43 | if (out == "application/octet-stream" || out.empty()) |
[email protected] | bff1ddf8 | 2009-05-16 04:30:05 | [diff] [blame] | 44 | return false; |
[email protected] | 449c5e76 | 2009-05-16 06:54:06 | [diff] [blame] | 45 | |
| 46 | // GetFileMimeType returns image/x-ico because that's what's in the XDG |
| 47 | // mime database. That database is the merger of the Gnome and KDE mime |
| 48 | // databases. Apparently someone working on KDE in 2001 decided .ico |
| 49 | // resolves to image/x-ico, whereas the rest of the world uses image/x-icon. |
| 50 | // FWIW, image/vnd.microsoft.icon is the official IANA assignment. |
| 51 | if (out == "image/x-ico") |
| 52 | out = "image/x-icon"; |
| 53 | |
[email protected] | bff1ddf8 | 2009-05-16 04:30:05 | [diff] [blame] | 54 | *result = out; |
| 55 | return true; |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 56 | } |
| 57 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 58 | #endif // BUILDFLAG(IS_ANDROID) |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 59 | |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 60 | bool PlatformMimeUtil::GetPlatformPreferredExtensionForMimeType( |
| 61 | const std::string& mime_type, |
| 62 | base::FilePath::StringType* ext) 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 . |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
[email protected] | 01651e6f | 2012-05-08 22:12:44 | [diff] [blame] | 68 | void PlatformMimeUtil::GetPlatformExtensionsForMimeType( |
| 69 | const std::string& mime_type, |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 70 | std::unordered_set<base::FilePath::StringType>* extensions) const { |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 71 | // xdg_mime doesn't provide an API to get extension from a MIME type, so we |
| 72 | // rely on the mappings hardcoded in mime_util.cc . |
[email protected] | 01651e6f | 2012-05-08 22:12:44 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 085cb5a | 2008-09-08 21:45:05 | [diff] [blame] | 75 | } // namespace net |