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 namespace { | |
22 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
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) { |
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 #endif // defined(OS_MACOSX) | |
43 | |
44 const base::FilePath::CharType kHomeShortcut[] = FILE_PATH_LITERAL("~"); | |
Devlin
2014/08/07 15:53:45
no need for an array, right?
gpdavis
2014/08/07 18:19:21
It doesn't compile if it's just a single CharType,
Devlin
2014/08/07 18:26:31
Hmm. How annoying. I wonder why we don't have a
| |
45 | |
42 } // namespace | 46 } // namespace |
43 | 47 |
44 base::FilePath PrettifyPath(const base::FilePath& source_path) { | 48 base::FilePath PrettifyPath(const base::FilePath& source_path) { |
45 base::FilePath home_path; | 49 base::FilePath home_path; |
46 PathService::Get(base::DIR_HOME, &home_path); | 50 if (source_path.empty() || !PathService::Get(base::DIR_HOME, &home_path)) |
51 return source_path; | |
52 | |
53 base::FilePath display_path = base::FilePath(kHomeShortcut); | |
54 if (source_path == home_path) | |
55 return display_path; | |
56 | |
57 #if defined(OS_MACOSX) | |
47 DCHECK(source_path.IsAbsolute()); | 58 DCHECK(source_path.IsAbsolute()); |
48 | 59 |
49 // Break down the incoming path into components, and grab the display name | 60 // Break down the incoming path into components, and grab the display name |
50 // for every component. This will match app bundles, ".localized" folders, | 61 // for every component. This will match app bundles, ".localized" folders, |
51 // and localized subfolders of the user's home directory. | 62 // 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 | 63 // Don't grab the display name of the first component, i.e., "/", as it'll |
53 // show up as the HDD name. | 64 // show up as the HDD name. |
54 std::vector<base::FilePath::StringType> components; | 65 std::vector<base::FilePath::StringType> components; |
55 source_path.GetComponents(&components); | 66 source_path.GetComponents(&components); |
56 base::FilePath display_path = base::FilePath(components[0]); | 67 display_path = base::FilePath(components[0]); |
57 base::FilePath actual_path = display_path; | 68 base::FilePath actual_path = display_path; |
58 for (std::vector<base::FilePath::StringType>::iterator i = | 69 for (std::vector<base::FilePath::StringType>::iterator i = |
59 components.begin() + 1; i != components.end(); ++i) { | 70 components.begin() + 1; i != components.end(); ++i) { |
60 actual_path = actual_path.Append(*i); | 71 actual_path = actual_path.Append(*i); |
61 if (actual_path == home_path) { | 72 if (actual_path == home_path) { |
62 display_path = base::FilePath("~"); | 73 display_path = base::FilePath(kHomeShortcut); |
63 home_path = base::FilePath(); | 74 home_path = base::FilePath(); |
64 continue; | 75 continue; |
65 } | 76 } |
66 std::string display = GetDisplayBaseName(actual_path); | 77 std::string display = GetDisplayBaseName(actual_path); |
67 display_path = display_path.Append(display); | 78 display_path = display_path.Append(display); |
68 } | 79 } |
69 DCHECK_EQ(actual_path.value(), source_path.value()); | 80 DCHECK_EQ(actual_path.value(), source_path.value()); |
70 return display_path; | 81 return display_path; |
71 } | 82 #else // defined(OS_MACOSX) |
72 #else // defined(OS_MACOSX) | 83 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; | 84 return display_path; |
79 return source_path; | 85 return source_path; |
86 #endif // defined(OS_MACOSX) | |
80 } | 87 } |
81 #endif // defined(OS_MACOSX) | |
82 | 88 |
83 } // namespace path_util | 89 } // namespace path_util |
84 } // namespace extensions | 90 } // namespace extensions |
OLD | NEW |