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 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <mach-o/dyld.h> | 8 #include <mach-o/dyld.h> |
9 | 9 |
| 10 #include "base/compiler_specific.h" |
10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/mac_util.h" | 14 #include "base/mac_util.h" |
14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
16 | 17 |
| 18 namespace { |
| 19 |
| 20 bool GetNSExecutablePath(FilePath* path) WARN_UNUSED_RESULT; |
| 21 |
| 22 bool GetNSExecutablePath(FilePath* path) { |
| 23 DCHECK(path); |
| 24 // Executable path can have relative references ("..") depending on |
| 25 // how the app was launched. |
| 26 uint32_t executable_length = 0; |
| 27 _NSGetExecutablePath(NULL, &executable_length); |
| 28 DCHECK_GE(executable_length, 1u); |
| 29 std::string executable_path; |
| 30 char* executable_path_c = WriteInto(&executable_path, executable_length); |
| 31 int rv = _NSGetExecutablePath(executable_path_c, &executable_length); |
| 32 DCHECK_EQ(rv, 0); |
| 33 DCHECK(!executable_path.empty()); |
| 34 if ((rv != 0) || (executable_path.empty())) |
| 35 return false; |
| 36 *path = FilePath(executable_path); |
| 37 return true; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
17 namespace base { | 42 namespace base { |
18 | 43 |
19 bool PathProviderMac(int key, FilePath* result) { | 44 bool PathProviderMac(int key, FilePath* result) { |
20 std::string cur; | |
21 switch (key) { | 45 switch (key) { |
22 case base::FILE_EXE: | 46 case base::FILE_EXE: |
23 case base::FILE_MODULE: { | 47 case base::FILE_MODULE: { |
24 // Executable path can have relative references ("..") depending on | 48 return GetNSExecutablePath(result); |
25 // how the app was launched. | |
26 uint32_t executable_length = 0; | |
27 _NSGetExecutablePath(NULL, &executable_length); | |
28 DCHECK_GT(executable_length, 1u); | |
29 char* executable = WriteInto(&cur, executable_length); | |
30 int rv = _NSGetExecutablePath(executable, &executable_length); | |
31 DCHECK_EQ(rv, 0); | |
32 DCHECK(!cur.empty()); | |
33 break; | |
34 } | 49 } |
35 case base::DIR_USER_CACHE: | 50 case base::DIR_USER_CACHE: |
36 return mac_util::GetUserDirectory(NSCachesDirectory, result); | 51 return mac_util::GetUserDirectory(NSCachesDirectory, result); |
37 case base::DIR_APP_DATA: | 52 case base::DIR_APP_DATA: |
38 return mac_util::GetUserDirectory(NSApplicationSupportDirectory, result); | 53 return mac_util::GetUserDirectory(NSApplicationSupportDirectory, result); |
39 case base::DIR_SOURCE_ROOT: { | 54 case base::DIR_SOURCE_ROOT: { |
40 PathService::Get(base::DIR_EXE, result); | 55 if (GetNSExecutablePath(result)) { |
41 if (mac_util::AmIBundled()) { | 56 // Start with the executable's directory. |
42 // The bundled app executables (Chromium, TestShell, etc) live five | 57 *result = result->DirName(); |
43 // levels down, eg: | 58 if (mac_util::AmIBundled()) { |
44 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium. | 59 // The bundled app executables (Chromium, TestShell, etc) live five |
45 *result = result->DirName().DirName().DirName().DirName().DirName(); | 60 // levels down, eg: |
46 } else { | 61 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
47 // Unit tests execute two levels deep from the source root, eg: | 62 *result = result->DirName().DirName().DirName().DirName().DirName(); |
48 // src/xcodebuild/{Debug|Release}/base_unittests | 63 } else { |
49 *result = result->DirName().DirName(); | 64 // Unit tests execute two levels deep from the source root, eg: |
| 65 // src/xcodebuild/{Debug|Release}/base_unittests |
| 66 *result = result->DirName().DirName(); |
| 67 } |
50 } | 68 } |
51 return true; | 69 return true; |
52 } | 70 } |
53 default: | 71 default: |
54 return false; | 72 return false; |
55 } | 73 } |
56 | |
57 *result = FilePath(cur); | |
58 return true; | |
59 } | 74 } |
60 | 75 |
61 } // namespace base | 76 } // namespace base |
OLD | NEW |