Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/path_util.h" | 5 #include "chrome/browser/extensions/path_util.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 | 9 |
| 10 #if defined(OS_MACOSX) | 10 #if defined(OS_MACOSX) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 CFStringRef str; | 33 CFStringRef str; |
| 34 if (LSCopyDisplayNameForURL(url, &str) != noErr) | 34 if (LSCopyDisplayNameForURL(url, &str) != noErr) |
| 35 return path.BaseName().value(); | 35 return path.BaseName().value(); |
| 36 | 36 |
| 37 std::string result(base::SysCFStringRefToUTF8(str)); | 37 std::string result(base::SysCFStringRefToUTF8(str)); |
| 38 CFRelease(str); | 38 CFRelease(str); |
| 39 return result; | 39 return result; |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 #endif // defined(OS_MACOSX) | |
| 43 | 44 |
| 44 base::FilePath PrettifyPath(const base::FilePath& source_path) { | 45 base::FilePath PrettifyPath(const base::FilePath& source_path) { |
| 45 base::FilePath home_path; | 46 base::FilePath home_path; |
| 46 PathService::Get(base::DIR_HOME, &home_path); | 47 if (source_path.empty() || !PathService::Get(base::DIR_HOME, &home_path)) |
| 48 return source_path; | |
| 49 | |
| 50 base::FilePath display_path = base::FilePath(FILE_PATH_LITERAL("~")); | |
| 51 if (source_path == home_path) | |
| 52 return display_path; | |
| 53 | |
| 54 #if defined(OS_MACOSX) | |
| 47 DCHECK(source_path.IsAbsolute()); | 55 DCHECK(source_path.IsAbsolute()); |
| 48 | 56 |
| 49 // Break down the incoming path into components, and grab the display name | 57 // Break down the incoming path into components, and grab the display name |
| 50 // for every component. This will match app bundles, ".localized" folders, | 58 // for every component. This will match app bundles, ".localized" folders, |
| 51 // and localized subfolders of the user's home directory. | 59 // and localized subfolders of the user's home directory. |
| 52 // Don't grab the display name of the first component, i.e., "/", as it'll | 60 // Don't grab the display name of the first component, i.e., "/", as it'll |
| 53 // show up as the HDD name. | 61 // show up as the HDD name. |
| 54 std::vector<base::FilePath::StringType> components; | 62 std::vector<base::FilePath::StringType> components; |
| 55 source_path.GetComponents(&components); | 63 source_path.GetComponents(&components); |
| 56 base::FilePath display_path = base::FilePath(components[0]); | 64 display_path = base::FilePath(components[0]); |
| 57 base::FilePath actual_path = display_path; | 65 base::FilePath actual_path = display_path; |
| 58 for (std::vector<base::FilePath::StringType>::iterator i = | 66 for (std::vector<base::FilePath::StringType>::iterator i = |
| 59 components.begin() + 1; i != components.end(); ++i) { | 67 components.begin() + 1; i != components.end(); ++i) { |
| 60 actual_path = actual_path.Append(*i); | 68 actual_path = actual_path.Append(*i); |
| 61 if (actual_path == home_path) { | 69 if (actual_path == home_path) { |
| 62 display_path = base::FilePath("~"); | 70 display_path = base::FilePath(FILE_PATH_LITERAL("~")); |
|
Devlin
2014/08/06 23:35:56
Save the FILE_PATH_LITERAL so you don't have to ke
gpdavis
2014/08/07 01:20:06
Done.
| |
| 63 home_path = base::FilePath(); | 71 home_path = base::FilePath(); |
| 64 continue; | 72 continue; |
| 65 } | 73 } |
| 66 std::string display = GetDisplayBaseName(actual_path); | 74 std::string display = GetDisplayBaseName(actual_path); |
| 67 display_path = display_path.Append(display); | 75 display_path = display_path.Append(display); |
| 68 } | 76 } |
| 69 DCHECK_EQ(actual_path.value(), source_path.value()); | 77 DCHECK_EQ(actual_path.value(), source_path.value()); |
| 70 return display_path; | 78 return display_path; |
| 71 } | 79 #else // defined(OS_MACOSX) |
| 72 #else // defined(OS_MACOSX) | 80 if (home_path.AppendRelativePath(source_path, &display_path)) |
| 73 base::FilePath PrettifyPath(const base::FilePath& source_path) { | |
| 74 base::FilePath home_path; | |
| 75 base::FilePath display_path = base::FilePath::FromUTF8Unsafe("~"); | |
| 76 if (PathService::Get(base::DIR_HOME, &home_path) && | |
| 77 home_path.AppendRelativePath(source_path, &display_path)) | |
| 78 return display_path; | 81 return display_path; |
| 79 return source_path; | 82 return source_path; |
| 83 #endif // defined(OS_MACOSX) | |
| 80 } | 84 } |
| 81 #endif // defined(OS_MACOSX) | |
| 82 | 85 |
| 83 } // namespace path_util | 86 } // namespace path_util |
| 84 } // namespace extensions | 87 } // namespace extensions |
| OLD | NEW |