main.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. *
  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.
  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.
  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. #import <Cocoa/Cocoa.h>
  29. #import <CoreFoundation/CoreFoundation.h>
  30. // We need to weak-import posix_spawn and friends as they're not available on Tiger.
  31. // The BSD-level system headers do not have availability macros, so we redeclare the
  32. // functions ourselves with the "weak" attribute.
  33. #define WEAK_IMPORT __attribute__((weak))
  34. #define POSIX_SPAWN_SETEXEC 0x0040
  35. typedef void *posix_spawnattr_t;
  36. typedef void *posix_spawn_file_actions_t;
  37. int posix_spawnattr_init(posix_spawnattr_t *) WEAK_IMPORT;
  38. 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;
  39. int posix_spawnattr_setbinpref_np(posix_spawnattr_t * __restrict, size_t, cpu_type_t *__restrict, size_t *__restrict) WEAK_IMPORT;
  40. int posix_spawnattr_setflags(posix_spawnattr_t *, short) WEAK_IMPORT;
  41. static void displayErrorAndQuit(NSString *title, NSString *message)
  42. {
  43. NSApplicationLoad();
  44. NSRunCriticalAlertPanel(title, message, @"Quit", nil, nil);
  45. exit(0);
  46. }
  47. static int getLastVersionShown()
  48. {
  49. [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:@"-1" forKey:@"StartPageShownInVersion"]];
  50. return [[NSUserDefaults standardUserDefaults] integerForKey:@"StartPageShownInVersion"];
  51. }
  52. static void saveLastVersionShown(int lastVersion)
  53. {
  54. [[NSUserDefaults standardUserDefaults] setInteger:lastVersion forKey:@"StartPageShownInVersion"];
  55. [[NSUserDefaults standardUserDefaults] synchronize];
  56. }
  57. static NSString *getPathForStartPage()
  58. {
  59. return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"start.html"];
  60. }
  61. static int getCurrentVersion()
  62. {
  63. return [[[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey] intValue];
  64. }
  65. static int getShowStartPageVersion()
  66. {
  67. return getCurrentVersion() + 1;
  68. }
  69. static BOOL startPageDisabled()
  70. {
  71. return [[NSUserDefaults standardUserDefaults] boolForKey:@"StartPageDisabled"];
  72. }
  73. static void addStartPageToArgumentsIfNeeded(NSMutableArray *arguments)
  74. {
  75. if (startPageDisabled())
  76. return;
  77. if (getLastVersionShown() < getShowStartPageVersion()) {
  78. saveLastVersionShown(getCurrentVersion());
  79. NSString *startPagePath = getPathForStartPage();
  80. if (startPagePath)
  81. [arguments addObject:startPagePath];
  82. }
  83. }
  84. static cpu_type_t preferredArchitecture()
  85. {
  86. #if defined(__ppc__)
  87. return CPU_TYPE_POWERPC;
  88. #elif defined(__LP64__)
  89. return CPU_TYPE_X86_64;
  90. #else
  91. return CPU_TYPE_X86;
  92. #endif
  93. }
  94. static void myExecve(NSString *executable, NSArray *args, NSDictionary *environment)
  95. {
  96. char **argv = (char **)calloc(sizeof(char *), [args count] + 1);
  97. char **env = (char **)calloc(sizeof(char *), [environment count] + 1);
  98. NSEnumerator *e = [args objectEnumerator];
  99. NSString *s;
  100. int i = 0;
  101. while ((s = [e nextObject]))
  102. argv[i++] = (char *) [s UTF8String];
  103. e = [environment keyEnumerator];
  104. i = 0;
  105. while ((s = [e nextObject]))
  106. env[i++] = (char *) [[NSString stringWithFormat:@"%@=%@", s, [environment objectForKey:s]] UTF8String];
  107. if (posix_spawnattr_init && posix_spawn && posix_spawnattr_setbinpref_np && posix_spawnattr_setflags) {
  108. posix_spawnattr_t attr;
  109. posix_spawnattr_init(&attr);
  110. cpu_type_t architecturePreference[] = { preferredArchitecture(), CPU_TYPE_X86 };
  111. posix_spawnattr_setbinpref_np(&attr, 2, architecturePreference, 0);
  112. short flags = POSIX_SPAWN_SETEXEC;
  113. posix_spawnattr_setflags(&attr, flags);
  114. posix_spawn(NULL, [executable fileSystemRepresentation], NULL, &attr, argv, env);
  115. } else
  116. execve([executable fileSystemRepresentation], argv, env);
  117. }
  118. static NSBundle *locateSafariBundle()
  119. {
  120. NSArray *applicationDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSAllDomainsMask, YES);
  121. NSEnumerator *e = [applicationDirectories objectEnumerator];
  122. NSString *applicationDirectory;
  123. while ((applicationDirectory = [e nextObject])) {
  124. NSString *possibleSafariPath = [applicationDirectory stringByAppendingPathComponent:@"Safari.app"];
  125. NSBundle *possibleSafariBundle = [NSBundle bundleWithPath:possibleSafariPath];
  126. if ([[possibleSafariBundle bundleIdentifier] isEqualToString:@"com.apple.Safari"])
  127. return possibleSafariBundle;
  128. }
  129. CFURLRef safariURL = nil;
  130. OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, CFSTR("com.apple.Safari"), nil, nil, &safariURL);
  131. if (err != noErr)
  132. displayErrorAndQuit(@"Unable to locate Safari", @"Nightly builds of WebKit require Safari to run. Please check that it is available and then try again.");
  133. NSBundle *safariBundle = [NSBundle bundleWithPath:[(NSURL *)safariURL path]];
  134. CFRelease(safariURL);
  135. return safariBundle;
  136. }
  137. static NSString *currentMacOSXVersion()
  138. {
  139. SInt32 version;
  140. if (Gestalt(gestaltSystemVersion, &version) != noErr)
  141. return @"10.4";
  142. return [NSString stringWithFormat:@"%x.%x", (version & 0xFF00) >> 8, (version & 0x00F0) >> 4];
  143. }
  144. static NSString *fallbackMacOSXVersion(NSString *systemVersion)
  145. {
  146. NSDictionary *fallbackVersionMap = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"FallbackSystemVersions"];
  147. if (!fallbackVersionMap)
  148. return nil;
  149. NSString *fallbackSystemVersion = [fallbackVersionMap objectForKey:systemVersion];
  150. if (!fallbackSystemVersion || ![fallbackSystemVersion isKindOfClass:[NSString class]])
  151. return nil;
  152. return fallbackSystemVersion;
  153. }
  154. static BOOL checkFrameworkPath(NSString *frameworkPath)
  155. {
  156. BOOL isDirectory = NO;
  157. return [[NSFileManager defaultManager] fileExistsAtPath:frameworkPath isDirectory:&isDirectory] && isDirectory;
  158. }
  159. static BOOL checkSafariVersion(NSBundle *safariBundle)
  160. {
  161. NSString *safariBundleVersion = [[safariBundle infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
  162. NSString *majorComponent = [[safariBundleVersion componentsSeparatedByString:@"."] objectAtIndex:0];
  163. NSString *majorVersion = [majorComponent substringFromIndex:[majorComponent length] - 3];
  164. return [majorVersion intValue] >= 530;
  165. }
  166. int main(int argc, char *argv[])
  167. {
  168. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  169. NSString *systemVersion = currentMacOSXVersion();
  170. NSString *frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:systemVersion];
  171. BOOL frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
  172. if (!frameworkPathIsUsable) {
  173. NSString *fallbackSystemVersion = fallbackMacOSXVersion(systemVersion);
  174. if (fallbackSystemVersion) {
  175. frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:fallbackSystemVersion];
  176. frameworkPathIsUsable = checkFrameworkPath(frameworkPath);
  177. }
  178. }
  179. if (!frameworkPathIsUsable)
  180. displayErrorAndQuit([NSString stringWithFormat:@"Mac OS X %@ is not supported", systemVersion],
  181. [NSString stringWithFormat:@"Nightly builds of WebKit are not supported on Mac OS X %@ at this time.", systemVersion]);
  182. NSString *pathToEnablerLib = [[NSBundle mainBundle] pathForResource:@"WebKitNightlyEnabler" ofType:@"dylib"];
  183. NSBundle *safariBundle = locateSafariBundle();
  184. NSString *executablePath = [safariBundle executablePath];
  185. if (!checkSafariVersion(safariBundle)) {
  186. NSString *safariVersion = [[safariBundle localizedInfoDictionary] objectForKey:@"CFBundleShortVersionString"];
  187. displayErrorAndQuit([NSString stringWithFormat:@"Safari %@ is not supported", safariVersion],
  188. [NSString stringWithFormat:@"Nightly builds of WebKit are not supported with Safari %@ at this time. Please update to a newer version of Safari.", safariVersion]);
  189. }
  190. if ([frameworkPath rangeOfString:@":"].location != NSNotFound ||
  191. [pathToEnablerLib rangeOfString:@":"].location != NSNotFound)
  192. displayErrorAndQuit(@"Unable to launch Safari",
  193. @"WebKit is located at a path containing an unsupported character. Please move WebKit to a different location and try again.");
  194. NSMutableArray *arguments = [NSMutableArray arrayWithObject:executablePath];
  195. NSMutableDictionary *environment = [[[NSDictionary dictionaryWithObjectsAndKeys:frameworkPath, @"DYLD_FRAMEWORK_PATH", @"YES", @"WEBKIT_UNSET_DYLD_FRAMEWORK_PATH",
  196. pathToEnablerLib, @"DYLD_INSERT_LIBRARIES", [[NSBundle mainBundle] executablePath], @"WebKitAppPath", nil] mutableCopy] autorelease];
  197. [environment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
  198. addStartPageToArgumentsIfNeeded(arguments);
  199. while (*++argv)
  200. [arguments addObject:[NSString stringWithUTF8String:*argv]];
  201. myExecve(executablePath, arguments, environment);
  202. char *error = strerror(errno);
  203. NSString *errorMessage = [NSString stringWithFormat:@"Launching Safari at %@ failed with the error '%s' (%d)", [safariBundle bundlePath], error, errno];
  204. displayErrorAndQuit(@"Unable to launch Safari", errorMessage);
  205. [pool release];
  206. return 0;
  207. }