2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
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.
29 #import <Cocoa/Cocoa.h>
30 #import <CoreFoundation/CoreFoundation.h>
32 // 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.
36 #define WEAK_IMPORT __attribute__((weak))
38 #define POSIX_SPAWN_SETEXEC 0x0040
39 typedef void *posix_spawnattr_t;
40 typedef void *posix_spawn_file_actions_t;
41 int posix_spawnattr_init(posix_spawnattr_t *) WEAK_IMPORT;
42 int 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;
43 int posix_spawnattr_setbinpref_np(posix_spawnattr_t * __restrict, size_t, cpu_type_t *__restrict, size_t *__restrict) WEAK_IMPORT;
44 int posix_spawnattr_setflags(posix_spawnattr_t *, short) WEAK_IMPORT;
47 static void displayErrorAndQuit(NSString *title, NSString *message)
50 NSRunCriticalAlertPanel(title, message, @"Quit", nil, nil);
54 static int getLastVersionShown()
56 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:@"-1" forKey:@"StartPageShownInVersion"]];
57 return [[NSUserDefaults standardUserDefaults] integerForKey:@"StartPageShownInVersion"];
60 static void saveLastVersionShown(int lastVersion)
62 [[NSUserDefaults standardUserDefaults] setInteger:lastVersion forKey:@"StartPageShownInVersion"];
63 [[NSUserDefaults standardUserDefaults] synchronize];
66 static NSString *getPathForStartPage()
68 return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"start.html"];
71 static int getCurrentVersion()
73 return [[[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey] intValue];
76 static int getShowStartPageVersion()
78 return getCurrentVersion() + 1;
81 static BOOL startPageDisabled()
83 return [[NSUserDefaults standardUserDefaults] boolForKey:@"StartPageDisabled"];
86 static void addStartPageToArgumentsIfNeeded(NSMutableArray *arguments)
88 if (startPageDisabled())
91 if (getLastVersionShown() < getShowStartPageVersion()) {
92 saveLastVersionShown(getCurrentVersion());
93 NSString *startPagePath = getPathForStartPage();
95 [arguments addObject:startPagePath];
99 static cpu_type_t preferredArchitecture()
102 return CPU_TYPE_POWERPC;
103 #elif defined(__LP64__)
104 return CPU_TYPE_X86_64;
110 static void myExecve(NSString *executable, NSArray *args, NSDictionary *environment)
112 char **argv = (char **)calloc(sizeof(char *), [args count] + 1);
113 char **env = (char **)calloc(sizeof(char *), [environment count] + 1);
115 NSEnumerator *e = [args objectEnumerator];
118 while ((s = [e nextObject]))
119 argv[i++] = (char *) [s UTF8String];
121 e = [environment keyEnumerator];
123 while ((s = [e nextObject]))
124 env[i++] = (char *) [[NSString stringWithFormat:@"%@=%@", s, [environment objectForKey:s]] UTF8String];
126 if (posix_spawnattr_init && posix_spawn && posix_spawnattr_setbinpref_np && posix_spawnattr_setflags) {
127 posix_spawnattr_t attr;
128 posix_spawnattr_init(&attr);
129 cpu_type_t architecturePreference[] = { preferredArchitecture(), CPU_TYPE_X86 };
130 posix_spawnattr_setbinpref_np(&attr, 2, architecturePreference, 0);
131 short flags = POSIX_SPAWN_SETEXEC;
132 posix_spawnattr_setflags(&attr, flags);
133 posix_spawn(NULL, [executable fileSystemRepresentation], NULL, &attr, argv, env);
135 execve([executable fileSystemRepresentation], argv, env);
138 static NSBundle *locateSafariBundle()
140 NSArray *applicationDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
141 NSEnumerator *e = [applicationDirectories objectEnumerator];
142 NSString *applicationDirectory;
143 while ((applicationDirectory = [e nextObject])) {
144 NSString *possibleSafariPath = [applicationDirectory stringByAppendingPathComponent:@"Safari.app"];
145 NSBundle *possibleSafariBundle = [NSBundle bundleWithPath:possibleSafariPath];
146 if ([[possibleSafariBundle bundleIdentifier] isEqualToString:@"com.apple.Safari"])
147 return possibleSafariBundle;
150 CFURLRef safariURL = nil;
151 OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, CFSTR("com.apple.Safari"), nil, nil, &safariURL);
153 displayErrorAndQuit(@"Unable to locate Safari", @"Nightly builds of WebKit require Safari to run. Please check that it is available and then try again.");
155 NSBundle *safariBundle = [NSBundle bundleWithPath:[(NSURL *)safariURL path]];
156 CFRelease(safariURL);
160 static NSString *currentMacOSXVersion()
163 if (Gestalt(gestaltSystemVersion, &version) != noErr)
166 return [NSString stringWithFormat:@"%x.%x", (version & 0xFF00) >> 8, (version & 0x00F0) >> 4];
169 static NSString *fallbackMacOSXVersion(NSString *systemVersion)
171 NSDictionary *fallbackVersionMap = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"FallbackSystemVersions"];
172 if (!fallbackVersionMap)
174 NSString *fallbackSystemVersion = [fallbackVersionMap objectForKey:systemVersion];
175 if (!fallbackSystemVersion || ![fallbackSystemVersion isKindOfClass:[NSString class]])
177 return fallbackSystemVersion;
180 static BOOL checkFrameworkPath(NSString *frameworkPath)
182 BOOL isDirectory = NO;
183 return [[NSFileManager defaultManager] fileExistsAtPath:frameworkPath isDirectory:&isDirectory] && isDirectory;
186 static BOOL checkSafariVersion(NSBundle *safariBundle)
188 NSString *safariBundleVersion = [[safariBundle infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
189 NSString *majorComponent = [[safariBundleVersion componentsSeparatedByString:@"."] objectAtIndex:0];
190 NSString *majorVersion = [majorComponent substringFromIndex:[majorComponent length] - 3];
191 return [majorVersion intValue] >= 530;
194 int main(int argc, char *argv[])
196 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
198 NSString *systemVersion = currentMacOSXVersion();
199 NSString *frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:systemVersion];
201 BOOL frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
203 if (!frameworkPathIsUsable) {
204 NSString *fallbackSystemVersion = fallbackMacOSXVersion(systemVersion);
205 if (fallbackSystemVersion) {
206 frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:fallbackSystemVersion];
207 frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
211 if (!frameworkPathIsUsable)
212 displayErrorAndQuit([NSString stringWithFormat:@"Mac OS X %@ is not supported", systemVersion],
213 [NSString stringWithFormat:@"Nightly builds of WebKit are not supported on Mac OS X %@ at this time.", systemVersion]);
215 NSString *pathToEnablerLib = [[NSBundle mainBundle] pathForResource:@"WebKitNightlyEnabler" ofType:@"dylib"];
217 NSBundle *safariBundle = locateSafariBundle();
218 NSString *executablePath = [safariBundle executablePath];
220 if (!checkSafariVersion(safariBundle)) {
221 NSString *safariVersion = [[safariBundle localizedInfoDictionary] objectForKey:@"CFBundleShortVersionString"];
222 displayErrorAndQuit([NSString stringWithFormat:@"Safari %@ is not supported", safariVersion],
223 [NSString stringWithFormat:@"Nightly builds of WebKit are not supported with Safari %@ at this time. Please update to a newer version of Safari.", safariVersion]);
226 if ([frameworkPath rangeOfString:@":"].location != NSNotFound ||
227 [pathToEnablerLib rangeOfString:@":"].location != NSNotFound)
228 displayErrorAndQuit(@"Unable to launch Safari",
229 @"WebKit is located at a path containing an unsupported character. Please move WebKit to a different location and try again.");
231 NSMutableArray *arguments = [NSMutableArray arrayWithObject:executablePath];
232 NSMutableDictionary *environment = [[[NSDictionary dictionaryWithObjectsAndKeys:frameworkPath, @"DYLD_FRAMEWORK_PATH", @"YES", @"WEBKIT_UNSET_DYLD_FRAMEWORK_PATH",
233 pathToEnablerLib, @"DYLD_INSERT_LIBRARIES", [[NSBundle mainBundle] executablePath], @"WebKitAppPath", nil] mutableCopy] autorelease];
234 [environment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
235 addStartPageToArgumentsIfNeeded(arguments);
238 [arguments addObject:[NSString stringWithUTF8String:*argv]];
240 myExecve(executablePath, arguments, environment);
242 char *error = strerror(errno);
243 NSString *errorMessage = [NSString stringWithFormat:@"Launching Safari at %@ failed with the error '%s' (%d)", [safariBundle bundlePath], error, errno];
244 displayErrorAndQuit(@"Unable to launch Safari", errorMessage);