blob: 12108341d69bf3dc6ba2210c0bd51bbf3e1bd6da [file] [log] [blame]
[email protected]49df6022008-08-27 19:03:431// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]880a6d5e2010-11-16 22:25:477#import <Foundation/Foundation.h>
[email protected]b88a291b2009-10-15 01:22:518#include <mach-o/dyld.h>
[email protected]5af2edb92008-08-08 20:16:089
[email protected]a6849212010-08-24 21:32:3510#include "base/compiler_specific.h"
[email protected]3bd807452008-11-19 16:55:4311#include "base/file_path.h"
[email protected]37088fef2008-08-15 17:32:1012#include "base/file_util.h"
[email protected]5af2edb92008-08-08 20:16:0813#include "base/logging.h"
[email protected]0378bf42011-01-01 18:20:1414#include "base/mac/mac_util.h"
[email protected]37088fef2008-08-15 17:32:1015#include "base/path_service.h"
[email protected]5af2edb92008-08-08 20:16:0816#include "base/string_util.h"
17
[email protected]a6849212010-08-24 21:32:3518namespace {
19
20bool GetNSExecutablePath(FilePath* path) WARN_UNUSED_RESULT;
21
22bool GetNSExecutablePath(FilePath* path) {
23 DCHECK(path);
24 // Executable path can have relative references ("..") depending on
25 // how the app was launched.
26 uint32_t executable_length = 0;
27 _NSGetExecutablePath(NULL, &executable_length);
28 DCHECK_GE(executable_length, 1u);
29 std::string executable_path;
30 char* executable_path_c = WriteInto(&executable_path, executable_length);
31 int rv = _NSGetExecutablePath(executable_path_c, &executable_length);
32 DCHECK_EQ(rv, 0);
33 DCHECK(!executable_path.empty());
34 if ((rv != 0) || (executable_path.empty()))
35 return false;
36 *path = FilePath(executable_path);
37 return true;
38}
39
40} // namespace
41
[email protected]5af2edb92008-08-08 20:16:0842namespace base {
43
[email protected]4792a262008-11-19 16:50:0344bool PathProviderMac(int key, FilePath* result) {
[email protected]5af2edb92008-08-08 20:16:0845 switch (key) {
46 case base::FILE_EXE:
47 case base::FILE_MODULE: {
[email protected]a6849212010-08-24 21:32:3548 return GetNSExecutablePath(result);
[email protected]5af2edb92008-08-08 20:16:0849 }
[email protected]b411da32010-11-24 02:23:1550 case base::DIR_CACHE:
[email protected]0378bf42011-01-01 18:20:1451 return base::mac::GetUserDirectory(NSCachesDirectory, result);
[email protected]405a64b2009-09-16 21:03:4452 case base::DIR_APP_DATA:
[email protected]0378bf42011-01-01 18:20:1453 return base::mac::GetUserDirectory(NSApplicationSupportDirectory, result);
[email protected]e2ac70002008-12-09 14:58:1354 case base::DIR_SOURCE_ROOT: {
[email protected]7fac8882010-11-15 19:52:5255 // Go through PathService to catch overrides.
56 if (PathService::Get(base::FILE_EXE, result)) {
[email protected]a6849212010-08-24 21:32:3557 // Start with the executable's directory.
58 *result = result->DirName();
[email protected]0378bf42011-01-01 18:20:1459 if (base::mac::AmIBundled()) {
[email protected]a6849212010-08-24 21:32:3560 // The bundled app executables (Chromium, TestShell, etc) live five
61 // levels down, eg:
62 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
63 *result = result->DirName().DirName().DirName().DirName().DirName();
64 } else {
65 // Unit tests execute two levels deep from the source root, eg:
66 // src/xcodebuild/{Debug|Release}/base_unittests
67 *result = result->DirName().DirName();
68 }
[email protected]d480bc82009-04-22 18:35:1069 }
[email protected]e2ac70002008-12-09 14:58:1370 return true;
71 }
[email protected]5af2edb92008-08-08 20:16:0872 default:
73 return false;
74 }
[email protected]5af2edb92008-08-08 20:16:0875}
76
77} // namespace base