[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 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 5 | // Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac |
| 6 | // in base/path_service.cc. |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 7 | |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 8 | #include <dlfcn.h> |
[email protected] | 880a6d5e | 2010-11-16 22:25:47 | [diff] [blame] | 9 | #import <Foundation/Foundation.h> |
[email protected] | b88a291b | 2009-10-15 01:22:51 | [diff] [blame] | 10 | #include <mach-o/dyld.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include <stdint.h> |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 12 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 13 | #include "base/base_paths.h" |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 17 | #include "base/logging.h" |
[email protected] | 542cbb69 | 2011-06-14 19:03:16 | [diff] [blame] | 18 | #include "base/mac/foundation_util.h" |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 19 | #include "base/path_service.h" |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 22 | |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 23 | namespace { |
| 24 | |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 25 | void GetNSExecutablePath(base::FilePath* path) { |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 26 | DCHECK(path); |
| 27 | // Executable path can have relative references ("..") depending on |
| 28 | // how the app was launched. |
| 29 | uint32_t executable_length = 0; |
| 30 | _NSGetExecutablePath(NULL, &executable_length); |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 31 | DCHECK_GT(executable_length, 1u); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 32 | std::string executable_path; |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 33 | int rv = _NSGetExecutablePath( |
| 34 | base::WriteInto(&executable_path, executable_length), |
| 35 | &executable_length); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 36 | DCHECK_EQ(rv, 0); |
[email protected] | 40fd705 | 2013-08-06 13:30:25 | [diff] [blame] | 37 | |
| 38 | // _NSGetExecutablePath may return paths containing ./ or ../ which makes |
| 39 | // FilePath::DirName() work incorrectly, convert it to absolute path so that |
| 40 | // paths such as DIR_SOURCE_ROOT can work, since we expect absolute paths to |
| 41 | // be returned here. |
| 42 | *path = base::MakeAbsoluteFilePath(base::FilePath(executable_path)); |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 45 | // Returns true if the module for |address| is found. |path| will contain |
| 46 | // the path to the module. Note that |path| may not be absolute. |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 47 | bool GetModulePathForAddress(base::FilePath* path, |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 48 | const void* address) WARN_UNUSED_RESULT; |
| 49 | |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 50 | bool GetModulePathForAddress(base::FilePath* path, const void* address) { |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 51 | Dl_info info; |
| 52 | if (dladdr(address, &info) == 0) |
| 53 | return false; |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 54 | *path = base::FilePath(info.dli_fname); |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
[email protected] | a684921 | 2010-08-24 21:32:35 | [diff] [blame] | 58 | } // namespace |
| 59 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 60 | namespace base { |
| 61 | |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 62 | bool PathProviderMac(int key, base::FilePath* result) { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 63 | switch (key) { |
| 64 | case base::FILE_EXE: |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 65 | GetNSExecutablePath(result); |
| 66 | return true; |
[email protected] | 699c26c | 2011-05-26 16:19:01 | [diff] [blame] | 67 | case base::FILE_MODULE: |
| 68 | return GetModulePathForAddress(result, |
| 69 | reinterpret_cast<const void*>(&base::PathProviderMac)); |
[email protected] | 8495af3 | 2012-08-01 00:29:15 | [diff] [blame] | 70 | case base::DIR_APP_DATA: { |
| 71 | bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory, |
| 72 | result); |
| 73 | #if defined(OS_IOS) |
| 74 | // On IOS, this directory does not exist unless it is created explicitly. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 75 | if (success && !base::PathExists(*result)) |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 76 | success = base::CreateDirectory(*result); |
[email protected] | 8495af3 | 2012-08-01 00:29:15 | [diff] [blame] | 77 | #endif // defined(OS_IOS) |
| 78 | return success; |
| 79 | } |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 80 | case base::DIR_SOURCE_ROOT: |
[email protected] | 7fac888 | 2010-11-15 19:52:52 | [diff] [blame] | 81 | // Go through PathService to catch overrides. |
[email protected] | b266ffea | 2011-04-12 06:07:25 | [diff] [blame] | 82 | if (!PathService::Get(base::FILE_EXE, result)) |
| 83 | return false; |
| 84 | |
| 85 | // Start with the executable's directory. |
| 86 | *result = result->DirName(); |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 87 | |
| 88 | #if !defined(OS_IOS) |
[email protected] | b266ffea | 2011-04-12 06:07:25 | [diff] [blame] | 89 | if (base::mac::AmIBundled()) { |
| 90 | // The bundled app executables (Chromium, TestShell, etc) live five |
| 91 | // levels down, eg: |
| 92 | // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
| 93 | *result = result->DirName().DirName().DirName().DirName().DirName(); |
| 94 | } else { |
| 95 | // Unit tests execute two levels deep from the source root, eg: |
| 96 | // src/xcodebuild/{Debug|Release}/base_unittests |
| 97 | *result = result->DirName().DirName(); |
[email protected] | d480bc8 | 2009-04-22 18:35:10 | [diff] [blame] | 98 | } |
[email protected] | da19703b | 2012-07-17 14:15:34 | [diff] [blame] | 99 | #endif |
[email protected] | e2ac7000 | 2008-12-09 14:58:13 | [diff] [blame] | 100 | return true; |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 101 | case base::DIR_USER_DESKTOP: |
[email protected] | ce576fe2 | 2012-09-25 18:16:25 | [diff] [blame] | 102 | #if defined(OS_IOS) |
| 103 | // iOS does not have desktop directories. |
| 104 | NOTIMPLEMENTED(); |
| 105 | return false; |
| 106 | #else |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 107 | return base::mac::GetUserDirectory(NSDesktopDirectory, result); |
[email protected] | ce576fe2 | 2012-09-25 18:16:25 | [diff] [blame] | 108 | #endif |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 109 | case base::DIR_CACHE: |
| 110 | return base::mac::GetUserDirectory(NSCachesDirectory, result); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 111 | default: |
| 112 | return false; |
| 113 | } |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace base |