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

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: Sync and rebase 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 #include <CoreFoundation/CoreFoundation.h> 13 #import <Cocoa/Cocoa.h>
14 #import <Foundation/Foundation.h>
15 14
16 #include "base/command_line.h" 15 #include "base/command_line.h"
17 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
18 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
19 #include "base/logging.h" 18 #include "base/logging.h"
20 #include "base/mac/foundation_util.h" 19 #include "base/mac/foundation_util.h"
21 #include "base/mac/launch_services_util.h" 20 #include "base/mac/launch_services_util.h"
22 #include "base/mac/scoped_nsautorelease_pool.h" 21 #include "base/mac/scoped_nsautorelease_pool.h"
23 #include "base/process/launch.h" 22 #include "base/process/launch.h"
23 #include "base/strings/string_number_conversions.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
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 app_data_dir = base::mac::NSStringToFilePath([app_bundle
76 objectForInfoDictionaryKey:app_mode::kCrAppModeUserDataDirKey]);
77 base::FilePath user_data_dir = app_data_dir.DirName().DirName().DirName();
78 CHECK(!user_data_dir.empty());
79
80 // If the version file does not exist, |cr_version_str| will be empty and
81 // app_mode::GetChromeBundleInfo will default to the latest version.
82 base::FilePath cr_version_str;
83 base::ReadSymbolicLink(
84 user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName),
85 &cr_version_str);
86
87 // If the version file does exist, it may have been left by a crashed Chrome
88 // process. Ensure the process is still running.
89 if (!cr_version_str.empty()) {
90 NSArray* existing_chrome = [NSRunningApplication
91 runningApplicationsWithBundleIdentifier:cr_bundle_id];
92 if ([existing_chrome count] == 0)
93 cr_version_str.clear();
94 }
95
96 // ** 3: Read information from the Chrome bundle.
72 base::FilePath executable_path; 97 base::FilePath executable_path;
73 base::string16 raw_version_str;
74 base::FilePath version_path; 98 base::FilePath version_path;
75 base::FilePath framework_shlib_path; 99 base::FilePath framework_shlib_path;
76 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, 100 if (!app_mode::GetChromeBundleInfo(cr_bundle_path,
101 cr_version_str.value(),
77 &executable_path, 102 &executable_path,
78 &raw_version_str,
79 &version_path, 103 &version_path,
80 &framework_shlib_path)) { 104 &framework_shlib_path)) {
81 LOG(FATAL) << "Couldn't ready Chrome bundle info"; 105 LOG(FATAL) << "Couldn't ready Chrome bundle info";
82 } 106 }
83 base::FilePath app_mode_bundle_path = 107 base::FilePath app_mode_bundle_path =
84 base::mac::NSStringToFilePath([app_bundle bundlePath]); 108 base::mac::NSStringToFilePath([app_bundle bundlePath]);
85 109
86 // ** 3: Fill in ChromeAppModeInfo. 110 // ** 4: Fill in ChromeAppModeInfo.
87 info->chrome_outer_bundle_path = cr_bundle_path; 111 info->chrome_outer_bundle_path = cr_bundle_path;
88 info->chrome_versioned_path = version_path; 112 info->chrome_versioned_path = version_path;
89 info->app_mode_bundle_path = app_mode_bundle_path; 113 info->app_mode_bundle_path = app_mode_bundle_path;
90 114
91 // Read information about the this app shortcut from the Info.plist. 115 // Read information about the this app shortcut from the Info.plist.
92 // Don't check for null-ness on optional items. 116 // Don't check for null-ness on optional items.
93 NSDictionary* info_plist = [app_bundle infoDictionary]; 117 NSDictionary* info_plist = [app_bundle infoDictionary];
94 CHECK(info_plist) << "couldn't get loader Info.plist"; 118 CHECK(info_plist) << "couldn't get loader Info.plist";
95 119
96 info->app_mode_id = SysNSStringToUTF8( 120 info->app_mode_id = SysNSStringToUTF8(
97 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); 121 [info_plist objectForKey:app_mode::kCrAppModeShortcutIDKey]);
98 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID"; 122 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID";
99 123
100 info->app_mode_name = SysNSStringToUTF16( 124 info->app_mode_name = SysNSStringToUTF16(
101 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]); 125 [info_plist objectForKey:app_mode::kCrAppModeShortcutNameKey]);
102 126
103 info->app_mode_url = SysNSStringToUTF8( 127 info->app_mode_url = SysNSStringToUTF8(
104 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]); 128 [info_plist objectForKey:app_mode::kCrAppModeShortcutURLKey]);
105 129
106 info->user_data_dir = base::mac::NSStringToFilePath( 130 info->user_data_dir = base::mac::NSStringToFilePath(
107 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]); 131 [info_plist objectForKey:app_mode::kCrAppModeUserDataDirKey]);
108 132
109 info->profile_dir = base::mac::NSStringToFilePath( 133 info->profile_dir = base::mac::NSStringToFilePath(
110 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]); 134 [info_plist objectForKey:app_mode::kCrAppModeProfileDirKey]);
111 135
112 // Open the framework. 136 // ** 5: Open the framework.
113 StartFun ChromeAppModeStart = NULL; 137 StartFun ChromeAppModeStart = NULL;
114 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY); 138 void* cr_dylib = dlopen(framework_shlib_path.value().c_str(), RTLD_LAZY);
115 if (cr_dylib) { 139 if (cr_dylib) {
116 // Find the entry point. 140 // Find the entry point.
117 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); 141 ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart");
118 if (!ChromeAppModeStart) 142 if (!ChromeAppModeStart)
119 LOG(ERROR) << "Couldn't get entry point: " << dlerror(); 143 LOG(ERROR) << "Couldn't get entry point: " << dlerror();
120 } else { 144 } else {
121 LOG(ERROR) << "Couldn't load framework: " << dlerror(); 145 LOG(ERROR) << "Couldn't load framework: " << dlerror();
122 } 146 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 // Hard coded info parameters. 197 // Hard coded info parameters.
174 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion; 198 info.major_version = app_mode::kCurrentChromeAppModeInfoMajorVersion;
175 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion; 199 info.minor_version = app_mode::kCurrentChromeAppModeInfoMinorVersion;
176 info.argc = argc; 200 info.argc = argc;
177 info.argv = argv; 201 info.argv = argv;
178 202
179 // Exit instead of returning to avoid the the removal of |main()| from stack 203 // Exit instead of returning to avoid the the removal of |main()| from stack
180 // backtraces under tail call optimization. 204 // backtraces under tail call optimization.
181 exit(LoadFrameworkAndStart(&info)); 205 exit(LoadFrameworkAndStart(&info));
182 } 206 }
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