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