blob: 839df0d7cc981d36f1759bea8ea5ba360745c091 [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
[email protected]dea1d7d2012-09-20 16:24:525// Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac
6// in base/path_service.cc.
[email protected]5af2edb92008-08-08 20:16:087
[email protected]699c26c2011-05-26 16:19:018#include <dlfcn.h>
[email protected]880a6d5e2010-11-16 22:25:479#import <Foundation/Foundation.h>
[email protected]b88a291b2009-10-15 01:22:5110#include <mach-o/dyld.h>
avi9b6f42932015-12-26 22:15:1411#include <stdint.h>
[email protected]5af2edb92008-08-08 20:16:0812
[email protected]dea1d7d2012-09-20 16:24:5213#include "base/base_paths.h"
[email protected]a6849212010-08-24 21:32:3514#include "base/compiler_specific.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1416#include "base/files/file_util.h"
[email protected]5af2edb92008-08-08 20:16:0817#include "base/logging.h"
[email protected]542cbb692011-06-14 19:03:1618#include "base/mac/foundation_util.h"
[email protected]37088fef2008-08-15 17:32:1019#include "base/path_service.h"
[email protected]251cd6e52013-06-11 13:36:3720#include "base/strings/string_util.h"
[email protected]dea1d7d2012-09-20 16:24:5221#include "build/build_config.h"
[email protected]5af2edb92008-08-08 20:16:0822
[email protected]a6849212010-08-24 21:32:3523namespace {
24
[email protected]aaa6df42013-02-17 19:36:0325void GetNSExecutablePath(base::FilePath* path) {
[email protected]a6849212010-08-24 21:32:3526 DCHECK(path);
27 // Executable path can have relative references ("..") depending on
28 // how the app was launched.
29 uint32_t executable_length = 0;
30 _NSGetExecutablePath(NULL, &executable_length);
[email protected]fdce4782011-11-29 20:06:1831 DCHECK_GT(executable_length, 1u);
[email protected]a6849212010-08-24 21:32:3532 std::string executable_path;
Brett Wilsone3c4d1a2015-07-07 23:38:0933 int rv = _NSGetExecutablePath(
34 base::WriteInto(&executable_path, executable_length),
35 &executable_length);
[email protected]a6849212010-08-24 21:32:3536 DCHECK_EQ(rv, 0);
[email protected]40fd7052013-08-06 13:30:2537
38 // _NSGetExecutablePath may return paths containing ./ or ../ which makes
39 // FilePath::DirName() work incorrectly, convert it to absolute path so that
40 // paths such as DIR_SOURCE_ROOT can work, since we expect absolute paths to
41 // be returned here.
42 *path = base::MakeAbsoluteFilePath(base::FilePath(executable_path));
[email protected]a6849212010-08-24 21:32:3543}
44
[email protected]699c26c2011-05-26 16:19:0145// Returns true if the module for |address| is found. |path| will contain
46// the path to the module. Note that |path| may not be absolute.
[email protected]aaa6df42013-02-17 19:36:0347bool GetModulePathForAddress(base::FilePath* path,
[email protected]699c26c2011-05-26 16:19:0148 const void* address) WARN_UNUSED_RESULT;
49
[email protected]aaa6df42013-02-17 19:36:0350bool GetModulePathForAddress(base::FilePath* path, const void* address) {
[email protected]699c26c2011-05-26 16:19:0151 Dl_info info;
52 if (dladdr(address, &info) == 0)
53 return false;
[email protected]aaa6df42013-02-17 19:36:0354 *path = base::FilePath(info.dli_fname);
[email protected]699c26c2011-05-26 16:19:0155 return true;
56}
57
[email protected]a6849212010-08-24 21:32:3558} // namespace
59
[email protected]5af2edb92008-08-08 20:16:0860namespace base {
61
[email protected]aaa6df42013-02-17 19:36:0362bool PathProviderMac(int key, base::FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0863 switch (key) {
64 case base::FILE_EXE:
[email protected]fdce4782011-11-29 20:06:1865 GetNSExecutablePath(result);
66 return true;
[email protected]699c26c2011-05-26 16:19:0167 case base::FILE_MODULE:
68 return GetModulePathForAddress(result,
69 reinterpret_cast<const void*>(&base::PathProviderMac));
[email protected]8495af32012-08-01 00:29:1570 case base::DIR_APP_DATA: {
71 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
72 result);
73#if defined(OS_IOS)
74 // On IOS, this directory does not exist unless it is created explicitly.
[email protected]7567484142013-07-11 17:36:0775 if (success && !base::PathExists(*result))
[email protected]426d1c92013-12-03 20:08:5476 success = base::CreateDirectory(*result);
[email protected]8495af32012-08-01 00:29:1577#endif // defined(OS_IOS)
78 return success;
79 }
[email protected]dea1d7d2012-09-20 16:24:5280 case base::DIR_SOURCE_ROOT:
[email protected]7fac8882010-11-15 19:52:5281 // Go through PathService to catch overrides.
[email protected]b266ffea2011-04-12 06:07:2582 if (!PathService::Get(base::FILE_EXE, result))
83 return false;
84
85 // Start with the executable's directory.
86 *result = result->DirName();
[email protected]da19703b2012-07-17 14:15:3487
88#if !defined(OS_IOS)
[email protected]b266ffea2011-04-12 06:07:2589 if (base::mac::AmIBundled()) {
90 // The bundled app executables (Chromium, TestShell, etc) live five
91 // levels down, eg:
92 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
93 *result = result->DirName().DirName().DirName().DirName().DirName();
94 } else {
95 // Unit tests execute two levels deep from the source root, eg:
96 // src/xcodebuild/{Debug|Release}/base_unittests
97 *result = result->DirName().DirName();
[email protected]d480bc82009-04-22 18:35:1098 }
[email protected]da19703b2012-07-17 14:15:3499#endif
[email protected]e2ac70002008-12-09 14:58:13100 return true;
[email protected]dea1d7d2012-09-20 16:24:52101 case base::DIR_USER_DESKTOP:
[email protected]ce576fe22012-09-25 18:16:25102#if defined(OS_IOS)
103 // iOS does not have desktop directories.
104 NOTIMPLEMENTED();
105 return false;
106#else
[email protected]dea1d7d2012-09-20 16:24:52107 return base::mac::GetUserDirectory(NSDesktopDirectory, result);
[email protected]ce576fe22012-09-25 18:16:25108#endif
[email protected]dea1d7d2012-09-20 16:24:52109 case base::DIR_CACHE:
110 return base::mac::GetUserDirectory(NSCachesDirectory, result);
[email protected]5af2edb92008-08-08 20:16:08111 default:
112 return false;
113 }
[email protected]5af2edb92008-08-08 20:16:08114}
115
116} // namespace base