blob: faf1ab72106be8aa19346dfd9affb7f21463f91b [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]085cb5a2008-09-08 21:45:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]31de2b52011-02-25 02:47:065#include "net/base/platform_mime_util.h"
6
[email protected]085cb5a2008-09-08 21:45:057#include <string>
8
[email protected]6dbdaa82011-08-11 16:05:569#include "build/build_config.h"
Yuta Hijikata101ed2a2020-11-18 07:50:3910#include "build/chromeos_buildflags.h"
[email protected]6dbdaa82011-08-11 16:05:5611
Xiaohan Wang2a6845b2022-01-08 04:40:5712#if BUILDFLAG(IS_ANDROID)
[email protected]6dbdaa82011-08-11 16:05:5613#include "net/android/network_library.h"
Joel Hockeyb86535f32022-09-12 21:20:3514#elif BUILDFLAG(IS_CHROMEOS)
Joel Hockeyb4a0c8b92022-08-30 04:33:0215#include "third_party/xdg_shared_mime_info/mime_cache.h"
[email protected]6dbdaa82011-08-11 16:05:5616#else
[email protected]e52858922011-11-11 20:41:2117#include "base/nix/mime_util_xdg.h"
[email protected]6dbdaa82011-08-11 16:05:5618#endif
[email protected]085cb5a2008-09-08 21:45:0519
20namespace net {
21
Xiaohan Wang2a6845b2022-01-08 04:40:5722#if BUILDFLAG(IS_ANDROID)
[email protected]6dbdaa82011-08-11 16:05:5623bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
Sergey Ulanov77132a22017-07-21 02:34:0524 const base::FilePath::StringType& ext,
25 std::string* result) const {
[email protected]6dbdaa82011-08-11 16:05:5626 return android::GetMimeTypeFromExtension(ext, result);
27}
Joel Hockeyb86535f32022-09-12 21:20:3528#elif BUILDFLAG(IS_CHROMEOS)
yawanoc8297362015-05-19 05:47:5929bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
30 const base::FilePath::StringType& ext,
31 std::string* result) const {
Joel Hockeyb4a0c8b92022-08-30 04:33:0232 return xdg_shared_mime_info::GetMimeCacheTypeFromExtension(ext, result);
yawanoc8297362015-05-19 05:47:5933}
[email protected]6dbdaa82011-08-11 16:05:5634#else
[email protected]085cb5a2008-09-08 21:45:0535bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
Sergey Ulanov77132a22017-07-21 02:34:0536 const base::FilePath::StringType& ext,
37 std::string* result) const {
[email protected]6cdfd7f2013-02-08 20:40:1538 base::FilePath dummy_path("foo." + ext);
[email protected]e52858922011-11-11 20:41:2139 std::string out = base::nix::GetFileMimeType(dummy_path);
[email protected]449c5e762009-05-16 06:54:0640
[email protected]bff1ddf82009-05-16 04:30:0541 // GetFileMimeType likes to return application/octet-stream
42 // for everything it doesn't know - ignore that.
[email protected]dcefa302009-05-20 00:24:3943 if (out == "application/octet-stream" || out.empty())
[email protected]bff1ddf82009-05-16 04:30:0544 return false;
[email protected]449c5e762009-05-16 06:54:0645
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]bff1ddf82009-05-16 04:30:0554 *result = out;
55 return true;
[email protected]085cb5a2008-09-08 21:45:0556}
57
Xiaohan Wang2a6845b2022-01-08 04:40:5758#endif // BUILDFLAG(IS_ANDROID)
[email protected]6dbdaa82011-08-11 16:05:5659
Sergey Ulanov77132a22017-07-21 02:34:0560bool 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]085cb5a2008-09-08 21:45:0565 return false;
66}
67
[email protected]01651e6f2012-05-08 22:12:4468void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
69 const std::string& mime_type,
davidben1e912ea2016-04-20 19:17:0770 std::unordered_set<base::FilePath::StringType>* extensions) const {
Sergey Ulanov77132a22017-07-21 02:34:0571 // 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]01651e6f2012-05-08 22:12:4473}
74
[email protected]085cb5a2008-09-08 21:45:0575} // namespace net