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