1// Copyright (c) 2012 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. 4 5// On Mac, shortcuts can't have command-line arguments. Instead, produce small 6// app bundles which locate the Chromium framework and load it, passing the 7// appropriate data. This is the code for such an app bundle. It should be kept 8// minimal and do as little work as possible (with as much work done on 9// framework side as possible). 10 11#include <dlfcn.h> 12 13#include <CoreFoundation/CoreFoundation.h> 14#import <Foundation/Foundation.h> 15 16#include "base/command_line.h" 17#include "base/file_util.h" 18#include "base/files/file_path.h" 19#include "base/logging.h" 20#include "base/mac/foundation_util.h" 21#include "base/mac/launch_services_util.h" 22#include "base/mac/scoped_nsautorelease_pool.h" 23#include "base/process/launch.h" 24#include "base/strings/sys_string_conversions.h" 25#include "chrome/common/chrome_switches.h" 26#import "chrome/common/mac/app_mode_chrome_locator.h" 27#include "chrome/common/mac/app_mode_common.h" 28 29namespace { 30 31typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*); 32 33int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) { 34 using base::SysNSStringToUTF8; 35 using base::SysNSStringToUTF16; 36 using base::mac::CFToNSCast; 37 using base::mac::CFCastStrict; 38 using base::mac::NSToCFCast; 39 40 base::mac::ScopedNSAutoreleasePool scoped_pool; 41 42 // Get the current main bundle, i.e., that of the app loader that's running. 43 NSBundle* app_bundle = [NSBundle mainBundle]; 44 CHECK(app_bundle) << "couldn't get loader bundle"; 45 46 // ** 1: Get path to outer Chrome bundle. 47 // Get the bundle ID of the browser that created this app bundle. 48 NSString* cr_bundle_id = base::mac::ObjCCast<NSString>( 49 [app_bundle objectForInfoDictionaryKey:app_mode::kBrowserBundleIDKey]); 50 CHECK(cr_bundle_id) << "couldn't get browser bundle ID"; 51 52 // First check if Chrome exists at the last known location. 53 base::FilePath cr_bundle_path; 54 NSString* cr_bundle_path_ns = 55 [CFToNSCast(CFCastStrict<CFStringRef>(CFPreferencesCopyAppValue( 56 NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey), 57 NSToCFCast(cr_bundle_id)))) autorelease]; 58 cr_bundle_path = base::mac::NSStringToFilePath(cr_bundle_path_ns); 59 bool found_bundle = 60 !cr_bundle_path.empty() && base::DirectoryExists(cr_bundle_path); 61 62 if (!found_bundle) { 63 // If no such bundle path exists, try to search by bundle ID. 64 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) { 65 // TODO(jeremy): Display UI to allow user to manually locate the Chrome 66 // bundle. 67 LOG(FATAL) << "Failed to locate bundle by identifier"; 68 } 69 } 70 71 // ** 2: Read information from the Chrome bundle. 72 base::FilePath executable_path; 73 base::string16 raw_version_str; 74 base::FilePath version_path; 75 base::FilePath framework_shlib_path; 76 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, 77 &executable_path, 78 &raw_version_str, 79 &version_path, 80 &framework_shlib_path)) { 81 LOG(FATAL) << "Couldn't ready Chrome bundle info"; 82 } 83 base::FilePath app_mode_bundle_path = 84 base::mac::NSStringToFilePath([app_bundle bundlePath]); 85 86 // ** 3: Fill in ChromeAppModeInfo. 87 info->chrome_outer_bundle_path = cr_bundle_path; 88 info->chrome_versioned_path = version_path; 89 info->app_mode_bundle_path = app_mode_bundle_path; 90 91 // Read information about the this app shortcut from the Info.plist. 92 // Don't check for null-ness on optional items. 93 NSDictionary* info_plist = [app_bundle infoDictionary]; 94 CHECK(info_plist) << "couldn't get loader Info.plist"; 95 96 info->app_mode_id = SysNSStringToUTF8( 97 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); 98 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID"; 99 100 info->app_mode_name = SysNSStringToUTF16( 101 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); 102 103 info->app_mode_url = SysNSStringToUTF8( 104 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); 105 106 info->user_data_dir = base::mac::NSStringToFilePath( 107 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]); 108 109 info->profile_dir = base::mac::NSStringToFilePath( 110 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]); 111 112 // Open the framework. 113 StartFun ChromeAppModeStart = NULL; 114 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY); 115 if (cr_dylib) { 116 // Find the entry point. 117 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); 118 if (!ChromeAppModeStart) 119 LOG(ERROR) << "Couldn't get entry point: " << dlerror(); 120 } else { 121 LOG(ERROR) << "Couldn't load framework: " << dlerror(); 122 } 123 124 if (ChromeAppModeStart) 125 return ChromeAppModeStart(info); 126 127 LOG(ERROR) << "Loading Chrome failed, launching Chrome with command line"; 128 CommandLine command_line(executable_path); 129 // The user_data_dir from the plist is actually the app data dir. 130 command_line.AppendSwitchPath( 131 switches::kUserDataDir, 132 info->user_data_dir.DirName().DirName().DirName()); 133 if (CommandLine::ForCurrentProcess()->HasSwitch( 134 app_mode::kLaunchedByChromeProcessId) || 135 info->app_mode_id == app_mode::kAppListModeId) { 136 // Pass --app-shim-error to have Chrome rebuild this shim. 137 // If Chrome has rebuilt this shim once already, then rebuilding doesn't fix 138 // the problem, so don't try again. 139 if (!CommandLine::ForCurrentProcess()->HasSwitch( 140 app_mode::kLaunchedAfterRebuild)) { 141 command_line.AppendSwitchPath(app_mode::kAppShimError, 142 app_mode_bundle_path); 143 } 144 } else { 145 // If the shim was launched directly (instead of by Chrome), first ask 146 // Chrome to launch the app. Chrome will launch the shim again, the same 147 // error will occur and be handled above. This approach allows the app to be 148 // started without blocking on fixing the shim and guarantees that the 149 // profile is loaded when Chrome receives --app-shim-error. 150 command_line.AppendSwitchPath(switches::kProfileDirectory, 151 info->profile_dir); 152 command_line.AppendSwitchASCII(switches::kAppId, info->app_mode_id); 153 } 154 // Launch the executable directly since base::mac::OpenApplicationWithPath 155 // uses LSOpenApplication which doesn't pass command line arguments if the 156 // application is already running. 157 if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) { 158 LOG(ERROR) << "Could not launch Chrome: " 159 << command_line.GetCommandLineString(); 160 return 1; 161 } 162 163 return 0; 164} 165 166} // namespace 167 168__attribute__((visibility("default"))) 169int main(int argc, char** argv) { 170 CommandLine::Init(argc, argv); 171 app_mode::ChromeAppModeInfo info; 172 173 // Hard coded info parameters. 174 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion; 175 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion; 176 info.argc = argc; 177 info.argv = argv; 178 179 // Exit instead of returning to avoid the the removal of |main()| from stack 180 // backtraces under tail call optimization. 181 exit(LoadFrameworkAndStart(&info)); 182} 183