| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // On Mac, shortcuts can't have command-line arguments. Instead, produce small | 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 | 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 | 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 | 8 // minimal and do as little work as possible (with as much work done on |
| 9 // framework side as possible). | 9 // framework side as possible). |
| 10 | 10 |
| 11 #include <dlfcn.h> | 11 #include <dlfcn.h> |
| 12 | 12 |
| 13 #include <CoreFoundation/CoreFoundation.h> | 13 #include <CoreFoundation/CoreFoundation.h> |
| 14 #import <Foundation/Foundation.h> | 14 #import <Foundation/Foundation.h> |
| 15 | 15 |
| 16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/file_util.h" | 17 #include "base/file_util.h" |
| 18 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/mac/foundation_util.h" | 20 #include "base/mac/foundation_util.h" |
| 21 #include "base/mac/launch_services_util.h" | 21 #include "base/mac/launch_services_util.h" |
| 22 #include "base/mac/scoped_nsautorelease_pool.h" | 22 #include "base/mac/scoped_nsautorelease_pool.h" |
| 23 #include "base/process/launch.h" | 23 #include "base/process/launch.h" |
| 24 #include "base/strings/sys_string_conversions.h" | 24 #include "base/strings/sys_string_conversions.h" |
| 25 #include "chrome/common/chrome_constants.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #import "chrome/common/mac/app_mode_chrome_locator.h" | 27 #import "chrome/common/mac/app_mode_chrome_locator.h" |
| 27 #include "chrome/common/mac/app_mode_common.h" | 28 #include "chrome/common/mac/app_mode_common.h" |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*); | 32 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*); |
| 32 | 33 |
| 33 int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) { | 34 int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) { |
| 34 using base::SysNSStringToUTF8; | 35 using base::SysNSStringToUTF8; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 61 | 62 |
| 62 if (!found_bundle) { | 63 if (!found_bundle) { |
| 63 // If no such bundle path exists, try to search by bundle ID. | 64 // If no such bundle path exists, try to search by bundle ID. |
| 64 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) { | 65 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) { |
| 65 // TODO(jeremy): Display UI to allow user to manually locate the Chrome | 66 // TODO(jeremy): Display UI to allow user to manually locate the Chrome |
| 66 // bundle. | 67 // bundle. |
| 67 LOG(FATAL) << "Failed to locate bundle by identifier"; | 68 LOG(FATAL) << "Failed to locate bundle by identifier"; |
| 68 } | 69 } |
| 69 } | 70 } |
| 70 | 71 |
| 71 // ** 2: Read information from the Chrome bundle. | 72 // ** 2: Read the running Chrome version. |
| 73 // The user_data_dir for shims actually contains the app_data_path. |
| 74 // I.e. <user_data_dir>/<profile_dir>/Web Applications/_crx_extensionid/ |
| 75 base::FilePath user_data_dir = |
| 76 info->user_data_dir.DirName().DirName().DirName(); |
| 77 CHECK(!user_data_dir.empty()); |
| 78 |
| 79 // If this file does not exist, |cr_version_str| will be empty and |
| 80 // app_mode::GetChromeBundleInfo will default to the latest version. |
| 81 base::FilePath cr_version_str; |
| 82 base::ReadSymbolicLink( |
| 83 user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName), |
| 84 &cr_version_str); |
| 85 |
| 86 // ** 3: Read information from the Chrome bundle. |
| 72 base::FilePath executable_path; | 87 base::FilePath executable_path; |
| 73 base::string16 raw_version_str; | |
| 74 base::FilePath version_path; | 88 base::FilePath version_path; |
| 75 base::FilePath framework_shlib_path; | 89 base::FilePath framework_shlib_path; |
| 76 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, | 90 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, |
| 91 cr_version_str.value(), |
| 77 &executable_path, | 92 &executable_path, |
| 78 &raw_version_str, | |
| 79 &version_path, | 93 &version_path, |
| 80 &framework_shlib_path)) { | 94 &framework_shlib_path)) { |
| 81 LOG(FATAL) << "Couldn't ready Chrome bundle info"; | 95 LOG(FATAL) << "Couldn't ready Chrome bundle info"; |
| 82 } | 96 } |
| 83 base::FilePath app_mode_bundle_path = | 97 base::FilePath app_mode_bundle_path = |
| 84 base::mac::NSStringToFilePath([app_bundle bundlePath]); | 98 base::mac::NSStringToFilePath([app_bundle bundlePath]); |
| 85 | 99 |
| 86 // ** 3: Fill in ChromeAppModeInfo. | 100 // ** 4: Fill in ChromeAppModeInfo. |
| 87 info->chrome_outer_bundle_path = cr_bundle_path; | 101 info->chrome_outer_bundle_path = cr_bundle_path; |
| 88 info->chrome_versioned_path = version_path; | 102 info->chrome_versioned_path = version_path; |
| 89 info->app_mode_bundle_path = app_mode_bundle_path; | 103 info->app_mode_bundle_path = app_mode_bundle_path; |
| 90 | 104 |
| 91 // Read information about the this app shortcut from the Info.plist. | 105 // Read information about the this app shortcut from the Info.plist. |
| 92 // Don't check for null-ness on optional items. | 106 // Don't check for null-ness on optional items. |
| 93 NSDictionary* info_plist = [app_bundle infoDictionary]; | 107 NSDictionary* info_plist = [app_bundle infoDictionary]; |
| 94 CHECK(info_plist) << "couldn't get loader Info.plist"; | 108 CHECK(info_plist) << "couldn't get loader Info.plist"; |
| 95 | 109 |
| 96 info->app_mode_id = SysNSStringToUTF8( | 110 info->app_mode_id = SysNSStringToUTF8( |
| 97 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); | 111 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); |
| 98 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID"; | 112 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID"; |
| 99 | 113 |
| 100 info->app_mode_name = SysNSStringToUTF16( | 114 info->app_mode_name = SysNSStringToUTF16( |
| 101 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); | 115 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); |
| 102 | 116 |
| 103 info->app_mode_url = SysNSStringToUTF8( | 117 info->app_mode_url = SysNSStringToUTF8( |
| 104 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); | 118 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); |
| 105 | 119 |
| 106 info->user_data_dir = base::mac::NSStringToFilePath( | 120 info->user_data_dir = base::mac::NSStringToFilePath( |
| 107 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]); | 121 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]); |
| 108 | 122 |
| 109 info->profile_dir = base::mac::NSStringToFilePath( | 123 info->profile_dir = base::mac::NSStringToFilePath( |
| 110 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]); | 124 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]); |
| 111 | 125 |
| 112 // Open the framework. | 126 // ** 5: Open the framework. |
| 113 StartFun ChromeAppModeStart = NULL; | 127 StartFun ChromeAppModeStart = NULL; |
| 114 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY); | 128 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY); |
| 115 if (cr_dylib) { | 129 if (cr_dylib) { |
| 116 // Find the entry point. | 130 // Find the entry point. |
| 117 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); | 131 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); |
| 118 if (!ChromeAppModeStart) | 132 if (!ChromeAppModeStart) |
| 119 LOG(ERROR) << "Couldn't get entry point: " << dlerror(); | 133 LOG(ERROR) << "Couldn't get entry point: " << dlerror(); |
| 120 } else { | 134 } else { |
| 121 LOG(ERROR) << "Couldn't load framework: " << dlerror(); | 135 LOG(ERROR) << "Couldn't load framework: " << dlerror(); |
| 122 } | 136 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // Hard coded info parameters. | 187 // Hard coded info parameters. |
| 174 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion; | 188 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion; |
| 175 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion; | 189 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion; |
| 176 info.argc = argc; | 190 info.argc = argc; |
| 177 info.argv = argv; | 191 info.argv = argv; |
| 178 | 192 |
| 179 // Exit instead of returning to avoid the the removal of |main()| from stack | 193 // Exit instead of returning to avoid the the removal of |main()| from stack |
| 180 // backtraces under tail call optimization. | 194 // backtraces under tail call optimization. |
| 181 exit(LoadFrameworkAndStart(&info)); | 195 exit(LoadFrameworkAndStart(&info)); |
| 182 } | 196 } |
| OLD | NEW |