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