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

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

Issue 9351014: Mac app mode: locate Chrome + refactor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Misc. fixes Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 12
16 #include <CoreFoundation/CoreFoundation.h> 13 #include <CoreFoundation/CoreFoundation.h>
17 #import <Foundation/Foundation.h> 14 #import <Foundation/Foundation.h>
18 15
16 #include "base/file_path.h"
17 #include "base/file_util.h"
18 #include "base/logging.h"
19 #include "base/mac/scoped_nsautorelease_pool.h"
20 #include "base/sys_string_conversions.h"
21 #import "chrome/common/mac/app_mode_chrome_locator.h"
19 #include "chrome/common/mac/app_mode_common.h" 22 #include "chrome/common/mac/app_mode_common.h"
20 23
21 namespace { 24 namespace {
22 25
23 // Checks that condition |x| is true. If not, outputs |msg| (together with 26 void LoadFramework(void** cr_dylib, app_mode::ChromeAppModeInfo* info) {
24 // source filename and line number) and exits. 27 using base::SysNSStringToUTF8;
25 #define CHECK_MSG(x, msg) if (!(x)) check_msg_helper(__FILE__, __LINE__, msg); 28 using base::SysNSStringToUTF16;
26 void check_msg_helper(const char* file, int line, const char* msg) { 29
27 fprintf(stderr, "%s (%d): %s\n", file, line, msg); 30 base::mac::ScopedNSAutoreleasePool scoped_pool;
28 exit(1); 31
32 // Get the current main bundle, i.e., that of the app loader that's running.
33 NSBundle* app_bundle = [NSBundle mainBundle];
Mark Mentovai 2012/02/07 17:28:06 Didn’t you add your own functions to wrap this?
jeremy 2012/02/09 10:52:51 Yes, but this code is called from the stub launche
34 CHECK(app_bundle) << "couldn't get loader bundle";
35
36 // ** 1: Get path to outer Chrome bundle.
37
38 // Get the bundle ID of the browser that created this app bundle.
39 NSString* cr_bundle_id = [app_bundle
40 objectForInfoDictionaryKey:(NSString*)app_mode::kBrowserBundleIDKey];
41 CHECK(cr_bundle_id) << "couldn't get browser bundle ID";
42
43 // First check if Chrome exists at the last known location.
44 FilePath cr_bundle_path;
45 NSString* cr_bundle_path_ns =
46 [(NSString*)CFPreferencesCopyAppValue(
Mark Mentovai 2012/02/07 17:28:06 Don’t cast NS-to-CF like this. Use NSToCFCast<> an
jeremy 2012/02/09 10:52:51 Done.
47 app_mode::kLastRunAppBundlePathPrefsKey,
48 (CFStringRef)cr_bundle_id) autorelease];
49 cr_bundle_path = FilePath([cr_bundle_path_ns fileSystemRepresentation]);
50 bool found_bundle =
51 cr_bundle_path_ns && file_util::DirectoryExists(cr_bundle_path);
52
53 if (!found_bundle) {
54 // If no such bundle path exists, try to search by bundle ID.
55 if (!app_mode::FindBundleById(cr_bundle_id, &cr_bundle_path)) {
56 // TODO(jeremy): Display UI to allow user to manually locate the Chrome
57 // bundle.
58 LOG(FATAL) << "Failed to locate bundle by identifier";
59 }
60 }
61
62 // ** 2: Read information from the Chrome bundle.
63 string16 raw_version_str;
64 FilePath version_path;
65 FilePath framework_shlib_path;
66 if (!app_mode::GetChromeBundleInfo(cr_bundle_path, &raw_version_str,
67 &version_path, &framework_shlib_path)) {
68 LOG(FATAL) << "Couldn't ready Chrome bundle info";
69 }
70
71 // TODO(jeremy): Check that Chrome is new enough.
72 (void)raw_version_str;
Mark Mentovai 2012/02/07 17:28:06 ?
jeremy 2012/02/09 10:52:51 Removed for now, the idea was to mark that we need
73
74 // ** 3: Fill in ChromeAppModeInfo.
75 info->chrome_versioned_path = version_path;
76 info->app_mode_bundle_path =
77 FilePath([[app_bundle bundlePath] fileSystemRepresentation]);
78
79 // Read information about the this app shortcut from the Info.plist.
80 // Don't check for null-ness on optional items.
81 NSDictionary* info_plist = [app_bundle infoDictionary];
82 CHECK(info_plist) << "couldn't get loader Info.plist";
83
84 info->app_mode_id = SysNSStringToUTF8(
85 [info_plist objectForKey:@"CrAppModeShortcutID"]);
Mark Mentovai 2012/02/07 17:28:06 Do we not have symbolic constants for any of these
jeremy 2012/02/09 10:52:51 Sail has a patch ready to land that does that.
86 CHECK(info->app_mode_id.size()) << "couldn't get app shortcut ID";
87
88 info->app_mode_short_name = SysNSStringToUTF16(
89 [info_plist objectForKey:@"CrAppModeShortcutShortName"]);
90
91 info->app_mode_name = SysNSStringToUTF16(
92 [info_plist objectForKey:@"CrAppModeShortcutName"]);
93
94 info->app_mode_url = SysNSStringToUTF8(
95 [info_plist objectForKey:@"CrAppModeShortcutURL"]);
96 CHECK(info->app_mode_url.size()) << "couldn't get app shortcut URL";
97
98 // Open the framework.
99 *cr_dylib = dlopen(framework_shlib_path.value().c_str(),
100 RTLD_LAZY);
Mark Mentovai 2012/02/07 17:28:06 This didn’t need to wrap.
jeremy 2012/02/09 10:52:51 Done.
101 CHECK(cr_dylib) << "couldn't load framework";
Mark Mentovai 2012/02/07 17:28:06 Print some more useful information from dlerror()?
jeremy 2012/02/09 10:52:51 Done.
29 } 102 }
30 103
31 // Converts an NSString to a UTF8 C string (which is allocated, and may be freed 104 } // namespace
32 // using |free()|). If |s| is nil or can't produce such a string, this returns
33 // |NULL|.
34 char* NSStringToUTF8CString(NSString* s) {
35 CHECK_MSG([s isKindOfClass:[NSString class]], "expected an NSString");
36 const char* cstring = [s UTF8String];
37 return cstring ? strdup(cstring) : NULL;
38 }
39
40 // Converts an NSString to a file-system representation C string (which is
41 // allocated, and may be freed using |free()|). If |s| is nil or can't produce
42 // such a string, this returns |NULL|.
43 char* NSStringToFSCString(NSString* s) {
44 CHECK_MSG([s isKindOfClass:[NSString class]], "expected an NSString");
45 const char* cstring = [s fileSystemRepresentation];
46 return cstring ? strdup(cstring) : NULL;
47 }
48
49 } // namespace
50 105
51 __attribute__((visibility("default"))) 106 __attribute__((visibility("default")))
52 int main(int argc, char** argv) { 107 int main(int argc, char** argv) {
53 app_mode::ChromeAppModeInfo info; 108 app_mode::ChromeAppModeInfo info;
54 info.major_version = 0; // v0.1 109
55 info.minor_version = 1; 110 // Hard coded info parameters.
111 info.major_version = 1; // v1.0
112 info.minor_version = 0;
56 info.argc = argc; 113 info.argc = argc;
57 info.argv = argv; 114 info.argv = argv;
58 115
59 // The Cocoa APIs are a bit more convenient; for this an autorelease pool is 116 // Load the Chrome framework.
60 // needed. 117 void *cr_dylib;
61 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 118 LoadFramework(&cr_dylib, &info);
62
63 // Get the current main bundle, i.e., that of the app loader that's running.
64 NSBundle* app_bundle = [NSBundle mainBundle];
65 CHECK_MSG(app_bundle, "couldn't get loader bundle");
66
67 // Get the bundle ID of the browser that created this app bundle.
68 NSString* cr_bundle_id = [app_bundle
69 objectForInfoDictionaryKey:(NSString*)app_mode::kBrowserBundleIDKey];
70 CHECK_MSG(cr_bundle_id, "couldn't get browser bundle ID");
71
72 // Get the browser bundle path.
73 // TODO(viettrungluu): more fun
74 NSString* cr_bundle_path =
75 [(NSString*)CFPreferencesCopyAppValue(
76 app_mode::kLastRunAppBundlePathPrefsKey,
77 (CFStringRef)cr_bundle_id) autorelease];
78 CHECK_MSG(cr_bundle_path, "couldn't get browser bundle path");
79
80 // Get the browser bundle.
81 NSBundle* cr_bundle = [NSBundle bundleWithPath:cr_bundle_path];
82 CHECK_MSG(cr_bundle, "couldn't get browser bundle");
83
84 // Get the current browser version.
85 NSString* cr_version =
86 [cr_bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
87 CHECK_MSG(cr_version, "couldn't get browser version");
88
89 // Get the current browser versioned directory.
90 NSArray* cr_versioned_path_components =
91 [NSArray arrayWithObjects:cr_bundle_path,
92 @"Contents",
93 @"Versions",
94 cr_version,
95 nil];
96 NSString* cr_versioned_path =
97 [[NSString pathWithComponents:cr_versioned_path_components]
98 stringByStandardizingPath];
99 CHECK_MSG(cr_versioned_path, "couldn't get browser versioned path");
100 // And copy it, since |cr_versioned_path| will go away with the pool.
101 info.chrome_versioned_path = NSStringToFSCString(cr_versioned_path);
102
103 // Optional, so okay if it's NULL.
104 info.app_mode_bundle_path = NSStringToFSCString([app_bundle bundlePath]);
105
106 // Read information about the this app shortcut from the Info.plist.
107 // Don't check for null-ness on optional items.
108 NSDictionary* info_plist = [app_bundle infoDictionary];
109 CHECK_MSG(info_plist, "couldn't get loader Info.plist");
110
111 info.app_mode_id = NSStringToUTF8CString(
112 [info_plist objectForKey:@"CrAppModeShortcutID"]);
113 CHECK_MSG(info.app_mode_id, "couldn't get app shortcut ID");
114
115 info.app_mode_short_name = NSStringToUTF8CString(
116 [info_plist objectForKey:@"CrAppModeShortcutShortName"]);
117
118 info.app_mode_name = NSStringToUTF8CString(
119 [info_plist objectForKey:@"CrAppModeShortcutName"]);
120
121 info.app_mode_url = NSStringToUTF8CString(
122 [info_plist objectForKey:@"CrAppModeShortcutURL"]);
123 CHECK_MSG(info.app_mode_url, "couldn't get app shortcut URL");
124
125 // Get the framework path.
126 NSString* cr_bundle_exe =
127 [cr_bundle objectForInfoDictionaryKey:@"CFBundleExecutable"];
128 NSString* cr_framework_path =
129 [cr_versioned_path stringByAppendingPathComponent:
130 [cr_bundle_exe stringByAppendingString:@" Framework.framework"]];
131 cr_framework_path =
132 [cr_framework_path stringByAppendingPathComponent:
133 [cr_bundle_exe stringByAppendingString:@" Framework"]];
134
135 // Open the framework.
136 void* cr_dylib = dlopen([cr_framework_path fileSystemRepresentation],
137 RTLD_LAZY);
138 CHECK_MSG(cr_dylib, "couldn't load framework");
139
140 // Drain the pool as late as possible.
141 [pool drain];
142 119
143 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*); 120 typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*);
144 StartFun ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart"); 121 StartFun ChromeAppModeStart = (StartFun)dlsym(cr_dylib, "ChromeAppModeStart");
145 CHECK_MSG(ChromeAppModeStart, "couldn't get entry point"); 122 CHECK(ChromeAppModeStart) << "couldn't get entry point";
146 123
147 // Exit instead of returning to avoid the the removal of |main()| from stack 124 // Exit instead of returning to avoid the the removal of |main()| from stack
148 // backtraces under tail call optimization. 125 // backtraces under tail call optimization.
149 int rv = ChromeAppModeStart(&info); 126 int rv = ChromeAppModeStart(&info);
150 exit(rv); 127 exit(rv);
151 } 128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698