filename_util_icu.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 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/filename_util.h"
  5. #include <string>
  6. #include "base/check.h"
  7. #include "base/files/file_path.h"
  8. #include "base/i18n/file_util_icu.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "net/base/filename_util_internal.h"
  11. class GURL;
  12. namespace net {
  13. bool IsSafePortablePathComponent(const base::FilePath& component) {
  14. std::u16string component16;
  15. base::FilePath::StringType sanitized = component.value();
  16. SanitizeGeneratedFileName(&sanitized, true);
  17. base::FilePath::StringType extension = component.Extension();
  18. if (!extension.empty())
  19. extension.erase(extension.begin()); // Erase preceding '.'.
  20. return !component.empty() && (component == component.BaseName()) &&
  21. (component == component.StripTrailingSeparators()) &&
  22. FilePathToString16(component, &component16) &&
  23. base::i18n::IsFilenameLegal(component16) &&
  24. !IsShellIntegratedExtension(extension) &&
  25. (sanitized == component.value()) &&
  26. !IsReservedNameOnWindows(component.value());
  27. }
  28. bool IsSafePortableRelativePath(const base::FilePath& path) {
  29. if (path.empty() || path.IsAbsolute() || path.EndsWithSeparator())
  30. return false;
  31. std::vector<base::FilePath::StringType> components = path.GetComponents();
  32. if (components.empty())
  33. return false;
  34. for (size_t i = 0; i < components.size() - 1; ++i) {
  35. if (!IsSafePortablePathComponent(base::FilePath(components[i])))
  36. return false;
  37. }
  38. return IsSafePortablePathComponent(path.BaseName());
  39. }
  40. std::u16string GetSuggestedFilename(const GURL& url,
  41. const std::string& content_disposition,
  42. const std::string& referrer_charset,
  43. const std::string& suggested_name,
  44. const std::string& mime_type,
  45. const std::string& default_name) {
  46. return GetSuggestedFilenameImpl(url, content_disposition, referrer_charset,
  47. suggested_name, mime_type, default_name,
  48. false, /* should_replace_extension */
  49. &base::i18n::ReplaceIllegalCharactersInPath);
  50. }
  51. base::FilePath GenerateFileName(const GURL& url,
  52. const std::string& content_disposition,
  53. const std::string& referrer_charset,
  54. const std::string& suggested_name,
  55. const std::string& mime_type,
  56. const std::string& default_file_name) {
  57. return GenerateFileName(url, content_disposition, referrer_charset,
  58. suggested_name, mime_type, default_file_name,
  59. false /* should_replace_extension */);
  60. }
  61. base::FilePath GenerateFileName(const GURL& url,
  62. const std::string& content_disposition,
  63. const std::string& referrer_charset,
  64. const std::string& suggested_name,
  65. const std::string& mime_type,
  66. const std::string& default_file_name,
  67. bool should_replace_extension) {
  68. base::FilePath generated_name(GenerateFileNameImpl(
  69. url, content_disposition, referrer_charset, suggested_name, mime_type,
  70. default_file_name, should_replace_extension,
  71. &base::i18n::ReplaceIllegalCharactersInPath));
  72. #if BUILDFLAG(IS_CHROMEOS_ASH)
  73. // When doing file manager operations on ChromeOS, the file paths get
  74. // normalized in WebKit layer, so let's ensure downloaded files have
  75. // normalized names. Otherwise, we won't be able to handle files with NFD
  76. // utf8 encoded characters in name.
  77. base::i18n::NormalizeFileNameEncoding(&generated_name);
  78. #endif
  79. DCHECK(!generated_name.empty());
  80. return generated_name;
  81. }
  82. } // namespace net