blob: 90a274cdd91a0550b56412e4ad76a0caee825e63 [file] [log] [blame]
[email protected]699c26c2011-05-26 16:19:011// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]49df6022008-08-27 19:03:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]5af2edb92008-08-08 20:16:084
5#include "base/base_paths_mac.h"
6
[email protected]699c26c2011-05-26 16:19:017#include <dlfcn.h>
[email protected]880a6d5e2010-11-16 22:25:478#import <Foundation/Foundation.h>
[email protected]b88a291b2009-10-15 01:22:519#include <mach-o/dyld.h>
[email protected]5af2edb92008-08-08 20:16:0810
[email protected]a6849212010-08-24 21:32:3511#include "base/compiler_specific.h"
[email protected]3bd807452008-11-19 16:55:4312#include "base/file_path.h"
[email protected]37088fef2008-08-15 17:32:1013#include "base/file_util.h"
[email protected]5af2edb92008-08-08 20:16:0814#include "base/logging.h"
[email protected]0378bf42011-01-01 18:20:1415#include "base/mac/mac_util.h"
[email protected]37088fef2008-08-15 17:32:1016#include "base/path_service.h"
[email protected]5af2edb92008-08-08 20:16:0817#include "base/string_util.h"
18
[email protected]a6849212010-08-24 21:32:3519namespace {
20
21bool GetNSExecutablePath(FilePath* path) WARN_UNUSED_RESULT;
22
23bool GetNSExecutablePath(FilePath* path) {
24 DCHECK(path);
25 // Executable path can have relative references ("..") depending on
26 // how the app was launched.
27 uint32_t executable_length = 0;
28 _NSGetExecutablePath(NULL, &executable_length);
29 DCHECK_GE(executable_length, 1u);
30 std::string executable_path;
31 char* executable_path_c = WriteInto(&executable_path, executable_length);
32 int rv = _NSGetExecutablePath(executable_path_c, &executable_length);
33 DCHECK_EQ(rv, 0);
34 DCHECK(!executable_path.empty());
35 if ((rv != 0) || (executable_path.empty()))
36 return false;
37 *path = FilePath(executable_path);
38 return true;
39}
40
[email protected]699c26c2011-05-26 16:19:0141// 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.
43bool GetModulePathForAddress(FilePath* path,
44 const void* address) WARN_UNUSED_RESULT;
45
46bool 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
[email protected]a6849212010-08-24 21:32:3554} // namespace
55
[email protected]5af2edb92008-08-08 20:16:0856namespace base {
57
[email protected]4792a262008-11-19 16:50:0358bool PathProviderMac(int key, FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0859 switch (key) {
60 case base::FILE_EXE:
[email protected]a6849212010-08-24 21:32:3561 return GetNSExecutablePath(result);
[email protected]699c26c2011-05-26 16:19:0162 case base::FILE_MODULE:
63 return GetModulePathForAddress(result,
64 reinterpret_cast<const void*>(&base::PathProviderMac));
[email protected]b411da32010-11-24 02:23:1565 case base::DIR_CACHE:
[email protected]0378bf42011-01-01 18:20:1466 return base::mac::GetUserDirectory(NSCachesDirectory, result);
[email protected]405a64b2009-09-16 21:03:4467 case base::DIR_APP_DATA:
[email protected]0378bf42011-01-01 18:20:1468 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result);
[email protected]e2ac70002008-12-09 14:58:1369 case base::DIR_SOURCE_ROOT: {
[email protected]7fac8882010-11-15 19:52:5270 // Go through PathService to catch overrides.
[email protected]b266ffea2011-04-12 06:07:2571 if (!PathService::Get(base::FILE_EXE, result))
72 return false;
73
74 // Start with the executable's directory.
75 *result = result->DirName();
76 if (base::mac::AmIBundled()) {
77 // The bundled app executables (Chromium, TestShell, etc) live five
78 // levels down, eg:
79 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
80 *result = result->DirName().DirName().DirName().DirName().DirName();
81 } else {
82 // Unit tests execute two levels deep from the source root, eg:
83 // src/xcodebuild/{Debug|Release}/base_unittests
84 *result = result->DirName().DirName();
[email protected]d480bc82009-04-22 18:35:1085 }
[email protected]e2ac70002008-12-09 14:58:1386 return true;
87 }
[email protected]5af2edb92008-08-08 20:16:0888 default:
89 return false;
90 }
[email protected]5af2edb92008-08-08 20:16:0891}
92
93} // namespace base