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) { |
| 46 if (source_path.empty()) | |
| 47 return base::FilePath(); | |
| 48 | |
| 45 base::FilePath home_path; | 49 base::FilePath home_path; |
| 46 PathService::Get(base::DIR_HOME, &home_path); | 50 if (!PathService::Get(base::DIR_HOME, &home_path)) |
| 51 return source_path; | |
| 52 | |
| 53 #if defined(OS_MACOSX) | |
| 47 DCHECK(source_path.IsAbsolute()); | 54 DCHECK(source_path.IsAbsolute()); |
| 48 | 55 |
| 49 // Break down the incoming path into components, and grab the display name | 56 // Break down the incoming path into components, and grab the display name |
| 50 // for every component. This will match app bundles, ".localized" folders, | 57 // for every component. This will match app bundles, ".localized" folders, |
| 51 // and localized subfolders of the user's home directory. | 58 // 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 | 59 // Don't grab the display name of the first component, i.e., "/", as it'll |
| 53 // show up as the HDD name. | 60 // show up as the HDD name. |
| 54 std::vector<base::FilePath::StringType> components; | 61 std::vector<base::FilePath::StringType> components; |
| 55 source_path.GetComponents(&components); | 62 source_path.GetComponents(&components); |
| 56 base::FilePath display_path = base::FilePath(components[0]); | 63 base::FilePath display_path = base::FilePath(components[0]); |
| 57 base::FilePath actual_path = display_path; | 64 base::FilePath actual_path = display_path; |
| 58 for (std::vector<base::FilePath::StringType>::iterator i = | 65 for (std::vector<base::FilePath::StringType>::iterator i = |
| 59 components.begin() + 1; i != components.end(); ++i) { | 66 components.begin() + 1; i != components.end(); ++i) { |
| 60 actual_path = actual_path.Append(*i); | 67 actual_path = actual_path.Append(*i); |
| 61 if (actual_path == home_path) { | 68 if (actual_path == home_path) { |
| 62 display_path = base::FilePath("~"); | 69 display_path = base::FilePath("~"); |
| 63 home_path = base::FilePath(); | 70 home_path = base::FilePath(); |
| 64 continue; | 71 continue; |
| 65 } | 72 } |
| 66 std::string display = GetDisplayBaseName(actual_path); | 73 std::string display = GetDisplayBaseName(actual_path); |
| 67 display_path = display_path.Append(display); | 74 display_path = display_path.Append(display); |
| 68 } | 75 } |
| 69 DCHECK_EQ(actual_path.value(), source_path.value()); | 76 DCHECK_EQ(actual_path.value(), source_path.value()); |
| 70 return display_path; | 77 return display_path; |
| 71 } | 78 #else // defined(OS_MACOSX) |
| 72 #else // defined(OS_MACOSX) | |
| 73 base::FilePath PrettifyPath(const base::FilePath& source_path) { | |
| 74 base::FilePath home_path; | |
| 75 base::FilePath display_path = base::FilePath::FromUTF8Unsafe("~"); | 79 base::FilePath display_path = base::FilePath::FromUTF8Unsafe("~"); |
|
Devlin
2014/08/05 23:40:35
nit: prefer to make this a FILE_PATH_LITERAL, and
gpdavis
2014/08/06 18:37:26
You mean like this, right?
base::FilePath display
| |
| 76 if (PathService::Get(base::DIR_HOME, &home_path) && | 80 if (home_path.AppendRelativePath(source_path, &display_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 |