1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/platform_mime_util.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
12 #if defined(OS_ANDROID)
13 #include "net/android/network_library.h"
14 #elif defined(OS_CHROMEOS)
15 #include "net/base/mime_extension_chromeos.h"
17 #include "base/nix/mime_util_xdg.h"
22 #if defined(OS_ANDROID)
23 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
24 const base::FilePath::StringType
& ext
, std::string
* result
) const {
25 return android::GetMimeTypeFromExtension(ext
, result
);
27 #elif defined(OS_CHROMEOS)
28 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
29 const base::FilePath::StringType
& ext
,
30 std::string
* result
) const {
31 return chromeos::GetPlatformMimeTypeFromExtension(ext
, result
);
34 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
35 const base::FilePath::StringType
& ext
, std::string
* result
) const {
36 base::FilePath
dummy_path("foo." + ext
);
37 std::string out
= base::nix::GetFileMimeType(dummy_path
);
39 // GetFileMimeType likes to return application/octet-stream
40 // for everything it doesn't know - ignore that.
41 if (out
== "application/octet-stream" || out
.empty())
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")
56 #endif // defined(OS_ANDROID)
59 const char* mime_type
;
63 const struct MimeToExt mime_type_ext_map
[] = {
64 {"application/pdf", "pdf"},
65 {"application/x-tar", "tar"},
66 {"application/zip", "zip"},
67 {"audio/mpeg", "mp3"},
69 {"image/jpeg", "jpg"},
71 {"text/html", "html"},
73 {"video/mpeg", "mpg"},
74 {"text/plain", "txt"},
78 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
79 const std::string
& mime_type
, base::FilePath::StringType
* ext
) const {
82 x
< (sizeof(mime_type_ext_map
) / sizeof(MimeToExt
));
84 if (mime_type_ext_map
[x
].mime_type
== mime_type
) {
85 *ext
= mime_type_ext_map
[x
].ext
;
90 // TODO(dhg): Fix this the right way by implementing what's said below.
91 // Unlike GetPlatformMimeTypeFromExtension, this method doesn't have a
92 // default list that it uses, but for now we are also returning false since
93 // this doesn't really matter as much under Linux.
95 // If we wanted to do this properly, we would read the mime.cache file which
96 // has a section where they assign a glob (*.gif) to a mimetype
97 // (image/gif). We look up the "heaviest" glob for a certain mime type and
98 // then then try to chop off "*.".
103 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
104 const std::string
& mime_type
,
105 base::hash_set
<base::FilePath::StringType
>* extensions
) const {
106 base::FilePath::StringType ext
;
107 if (GetPreferredExtensionForMimeType(mime_type
, &ext
))
108 extensions
->insert(ext
);