diff options
-rw-r--r-- | base/base_paths_mac.mm | 24 | ||||
-rw-r--r-- | base/mac_util.h | 12 | ||||
-rw-r--r-- | base/mac_util.mm | 28 | ||||
-rw-r--r-- | base/mac_util_unittest.mm | 16 | ||||
-rw-r--r-- | chrome/browser/importer/firefox_importer_utils_mac.mm | 27 | ||||
-rw-r--r-- | chrome/common/chrome_paths_mac.mm | 33 |
6 files changed, 59 insertions, 81 deletions
diff --git a/base/base_paths_mac.mm b/base/base_paths_mac.mm index ee0de67..df9f362 100644 --- a/base/base_paths_mac.mm +++ b/base/base_paths_mac.mm @@ -16,26 +16,6 @@ namespace base { -namespace { - -// TODO(akalin): Export this function somewhere and use it in -// chrome_paths_mac.mm and mac_util.mm. This is tricky because -// NSSearchPathDirectory is declared in an Objective C header so we -// cannot put it in one of the usual locations (where pure C++ files -// would include them). -bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result) { - NSArray* dirs = - NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES); - if ([dirs count] < 1) { - return false; - } - NSString* path = [dirs objectAtIndex:0]; - *result = FilePath([path fileSystemRepresentation]); - return true; -} - -} // namespace - bool PathProviderMac(int key, FilePath* result) { std::string cur; switch (key) { @@ -53,9 +33,9 @@ bool PathProviderMac(int key, FilePath* result) { break; } case base::DIR_USER_CACHE: - return GetUserDirectory(NSCachesDirectory, result); + return mac_util::GetUserDirectory(NSCachesDirectory, result); case base::DIR_APP_DATA: - return GetUserDirectory(NSApplicationSupportDirectory, result); + return mac_util::GetUserDirectory(NSApplicationSupportDirectory, result); case base::DIR_SOURCE_ROOT: { PathService::Get(base::DIR_EXE, result); if (mac_util::AmIBundled()) { diff --git a/base/mac_util.h b/base/mac_util.h index 7b697c9..8628e6c 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -19,6 +19,13 @@ class NSBundle; class NSWindow; #endif +// Adapted from NSPathUtilities.h and NSObjCRuntime.h. +#if __LP64__ || NS_BUILD_32_LIKE_64 +typedef unsigned long NSSearchPathDirectory; +#else +typedef unsigned int NSSearchPathDirectory; +#endif + namespace mac_util { std::string PathFromFSRef(const FSRef& ref); @@ -52,6 +59,11 @@ OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); // app bundle's creator code anyway. OSType CreatorCodeForApplication(); +// Searches for directories for the given key in only the user domain. +// If found, fills result (which must always be non-NULL) with the +// first found directory and returns true. Otherwise, returns false. +bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result); + // Returns the ~/Library directory. FilePath GetUserLibraryPath(); diff --git a/base/mac_util.mm b/base/mac_util.mm index b1f08a8..1c4ef73b 100644 --- a/base/mac_util.mm +++ b/base/mac_util.mm @@ -96,18 +96,24 @@ OSType CreatorCodeForApplication() { return CreatorCodeForCFBundleRef(bundle); } -FilePath GetUserLibraryPath() { - NSArray* dirs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, - NSUserDomainMask, YES); - if ([dirs count] == 0) - return FilePath(); - - NSString* library_dir = [dirs objectAtIndex:0]; - const char* library_dir_path = [library_dir fileSystemRepresentation]; - if (!library_dir_path) - return FilePath(); +bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result) { + DCHECK(result); + NSArray* dirs = + NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES); + if ([dirs count] < 1) { + return false; + } + NSString* path = [dirs objectAtIndex:0]; + *result = FilePath([path fileSystemRepresentation]); + return true; +} - return FilePath(library_dir_path); +FilePath GetUserLibraryPath() { + FilePath user_library_path; + if (!GetUserDirectory(NSLibraryDirectory, &user_library_path)) { + LOG(WARNING) << "Could not get user library path"; + } + return user_library_path; } CGColorSpaceRef GetSRGBColorSpace() { diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm index 676353b..801922a 100644 --- a/base/mac_util_unittest.mm +++ b/base/mac_util_unittest.mm @@ -29,6 +29,22 @@ TEST_F(MacUtilTest, TestFSRef) { EXPECT_EQ(path, PathFromFSRef(ref)); } +TEST_F(MacUtilTest, GetUserDirectoryTest) { + // Try a few keys, make sure they come back with non-empty paths. + FilePath caches_dir; + EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir)); + EXPECT_FALSE(caches_dir.empty()); + + FilePath application_support_dir; + EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory, + &application_support_dir)); + EXPECT_FALSE(application_support_dir.empty()); + + FilePath library_dir; + EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir)); + EXPECT_FALSE(library_dir.empty()); +} + TEST_F(MacUtilTest, TestLibraryPath) { FilePath library_dir = GetUserLibraryPath(); // Make sure the string isn't empty. diff --git a/chrome/browser/importer/firefox_importer_utils_mac.mm b/chrome/browser/importer/firefox_importer_utils_mac.mm index bea6b71..08f222d 100644 --- a/chrome/browser/importer/firefox_importer_utils_mac.mm +++ b/chrome/browser/importer/firefox_importer_utils_mac.mm @@ -7,27 +7,18 @@ #include "chrome/browser/importer/firefox_importer_utils.h" #include "base/file_util.h" +#include "base/path_service.h" FilePath GetProfilesINI() { - FilePath ini_file; - NSArray* dirs = - NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, - NSUserDomainMask, YES); - if ([dirs count]) { - NSString* app_support_dir = [dirs objectAtIndex:0]; - NSString* firefox_dir = [app_support_dir - stringByAppendingPathComponent:@"Firefox"]; - NSString* profiles_ini = [firefox_dir - stringByAppendingPathComponent:@"profiles.ini"]; - if (profiles_ini) { - ini_file = FilePath([profiles_ini fileSystemRepresentation]); - } + FilePath app_data_path; + if (!PathService::Get(base::DIR_APP_DATA, &app_data_path)) { + return FilePath(); } - - if (file_util::PathExists(ini_file)) - return ini_file; - - return FilePath(); + FilePath ini_file = app_data_path.Append("Firefox").Append("profiles.ini"); + if (!file_util::PathExists(ini_file)) { + return FilePath(); + } + return ini_file; } FilePath GetFirefoxDylibPath() { diff --git a/chrome/common/chrome_paths_mac.mm b/chrome/common/chrome_paths_mac.mm index f401751..21868cd 100644 --- a/chrome/common/chrome_paths_mac.mm +++ b/chrome/common/chrome_paths_mac.mm @@ -41,42 +41,15 @@ bool GetChromeFrameUserDataDirectory(FilePath* result) { } bool GetUserDocumentsDirectory(FilePath* result) { - bool success = false; - NSArray* docArray = - NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, - NSUserDomainMask, - YES); - if ([docArray count] && result) { - *result = FilePath([[docArray objectAtIndex:0] fileSystemRepresentation]); - success = true; - } - return success; + return mac_util::GetUserDirectory(NSDocumentDirectory, result); } bool GetUserDownloadsDirectory(FilePath* result) { - bool success = false; - NSArray* docArray = - NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, - NSUserDomainMask, - YES); - if ([docArray count] && result) { - *result = FilePath([[docArray objectAtIndex:0] fileSystemRepresentation]); - success = true; - } - return success; + return mac_util::GetUserDirectory(NSDownloadsDirectory, result); } bool GetUserDesktop(FilePath* result) { - bool success = false; - NSArray* docArray = - NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, - NSUserDomainMask, - YES); - if ([docArray count] && result) { - *result = FilePath([[docArray objectAtIndex:0] fileSystemRepresentation]); - success = true; - } - return success; + return mac_util::GetUserDirectory(NSDesktopDirectory, result); } FilePath GetVersionedDirectory() { |