blob: 5d4461cb236c65b9ee9e3e3cb3c25adb5de16bf7 [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>
[email protected]5af2edb92008-08-08 20:16:0811
[email protected]dea1d7d2012-09-20 16:24:5212#include "base/base_paths.h"
[email protected]a6849212010-08-24 21:32:3513#include "base/compiler_specific.h"
[email protected]37088fef2008-08-15 17:32:1014#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]5af2edb92008-08-08 20:16:0816#include "base/logging.h"
[email protected]542cbb692011-06-14 19:03:1617#include "base/mac/foundation_util.h"
[email protected]37088fef2008-08-15 17:32:1018#include "base/path_service.h"
[email protected]251cd6e52013-06-11 13:36:3719#include "base/strings/string_util.h"
[email protected]dea1d7d2012-09-20 16:24:5220#include "build/build_config.h"
[email protected]5af2edb92008-08-08 20:16:0821
[email protected]a6849212010-08-24 21:32:3522namespace {
23
[email protected]aaa6df42013-02-17 19:36:0324void GetNSExecutablePath(base::FilePath* path) {
[email protected]a6849212010-08-24 21:32:3525 DCHECK(path);
26 // Executable path can have relative references ("..") depending on
27 // how the app was launched.
28 uint32_t executable_length = 0;
29 _NSGetExecutablePath(NULL, &executable_length);
[email protected]fdce4782011-11-29 20:06:1830 DCHECK_GT(executable_length, 1u);
[email protected]a6849212010-08-24 21:32:3531 std::string executable_path;
[email protected]fdce4782011-11-29 20:06:1832 int rv = _NSGetExecutablePath(WriteInto(&executable_path, executable_length),
33 &executable_length);
[email protected]a6849212010-08-24 21:32:3534 DCHECK_EQ(rv, 0);
[email protected]40fd7052013-08-06 13:30:2535
36 // _NSGetExecutablePath may return paths containing ./ or ../ which makes
37 // FilePath::DirName() work incorrectly, convert it to absolute path so that
38 // paths such as DIR_SOURCE_ROOT can work, since we expect absolute paths to
39 // be returned here.
40 *path = base::MakeAbsoluteFilePath(base::FilePath(executable_path));
[email protected]a6849212010-08-24 21:32:3541}
42
[email protected]699c26c2011-05-26 16:19:0143// Returns true if the module for |address| is found. |path| will contain
44// the path to the module. Note that |path| may not be absolute.
[email protected]aaa6df42013-02-17 19:36:0345bool GetModulePathForAddress(base::FilePath* path,
[email protected]699c26c2011-05-26 16:19:0146 const void* address) WARN_UNUSED_RESULT;
47
[email protected]aaa6df42013-02-17 19:36:0348bool GetModulePathForAddress(base::FilePath* path, const void* address) {
[email protected]699c26c2011-05-26 16:19:0149 Dl_info info;
50 if (dladdr(address, &info) == 0)
51 return false;
[email protected]aaa6df42013-02-17 19:36:0352 *path = base::FilePath(info.dli_fname);
[email protected]699c26c2011-05-26 16:19:0153 return true;
54}
55
[email protected]a6849212010-08-24 21:32:3556} // namespace
57
[email protected]5af2edb92008-08-08 20:16:0858namespace base {
59
[email protected]aaa6df42013-02-17 19:36:0360bool PathProviderMac(int key, base::FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0861 switch (key) {
62 case base::FILE_EXE:
[email protected]fdce4782011-11-29 20:06:1863 GetNSExecutablePath(result);
64 return true;
[email protected]699c26c2011-05-26 16:19:0165 case base::FILE_MODULE:
66 return GetModulePathForAddress(result,
67 reinterpret_cast<const void*>(&base::PathProviderMac));
[email protected]8495af32012-08-01 00:29:1568 case base::DIR_APP_DATA: {
69 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
70 result);
71#if defined(OS_IOS)
72 // On IOS, this directory does not exist unless it is created explicitly.
[email protected]7567484142013-07-11 17:36:0773 if (success && !base::PathExists(*result))
[email protected]8495af32012-08-01 00:29:1574 success = file_util::CreateDirectory(*result);
75#endif // defined(OS_IOS)
76 return success;
77 }
[email protected]dea1d7d2012-09-20 16:24:5278 case base::DIR_SOURCE_ROOT:
[email protected]7fac8882010-11-15 19:52:5279 // Go through PathService to catch overrides.
[email protected]b266ffea2011-04-12 06:07:2580 if (!PathService::Get(base::FILE_EXE, result))
81 return false;
82
83 // Start with the executable's directory.
84 *result = result->DirName();
[email protected]da19703b2012-07-17 14:15:3485
86#if !defined(OS_IOS)
[email protected]b266ffea2011-04-12 06:07:2587 if (base::mac::AmIBundled()) {
88 // The bundled app executables (Chromium, TestShell, etc) live five
89 // levels down, eg:
90 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
91 *result = result->DirName().DirName().DirName().DirName().DirName();
92 } else {
93 // Unit tests execute two levels deep from the source root, eg:
94 // src/xcodebuild/{Debug|Release}/base_unittests
95 *result = result->DirName().DirName();
[email protected]d480bc82009-04-22 18:35:1096 }
[email protected]da19703b2012-07-17 14:15:3497#endif
[email protected]e2ac70002008-12-09 14:58:1398 return true;
[email protected]dea1d7d2012-09-20 16:24:5299 case base::DIR_USER_DESKTOP:
[email protected]ce576fe22012-09-25 18:16:25100#if defined(OS_IOS)
101 // iOS does not have desktop directories.
102 NOTIMPLEMENTED();
103 return false;
104#else
[email protected]dea1d7d2012-09-20 16:24:52105 return base::mac::GetUserDirectory(NSDesktopDirectory, result);
[email protected]ce576fe22012-09-25 18:16:25106#endif
[email protected]dea1d7d2012-09-20 16:24:52107 case base::DIR_CACHE:
108 return base::mac::GetUserDirectory(NSCachesDirectory, result);
109 case base::DIR_HOME:
[email protected]92e44ae0a2012-07-23 08:22:44110 *result = base::mac::NSStringToFilePath(NSHomeDirectory());
111 return true;
[email protected]5af2edb92008-08-08 20:16:08112 default:
113 return false;
114 }
[email protected]5af2edb92008-08-08 20:16:08115}
116
117} // namespace base