• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 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.
4 
5 #include "net/base/platform_mime_util.h"
6 
7 #include <string>
8 
9 #include "base/mime_util.h"
10 
11 namespace net {
12 
GetPlatformMimeTypeFromExtension(const FilePath::StringType & ext,std::string * result) const13 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
14     const FilePath::StringType& ext, std::string* result) const {
15   // TODO(thestig) This is a temporary hack until we can fix this
16   // properly in test shell / webkit.
17   // We have to play dumb and not return application/x-perl here
18   // to make the reload-subframe-object layout test happy.
19   if (ext == "pl")
20     return false;
21 
22   FilePath dummy_path("foo." + ext);
23   std::string out = mime_util::GetFileMimeType(dummy_path);
24 
25   // GetFileMimeType likes to return application/octet-stream
26   // for everything it doesn't know - ignore that.
27   if (out == "application/octet-stream" || out.empty())
28     return false;
29 
30   // GetFileMimeType returns image/x-ico because that's what's in the XDG
31   // mime database. That database is the merger of the Gnome and KDE mime
32   // databases. Apparently someone working on KDE in 2001 decided .ico
33   // resolves to image/x-ico, whereas the rest of the world uses image/x-icon.
34   // FWIW, image/vnd.microsoft.icon is the official IANA assignment.
35   if (out == "image/x-ico")
36     out = "image/x-icon";
37 
38   *result = out;
39   return true;
40 }
41 
42 struct MimeToExt {
43   const char* mime_type;
44   const char* ext;
45 };
46 
47 const struct MimeToExt mime_type_ext_map[] = {
48   {"application/pdf", "pdf"},
49   {"application/x-tar", "tar"},
50   {"audio/mpeg", "mp3"},
51   {"image/gif", "gif"},
52   {"image/jpeg", "jpg"},
53   {"image/png", "png"},
54   {"text/html", "html"},
55   {"video/mp4", "mp4"},
56   {"video/mpeg", "mpg"},
57   {"text/plain", "txt"},
58   {"text/x-sh", "sh"},
59 };
60 
GetPreferredExtensionForMimeType(const std::string & mime_type,FilePath::StringType * ext) const61 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
62     const std::string& mime_type, FilePath::StringType* ext) const {
63 
64   for (size_t x = 0;
65        x < (sizeof(mime_type_ext_map) / sizeof(MimeToExt));
66        x++) {
67     if (mime_type_ext_map[x].mime_type == mime_type) {
68       *ext = mime_type_ext_map[x].ext;
69       return true;
70     }
71   }
72 
73   // TODO(dhg): Fix this the right way by implementing whats said below.
74   // Unlike GetPlatformMimeTypeFromExtension, this method doesn't have a
75   // default list that it uses, but for now we are also returning false since
76   // this doesn't really matter as much under Linux.
77   //
78   // If we wanted to do this properly, we would read the mime.cache file which
79   // has a section where they assign a glob (*.gif) to a mimetype
80   // (image/gif). We look up the "heaviest" glob for a certain mime type and
81   // then then try to chop off "*.".
82 
83   return false;
84 }
85 
86 }  // namespace net
87