1// Copyright 2012 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac 6// in base/path_service.cc. 7 8#include <dlfcn.h> 9#import <Foundation/Foundation.h> 10#include <mach-o/dyld.h> 11#include <stdint.h> 12 13#include "base/base_paths.h" 14#include "base/check_op.h" 15#include "base/files/file_path.h" 16#include "base/files/file_util.h" 17#include "base/mac/bundle_locations.h" 18#include "base/mac/foundation_util.h" 19#include "base/notreached.h" 20#include "base/path_service.h" 21#include "base/strings/string_util.h" 22#include "base/threading/thread_restrictions.h" 23#include "build/build_config.h" 24 25namespace { 26 27// Returns true if the module for |address| is found. |path| will contain 28// the path to the module. Note that |path| may not be absolute. 29[[nodiscard]] bool GetModulePathForAddress(base::FilePath* path, 30 const void* address); 31 32bool GetModulePathForAddress(base::FilePath* path, const void* address) { 33 Dl_info info; 34 if (dladdr(address, &info) == 0) 35 return false; 36 *path = base::FilePath(info.dli_fname); 37 return true; 38} 39 40} // namespace 41 42namespace base { 43 44void GetNSExecutablePath(base::FilePath* path) { 45 DCHECK(path); 46 // Executable path can have relative references ("..") depending on 47 // how the app was launched. 48 uint32_t executable_length = 0; 49 _NSGetExecutablePath(NULL, &executable_length); 50 DCHECK_GT(executable_length, 1u); 51 std::string executable_path; 52 int rv = _NSGetExecutablePath( 53 base::WriteInto(&executable_path, executable_length), 54 &executable_length); 55 DCHECK_EQ(rv, 0); 56 57 // _NSGetExecutablePath may return paths containing ./ or ../ which makes 58 // FilePath::DirName() work incorrectly, convert it to absolute path so that 59 // paths such as DIR_SRC_TEST_DATA_ROOT can work, since we expect absolute 60 // paths to be returned here. 61 // TODO(bauerb): http://crbug.com/259796, http://crbug.com/373477 62 base::ScopedAllowBlocking allow_blocking; 63 *path = base::MakeAbsoluteFilePath(base::FilePath(executable_path)); 64} 65 66bool PathProviderMac(int key, base::FilePath* result) { 67 switch (key) { 68 case base::FILE_EXE: 69 GetNSExecutablePath(result); 70 return true; 71 case base::FILE_MODULE: 72 return GetModulePathForAddress(result, 73 reinterpret_cast<const void*>(&base::PathProviderMac)); 74 case base::DIR_APP_DATA: { 75 bool success = base::mac::GetUserDirectory(NSApplicationSupportDirectory, 76 result); 77#if BUILDFLAG(IS_IOS) 78 // On IOS, this directory does not exist unless it is created explicitly. 79 if (success && !base::PathExists(*result)) 80 success = base::CreateDirectory(*result); 81#endif // BUILDFLAG(IS_IOS) 82 return success; 83 } 84 case base::DIR_SRC_TEST_DATA_ROOT: 85#if BUILDFLAG(IS_IOS) 86 // On iOS, there is no access to source root, however, the necessary 87 // resources are packaged into the test as assets. 88 return PathService::Get(base::DIR_ASSETS, result); 89#else 90 // Go through PathService to catch overrides. 91 if (!PathService::Get(base::FILE_EXE, result)) 92 return false; 93 94 // Start with the executable's directory. 95 *result = result->DirName(); 96 97 if (base::mac::AmIBundled()) { 98 // The bundled app executables (Chromium, TestShell, etc) live five 99 // levels down, eg: 100 // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium 101 *result = result->DirName().DirName().DirName().DirName().DirName(); 102 } else { 103 // Unit tests execute two levels deep from the source root, eg: 104 // src/xcodebuild/{Debug|Release}/base_unittests 105 *result = result->DirName().DirName(); 106 } 107 return true; 108#endif // BUILDFLAG(IS_IOS) 109 case base::DIR_USER_DESKTOP: 110#if BUILDFLAG(IS_IOS) 111 // iOS does not have desktop directories. 112 NOTIMPLEMENTED(); 113 return false; 114#else 115 return base::mac::GetUserDirectory(NSDesktopDirectory, result); 116#endif 117 case base::DIR_ASSETS: 118#if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_MACCATALYST) 119 // On iOS, the assets are located next to the module binary. 120 return PathService::Get(base::DIR_MODULE, result); 121#else 122 if (!base::mac::AmIBundled()) { 123 return PathService::Get(base::DIR_MODULE, result); 124 } 125#if BUILDFLAG(IS_IOS_MACCATALYST) 126 *result = base::mac::MainBundlePath() 127 .Append(FILE_PATH_LITERAL("Contents")) 128 .Append(FILE_PATH_LITERAL("Resources")); 129#else 130 *result = base::mac::FrameworkBundlePath().Append( 131 FILE_PATH_LITERAL("Resources")); 132#endif // BUILDFLAG(IS_IOS_MACCATALYST) 133 return true; 134#endif // BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_MACCATALYST) 135 case base::DIR_CACHE: 136 return base::mac::GetUserDirectory(NSCachesDirectory, result); 137 default: 138 return false; 139 } 140} 141 142} // namespace base 143