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