[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 49df602 | 2008-08-27 19:03:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 4 | |
| 5 | #include "base/base_paths_mac.h" |
| 6 | |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 7 | #include <dlfcn.h> |
[email protected] | 880a6d5e | 2010-11-16 22:25:47 | [diff] [blame] | 8 | #import <Foundation/Foundation.h> |
[email protected] | b88a291b | 2009-10-15 01:22:51 | [diff] [blame] | 9 | #include <mach-o/dyld.h> |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 10 | |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 3bd80745 | 2008-11-19 16:55:43 | [diff] [blame] | 12 | #include "base/file_path.h" |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 13 | #include "base/file_util.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 542cbb69 | 2011-06-14 19:03:16 | [diff] [blame] | 15 | #include "base/mac/foundation_util.h" |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 17 | #include "base/string_util.h" |
| 18 | |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 21 | void GetNSExecutablePath(FilePath* path) { |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 22 | DCHECK(path); |
| 23 | // Executable path can have relative references ("..") depending on |
| 24 | // how the app was launched. |
| 25 | uint32_t executable_length = 0; |
| 26 | _NSGetExecutablePath(NULL, &executable_length); |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 27 | DCHECK_GT(executable_length, 1u); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 28 | std::string executable_path; |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 29 | int rv = _NSGetExecutablePath(WriteInto(&executable_path, executable_length), |
| 30 | &executable_length); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 31 | DCHECK_EQ(rv, 0); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 32 | *path = FilePath(executable_path); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 35 | // Returns true if the module for |address| is found. |path| will contain |
| 36 | // the path to the module. Note that |path| may not be absolute. |
| 37 | bool GetModulePathForAddress(FilePath* path, |
| 38 | const void* address) WARN_UNUSED_RESULT; |
| 39 | |
| 40 | bool GetModulePathForAddress(FilePath* path, const void* address) { |
| 41 | Dl_info info; |
| 42 | if (dladdr(address, &info) == 0) |
| 43 | return false; |
| 44 | *path = FilePath(info.dli_fname); |
| 45 | return true; |
| 46 | } |
| 47 | |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 48 | } // namespace |
| 49 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 50 | namespace base { |
| 51 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 52 | bool PathProviderMac(int key, FilePath* result) { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 53 | switch (key) { |
| 54 | case base::FILE_EXE: |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 55 | GetNSExecutablePath(result); |
| 56 | return true; |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 57 | case base::FILE_MODULE: |
| 58 | return GetModulePathForAddress(result, |
| 59 | reinterpret_cast<const void*>(&base::PathProviderMac)); |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 60 | case base::DIR_CACHE: |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 61 | return base::mac::GetUserDirectory(NSCachesDirectory, result); |
[email protected] | 8495af3 | 2012-08-01 00:29:15 | [diff] [blame^] | 62 | case base::DIR_APP_DATA: { |
| 63 | bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory, |
| 64 | result); |
| 65 | #if defined(OS_IOS) |
| 66 | // On IOS, this directory does not exist unless it is created explicitly. |
| 67 | if (success && !file_util::PathExists(*result)) |
| 68 | success = file_util::CreateDirectory(*result); |
| 69 | #endif // defined(OS_IOS) |
| 70 | return success; |
| 71 | } |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 72 | case base::DIR_SOURCE_ROOT: { |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 73 | // Go through PathService to catch overrides. |
[email protected] | b266ffea | 2011-04-12 06:07:25 | [diff] [blame] | 74 | if (!PathService::Get(base::FILE_EXE, result)) |
| 75 | return false; |
| 76 | |
| 77 | // Start with the executable's directory. |
| 78 | *result = result->DirName(); |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 79 | |
| 80 | #if !defined(OS_IOS) |
[email protected] | b266ffea | 2011-04-12 06:07:25 | [diff] [blame] | 81 | if (base::mac::AmIBundled()) { |
| 82 | // The bundled app executables (Chromium, TestShell, etc) live five |
| 83 | // levels down, eg: |
| 84 | // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
| 85 | *result = result->DirName().DirName().DirName().DirName().DirName(); |
| 86 | } else { |
| 87 | // Unit tests execute two levels deep from the source root, eg: |
| 88 | // src/xcodebuild/{Debug|Release}/base_unittests |
| 89 | *result = result->DirName().DirName(); |
[email protected] | d480bc8 | 2009-04-22 18:35:10 | [diff] [blame] | 90 | } |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 91 | #endif |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 92 | return true; |
| 93 | } |
[email protected] | 92e44ae0a | 2012-07-23 08:22:44 | [diff] [blame] | 94 | case base::DIR_HOME: { |
| 95 | *result = base::mac::NSStringToFilePath(NSHomeDirectory()); |
| 96 | return true; |
| 97 | } |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 98 | default: |
| 99 | return false; |
| 100 | } |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace base |