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.
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).
13 #include <CoreFoundation/CoreFoundation.h>
14 #import <Foundation/Foundation.h>
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/strings/sys_string_conversions.h"
24 #import "chrome/common/mac/app_mode_chrome_locator.h"
25 #include "chrome/common/mac/app_mode_common.h"
29 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*);
31 int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) {
32 using base::SysNSStringToUTF8;
33 using base::SysNSStringToUTF16;
34 using base::mac::CFToNSCast;
35 using base::mac::CFCastStrict;
36 using base::mac::NSToCFCast;
38 base::mac::ScopedNSAutoreleasePool scoped_pool;
40 // Get the current main bundle, i.e., that of the app loader that's running.
41 NSBundle* app_bundle = [NSBundle mainBundle];
42 CHECK(app_bundle) << "couldn't get loader bundle";
44 // ** 1: Get path to outer Chrome bundle.
45 // Get the bundle ID of the browser that created this app bundle.
46 NSString* cr_bundle_id = base::mac::ObjCCast<NSString>(
47 [app_bundle objectForInfoDictionaryKey:app_mode::kBrowserBundleIDKey]);
48 CHECK(cr_bundle_id) << "couldn't get browser bundle ID";
50 // First check if Chrome exists at the last known location.
51 base::FilePath cr_bundle_path;
52 NSString* cr_bundle_path_ns =
53 [CFToNSCast(CFCastStrict<CFStringRef>(CFPreferencesCopyAppValue(
54 NSToCFCast(app_mode::kLastRunAppBundlePathPrefsKey),
55 NSToCFCast(cr_bundle_id)))) autorelease];
56 cr_bundle_path = base::mac::NSStringToFilePath(cr_bundle_path_ns);
58 !cr_bundle_path.empty() && base::DirectoryExists(cr_bundle_path);
61 // If no such bundle path exists, try to search by bundle ID.
62 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) {
63 // TODO(jeremy): Display UI to allow user to manually locate the Chrome
65 LOG(FATAL) << "Failed to locate bundle by identifier";
69 // ** 2: Read information from the Chrome bundle.
70 base::string16 raw_version_str;
71 base::FilePath version_path;
72 base::FilePath framework_shlib_path;
73 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, &raw_version_str,
74 &version_path, &framework_shlib_path)) {
75 LOG(FATAL) << "Couldn't ready Chrome bundle info";
77 base::FilePath app_mode_bundle_path =
78 base::mac::NSStringToFilePath([app_bundle bundlePath]);
80 // ** 3: Fill in ChromeAppModeInfo.
81 info->chrome_outer_bundle_path = cr_bundle_path;
82 info->chrome_versioned_path = version_path;
83 info->app_mode_bundle_path = app_mode_bundle_path;
85 // Read information about the this app shortcut from the Info.plist.
86 // Don't check for null-ness on optional items.
87 NSDictionary* info_plist = [app_bundle infoDictionary];
88 CHECK(info_plist) << "couldn't get loader Info.plist";
90 info->app_mode_id = SysNSStringToUTF8(
91 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]);
92 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID";
94 info->app_mode_name = SysNSStringToUTF16(
95 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]);
97 info->app_mode_url = SysNSStringToUTF8(
98 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]);
100 info->user_data_dir = base::mac::NSStringToFilePath(
101 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]);
103 info->profile_dir = base::mac::NSStringToFilePath(
104 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]);
106 // Open the framework.
107 StartFun ChromeAppModeStart = NULL;
108 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY);
110 // Find the entry point.
111 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart");
112 if (!ChromeAppModeStart)
113 LOG(ERROR) << "Couldn't get entry point: " << dlerror();
115 LOG(ERROR) << "Couldn't load framework: " << dlerror();
118 if (ChromeAppModeStart) {
119 return ChromeAppModeStart(info);
121 LOG(ERROR) << "Loading Chrome failed, launching with command line.";
122 // Launch Chrome instead and have it update this app_mode_loader bundle.
123 CommandLine command_line(CommandLine::NO_PROGRAM);
124 command_line.AppendSwitchPath(app_mode::kAppShimError,
125 app_mode_bundle_path);
126 if (!base::mac::OpenApplicationWithPath(
127 cr_bundle_path, command_line, kLSLaunchDefaults, NULL)) {
128 LOG(ERROR) << "Could not launch Chrome from: " << cr_bundle_path.value();
138 __attribute__((visibility("default")))
139 int main(int argc, char** argv) {
140 app_mode::ChromeAppModeInfo info;
142 // Hard coded info parameters.
143 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion;
144 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion;
148 // Exit instead of returning to avoid the the removal of |main()| from stack
149 // backtraces under tail call optimization.
150 exit(LoadFrameworkAndStart(&info));