diff options
author | tbarzic@chromium.org <tbarzic@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-27 04:12:31 +0000 |
---|---|---|
committer | tbarzic@chromium.org <tbarzic@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-27 04:12:31 +0000 |
commit | 4915ec608f743890a74438f68571fed4dd2d54c2 (patch) | |
tree | 760be40ea871b53d1e9821648a288874e2c96e25 | |
parent | 3d5173ecb8d32eb3fec477122044d5e2322ae489 (diff) | |
download | chromium_src-4915ec608f743890a74438f68571fed4dd2d54c2.zip chromium_src-4915ec608f743890a74438f68571fed4dd2d54c2.tar.gz chromium_src-4915ec608f743890a74438f68571fed4dd2d54c2.tar.bz2 |
Normalize download file name on chromeos
ChromeOS file manager cannot handle non NFC encoded utf8 file names (because of
file path normalization done in webkit layer), so let's ensure we normalize
downloaded file names.
BUG=chromium-os:26028
TEST=downloaded file with nfd encoded names and made sure they can be handled by file manager
Review URL: https://chromiumcodereview.appspot.com/9854011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129128 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/i18n/file_util_icu.cc | 14 | ||||
-rw-r--r-- | base/i18n/file_util_icu.h | 6 | ||||
-rw-r--r-- | base/i18n/file_util_icu_unittest.cc | 24 | ||||
-rw-r--r-- | chrome/browser/download/download_file_picker_chromeos.cc | 6 | ||||
-rw-r--r-- | chrome/browser/download/save_package_file_picker_chromeos.cc | 6 | ||||
-rw-r--r-- | net/base/net_util.cc | 9 | ||||
-rw-r--r-- | net/base/net_util_unittest.cc | 13 |
7 files changed, 72 insertions, 6 deletions
diff --git a/base/i18n/file_util_icu.cc b/base/i18n/file_util_icu.cc index 0c7c09d..f44af10 100644 --- a/base/i18n/file_util_icu.cc +++ b/base/i18n/file_util_icu.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,6 +7,7 @@ #include "base/i18n/file_util_icu.h" #include "base/file_path.h" +#include "base/i18n/icu_string_conversions.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/memory/singleton.h" @@ -199,4 +200,15 @@ bool LocaleAwareCompareFilenames(const FilePath& a, const FilePath& b) { #endif } +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(FilePath(normalized_str)); + } +#endif +} + } // namespace diff --git a/base/i18n/file_util_icu.h b/base/i18n/file_util_icu.h index 57acea6..20435e7 100644 --- a/base/i18n/file_util_icu.h +++ b/base/i18n/file_util_icu.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -35,6 +35,10 @@ BASE_I18N_EXPORT void ReplaceIllegalCharactersInPath( 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(FilePath* file_name); + } // namespace file_util #endif // BASE_I18N_FILE_UTIL_ICU_H_ diff --git a/base/i18n/file_util_icu_unittest.cc b/base/i18n/file_util_icu_unittest.cc index 6c5d34d..dfc4943 100644 --- a/base/i18n/file_util_icu_unittest.cc +++ b/base/i18n/file_util_icu_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -82,3 +82,25 @@ TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) { #endif +#if defined(OS_CHROMEOS) +static const struct normalize_name_encoding_test_cases { + const char* original_path; + const char* normalized_path; +} kNormalizeFileNameEncodingTestCases[] = { + { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"}, + { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo", + "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"}, + { "", ""}, + { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"} +}; + +TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) { + for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) { + FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path); + file_util::NormalizeFileNameEncoding(&path); + EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path), + path); + } +} + +#endif diff --git a/chrome/browser/download/download_file_picker_chromeos.cc b/chrome/browser/download/download_file_picker_chromeos.cc index b51e84b..2f3fdc4 100644 --- a/chrome/browser/download/download_file_picker_chromeos.cc +++ b/chrome/browser/download/download_file_picker_chromeos.cc @@ -6,6 +6,7 @@ #include "base/bind.h" #include "base/file_util.h" +#include "base/i18n/file_util_icu.h" #include "base/memory/ref_counted.h" #include "base/threading/sequenced_worker_pool.h" #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" @@ -45,9 +46,12 @@ DownloadFilePickerChromeOS::DownloadFilePickerChromeOS( DownloadFilePickerChromeOS::~DownloadFilePickerChromeOS() { } -void DownloadFilePickerChromeOS::FileSelected(const FilePath& path, +void DownloadFilePickerChromeOS::FileSelected(const FilePath& selected_path, int index, void* params) { + FilePath path = selected_path; + file_util::NormalizeFileNameEncoding(&path); + RecordFileSelected(path); if (download_manager_) { diff --git a/chrome/browser/download/save_package_file_picker_chromeos.cc b/chrome/browser/download/save_package_file_picker_chromeos.cc index bb589c4..6fea929 100644 --- a/chrome/browser/download/save_package_file_picker_chromeos.cc +++ b/chrome/browser/download/save_package_file_picker_chromeos.cc @@ -6,6 +6,7 @@ #include "base/bind.h" #include "base/bind_helpers.h" +#include "base/i18n/file_util_icu.h" #include "base/threading/sequenced_worker_pool.h" #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" #include "chrome/browser/chromeos/gdata/gdata_file_system.h" @@ -38,7 +39,7 @@ SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { } -void SavePackageFilePickerChromeOS::FileSelected(const FilePath& path, +void SavePackageFilePickerChromeOS::FileSelected(const FilePath& selected_path, int index, void* params) { if (!web_contents()) { @@ -46,6 +47,9 @@ void SavePackageFilePickerChromeOS::FileSelected(const FilePath& path, return; } + FilePath path = selected_path; + file_util::NormalizeFileNameEncoding(&path); + gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); if (gdata_filesystem && gdata::util::IsUnderGDataMountPoint(path)) { FilePath gdata_tmp_download_dir = diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 2ba3b46..21fb07f 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -1505,6 +1505,15 @@ FilePath GenerateFileName(const GURL& url, #else FilePath generated_name(base::SysWideToNativeMB(UTF16ToWide(file_name))); #endif + +#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); +#endif + DCHECK(!generated_name.empty()); return generated_name; diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc index 0577989..98d79a7 100644 --- a/net/base/net_util_unittest.cc +++ b/net/base/net_util_unittest.cc @@ -2080,7 +2080,18 @@ TEST(NetUtilTest, GenerateFileName) { "image/jpeg", L"download", L"image" JPEG_EXT - } + }, +#if defined(OS_CHROMEOS) + { // http://crosbug.com/26028 + "http://www.example.com/fooa%cc%88.txt", + "", + "", + "", + "image/jpeg", + L"foo\xe4", + L"foo\xe4.txt" + }, +#endif }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(selection_tests); ++i) |