Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/base_paths_mac.h" |
| 6 | |
Ben Murdoch | 4a5e2dc | 2010-11-25 19:40:10 +0000 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 8 | #include <mach-o/dyld.h> |
| 9 | |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 11 | #include "base/file_path.h" |
| 12 | #include "base/file_util.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/mac_util.h" |
| 15 | #include "base/path_service.h" |
| 16 | #include "base/string_util.h" |
| 17 | |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 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 | |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 42 | namespace base { |
| 43 | |
| 44 | bool PathProviderMac(int key, FilePath* result) { |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 45 | switch (key) { |
| 46 | case base::FILE_EXE: |
| 47 | case base::FILE_MODULE: { |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 48 | return GetNSExecutablePath(result); |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 49 | } |
| 50 | case base::DIR_USER_CACHE: |
| 51 | return mac_util::GetUserDirectory(NSCachesDirectory, result); |
| 52 | case base::DIR_APP_DATA: |
| 53 | return mac_util::GetUserDirectory(NSApplicationSupportDirectory, result); |
| 54 | case base::DIR_SOURCE_ROOT: { |
Ben Murdoch | 4a5e2dc | 2010-11-25 19:40:10 +0000 | [diff] [blame] | 55 | // Go through PathService to catch overrides. |
| 56 | if (PathService::Get(base::FILE_EXE, result)) { |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 57 | // Start with the executable's directory. |
| 58 | *result = result->DirName(); |
| 59 | if (mac_util::AmIBundled()) { |
| 60 | // The bundled app executables (Chromium, TestShell, etc) live five |
| 61 | // levels down, eg: |
| 62 | // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium |
| 63 | *result = result->DirName().DirName().DirName().DirName().DirName(); |
| 64 | } else { |
| 65 | // Unit tests execute two levels deep from the source root, eg: |
| 66 | // src/xcodebuild/{Debug|Release}/base_unittests |
| 67 | *result = result->DirName().DirName(); |
| 68 | } |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | default: |
| 73 | return false; |
| 74 | } |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace base |