| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/base_paths_mac.h" | 5 // Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac |
| 6 // in base/path_service.cc. |
| 6 | 7 |
| 7 #include <dlfcn.h> | 8 #include <dlfcn.h> |
| 8 #import <Foundation/Foundation.h> | 9 #import <Foundation/Foundation.h> |
| 9 #include <mach-o/dyld.h> | 10 #include <mach-o/dyld.h> |
| 10 | 11 |
| 12 #include "base/base_paths.h" |
| 11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 12 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/mac/foundation_util.h" | 17 #include "base/mac/foundation_util.h" |
| 16 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 17 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "build/build_config.h" |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 void GetNSExecutablePath(FilePath* path) { | 24 void GetNSExecutablePath(FilePath* path) { |
| 22 DCHECK(path); | 25 DCHECK(path); |
| 23 // Executable path can have relative references ("..") depending on | 26 // Executable path can have relative references ("..") depending on |
| 24 // how the app was launched. | 27 // how the app was launched. |
| 25 uint32_t executable_length = 0; | 28 uint32_t executable_length = 0; |
| 26 _NSGetExecutablePath(NULL, &executable_length); | 29 _NSGetExecutablePath(NULL, &executable_length); |
| 27 DCHECK_GT(executable_length, 1u); | 30 DCHECK_GT(executable_length, 1u); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 namespace base { | 53 namespace base { |
| 51 | 54 |
| 52 bool PathProviderMac(int key, FilePath* result) { | 55 bool PathProviderMac(int key, FilePath* result) { |
| 53 switch (key) { | 56 switch (key) { |
| 54 case base::FILE_EXE: | 57 case base::FILE_EXE: |
| 55 GetNSExecutablePath(result); | 58 GetNSExecutablePath(result); |
| 56 return true; | 59 return true; |
| 57 case base::FILE_MODULE: | 60 case base::FILE_MODULE: |
| 58 return GetModulePathForAddress(result, | 61 return GetModulePathForAddress(result, |
| 59 reinterpret_cast<const void*>(&base::PathProviderMac)); | 62 reinterpret_cast<const void*>(&base::PathProviderMac)); |
| 60 case base::DIR_CACHE: | |
| 61 return base::mac::GetUserDirectory(NSCachesDirectory, result); | |
| 62 case base::DIR_APP_DATA: { | 63 case base::DIR_APP_DATA: { |
| 63 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory, | 64 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory, |
| 64 result); | 65 result); |
| 65 #if defined(OS_IOS) | 66 #if defined(OS_IOS) |
| 66 // On IOS, this directory does not exist unless it is created explicitly. | 67 // On IOS, this directory does not exist unless it is created explicitly. |
| 67 if (success && !file_util::PathExists(*result)) | 68 if (success && !file_util::PathExists(*result)) |
| 68 success = file_util::CreateDirectory(*result); | 69 success = file_util::CreateDirectory(*result); |
| 69 #endif // defined(OS_IOS) | 70 #endif // defined(OS_IOS) |
| 70 return success; | 71 return success; |
| 71 } | 72 } |
| 72 case base::DIR_SOURCE_ROOT: { | 73 case base::DIR_SOURCE_ROOT: |
| 73 // Go through PathService to catch overrides. | 74 // Go through PathService to catch overrides. |
| 74 if (!PathService::Get(base::FILE_EXE, result)) | 75 if (!PathService::Get(base::FILE_EXE, result)) |
| 75 return false; | 76 return false; |
| 76 | 77 |
| 77 // Start with the executable's directory. | 78 // Start with the executable's directory. |
| 78 *result = result->DirName(); | 79 *result = result->DirName(); |
| 79 | 80 |
| 80 #if !defined(OS_IOS) | 81 #if !defined(OS_IOS) |
| 81 if (base::mac::AmIBundled()) { | 82 if (base::mac::AmIBundled()) { |
| 82 // The bundled app executables (Chromium, TestShell, etc) live five | 83 // The bundled app executables (Chromium, TestShell, etc) live five |
| 83 // levels down, eg: | 84 // levels down, eg: |
| 84 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium | 85 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
| 85 *result = result->DirName().DirName().DirName().DirName().DirName(); | 86 *result = result->DirName().DirName().DirName().DirName().DirName(); |
| 86 } else { | 87 } else { |
| 87 // Unit tests execute two levels deep from the source root, eg: | 88 // Unit tests execute two levels deep from the source root, eg: |
| 88 // src/xcodebuild/{Debug|Release}/base_unittests | 89 // src/xcodebuild/{Debug|Release}/base_unittests |
| 89 *result = result->DirName().DirName(); | 90 *result = result->DirName().DirName(); |
| 90 } | 91 } |
| 91 #endif | 92 #endif |
| 92 return true; | 93 return true; |
| 93 } | 94 case base::DIR_USER_DESKTOP: |
| 94 case base::DIR_HOME: { | 95 return base::mac::GetUserDirectory(NSDesktopDirectory, result); |
| 96 case base::DIR_CACHE: |
| 97 return base::mac::GetUserDirectory(NSCachesDirectory, result); |
| 98 case base::DIR_HOME: |
| 95 *result = base::mac::NSStringToFilePath(NSHomeDirectory()); | 99 *result = base::mac::NSStringToFilePath(NSHomeDirectory()); |
| 96 return true; | 100 return true; |
| 97 } | |
| 98 default: | 101 default: |
| 99 return false; | 102 return false; |
| 100 } | 103 } |
| 101 } | 104 } |
| 102 | 105 |
| 103 } // namespace base | 106 } // namespace base |
| OLD | NEW |