blob: 630f742a9ffbd16ac5e3779b1a99065714227397 [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]3bd807452008-11-19 16:55:4314#include "base/file_path.h"
[email protected]37088fef2008-08-15 17:32:1015#include "base/file_util.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]5af2edb92008-08-08 20:16:0819#include "base/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]fdce4782011-11-29 20:06:1824void GetNSExecutablePath(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]a6849212010-08-24 21:32:3535 *path = FilePath(executable_path);
[email protected]a6849212010-08-24 21:32:3536}
37
[email protected]699c26c2011-05-26 16:19:0138// Returns true if the module for |address| is found. |path| will contain
39// the path to the module. Note that |path| may not be absolute.
40bool GetModulePathForAddress(FilePath* path,
41 const void* address) WARN_UNUSED_RESULT;
42
43bool GetModulePathForAddress(FilePath* path, const void* address) {
44 Dl_info info;
45 if (dladdr(address, &info) == 0)
46 return false;
47 *path = FilePath(info.dli_fname);
48 return true;
49}
50
[email protected]a6849212010-08-24 21:32:3551} // namespace
52
[email protected]5af2edb92008-08-08 20:16:0853namespace base {
54
[email protected]4792a262008-11-19 16:50:0355bool PathProviderMac(int key, FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0856 switch (key) {
57 case base::FILE_EXE:
[email protected]fdce4782011-11-29 20:06:1858 GetNSExecutablePath(result);
59 return true;
[email protected]699c26c2011-05-26 16:19:0160 case base::FILE_MODULE:
61 return GetModulePathForAddress(result,
62 reinterpret_cast<const void*>(&base::PathProviderMac));
[email protected]8495af32012-08-01 00:29:1563 case base::DIR_APP_DATA: {
64 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory,
65 result);
66#if defined(OS_IOS)
67 // On IOS, this directory does not exist unless it is created explicitly.
68 if (success && !file_util::PathExists(*result))
69 success = file_util::CreateDirectory(*result);
70#endif // defined(OS_IOS)
71 return success;
72 }
[email protected]dea1d7d2012-09-20 16:24:5273 case base::DIR_SOURCE_ROOT:
[email protected]7fac8882010-11-15 19:52:5274 // Go through PathService to catch overrides.
[email protected]b266ffea2011-04-12 06:07:2575 if (!PathService::Get(base::FILE_EXE, result))
76 return false;
77
78 // Start with the executable's directory.
79 *result = result->DirName();
[email protected]da19703b2012-07-17 14:15:3480
81#if !defined(OS_IOS)
[email protected]b266ffea2011-04-12 06:07:2582 if (base::mac::AmIBundled()) {
83 // The bundled app executables (Chromium, TestShell, etc) live five
84 // levels down, eg:
85 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
86 *result = result->DirName().DirName().DirName().DirName().DirName();
87 } else {
88 // Unit tests execute two levels deep from the source root, eg:
89 // src/xcodebuild/{Debug|Release}/base_unittests
90 *result = result->DirName().DirName();
[email protected]d480bc82009-04-22 18:35:1091 }
[email protected]da19703b2012-07-17 14:15:3492#endif
[email protected]e2ac70002008-12-09 14:58:1393 return true;
[email protected]dea1d7d2012-09-20 16:24:5294 case base::DIR_USER_DESKTOP:
[email protected]ce576fe22012-09-25 18:16:2595#if defined(OS_IOS)
96 // iOS does not have desktop directories.
97 NOTIMPLEMENTED();
98 return false;
99#else
[email protected]dea1d7d2012-09-20 16:24:52100 return base::mac::GetUserDirectory(NSDesktopDirectory, result);
[email protected]ce576fe22012-09-25 18:16:25101#endif
[email protected]dea1d7d2012-09-20 16:24:52102 case base::DIR_CACHE:
103 return base::mac::GetUserDirectory(NSCachesDirectory, result);
104 case base::DIR_HOME:
[email protected]92e44ae0a2012-07-23 08:22:44105 *result = base::mac::NSStringToFilePath(NSHomeDirectory());
106 return true;
[email protected]5af2edb92008-08-08 20:16:08107 default:
108 return false;
109 }
[email protected]5af2edb92008-08-08 20:16:08110}
111
112} // namespace base