Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / platform_mime_util_linux.cc
blob13af8f80f68d2afebfa560fa3863b6f1245c3463
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"
7 #include <string>
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"
16 #else
17 #include "base/nix/mime_util_xdg.h"
18 #endif
20 namespace net {
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);
33 #else
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())
42 return false;
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";
52 *result = out;
53 return true;
56 #endif // defined(OS_ANDROID)
58 struct MimeToExt {
59 const char* mime_type;
60 const char* ext;
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"},
68 {"image/gif", "gif"},
69 {"image/jpeg", "jpg"},
70 {"image/png", "png"},
71 {"text/html", "html"},
72 {"video/mp4", "mp4"},
73 {"video/mpeg", "mpg"},
74 {"text/plain", "txt"},
75 {"text/x-sh", "sh"},
78 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
79 const std::string& mime_type, base::FilePath::StringType* ext) const {
81 for (size_t x = 0;
82 x < (sizeof(mime_type_ext_map) / sizeof(MimeToExt));
83 x++) {
84 if (mime_type_ext_map[x].mime_type == mime_type) {
85 *ext = mime_type_ext_map[x].ext;
86 return true;
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 "*.".
100 return false;
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);
111 } // namespace net