file_extension.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2022 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 "pdf/file_extension.h"
  5. #include <algorithm>
  6. #include <array>
  7. #include <iterator>
  8. #include <string>
  9. #include "base/check_op.h"
  10. #include "base/files/file_path.h"
  11. #include "base/strings/string_util.h"
  12. #if BUILDFLAG(IS_WIN)
  13. #include "base/strings/utf_string_conversions.h"
  14. #endif
  15. namespace chrome_pdf {
  16. // The order of the entries should always match `ViewFileType` in
  17. // tools/metrics/histograms/enums.xml and the indexes defined in
  18. // `ExtensionIndex`.
  19. constexpr std::array<const char*, 76> kFileExtensions = {
  20. "other", ".3ga", ".3gp",
  21. ".aac", ".alac", ".asf",
  22. ".avi", ".bmp", ".csv",
  23. ".doc", ".docx", ".flac",
  24. ".gif", ".jpeg", ".jpg",
  25. ".log", ".m3u", ".m3u8",
  26. ".m4a", ".m4v", ".mid",
  27. ".mkv", ".mov", ".mp3",
  28. ".mp4", ".mpg", ".odf",
  29. ".odp", ".ods", ".odt",
  30. ".oga", ".ogg", ".ogv",
  31. ".pdf", ".png", ".ppt",
  32. ".pptx", ".ra", ".ram",
  33. ".rar", ".rm", ".rtf",
  34. ".wav", ".webm", ".webp",
  35. ".wma", ".wmv", ".xls",
  36. ".xlsx", ".crdownload", ".crx",
  37. ".dmg", ".exe", ".html",
  38. ".htm", ".jar", ".ps",
  39. ".torrent", ".txt", ".zip",
  40. "directory", "no extension", "unknown extension",
  41. ".mhtml", ".gdoc", ".gsheet",
  42. ".gslides", ".arw", ".cr2",
  43. ".dng", ".nef", ".nrw",
  44. ".orf", ".raf", ".rw2",
  45. ".tini",
  46. };
  47. static_assert(kFileExtensions.size() ==
  48. static_cast<size_t>(ExtensionIndex::kMaxValue) + 1);
  49. enum ExtensionIndex FileNameToExtensionIndex(const std::u16string& file_name) {
  50. const base::FilePath::StringType extension_str =
  51. base::FilePath::FromUTF16Unsafe(file_name).Extension();
  52. if (extension_str.empty())
  53. return ExtensionIndex::kEmptyExt;
  54. // All known extensions are ASCII characters. So when an extension contains
  55. // non-ASCII characters, this extension is not recognizable.
  56. if (!base::IsStringASCII(extension_str))
  57. return ExtensionIndex::kOtherExt;
  58. const base::FilePath::StringType extension_str_lower =
  59. base::ToLowerASCII(extension_str);
  60. #if BUILDFLAG(IS_WIN)
  61. const std::string extension = base::WideToUTF8(extension_str_lower);
  62. #else
  63. const std::string& extension = extension_str_lower;
  64. #endif
  65. auto* const* it =
  66. std::find(kFileExtensions.begin(), kFileExtensions.end(), extension);
  67. if (it == kFileExtensions.end())
  68. return ExtensionIndex::kOtherExt;
  69. const int distance = std::distance(kFileExtensions.begin(), it);
  70. DCHECK_GT(distance, 0);
  71. DCHECK_LT(static_cast<size_t>(distance), kFileExtensions.size());
  72. return static_cast<enum ExtensionIndex>(distance);
  73. }
  74. } // namespace chrome_pdf