Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/app/app_mode_loader_mac.mm

Issue 501303002: [Mac] Make app shims load the same framework version as the running Chrome process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « apps/app_shim/app_shim_host_manager_mac.mm ('k') | chrome/browser/apps/shortcut_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #import <Cocoa/Cocoa.h>
13 #include <CoreFoundation/CoreFoundation.h> 14 #include <CoreFoundation/CoreFoundation.h>
14 #import <Foundation/Foundation.h> 15 #import <Foundation/Foundation.h>
15 16
16 #include "base/command_line.h" 17 #include "base/command_line.h"
17 #include "base/file_util.h" 18 #include "base/file_util.h"
18 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
20 #include "base/mac/foundation_util.h" 21 #include "base/mac/foundation_util.h"
21 #include "base/mac/launch_services_util.h" 22 #include "base/mac/launch_services_util.h"
22 #include "base/mac/scoped_nsautorelease_pool.h" 23 #include "base/mac/scoped_nsautorelease_pool.h"
23 #include "base/process/launch.h" 24 #include "base/process/launch.h"
25 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/sys_string_conversions.h" 26 #include "base/strings/sys_string_conversions.h"
27 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
26 #import "chrome/common/mac/app_mode_chrome_locator.h" 29 #import "chrome/common/mac/app_mode_chrome_locator.h"
27 #include "chrome/common/mac/app_mode_common.h" 30 #include "chrome/common/mac/app_mode_common.h"
28 31
29 namespace { 32 namespace {
30 33
31 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*); 34 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*);
32 35
33 int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) { 36 int LoadFrameworkAndStart(app_mode::ChromeAppModeInfo* info) {
34 using base::SysNSStringToUTF8; 37 using base::SysNSStringToUTF8;
(...skipping 26 matching lines...) Expand all
61 64
62 if (!found_bundle) { 65 if (!found_bundle) {
63 // If no such bundle path exists, try to search by bundle ID. 66 // If no such bundle path exists, try to search by bundle ID.
64 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) { 67 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) {
65 // TODO(jeremy): Display UI to allow user to manually locate the Chrome 68 // TODO(jeremy): Display UI to allow user to manually locate the Chrome
66 // bundle. 69 // bundle.
67 LOG(FATAL) << "Failed to locate bundle by identifier"; 70 LOG(FATAL) << "Failed to locate bundle by identifier";
68 } 71 }
69 } 72 }
70 73
71 // ** 2: Read information from the Chrome bundle. 74 // ** 2: Read the running Chrome version.
75 // The user_data_dir for shims actually contains the app_data_path.
76 // I.e. <user_data_dir>/<profile_dir>/Web Applications/_crx_extensionid/
77 base::FilePath app_data_dir = base::mac::NSStringToFilePath([app_bundle
78 objectForInfoDictionaryKey:app_mode::kCrAppModeUserDataDirKey]);
79 base::FilePath user_data_dir = app_data_dir.DirName().DirName().DirName();
80 CHECK(!user_data_dir.empty());
81
82 // If the version file does not exist, |cr_version_str| will be empty and
83 // app_mode::GetChromeBundleInfo will default to the latest version.
84 base::FilePath cr_version_str;
85 base::ReadSymbolicLink(
86 user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName),
87 &cr_version_str);
88
89 // If the version file does exist, it may have been left by a crashed Chrome
90 // process. Ensure the process is still running.
91 if (!cr_version_str.empty()) {
92 NSArray* existing_chrome = [NSRunningApplication
93 runningApplicationsWithBundleIdentifier:cr_bundle_id];
94 if ([existing_chrome count] == 0)
95 cr_version_str.clear();
96 }
tapted 2014/08/27 03:51:16 // TODO(): Also check that the running Chrome has
jackhou1 2014/08/28 01:04:56 chrome_main_app_mode_mac checks for a running Chro
97
98 // ** 3: Read information from the Chrome bundle.
72 base::FilePath executable_path; 99 base::FilePath executable_path;
73 base::string16 raw_version_str;
74 base::FilePath version_path; 100 base::FilePath version_path;
75 base::FilePath framework_shlib_path; 101 base::FilePath framework_shlib_path;
76 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, 102 if (!app_mode::GetChromeBundleInfo(cr_bundle_path,
103 cr_version_str.value(),
77 &executable_path, 104 &executable_path,
78 &raw_version_str,
79 &version_path, 105 &version_path,
80 &framework_shlib_path)) { 106 &framework_shlib_path)) {
81 LOG(FATAL) << "Couldn't ready Chrome bundle info"; 107 LOG(FATAL) << "Couldn't ready Chrome bundle info";
82 } 108 }
83 base::FilePath app_mode_bundle_path = 109 base::FilePath app_mode_bundle_path =
84 base::mac::NSStringToFilePath([app_bundle bundlePath]); 110 base::mac::NSStringToFilePath([app_bundle bundlePath]);
85 111
86 // ** 3: Fill in ChromeAppModeInfo. 112 // ** 4: Fill in ChromeAppModeInfo.
87 info->chrome_outer_bundle_path = cr_bundle_path; 113 info->chrome_outer_bundle_path = cr_bundle_path;
88 info->chrome_versioned_path = version_path; 114 info->chrome_versioned_path = version_path;
89 info->app_mode_bundle_path = app_mode_bundle_path; 115 info->app_mode_bundle_path = app_mode_bundle_path;
90 116
91 // Read information about the this app shortcut from the Info.plist. 117 // Read information about the this app shortcut from the Info.plist.
92 // Don't check for null-ness on optional items. 118 // Don't check for null-ness on optional items.
93 NSDictionary* info_plist = [app_bundle infoDictionary]; 119 NSDictionary* info_plist = [app_bundle infoDictionary];
94 CHECK(info_plist) << "couldn't get loader Info.plist"; 120 CHECK(info_plist) << "couldn't get loader Info.plist";
95 121
96 info->app_mode_id = SysNSStringToUTF8( 122 info->app_mode_id = SysNSStringToUTF8(
97 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); 123 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]);
98 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID"; 124 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID";
99 125
100 info->app_mode_name = SysNSStringToUTF16( 126 info->app_mode_name = SysNSStringToUTF16(
101 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); 127 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]);
102 128
103 info->app_mode_url = SysNSStringToUTF8( 129 info->app_mode_url = SysNSStringToUTF8(
104 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); 130 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]);
105 131
106 info->user_data_dir = base::mac::NSStringToFilePath( 132 info->user_data_dir = base::mac::NSStringToFilePath(
107 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]); 133 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]);
108 134
109 info->profile_dir = base::mac::NSStringToFilePath( 135 info->profile_dir = base::mac::NSStringToFilePath(
110 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]); 136 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]);
111 137
112 // Open the framework. 138 // ** 5: Open the framework.
113 StartFun ChromeAppModeStart = NULL; 139 StartFun ChromeAppModeStart = NULL;
114 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY); 140 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY);
115 if (cr_dylib) { 141 if (cr_dylib) {
116 // Find the entry point. 142 // Find the entry point.
117 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); 143 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart");
118 if (!ChromeAppModeStart) 144 if (!ChromeAppModeStart)
119 LOG(ERROR) << "Couldn't get entry point: " << dlerror(); 145 LOG(ERROR) << "Couldn't get entry point: " << dlerror();
120 } else { 146 } else {
121 LOG(ERROR) << "Couldn't load framework: " << dlerror(); 147 LOG(ERROR) << "Couldn't load framework: " << dlerror();
122 } 148 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 // Hard coded info parameters. 199 // Hard coded info parameters.
174 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion; 200 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion;
175 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion; 201 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion;
176 info.argc = argc; 202 info.argc = argc;
177 info.argv = argv; 203 info.argv = argv;
178 204
179 // Exit instead of returning to avoid the the removal of |main()| from stack 205 // Exit instead of returning to avoid the the removal of |main()| from stack
180 // backtraces under tail call optimization. 206 // backtraces under tail call optimization.
181 exit(LoadFrameworkAndStart(&info)); 207 exit(LoadFrameworkAndStart(&info));
182 } 208 }
OLDNEW
« no previous file with comments | « apps/app_shim/app_shim_host_manager_mac.mm ('k') | chrome/browser/apps/shortcut_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698