1// Copyright 2017 The Chromium Embedded Framework Authors. Portions copyright 2// 2011 The Chromium Authors. All rights reserved. Use of this source code is 3// governed by a BSD-style license that can be found in the LICENSE file. 4 5#include "libcef/common/util_mac.h" 6 7#include "libcef/common/cef_switches.h" 8 9#include "base/base_paths.h" 10#include "base/command_line.h" 11#include "base/files/file_path.h" 12#include "base/logging.h" 13#include "base/mac/bundle_locations.h" 14#include "base/mac/foundation_util.h" 15#include "base/path_service.h" 16#include "base/strings/sys_string_conversions.h" 17#include "content/public/common/content_paths.h" 18#include "content/public/common/content_switches.h" 19 20namespace util_mac { 21 22namespace { 23 24// Returns the path to the Frameworks directory inside the top-level app bundle. 25base::FilePath GetFrameworksPath() { 26 base::FilePath bundle_path = GetMainBundlePath(); 27 if (bundle_path.empty()) 28 return base::FilePath(); 29 30 return bundle_path.Append(FILE_PATH_LITERAL("Contents")) 31 .Append(FILE_PATH_LITERAL("Frameworks")); 32} 33 34void OverrideFrameworkBundlePath() { 35 base::FilePath framework_path = GetFrameworkDirectory(); 36 DCHECK(!framework_path.empty()); 37 38 base::mac::SetOverrideFrameworkBundlePath(framework_path); 39} 40 41void OverrideOuterBundlePath() { 42 base::FilePath bundle_path = GetMainBundlePath(); 43 DCHECK(!bundle_path.empty()); 44 45 base::mac::SetOverrideOuterBundlePath(bundle_path); 46} 47 48void OverrideBaseBundleID() { 49 std::string bundle_id = GetMainBundleID(); 50 DCHECK(!bundle_id.empty()); 51 52 base::mac::SetBaseBundleID(bundle_id.c_str()); 53} 54 55void OverrideChildProcessPath() { 56 base::FilePath child_process_path = 57 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( 58 switches::kBrowserSubprocessPath); 59 60 if (child_process_path.empty()) { 61 child_process_path = util_mac::GetChildProcessPath(); 62 DCHECK(!child_process_path.empty()); 63 } 64 65 // Used by ChildProcessHost::GetChildPath and PlatformCrashpadInitialization. 66 base::PathService::Override(content::CHILD_PROCESS_EXE, child_process_path); 67} 68 69} // namespace 70 71bool GetLocalLibraryDirectory(base::FilePath* result) { 72 return base::mac::GetLocalDirectory(NSLibraryDirectory, result); 73} 74 75base::FilePath GetFrameworkDirectory() { 76 base::FilePath frameworks_path = 77 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( 78 switches::kFrameworkDirPath); 79 if (!frameworks_path.empty()) 80 return frameworks_path; 81 82 frameworks_path = GetFrameworksPath(); 83 if (frameworks_path.empty()) 84 return base::FilePath(); 85 86 return frameworks_path.Append( 87 FILE_PATH_LITERAL("Chromium Embedded Framework.framework")); 88} 89 90base::FilePath GetFrameworkResourcesDirectory() { 91 base::FilePath frameworks_path = GetFrameworkDirectory(); 92 if (frameworks_path.empty()) 93 return base::FilePath(); 94 95 return frameworks_path.Append(FILE_PATH_LITERAL("Resources")); 96} 97 98base::FilePath GetMainProcessPath() { 99 base::FilePath path; 100 base::PathService::Get(base::FILE_EXE, &path); 101 DCHECK(!path.empty()); 102 return path; 103} 104 105base::FilePath GetMainBundlePath() { 106 base::FilePath main_bundle_path = 107 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( 108 switches::kMainBundlePath); 109 if (!main_bundle_path.empty()) 110 return main_bundle_path; 111 112 return base::mac::GetAppBundlePath(GetMainProcessPath()); 113} 114 115std::string GetMainBundleID() { 116 NSBundle* bundle = base::mac::OuterBundle(); 117 return base::SysNSStringToUTF8([bundle bundleIdentifier]); 118} 119 120base::FilePath GetMainResourcesDirectory() { 121 base::FilePath bundle_path = GetMainBundlePath(); 122 if (bundle_path.empty()) 123 return base::FilePath(); 124 125 return bundle_path.Append(FILE_PATH_LITERAL("Contents")) 126 .Append(FILE_PATH_LITERAL("Resources")); 127} 128 129base::FilePath GetChildProcessPath() { 130 base::FilePath frameworks_path = GetFrameworksPath(); 131 if (frameworks_path.empty()) 132 return base::FilePath(); 133 134 std::string exe_name = GetMainProcessPath().BaseName().value(); 135 return frameworks_path.Append(FILE_PATH_LITERAL(exe_name + " Helper.app")) 136 .Append(FILE_PATH_LITERAL("Contents")) 137 .Append(FILE_PATH_LITERAL("MacOS")) 138 .Append(FILE_PATH_LITERAL(exe_name + " Helper")); 139} 140 141void PreSandboxStartup() { 142 OverrideChildProcessPath(); 143} 144 145void BasicStartupComplete() { 146 OverrideFrameworkBundlePath(); 147 OverrideOuterBundlePath(); 148 OverrideBaseBundleID(); 149} 150 151} // namespace util_mac 152