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) |
| 11 #include <CoreFoundation/CoreFoundation.h> | 11 #include <CoreFoundation/CoreFoundation.h> |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #if defined(OS_CHROMEOS) | 15 #if defined(OS_CHROMEOS) |
| 16 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" | 16 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 namespace path_util { | 20 namespace path_util { |
| 21 | 21 |
| 22 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // Retrieves the localized display name for the base name of the given path. | 25 // Retrieves the localized display name for the base name of the given path. |
| 26 // If the path is not localized, this will just return the base name. | 26 // If the path is not localized, this will just return the base name. |
| 27 std::string GetDisplayBaseName(const base::FilePath& path) { | 27 std::string GetDisplayBaseName(const base::FilePath& path) { |
|
not at google - send to devlin
2014/08/04 23:39:07
put specific OS_MACOSX namespace around this funct
| |
| 28 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( | 28 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( |
| 29 NULL, (const UInt8*)path.value().c_str(), path.value().length(), true)); | 29 NULL, (const UInt8*)path.value().c_str(), path.value().length(), true)); |
| 30 if (!url) | 30 if (!url) |
| 31 return path.BaseName().value(); | 31 return path.BaseName().value(); |
| 32 | 32 |
| 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 | 43 |
| 44 base::FilePath PrettifyPath(const base::FilePath& source_path) { | 44 base::FilePath PrettifyPath(const base::FilePath& source_path) { |
|
not at google - send to devlin
2014/08/04 23:39:07
declare this function then do the empty-path check
| |
| 45 if (source_path.empty()) | |
| 46 return base::FilePath(); | |
| 45 base::FilePath home_path; | 47 base::FilePath home_path; |
| 46 PathService::Get(base::DIR_HOME, &home_path); | 48 PathService::Get(base::DIR_HOME, &home_path); |
| 47 DCHECK(source_path.IsAbsolute()); | 49 DCHECK(source_path.IsAbsolute()); |
| 48 | 50 |
| 49 // Break down the incoming path into components, and grab the display name | 51 // Break down the incoming path into components, and grab the display name |
| 50 // for every component. This will match app bundles, ".localized" folders, | 52 // for every component. This will match app bundles, ".localized" folders, |
| 51 // and localized subfolders of the user's home directory. | 53 // 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 | 54 // Don't grab the display name of the first component, i.e., "/", as it'll |
| 53 // show up as the HDD name. | 55 // show up as the HDD name. |
| 54 std::vector<base::FilePath::StringType> components; | 56 std::vector<base::FilePath::StringType> components; |
| 55 source_path.GetComponents(&components); | 57 source_path.GetComponents(&components); |
| 56 base::FilePath display_path = base::FilePath(components[0]); | 58 base::FilePath display_path = base::FilePath(components[0]); |
| 57 base::FilePath actual_path = display_path; | 59 base::FilePath actual_path = display_path; |
| 58 for (std::vector<base::FilePath::StringType>::iterator i = | 60 for (std::vector<base::FilePath::StringType>::iterator i = |
| 59 components.begin() + 1; i != components.end(); ++i) { | 61 components.begin() + 1; i != components.end(); ++i) { |
| 60 actual_path = actual_path.Append(*i); | 62 actual_path = actual_path.Append(*i); |
| 61 if (actual_path == home_path) { | 63 if (actual_path == home_path) { |
| 62 display_path = base::FilePath("~"); | 64 display_path = base::FilePath("~"); |
| 63 home_path = base::FilePath(); | 65 home_path = base::FilePath(); |
| 64 continue; | 66 continue; |
| 65 } | 67 } |
| 66 std::string display = GetDisplayBaseName(actual_path); | 68 std::string display = GetDisplayBaseName(actual_path); |
| 67 display_path = display_path.Append(display); | 69 display_path = display_path.Append(display); |
| 68 } | 70 } |
| 69 DCHECK_EQ(actual_path.value(), source_path.value()); | 71 DCHECK_EQ(actual_path.value(), source_path.value()); |
| 70 return display_path; | 72 return display_path; |
| 71 } | 73 } |
| 72 #else // defined(OS_MACOSX) | 74 #else // defined(OS_MACOSX) |
| 73 base::FilePath PrettifyPath(const base::FilePath& source_path) { | 75 base::FilePath PrettifyPath(const base::FilePath& source_path) { |
| 76 if (source_path.empty()) | |
| 77 return base::FilePath(); | |
| 74 base::FilePath home_path; | 78 base::FilePath home_path; |
| 75 base::FilePath display_path = base::FilePath::FromUTF8Unsafe("~"); | 79 base::FilePath display_path = base::FilePath::FromUTF8Unsafe("~"); |
| 76 if (PathService::Get(base::DIR_HOME, &home_path) && | 80 if (PathService::Get(base::DIR_HOME, &home_path) && |
| 77 home_path.AppendRelativePath(source_path, &display_path)) | 81 home_path.AppendRelativePath(source_path, &display_path)) |
| 78 return display_path; | 82 return display_path; |
| 79 return source_path; | 83 return source_path; |
| 80 } | 84 } |
| 81 #endif // defined(OS_MACOSX) | 85 #endif // defined(OS_MACOSX) |
| 82 | 86 |
| 83 } // namespace path_util | 87 } // namespace path_util |
| 84 } // namespace extensions | 88 } // namespace extensions |
| OLD | NEW |