OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <dlfcn.h> |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 #include <mach-o/dyld.h> | 9 #include <mach-o/dyld.h> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/mac/foundation_util.h" | 15 #include "base/mac/foundation_util.h" |
16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 bool GetNSExecutablePath(FilePath* path) WARN_UNUSED_RESULT; | 21 void GetNSExecutablePath(FilePath* path) { |
22 | |
23 bool GetNSExecutablePath(FilePath* path) { | |
24 DCHECK(path); | 22 DCHECK(path); |
25 // Executable path can have relative references ("..") depending on | 23 // Executable path can have relative references ("..") depending on |
26 // how the app was launched. | 24 // how the app was launched. |
27 uint32_t executable_length = 0; | 25 uint32_t executable_length = 0; |
28 _NSGetExecutablePath(NULL, &executable_length); | 26 _NSGetExecutablePath(NULL, &executable_length); |
29 DCHECK_GE(executable_length, 1u); | 27 DCHECK_GT(executable_length, 1u); |
30 std::string executable_path; | 28 std::string executable_path; |
31 char* executable_path_c = WriteInto(&executable_path, executable_length); | 29 int rv = _NSGetExecutablePath(WriteInto(&executable_path, executable_length), |
32 int rv = _NSGetExecutablePath(executable_path_c, &executable_length); | 30 &executable_length); |
33 DCHECK_EQ(rv, 0); | 31 DCHECK_EQ(rv, 0); |
34 DCHECK(!executable_path.empty()); | |
35 if ((rv != 0) || (executable_path.empty())) | |
36 return false; | |
37 *path = FilePath(executable_path); | 32 *path = FilePath(executable_path); |
38 return true; | |
39 } | 33 } |
40 | 34 |
41 // Returns true if the module for |address| is found. |path| will contain | 35 // 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. | 36 // the path to the module. Note that |path| may not be absolute. |
43 bool GetModulePathForAddress(FilePath* path, | 37 bool GetModulePathForAddress(FilePath* path, |
44 const void* address) WARN_UNUSED_RESULT; | 38 const void* address) WARN_UNUSED_RESULT; |
45 | 39 |
46 bool GetModulePathForAddress(FilePath* path, const void* address) { | 40 bool GetModulePathForAddress(FilePath* path, const void* address) { |
47 Dl_info info; | 41 Dl_info info; |
48 if (dladdr(address, &info) == 0) | 42 if (dladdr(address, &info) == 0) |
49 return false; | 43 return false; |
50 *path = FilePath(info.dli_fname); | 44 *path = FilePath(info.dli_fname); |
51 return true; | 45 return true; |
52 } | 46 } |
53 | 47 |
54 } // namespace | 48 } // namespace |
55 | 49 |
56 namespace base { | 50 namespace base { |
57 | 51 |
58 bool PathProviderMac(int key, FilePath* result) { | 52 bool PathProviderMac(int key, FilePath* result) { |
59 switch (key) { | 53 switch (key) { |
60 case base::FILE_EXE: | 54 case base::FILE_EXE: |
61 return GetNSExecutablePath(result); | 55 GetNSExecutablePath(result); |
| 56 return true; |
62 case base::FILE_MODULE: | 57 case base::FILE_MODULE: |
63 return GetModulePathForAddress(result, | 58 return GetModulePathForAddress(result, |
64 reinterpret_cast<const void*>(&base::PathProviderMac)); | 59 reinterpret_cast<const void*>(&base::PathProviderMac)); |
65 case base::DIR_CACHE: | 60 case base::DIR_CACHE: |
66 return base::mac::GetUserDirectory(NSCachesDirectory, result); | 61 return base::mac::GetUserDirectory(NSCachesDirectory, result); |
67 case base::DIR_APP_DATA: | 62 case base::DIR_APP_DATA: |
68 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result); | 63 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result); |
69 case base::DIR_SOURCE_ROOT: { | 64 case base::DIR_SOURCE_ROOT: { |
70 // Go through PathService to catch overrides. | 65 // Go through PathService to catch overrides. |
71 if (!PathService::Get(base::FILE_EXE, result)) | 66 if (!PathService::Get(base::FILE_EXE, result)) |
(...skipping 12 matching lines...) Expand all Loading... |
84 *result = result->DirName().DirName(); | 79 *result = result->DirName().DirName(); |
85 } | 80 } |
86 return true; | 81 return true; |
87 } | 82 } |
88 default: | 83 default: |
89 return false; | 84 return false; |
90 } | 85 } |
91 } | 86 } |
92 | 87 |
93 } // namespace base | 88 } // namespace base |
OLD | NEW |