platform_mime_util_linux.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "net/base/platform_mime_util.h"
  5. #include <string>
  6. #include "build/build_config.h"
  7. #include "build/chromeos_buildflags.h"
  8. #if BUILDFLAG(IS_ANDROID)
  9. #include "net/android/network_library.h"
  10. #else
  11. #include "base/nix/mime_util_xdg.h"
  12. #endif
  13. namespace net {
  14. #if BUILDFLAG(IS_ANDROID)
  15. bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
  16. const base::FilePath::StringType& ext,
  17. std::string* result) const {
  18. return android::GetMimeTypeFromExtension(ext, result);
  19. }
  20. #elif BUILDFLAG(IS_CHROMEOS_ASH)
  21. bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
  22. const base::FilePath::StringType& ext,
  23. std::string* result) const {
  24. return false;
  25. }
  26. #else
  27. bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
  28. const base::FilePath::StringType& ext,
  29. std::string* result) const {
  30. base::FilePath dummy_path("foo." + ext);
  31. std::string out = base::nix::GetFileMimeType(dummy_path);
  32. // GetFileMimeType likes to return application/octet-stream
  33. // for everything it doesn't know - ignore that.
  34. if (out == "application/octet-stream" || out.empty())
  35. return false;
  36. // GetFileMimeType returns image/x-ico because that's what's in the XDG
  37. // mime database. That database is the merger of the Gnome and KDE mime
  38. // databases. Apparently someone working on KDE in 2001 decided .ico
  39. // resolves to image/x-ico, whereas the rest of the world uses image/x-icon.
  40. // FWIW, image/vnd.microsoft.icon is the official IANA assignment.
  41. if (out == "image/x-ico")
  42. out = "image/x-icon";
  43. *result = out;
  44. return true;
  45. }
  46. #endif // BUILDFLAG(IS_ANDROID)
  47. bool PlatformMimeUtil::GetPlatformPreferredExtensionForMimeType(
  48. const std::string& mime_type,
  49. base::FilePath::StringType* ext) const {
  50. // xdg_mime doesn't provide an API to get extension from a MIME type, so we
  51. // rely on the mappings hardcoded in mime_util.cc .
  52. return false;
  53. }
  54. void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
  55. const std::string& mime_type,
  56. std::unordered_set<base::FilePath::StringType>* extensions) const {
  57. // xdg_mime doesn't provide an API to get extension from a MIME type, so we
  58. // rely on the mappings hardcoded in mime_util.cc .
  59. }
  60. } // namespace net