blob: 14d1c201ca765d35bca7ce8ba87e1fda32e394b7 [file] [log] [blame]
[email protected]da19703b2012-07-17 14:15:341// Copyright (c) 2012 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]542cbb692011-06-14 19:03:1615#include "base/mac/foundation_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
[email protected]fdce4782011-11-29 20:06:1821void GetNSExecutablePath(FilePath* path) {
[email protected]a6849212010-08-24 21:32:3522 DCHECK(path);
23 // Executable path can have relative references ("..") depending on
24 // how the app was launched.
25 uint32_t executable_length = 0;
26 _NSGetExecutablePath(NULL, &executable_length);
[email protected]fdce4782011-11-29 20:06:1827 DCHECK_GT(executable_length, 1u);
[email protected]a6849212010-08-24 21:32:3528 std::string executable_path;
[email protected]fdce4782011-11-29 20:06:1829 int rv = _NSGetExecutablePath(WriteInto(&executable_path, executable_length),
30 &executable_length);
[email protected]a6849212010-08-24 21:32:3531 DCHECK_EQ(rv, 0);
[email protected]a6849212010-08-24 21:32:3532 *path = FilePath(executable_path);
[email protected]a6849212010-08-24 21:32:3533}
34
[email protected]699c26c2011-05-26 16:19:0135// Returns true if the module for |address| is found. |path| will contain
36// the path to the module. Note that |path| may not be absolute.
37bool GetModulePathForAddress(FilePath* path,
38 const void* address) WARN_UNUSED_RESULT;
39
40bool GetModulePathForAddress(FilePath* path, const void* address) {
41 Dl_info info;
42 if (dladdr(address, &info) == 0)
43 return false;
44 *path = FilePath(info.dli_fname);
45 return true;
46}
47
[email protected]a6849212010-08-24 21:32:3548} // namespace
49
[email protected]5af2edb92008-08-08 20:16:0850namespace base {
51
[email protected]4792a262008-11-19 16:50:0352bool PathProviderMac(int key, FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0853 switch (key) {
54 case base::FILE_EXE:
[email protected]fdce4782011-11-29 20:06:1855 GetNSExecutablePath(result);
56 return true;
[email protected]699c26c2011-05-26 16:19:0157 case base::FILE_MODULE:
58 return GetModulePathForAddress(result,
59 reinterpret_cast<const void*>(&base::PathProviderMac));
[email protected]b411da32010-11-24 02:23:1560 case base::DIR_CACHE:
[email protected]0378bf42011-01-01 18:20:1461 return base::mac::GetUserDirectory(NSCachesDirectory, result);
[email protected]8495af32012-08-01 00:29:1562 case base::DIR_APP_DATA: {
63 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
64 result);
65#if defined(OS_IOS)
66 // On IOS, this directory does not exist unless it is created explicitly.
67 if (success && !file_util::PathExists(*result))
68 success = file_util::CreateDirectory(*result);
69#endif // defined(OS_IOS)
70 return success;
71 }
[email protected]e2ac70002008-12-09 14:58:1372 case base::DIR_SOURCE_ROOT: {
[email protected]7fac8882010-11-15 19:52:5273 // Go through PathService to catch overrides.
[email protected]b266ffea2011-04-12 06:07:2574 if (!PathService::Get(base::FILE_EXE, result))
75 return false;
76
77 // Start with the executable's directory.
78 *result = result->DirName();
[email protected]da19703b2012-07-17 14:15:3479
80#if !defined(OS_IOS)
[email protected]b266ffea2011-04-12 06:07:2581 if (base::mac::AmIBundled()) {
82 // The bundled app executables (Chromium, TestShell, etc) live five
83 // levels down, eg:
84 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
85 *result = result->DirName().DirName().DirName().DirName().DirName();
86 } else {
87 // Unit tests execute two levels deep from the source root, eg:
88 // src/xcodebuild/{Debug|Release}/base_unittests
89 *result = result->DirName().DirName();
[email protected]d480bc82009-04-22 18:35:1090 }
[email protected]da19703b2012-07-17 14:15:3491#endif
[email protected]e2ac70002008-12-09 14:58:1392 return true;
93 }
[email protected]92e44ae0a2012-07-23 08:22:4494 case base::DIR_HOME: {
95 *result = base::mac::NSStringToFilePath(NSHomeDirectory());
96 return true;
97 }
[email protected]5af2edb92008-08-08 20:16:0898 default:
99 return false;
100 }
[email protected]5af2edb92008-08-08 20:16:08101}
102
103} // namespace base