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 OS_MACOSX { |
not at google - send to devlin
2014/08/05 00:11:05
why do you have this extra namespace? it can stay
gpdavis
2014/08/05 01:53:51
I thought this was what you meant by "specific OS_
| |
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 } // namespace | 42 } // namespace OS_MACOSX |
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 = OS_MACOSX::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("~"); |
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 |