OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "base/base_paths_mac.h" |
6 | 6 |
7 #include <dlfcn.h> | |
8 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
9 #include <mach-o/dyld.h> | 8 #include <mach-o/dyld.h> |
10 | 9 |
11 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
12 #include "base/file_path.h" | 11 #include "base/file_path.h" |
13 #include "base/file_util.h" | 12 #include "base/file_util.h" |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
16 #include "base/path_service.h" | 15 #include "base/path_service.h" |
17 #include "base/string_util.h" | 16 #include "base/string_util.h" |
(...skipping 13 matching lines...) Expand all Loading... |
31 char* executable_path_c = WriteInto(&executable_path, executable_length); | 30 char* executable_path_c = WriteInto(&executable_path, executable_length); |
32 int rv = _NSGetExecutablePath(executable_path_c, &executable_length); | 31 int rv = _NSGetExecutablePath(executable_path_c, &executable_length); |
33 DCHECK_EQ(rv, 0); | 32 DCHECK_EQ(rv, 0); |
34 DCHECK(!executable_path.empty()); | 33 DCHECK(!executable_path.empty()); |
35 if ((rv != 0) || (executable_path.empty())) | 34 if ((rv != 0) || (executable_path.empty())) |
36 return false; | 35 return false; |
37 *path = FilePath(executable_path); | 36 *path = FilePath(executable_path); |
38 return true; | 37 return true; |
39 } | 38 } |
40 | 39 |
41 // Returns true if the module for |address| is found. |path| will contain | |
42 // the path to the module. Note that |path| may not be absolute. | |
43 bool GetModulePathForAddress(FilePath* path, | |
44 const void* address) WARN_UNUSED_RESULT; | |
45 | |
46 bool GetModulePathForAddress(FilePath* path, const void* address) { | |
47 Dl_info info; | |
48 if (dladdr(address, &info) == 0) | |
49 return false; | |
50 *path = FilePath(info.dli_fname); | |
51 return true; | |
52 } | |
53 | |
54 } // namespace | 40 } // namespace |
55 | 41 |
56 namespace base { | 42 namespace base { |
57 | 43 |
58 bool PathProviderMac(int key, FilePath* result) { | 44 bool PathProviderMac(int key, FilePath* result) { |
59 switch (key) { | 45 switch (key) { |
60 case base::FILE_EXE: | 46 case base::FILE_EXE: |
| 47 case base::FILE_MODULE: { |
61 return GetNSExecutablePath(result); | 48 return GetNSExecutablePath(result); |
62 case base::FILE_MODULE: | 49 } |
63 return GetModulePathForAddress(result, | |
64 reinterpret_cast<const void*>(&base::PathProviderMac)); | |
65 case base::DIR_CACHE: | 50 case base::DIR_CACHE: |
66 return base::mac::GetUserDirectory(NSCachesDirectory, result); | 51 return base::mac::GetUserDirectory(NSCachesDirectory, result); |
67 case base::DIR_APP_DATA: | 52 case base::DIR_APP_DATA: |
68 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result); | 53 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result); |
69 case base::DIR_SOURCE_ROOT: { | 54 case base::DIR_SOURCE_ROOT: { |
70 // Go through PathService to catch overrides. | 55 // Go through PathService to catch overrides. |
71 if (!PathService::Get(base::FILE_EXE, result)) | 56 if (!PathService::Get(base::FILE_EXE, result)) |
72 return false; | 57 return false; |
73 | 58 |
74 // Start with the executable's directory. | 59 // Start with the executable's directory. |
75 *result = result->DirName(); | 60 *result = result->DirName(); |
76 if (base::mac::AmIBundled()) { | 61 if (base::mac::AmIBundled()) { |
77 // The bundled app executables (Chromium, TestShell, etc) live five | 62 // The bundled app executables (Chromium, TestShell, etc) live five |
78 // levels down, eg: | 63 // levels down, eg: |
79 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium | 64 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
80 *result = result->DirName().DirName().DirName().DirName().DirName(); | 65 *result = result->DirName().DirName().DirName().DirName().DirName(); |
81 } else { | 66 } else { |
82 // Unit tests execute two levels deep from the source root, eg: | 67 // Unit tests execute two levels deep from the source root, eg: |
83 // src/xcodebuild/{Debug|Release}/base_unittests | 68 // src/xcodebuild/{Debug|Release}/base_unittests |
84 *result = result->DirName().DirName(); | 69 *result = result->DirName().DirName(); |
85 } | 70 } |
86 return true; | 71 return true; |
87 } | 72 } |
88 default: | 73 default: |
89 return false; | 74 return false; |
90 } | 75 } |
91 } | 76 } |
92 | 77 |
93 } // namespace base | 78 } // namespace base |
OLD | NEW |