Chromium Code Reviews| Index: chrome/browser/extensions/api/file_system/file_system_api.cc |
| diff --git a/chrome/browser/extensions/api/file_system/file_system_api.cc b/chrome/browser/extensions/api/file_system/file_system_api.cc |
| index 915da20379831171ffdeb5904a5685d5116a6609..94800b4cd59992ed3ec5b3017653458b49ace137 100644 |
| --- a/chrome/browser/extensions/api/file_system/file_system_api.cc |
| +++ b/chrome/browser/extensions/api/file_system/file_system_api.cc |
| @@ -29,6 +29,10 @@ |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/base/dialogs/select_file_dialog.h" |
| +#if defined(OS_MACOSX) |
| +#include "base/mac/foundation_util.h" |
| +#endif |
| + |
| using fileapi::IsolatedContext; |
| const char kInvalidParameters[] = "Invalid parameters"; |
| @@ -49,38 +53,57 @@ namespace ChooseFile = file_system::ChooseFile; |
| namespace { |
| -struct RewritePair { |
| - int path_key; |
| - const char* output; |
| -}; |
| - |
| -const RewritePair g_rewrite_pairs[] = { |
| +// Get the user's (optional) home directory via PathService. |
| +FilePath GetHome() { |
| + int home_key = 0; |
| #if defined(OS_WIN) |
| - {base::DIR_PROFILE, "~"}, |
| + home_key = base::DIR_PROFILE; |
| #elif defined(OS_POSIX) |
| - {base::DIR_HOME, "~"}, |
| + home_key = base::DIR_HOME; |
| #endif |
| -}; |
| + FilePath home_path; |
| + if (home_key && PathService::Get(home_key, &home_path)) { |
| + return home_path; |
| + } |
| + return FilePath(); |
| +} |
| -FilePath PrettifyPath(const FilePath& file_path) { |
| -#if defined(OS_WIN) || defined(OS_POSIX) |
| - for (size_t i = 0; i < arraysize(g_rewrite_pairs); ++i) { |
| - FilePath candidate_path; |
| - if (!PathService::Get(g_rewrite_pairs[i].path_key, &candidate_path)) |
| - continue; // We don't DCHECK this value, as Get will return false even |
| - // if the path_key gives a blank string as a result. |
| - |
| - FilePath output = FilePath::FromUTF8Unsafe(g_rewrite_pairs[i].output); |
| - if (candidate_path.AppendRelativePath(file_path, &output)) { |
| - // The output path must not be absolute, as it might collide with the |
| - // real filesystem. |
| - DCHECK(!output.IsAbsolute()); |
| - return output; |
| +// Prettifies |source_path|. If the path is inside the user's home directory, |
| +// then replace the home directory component with "~". |
| +FilePath PrettifyPath(const FilePath& source_path) { |
| + DCHECK(source_path.IsAbsolute()); |
| + FilePath home_path = GetHome(); |
| + |
| +#if defined(OS_MACOSX) |
| + // Break down the incoming path into components, and grab the display name |
| + // for every component. This will match app bundles, ".localized" folders, |
| + // and localized subfolders of the user's home directory. |
| + // Don't grab the display name of the first component, i.e., "/", as it'll |
| + // show up as the HDD name. |
| + std::vector<FilePath::StringType> components; |
| + source_path.GetComponents(&components); |
| + FilePath display_path = FilePath(components[0]); |
| + FilePath actual_path = display_path; |
| + for (std::vector<FilePath::StringType>::iterator i = components.begin() + 1; |
| + i != components.end(); ++i) { |
| + actual_path = actual_path.Append(*i); |
| + if (actual_path == home_path) { |
| + display_path = FilePath("~"); |
| + home_path = FilePath(); |
| + continue; |
| } |
| + std::string display = base::mac::GetDisplayBaseName(actual_path); |
| + display_path = display_path.Append(display); |
| } |
| + DCHECK(actual_path == source_path); |
|
Nico
2012/09/04 14:09:37
DCHECK_EQ
thorogood
2012/09/06 00:16:53
This seems to try to convert one of the arguments
|
| + return display_path; |
| +#else |
| + // If found, replace the leading home directory with "~". |
| + FilePath display_path("~"); |
| + if (!home_path.empty() && home_path.AppendRelativePath(source_path, &output)) |
| + return display_path; |
| + return source_path; |
| #endif |
| - |
| - return file_path; |
| } |
| bool g_skip_picker_for_test = false; |