Move file_util_icu to base::i18n namespace
TBR=sky Review URL: https://codereview.chromium.org/447403002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288170 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
base/i18n
chrome/browser
content/browser/download
net/base
printing
ui/base
@ -19,7 +19,8 @@
|
||||
#include "third_party/icu/source/common/unicode/uniset.h"
|
||||
#include "third_party/icu/source/i18n/unicode/coll.h"
|
||||
|
||||
using base::string16;
|
||||
namespace base {
|
||||
namespace i18n {
|
||||
|
||||
namespace {
|
||||
|
||||
@ -84,20 +85,18 @@ IllegalCharacters::IllegalCharacters() {
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace file_util {
|
||||
|
||||
bool IsFilenameLegal(const string16& file_name) {
|
||||
return IllegalCharacters::GetInstance()->containsNone(file_name);
|
||||
}
|
||||
|
||||
void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name,
|
||||
void ReplaceIllegalCharactersInPath(FilePath::StringType* file_name,
|
||||
char replace_char) {
|
||||
DCHECK(file_name);
|
||||
|
||||
DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char)));
|
||||
|
||||
// Remove leading and trailing whitespace.
|
||||
base::TrimWhitespace(*file_name, base::TRIM_ALL, file_name);
|
||||
TrimWhitespace(*file_name, TRIM_ALL, file_name);
|
||||
|
||||
IllegalCharacters* illegal = IllegalCharacters::GetInstance();
|
||||
int cursor = 0; // The ICU macros expect an int.
|
||||
@ -133,8 +132,7 @@ void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name,
|
||||
}
|
||||
}
|
||||
|
||||
bool LocaleAwareCompareFilenames(const base::FilePath& a,
|
||||
const base::FilePath& b) {
|
||||
bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) {
|
||||
UErrorCode error_code = U_ZERO_ERROR;
|
||||
// Use the default collator. The default locale should have been properly
|
||||
// set by the time this constructor is called.
|
||||
@ -144,31 +142,31 @@ bool LocaleAwareCompareFilenames(const base::FilePath& a,
|
||||
collator->setStrength(icu::Collator::TERTIARY);
|
||||
|
||||
#if defined(OS_WIN)
|
||||
return base::i18n::CompareString16WithCollator(collator.get(),
|
||||
base::WideToUTF16(a.value()), base::WideToUTF16(b.value())) == UCOL_LESS;
|
||||
return CompareString16WithCollator(collator.get(),
|
||||
WideToUTF16(a.value()), WideToUTF16(b.value())) == UCOL_LESS;
|
||||
|
||||
#elif defined(OS_POSIX)
|
||||
// On linux, the file system encoding is not defined. We assume
|
||||
// SysNativeMBToWide takes care of it.
|
||||
return base::i18n::CompareString16WithCollator(
|
||||
return CompareString16WithCollator(
|
||||
collator.get(),
|
||||
base::WideToUTF16(base::SysNativeMBToWide(a.value().c_str())),
|
||||
base::WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))
|
||||
) == UCOL_LESS;
|
||||
WideToUTF16(SysNativeMBToWide(a.value().c_str())),
|
||||
WideToUTF16(SysNativeMBToWide(b.value().c_str()))) == UCOL_LESS;
|
||||
#else
|
||||
#error Not implemented on your system
|
||||
#endif
|
||||
}
|
||||
|
||||
void NormalizeFileNameEncoding(base::FilePath* file_name) {
|
||||
void NormalizeFileNameEncoding(FilePath* file_name) {
|
||||
#if defined(OS_CHROMEOS)
|
||||
std::string normalized_str;
|
||||
if (base::ConvertToUtf8AndNormalize(file_name->BaseName().value(),
|
||||
base::kCodepageUTF8,
|
||||
&normalized_str)) {
|
||||
*file_name = file_name->DirName().Append(base::FilePath(normalized_str));
|
||||
if (ConvertToUtf8AndNormalize(file_name->BaseName().value(),
|
||||
kCodepageUTF8,
|
||||
&normalized_str)) {
|
||||
*file_name = file_name->DirName().Append(FilePath(normalized_str));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace i18n
|
||||
} // namespace base
|
||||
|
@ -11,11 +11,12 @@
|
||||
#include "base/i18n/base_i18n_export.h"
|
||||
#include "base/strings/string16.h"
|
||||
|
||||
namespace file_util {
|
||||
namespace base {
|
||||
namespace i18n {
|
||||
|
||||
// Returns true if file_name does not have any illegal character. The input
|
||||
// param has the same restriction as that for ReplaceIllegalCharacters.
|
||||
BASE_I18N_EXPORT bool IsFilenameLegal(const base::string16& file_name);
|
||||
BASE_I18N_EXPORT bool IsFilenameLegal(const string16& file_name);
|
||||
|
||||
// Replaces characters in 'file_name' that are illegal for file names with
|
||||
// 'replace_char'. 'file_name' must not be a full or relative path, but just the
|
||||
@ -25,19 +26,20 @@ BASE_I18N_EXPORT bool IsFilenameLegal(const base::string16& file_name);
|
||||
// file_name == "bad:file*name?.txt", changed to: "bad-file-name-.txt" when
|
||||
// 'replace_char' is '-'.
|
||||
BASE_I18N_EXPORT void ReplaceIllegalCharactersInPath(
|
||||
base::FilePath::StringType* file_name,
|
||||
FilePath::StringType* file_name,
|
||||
char replace_char);
|
||||
|
||||
// Compares two filenames using the current locale information. This can be
|
||||
// used to sort directory listings. It behaves like "operator<" for use in
|
||||
// std::sort.
|
||||
BASE_I18N_EXPORT bool LocaleAwareCompareFilenames(const base::FilePath& a,
|
||||
const base::FilePath& b);
|
||||
BASE_I18N_EXPORT bool LocaleAwareCompareFilenames(const FilePath& a,
|
||||
const FilePath& b);
|
||||
|
||||
// Calculates the canonical file-system representation of |file_name| base name.
|
||||
// Modifies |file_name| in place. No-op if not on ChromeOS.
|
||||
BASE_I18N_EXPORT void NormalizeFileNameEncoding(base::FilePath* file_name);
|
||||
BASE_I18N_EXPORT void NormalizeFileNameEncoding(FilePath* file_name);
|
||||
|
||||
} // namespace file_util
|
||||
} // namespace i18n
|
||||
} // namespace base
|
||||
|
||||
#endif // BASE_I18N_FILE_UTIL_ICU_H_
|
||||
|
@ -9,6 +9,9 @@
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "testing/platform_test.h"
|
||||
|
||||
namespace base {
|
||||
namespace i18n {
|
||||
|
||||
// file_util winds up using autoreleased objects on the Mac, so this needs
|
||||
// to be a PlatformTest
|
||||
class FileUtilICUTest : public PlatformTest {
|
||||
@ -29,7 +32,7 @@ static const struct goodbad_pair {
|
||||
TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) {
|
||||
for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
|
||||
std::string bad_name(kIllegalCharacterCases[i].bad_name);
|
||||
file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
|
||||
}
|
||||
}
|
||||
@ -70,12 +73,12 @@ TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
|
||||
for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
|
||||
#if defined(OS_WIN)
|
||||
std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
|
||||
file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
|
||||
#elif defined(OS_MACOSX)
|
||||
std::string bad_name(base::WideToUTF8(kIllegalCharacterCases[i].bad_name));
|
||||
file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
EXPECT_EQ(base::WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name);
|
||||
std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name));
|
||||
ReplaceIllegalCharactersInPath(&bad_name, '-');
|
||||
EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@ -96,12 +99,14 @@ static const struct normalize_name_encoding_test_cases {
|
||||
|
||||
TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) {
|
||||
for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) {
|
||||
base::FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path);
|
||||
file_util::NormalizeFileNameEncoding(&path);
|
||||
EXPECT_EQ(
|
||||
base::FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path),
|
||||
path);
|
||||
FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path);
|
||||
NormalizeFileNameEncoding(&path);
|
||||
EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path),
|
||||
path);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace i18n
|
||||
} // namespace base
|
||||
|
@ -255,7 +255,7 @@ void SavePackageFilePicker::FileSelected(
|
||||
}
|
||||
|
||||
base::FilePath path_copy(path);
|
||||
file_util::NormalizeFileNameEncoding(&path_copy);
|
||||
base::i18n::NormalizeFileNameEncoding(&path_copy);
|
||||
|
||||
download_prefs_->SetSaveFilePath(path_copy.DirName());
|
||||
|
||||
|
@ -85,7 +85,7 @@ base::FilePath GetDefaultFilepathForBookmarkExport() {
|
||||
base::TimeFormatShortDateNumeric(time));
|
||||
#endif
|
||||
|
||||
file_util::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
|
||||
base::FilePath default_path;
|
||||
PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_path);
|
||||
|
@ -695,7 +695,7 @@ base::FilePath GetWebShortcutFilename(const GURL& url) {
|
||||
// Use a prefix, because xdg-desktop-menu requires it.
|
||||
std::string filename =
|
||||
std::string(chrome::kBrowserProcessExecutableName) + "-" + url.spec();
|
||||
file_util::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
|
||||
base::FilePath desktop_path;
|
||||
if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_path))
|
||||
@ -725,7 +725,7 @@ base::FilePath GetExtensionShortcutFilename(const base::FilePath& profile_path,
|
||||
.append(extension_id)
|
||||
.append("-")
|
||||
.append(profile_path.BaseName().value());
|
||||
file_util::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
|
||||
// Spaces in filenames break xdg-desktop-menu
|
||||
// (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
|
||||
base::ReplaceChars(filename, " ", "_", &filename);
|
||||
@ -741,7 +741,7 @@ std::vector<base::FilePath> GetExistingProfileShortcutFilenames(
|
||||
prefix.append("-");
|
||||
std::string suffix("-");
|
||||
suffix.append(profile_path.BaseName().value());
|
||||
file_util::ReplaceIllegalCharactersInPath(&suffix, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&suffix, '_');
|
||||
// Spaces in filenames break xdg-desktop-menu
|
||||
// (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
|
||||
base::ReplaceChars(suffix, " ", "_", &suffix);
|
||||
|
@ -887,7 +887,7 @@ void PrintPreviewHandler::PrintToPdf() {
|
||||
base::UTF16ToUTF8(print_job_title_utf16);
|
||||
#endif
|
||||
|
||||
file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&print_job_title, '_');
|
||||
base::FilePath default_filename(print_job_title);
|
||||
default_filename =
|
||||
default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf"));
|
||||
|
@ -141,7 +141,7 @@ base::FilePath GetSanitizedFileName(const base::string16& name) {
|
||||
#else
|
||||
std::string file_name = base::UTF16ToUTF8(name);
|
||||
#endif
|
||||
file_util::ReplaceIllegalCharactersInPath(&file_name, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_');
|
||||
return base::FilePath(file_name);
|
||||
}
|
||||
|
||||
@ -426,7 +426,7 @@ void GetIconsInfo(const WebApplicationInfo& app_info,
|
||||
|
||||
#if defined(OS_LINUX)
|
||||
std::string GetWMClassFromAppName(std::string app_name) {
|
||||
file_util::ReplaceIllegalCharactersInPath(&app_name, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_');
|
||||
base::TrimString(app_name, "_", &app_name);
|
||||
return app_name;
|
||||
}
|
||||
|
@ -1244,7 +1244,7 @@ base::FilePath SavePackage::GetSuggestedNameForSaveAs(
|
||||
name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext);
|
||||
|
||||
base::FilePath::StringType file_name = name_with_proper_ext.value();
|
||||
file_util::ReplaceIllegalCharactersInPath(&file_name, ' ');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&file_name, ' ');
|
||||
return base::FilePath(file_name);
|
||||
}
|
||||
|
||||
|
@ -41,8 +41,8 @@ bool CompareAlphaDirsFirst(const DirectoryLister::DirectoryListerData& a,
|
||||
if (a_is_directory != b_is_directory)
|
||||
return a_is_directory;
|
||||
|
||||
return file_util::LocaleAwareCompareFilenames(a.info.GetName(),
|
||||
b.info.GetName());
|
||||
return base::i18n::LocaleAwareCompareFilenames(a.info.GetName(),
|
||||
b.info.GetName());
|
||||
}
|
||||
|
||||
bool CompareDate(const DirectoryLister::DirectoryListerData& a,
|
||||
@ -66,7 +66,7 @@ bool CompareDate(const DirectoryLister::DirectoryListerData& a,
|
||||
// Static.
|
||||
bool CompareFullPath(const DirectoryLister::DirectoryListerData& a,
|
||||
const DirectoryLister::DirectoryListerData& b) {
|
||||
return file_util::LocaleAwareCompareFilenames(a.path, b.path);
|
||||
return base::i18n::LocaleAwareCompareFilenames(a.path, b.path);
|
||||
}
|
||||
|
||||
void SortData(std::vector<DirectoryLister::DirectoryListerData>* data,
|
||||
|
@ -50,7 +50,7 @@ class ListerDelegate : public DirectoryLister::DirectoryListerDelegate {
|
||||
for (size_t previous = 0, current = 1;
|
||||
current < file_list_.size();
|
||||
previous++, current++) {
|
||||
EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
|
||||
EXPECT_TRUE(base::i18n::LocaleAwareCompareFilenames(
|
||||
paths_[previous], paths_[current]));
|
||||
}
|
||||
}
|
||||
@ -71,7 +71,7 @@ class ListerDelegate : public DirectoryLister::DirectoryListerDelegate {
|
||||
file_list_[current].GetName().BaseName().value());
|
||||
EXPECT_EQ(file_list_[previous].IsDirectory(),
|
||||
file_list_[current].IsDirectory());
|
||||
EXPECT_TRUE(file_util::LocaleAwareCompareFilenames(
|
||||
EXPECT_TRUE(base::i18n::LocaleAwareCompareFilenames(
|
||||
file_list_[previous].GetName(),
|
||||
file_list_[current].GetName()));
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ bool IsSafePortablePathComponent(const base::FilePath& component) {
|
||||
return !component.empty() && (component == component.BaseName()) &&
|
||||
(component == component.StripTrailingSeparators()) &&
|
||||
FilePathToString16(component, &component16) &&
|
||||
file_util::IsFilenameLegal(component16) &&
|
||||
base::i18n::IsFilenameLegal(component16) &&
|
||||
!IsShellIntegratedExtension(extension) &&
|
||||
(sanitized == component.value()) && !IsReservedName(component.value());
|
||||
}
|
||||
@ -56,7 +56,7 @@ base::string16 GetSuggestedFilename(const GURL& url,
|
||||
suggested_name,
|
||||
mime_type,
|
||||
default_name,
|
||||
base::Bind(&file_util::ReplaceIllegalCharactersInPath));
|
||||
base::Bind(&base::i18n::ReplaceIllegalCharactersInPath));
|
||||
}
|
||||
|
||||
base::FilePath GenerateFileName(const GURL& url,
|
||||
@ -72,14 +72,14 @@ base::FilePath GenerateFileName(const GURL& url,
|
||||
suggested_name,
|
||||
mime_type,
|
||||
default_file_name,
|
||||
base::Bind(&file_util::ReplaceIllegalCharactersInPath)));
|
||||
base::Bind(&base::i18n::ReplaceIllegalCharactersInPath)));
|
||||
|
||||
#if defined(OS_CHROMEOS)
|
||||
// When doing file manager operations on ChromeOS, the file paths get
|
||||
// normalized in WebKit layer, so let's ensure downloaded files have
|
||||
// normalized names. Otherwise, we won't be able to handle files with NFD
|
||||
// utf8 encoded characters in name.
|
||||
file_util::NormalizeFileNameEncoding(&generated_name);
|
||||
base::i18n::NormalizeFileNameEncoding(&generated_name);
|
||||
#endif
|
||||
|
||||
DCHECK(!generated_name.empty());
|
||||
|
@ -242,7 +242,7 @@ base::FilePath PrintedDocument::CreateDebugDumpPath(
|
||||
#else // OS_WIN
|
||||
system_filename = base::UTF16ToUTF8(filename);
|
||||
#endif // OS_WIN
|
||||
file_util::ReplaceIllegalCharactersInPath(&system_filename, '_');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_');
|
||||
return g_debug_dump_info.Get().Append(system_filename).AddExtension(
|
||||
extension);
|
||||
}
|
||||
|
@ -917,7 +917,7 @@ static void CreateValidFileNameFromTitle(const GURL& url,
|
||||
}
|
||||
} else {
|
||||
*validated = title;
|
||||
file_util::ReplaceIllegalCharactersInPath(validated, '-');
|
||||
base::i18n::ReplaceIllegalCharactersInPath(validated, '-');
|
||||
}
|
||||
static const wchar_t extension[] = L".url";
|
||||
static const size_t max_length = MAX_PATH - arraysize(extension);
|
||||
|
@ -225,7 +225,7 @@ bool IsLocalePartiallyPopulated(const std::string& locale_name) {
|
||||
bool IsLocaleAvailable(const std::string& locale) {
|
||||
// If locale has any illegal characters in it, we don't want to try to
|
||||
// load it because it may be pointing outside the locale data file directory.
|
||||
if (!file_util::IsFilenameLegal(base::ASCIIToUTF16(locale)))
|
||||
if (!base::i18n::IsFilenameLegal(base::ASCIIToUTF16(locale)))
|
||||
return false;
|
||||
|
||||
// IsLocalePartiallyPopulated() can be called here for an early return w/o
|
||||
|
Reference in New Issue
Block a user