blob: 43155fd64cc5e5c3bca4999f8635e7f5e86875f8 [file] [log] [blame]
mjs15819bc2006-04-06 04:14:24 +00001/*
mrowe@apple.com66498512009-05-24 05:14:44 +00002 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
mjs15819bc2006-04-06 04:14:24 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
mrowe@apple.com66498512009-05-24 05:14:44 +000013 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
mjs15819bc2006-04-06 04:14:24 +000014 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import <Cocoa/Cocoa.h>
30#import <CoreFoundation/CoreFoundation.h>
31
mrowe@apple.com66498512009-05-24 05:14:44 +000032// We need to weak-import posix_spawn and friends as they're not available on Tiger.
33// The BSD-level system headers do not have availability macros, so we redeclare the
34// functions ourselves with the "weak" attribute.
35
36#define WEAK_IMPORT __attribute__((weak))
37
38#define POSIX_SPAWN_SETEXEC 0x0040
39typedef void *posix_spawnattr_t;
40typedef void *posix_spawn_file_actions_t;
41int posix_spawnattr_init(posix_spawnattr_t *) WEAK_IMPORT;
42int posix_spawn(pid_t * __restrict, const char * __restrict, const posix_spawn_file_actions_t *, const posix_spawnattr_t * __restrict, char *const __argv[ __restrict], char *const __envp[ __restrict]) WEAK_IMPORT;
43int posix_spawnattr_setbinpref_np(posix_spawnattr_t * __restrict, size_t, cpu_type_t *__restrict, size_t *__restrict) WEAK_IMPORT;
44int posix_spawnattr_setflags(posix_spawnattr_t *, short) WEAK_IMPORT;
45
46
47static void displayErrorAndQuit(NSString *title, NSString *message)
mjs15819bc2006-04-06 04:14:24 +000048{
49 NSApplicationLoad();
ddkilzer@apple.com2bf57332018-08-31 10:54:16 +000050#pragma clang diagnostic push
51#pragma clang diagnostic ignored "-Wdeprecated-declarations"
lforschler@apple.com4b3be902015-09-04 20:47:33 +000052 NSRunCriticalAlertPanel(title, @"%@", @"Quit", nil, nil, message);
ddkilzer@apple.com2bf57332018-08-31 10:54:16 +000053#pragma clang diagnostic pop
mjs15819bc2006-04-06 04:14:24 +000054 exit(0);
55}
56
mrowe@apple.com66498512009-05-24 05:14:44 +000057static int getLastVersionShown()
mjs15819bc2006-04-06 04:14:24 +000058{
59 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:@"-1" forKey:@"StartPageShownInVersion"]];
60 return [[NSUserDefaults standardUserDefaults] integerForKey:@"StartPageShownInVersion"];
61}
62
mrowe@apple.com66498512009-05-24 05:14:44 +000063static void saveLastVersionShown(int lastVersion)
mjs15819bc2006-04-06 04:14:24 +000064{
65 [[NSUserDefaults standardUserDefaults] setInteger:lastVersion forKey:@"StartPageShownInVersion"];
66 [[NSUserDefaults standardUserDefaults] synchronize];
67}
68
mrowe@apple.com66498512009-05-24 05:14:44 +000069static NSString *getPathForStartPage()
mjs15819bc2006-04-06 04:14:24 +000070{
71 return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"start.html"];
72}
73
mrowe@apple.com66498512009-05-24 05:14:44 +000074static int getCurrentVersion()
mjs15819bc2006-04-06 04:14:24 +000075{
76 return [[[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey] intValue];
77}
78
mrowe@apple.com66498512009-05-24 05:14:44 +000079static int getShowStartPageVersion()
80{
81 return getCurrentVersion() + 1;
82}
83
84static BOOL startPageDisabled()
mjs15819bc2006-04-06 04:14:24 +000085{
86 return [[NSUserDefaults standardUserDefaults] boolForKey:@"StartPageDisabled"];
87}
88
mrowe@apple.com66498512009-05-24 05:14:44 +000089static void addStartPageToArgumentsIfNeeded(NSMutableArray *arguments)
mjs15819bc2006-04-06 04:14:24 +000090{
91 if (startPageDisabled())
92 return;
93
94 if (getLastVersionShown() < getShowStartPageVersion()) {
95 saveLastVersionShown(getCurrentVersion());
96 NSString *startPagePath = getPathForStartPage();
97 if (startPagePath)
98 [arguments addObject:startPagePath];
99 }
100}
101
mrowe@apple.com66498512009-05-24 05:14:44 +0000102static cpu_type_t preferredArchitecture()
103{
104#if defined(__ppc__)
105 return CPU_TYPE_POWERPC;
106#elif defined(__LP64__)
107 return CPU_TYPE_X86_64;
108#else
109 return CPU_TYPE_X86;
110#endif
111}
112
mjs15819bc2006-04-06 04:14:24 +0000113static void myExecve(NSString *executable, NSArray *args, NSDictionary *environment)
114{
115 char **argv = (char **)calloc(sizeof(char *), [args count] + 1);
116 char **env = (char **)calloc(sizeof(char *), [environment count] + 1);
mrowe@apple.com66498512009-05-24 05:14:44 +0000117
mjs15819bc2006-04-06 04:14:24 +0000118 NSEnumerator *e = [args objectEnumerator];
119 NSString *s;
120 int i = 0;
mrowe@apple.com66498512009-05-24 05:14:44 +0000121 while ((s = [e nextObject]))
mjs15819bc2006-04-06 04:14:24 +0000122 argv[i++] = (char *) [s UTF8String];
mrowe@apple.com66498512009-05-24 05:14:44 +0000123
mjs15819bc2006-04-06 04:14:24 +0000124 e = [environment keyEnumerator];
125 i = 0;
mrowe@apple.com66498512009-05-24 05:14:44 +0000126 while ((s = [e nextObject]))
mjs15819bc2006-04-06 04:14:24 +0000127 env[i++] = (char *) [[NSString stringWithFormat:@"%@=%@", s, [environment objectForKey:s]] UTF8String];
mrowe@apple.com66498512009-05-24 05:14:44 +0000128
129 if (posix_spawnattr_init && posix_spawn && posix_spawnattr_setbinpref_np && posix_spawnattr_setflags) {
130 posix_spawnattr_t attr;
131 posix_spawnattr_init(&attr);
132 cpu_type_t architecturePreference[] = { preferredArchitecture(), CPU_TYPE_X86 };
133 posix_spawnattr_setbinpref_np(&attr, 2, architecturePreference, 0);
134 short flags = POSIX_SPAWN_SETEXEC;
135 posix_spawnattr_setflags(&attr, flags);
136 posix_spawn(NULL, [executable fileSystemRepresentation], NULL, &attr, argv, env);
137 } else
138 execve([executable fileSystemRepresentation], argv, env);
mjs15819bc2006-04-06 04:14:24 +0000139}
140
mrowe@apple.com66498512009-05-24 05:14:44 +0000141static NSBundle *locateSafariBundle()
mjs15819bc2006-04-06 04:14:24 +0000142{
bdash0139db12007-03-13 09:21:41 +0000143 NSArray *applicationDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
144 NSEnumerator *e = [applicationDirectories objectEnumerator];
145 NSString *applicationDirectory;
mrowe@apple.com66498512009-05-24 05:14:44 +0000146 while ((applicationDirectory = [e nextObject])) {
bdash0139db12007-03-13 09:21:41 +0000147 NSString *possibleSafariPath = [applicationDirectory stringByAppendingPathComponent:@"Safari.app"];
148 NSBundle *possibleSafariBundle = [NSBundle bundleWithPath:possibleSafariPath];
149 if ([[possibleSafariBundle bundleIdentifier] isEqualToString:@"com.apple.Safari"])
150 return possibleSafariBundle;
151 }
mjs15819bc2006-04-06 04:14:24 +0000152
153 CFURLRef safariURL = nil;
ddkilzer@apple.com2bf57332018-08-31 10:54:16 +0000154#pragma clang diagnostic push
155#pragma clang diagnostic ignored "-Wdeprecated-declarations"
mjs15819bc2006-04-06 04:14:24 +0000156 OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, CFSTR("com.apple.Safari"), nil, nil, &safariURL);
ddkilzer@apple.com2bf57332018-08-31 10:54:16 +0000157#pragma clang diagnostic pop
mjs15819bc2006-04-06 04:14:24 +0000158 if (err != noErr)
159 displayErrorAndQuit(@"Unable to locate Safari", @"Nightly builds of WebKit require Safari to run. Please check that it is available and then try again.");
160
bdash0139db12007-03-13 09:21:41 +0000161 NSBundle *safariBundle = [NSBundle bundleWithPath:[(NSURL *)safariURL path]];
162 CFRelease(safariURL);
163 return safariBundle;
164}
165
dfarler@apple.com0b050652013-06-28 20:38:55 +0000166static NSString *determineExecutablePath(NSBundle *bundle)
167{
168 NSString *safariExecutablePath = [bundle executablePath];
169
170 NSString *safariForWebKitDevelopmentExecutablePath = [bundle pathForAuxiliaryExecutable:@"SafariForWebKitDevelopment"];
171 if (![[NSFileManager defaultManager] fileExistsAtPath:safariForWebKitDevelopmentExecutablePath])
172 return safariExecutablePath;
173
174 SecStaticCodeRef staticCode;
175 if (SecStaticCodeCreateWithPath((CFURLRef)[bundle executableURL], kSecCSDefaultFlags, &staticCode) != noErr)
176 return [bundle executablePath];
177
178 NSDictionary *codeInformation;
179 if (SecCodeCopySigningInformation(staticCode, kSecCSRequirementInformation, (CFDictionaryRef*)&codeInformation) != noErr) {
180 CFRelease(staticCode);
181 return safariExecutablePath;
182 }
183 CFRelease(staticCode);
184 [codeInformation autorelease];
185
186 if ([codeInformation objectForKey:(id)kSecCodeInfoEntitlements])
187 return safariForWebKitDevelopmentExecutablePath;
188
189 return safariExecutablePath;
190}
191
mrowe@apple.com66498512009-05-24 05:14:44 +0000192static NSString *currentMacOSXVersion()
193{
mrowe@apple.comcbb2ed42013-11-20 06:01:19 +0000194 // Can't use -[NSProcessInfo operatingSystemVersionString] because it has too much stuff we don't want.
195 NSString *systemLibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSSystemDomainMask, YES) objectAtIndex:0];
196 NSString *systemVersionPlistPath = [systemLibraryPath stringByAppendingPathComponent:@"CoreServices/SystemVersion.plist"];
197 NSDictionary *systemVersionInfo = [NSDictionary dictionaryWithContentsOfFile:systemVersionPlistPath];
198 return [systemVersionInfo objectForKey:@"ProductVersion"];
199}
mrowe@apple.com66498512009-05-24 05:14:44 +0000200
mrowe@apple.comcbb2ed42013-11-20 06:01:19 +0000201static NSString *currentMacOSXMajorVersion()
202{
203 NSArray *allComponents = [currentMacOSXVersion() componentsSeparatedByString:@"."];
204 NSArray *majorAndMinorComponents = [allComponents objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];
205 return [majorAndMinorComponents componentsJoinedByString:@"."];
mrowe@apple.com66498512009-05-24 05:14:44 +0000206}
207
mitz@apple.comed970db2010-01-23 09:51:47 +0000208static NSString *fallbackMacOSXVersion(NSString *systemVersion)
209{
210 NSDictionary *fallbackVersionMap = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"FallbackSystemVersions"];
211 if (!fallbackVersionMap)
212 return nil;
213 NSString *fallbackSystemVersion = [fallbackVersionMap objectForKey:systemVersion];
214 if (!fallbackSystemVersion || ![fallbackSystemVersion isKindOfClass:[NSString class]])
215 return nil;
216 return fallbackSystemVersion;
217}
218
mrowe@apple.com66498512009-05-24 05:14:44 +0000219static BOOL checkFrameworkPath(NSString *frameworkPath)
220{
221 BOOL isDirectory = NO;
222 return [[NSFileManager defaultManager] fileExistsAtPath:frameworkPath isDirectory:&isDirectory] && isDirectory;
223}
224
mrowe@apple.comddecc3e2009-07-28 23:15:42 +0000225static BOOL checkSafariVersion(NSBundle *safariBundle)
226{
227 NSString *safariBundleVersion = [[safariBundle infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
228 NSString *majorComponent = [[safariBundleVersion componentsSeparatedByString:@"."] objectAtIndex:0];
229 NSString *majorVersion = [majorComponent substringFromIndex:[majorComponent length] - 3];
230 return [majorVersion intValue] >= 530;
231}
232
bdash0139db12007-03-13 09:21:41 +0000233int main(int argc, char *argv[])
234{
235 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bdash0139db12007-03-13 09:21:41 +0000236
mrowe@apple.comcbb2ed42013-11-20 06:01:19 +0000237 NSString *systemVersion = currentMacOSXMajorVersion();
mrowe@apple.com66498512009-05-24 05:14:44 +0000238 NSString *frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:systemVersion];
mitz@apple.comed970db2010-01-23 09:51:47 +0000239
240 BOOL frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
241
242 if (!frameworkPathIsUsable) {
243 NSString *fallbackSystemVersion = fallbackMacOSXVersion(systemVersion);
244 if (fallbackSystemVersion) {
245 frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:fallbackSystemVersion];
246 frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
247 }
248 }
249
250 if (!frameworkPathIsUsable)
dfarler@apple.com0b050652013-06-28 20:38:55 +0000251 displayErrorAndQuit([NSString stringWithFormat:@"OS X %@ is not supported", systemVersion],
252 [NSString stringWithFormat:@"Nightly builds of WebKit are not supported on OS X %@ at this time.", systemVersion]);
mitz@apple.comed970db2010-01-23 09:51:47 +0000253
mjs15819bc2006-04-06 04:14:24 +0000254 NSString *pathToEnablerLib = [[NSBundle mainBundle] pathForResource:@"WebKitNightlyEnabler" ofType:@"dylib"];
dfarler@apple.com4476ec82013-12-16 18:17:01 +0000255 NSString *dyldInsertLibraries = pathToEnablerLib;
256 NSString *pathToASanCrashReporterLib = [[NSBundle mainBundle] pathForResource:@"libasancrashreporter" ofType:@"dylib"];
257 if (pathToASanCrashReporterLib)
258 dyldInsertLibraries = [@[ pathToASanCrashReporterLib, pathToEnablerLib ] componentsJoinedByString:@":"];
bdash90833852007-06-06 10:55:43 +0000259
mrowe@apple.comddecc3e2009-07-28 23:15:42 +0000260 NSBundle *safariBundle = locateSafariBundle();
dfarler@apple.com0b050652013-06-28 20:38:55 +0000261 NSString *executablePath = determineExecutablePath(safariBundle);
mrowe@apple.comddecc3e2009-07-28 23:15:42 +0000262
mrowe@apple.comddecc3e2009-07-28 23:15:42 +0000263 if (!checkSafariVersion(safariBundle)) {
264 NSString *safariVersion = [[safariBundle localizedInfoDictionary] objectForKey:@"CFBundleShortVersionString"];
265 displayErrorAndQuit([NSString stringWithFormat:@"Safari %@ is not supported", safariVersion],
266 [NSString stringWithFormat:@"Nightly builds of WebKit are not supported with Safari %@ at this time. Please update to a newer version of Safari.", safariVersion]);
267 }
268
bdash90833852007-06-06 10:55:43 +0000269 if ([frameworkPath rangeOfString:@":"].location != NSNotFound ||
270 [pathToEnablerLib rangeOfString:@":"].location != NSNotFound)
271 displayErrorAndQuit(@"Unable to launch Safari",
272 @"WebKit is located at a path containing an unsupported character. Please move WebKit to a different location and try again.");
mrowe@apple.com66498512009-05-24 05:14:44 +0000273
274 NSMutableArray *arguments = [NSMutableArray arrayWithObject:executablePath];
275 NSMutableDictionary *environment = [[[NSDictionary dictionaryWithObjectsAndKeys:frameworkPath, @"DYLD_FRAMEWORK_PATH", @"YES", @"WEBKIT_UNSET_DYLD_FRAMEWORK_PATH",
dfarler@apple.com4476ec82013-12-16 18:17:01 +0000276 dyldInsertLibraries, @"DYLD_INSERT_LIBRARIES", [[NSBundle mainBundle] executablePath], @"WebKitAppPath", nil] mutableCopy] autorelease];
mrowe@apple.com66498512009-05-24 05:14:44 +0000277 [environment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
mjs15819bc2006-04-06 04:14:24 +0000278 addStartPageToArgumentsIfNeeded(arguments);
279
280 while (*++argv)
281 [arguments addObject:[NSString stringWithUTF8String:*argv]];
282
bdash51548af2007-01-15 00:53:11 +0000283 myExecve(executablePath, arguments, environment);
mjs15819bc2006-04-06 04:14:24 +0000284
bdash51548af2007-01-15 00:53:11 +0000285 char *error = strerror(errno);
bdash0139db12007-03-13 09:21:41 +0000286 NSString *errorMessage = [NSString stringWithFormat:@"Launching Safari at %@ failed with the error '%s' (%d)", [safariBundle bundlePath], error, errno];
bdash51548af2007-01-15 00:53:11 +0000287 displayErrorAndQuit(@"Unable to launch Safari", errorMessage);
mjs15819bc2006-04-06 04:14:24 +0000288
289 [pool release];
290 return 0;
291}