WebKitNightlyEnabler.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2006 Graham Dennis. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #import <Cocoa/Cocoa.h>
  30. #import "WebKitNightlyEnablerSparkle.h"
  31. static void enableWebKitNightlyBehaviour() __attribute__ ((constructor));
  32. static NSString *WKNERunState = @"WKNERunState";
  33. static NSString *WKNEShouldMonitorShutdowns = @"WKNEShouldMonitorShutdowns";
  34. typedef enum {
  35. RunStateShutDown,
  36. RunStateInitializing,
  37. RunStateRunning
  38. } WKNERunStates;
  39. static char *webKitAppPath;
  40. static bool extensionBundlesWereLoaded = NO;
  41. static NSSet *extensionPaths = nil;
  42. static int32_t systemVersion()
  43. {
  44. static SInt32 version = 0;
  45. if (!version)
  46. Gestalt(gestaltSystemVersion, &version);
  47. return version;
  48. }
  49. static void myBundleDidLoad(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
  50. {
  51. NSBundle *bundle = (NSBundle *)object;
  52. NSString *bundlePath = [[bundle bundlePath] stringByAbbreviatingWithTildeInPath];
  53. NSString *bundleFileName = [bundlePath lastPathComponent];
  54. // Explicitly ignore SIMBL.bundle, as its only purpose is to load extensions
  55. // on a per-application basis. It's presence indicates a user has application
  56. // extensions, but not that any will be loaded into Safari
  57. if ([bundleFileName isEqualToString:@"SIMBL.bundle"])
  58. return;
  59. // If the bundle lives inside a known extension path, flag it as an extension
  60. NSEnumerator *e = [extensionPaths objectEnumerator];
  61. NSString *path = nil;
  62. while ((path = [e nextObject])) {
  63. if ([bundlePath length] < [path length])
  64. continue;
  65. if ([[bundlePath substringToIndex:[path length]] isEqualToString:path]) {
  66. NSLog(@"Extension detected: %@", bundlePath);
  67. extensionBundlesWereLoaded = YES;
  68. break;
  69. }
  70. }
  71. }
  72. static void myApplicationWillFinishLaunching(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
  73. {
  74. CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching, NULL, NULL);
  75. CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), &myBundleDidLoad, NULL, NULL);
  76. [extensionPaths release];
  77. extensionPaths = nil;
  78. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  79. [userDefaults setInteger:RunStateRunning forKey:WKNERunState];
  80. [userDefaults synchronize];
  81. if (extensionBundlesWereLoaded)
  82. NSRunInformationalAlertPanel(@"Safari extensions detected",
  83. @"Safari extensions were detected on your system. Extensions are incompatible with nightly builds of WebKit, and may cause crashes or incorrect behavior. Please disable them if you experience such behavior.", @"Continue",
  84. nil, nil);
  85. initializeSparkle();
  86. }
  87. static void myApplicationWillTerminate(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
  88. {
  89. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  90. [userDefaults setInteger:RunStateShutDown forKey:WKNERunState];
  91. [userDefaults synchronize];
  92. }
  93. NSBundle *webKitLauncherBundle()
  94. {
  95. NSString *executablePath = [NSString stringWithUTF8String:webKitAppPath];
  96. NSRange appLocation = [executablePath rangeOfString:@".app/" options:NSBackwardsSearch];
  97. NSString *appPath = [executablePath substringToIndex:appLocation.location + appLocation.length];
  98. return [NSBundle bundleWithPath:appPath];
  99. }
  100. extern char **_CFGetProcessPath() __attribute__((weak));
  101. extern OSStatus _RegisterApplication(CFDictionaryRef additionalAppInfoRef, ProcessSerialNumber* myPSN) __attribute__((weak));
  102. static void poseAsWebKitApp()
  103. {
  104. webKitAppPath = strdup(getenv("WebKitAppPath"));
  105. if (!webKitAppPath)
  106. return;
  107. unsetenv("WebKitAppPath");
  108. // Set up the main bundle early so it points at Safari.app
  109. CFBundleGetMainBundle();
  110. if (systemVersion() < 0x1060) {
  111. if (!_CFGetProcessPath)
  112. return;
  113. // Fiddle with CoreFoundation to have it pick up the executable path as being within WebKit.app
  114. char **processPath = _CFGetProcessPath();
  115. *processPath = NULL;
  116. setenv("CFProcessPath", webKitAppPath, 1);
  117. _CFGetProcessPath();
  118. unsetenv("CFProcessPath");
  119. } else {
  120. if (!_RegisterApplication)
  121. return;
  122. // Register the application with LaunchServices, passing a customized registration dictionary that
  123. // uses the WebKit launcher as the application bundle.
  124. NSBundle *bundle = webKitLauncherBundle();
  125. NSMutableDictionary *checkInDictionary = [[bundle infoDictionary] mutableCopy];
  126. [checkInDictionary setObject:[bundle bundlePath] forKey:@"LSBundlePath"];
  127. [checkInDictionary setObject:[checkInDictionary objectForKey:(NSString *)kCFBundleNameKey] forKey:@"LSDisplayName"];
  128. _RegisterApplication((CFDictionaryRef)checkInDictionary, 0);
  129. [checkInDictionary release];
  130. }
  131. }
  132. static BOOL insideSafari4OnTigerTrampoline()
  133. {
  134. // If we're not on Tiger then we can't be in the trampoline state.
  135. if ((systemVersion() & 0xFFF0) != 0x1040)
  136. return NO;
  137. // If we're running Safari < 4.0 then we can't be in the trampoline state.
  138. CFBundleRef safariBundle = CFBundleGetMainBundle();
  139. CFStringRef safariVersion = CFBundleGetValueForInfoDictionaryKey(safariBundle, CFSTR("CFBundleShortVersionString"));
  140. if ([(NSString *)safariVersion intValue] < 4)
  141. return NO;
  142. const char* frameworkPath = getenv("DYLD_FRAMEWORK_PATH");
  143. if (!frameworkPath)
  144. frameworkPath = "";
  145. // If the framework search path is empty or otherwise does not contain the Safari
  146. // framework's Frameworks directory then we are in the trampoline state.
  147. const char safariFrameworkSearchPath[] = "/System/Library/PrivateFrameworks/Safari.framework/Frameworks";
  148. return strstr(frameworkPath, safariFrameworkSearchPath) == 0;
  149. }
  150. static void enableWebKitNightlyBehaviour()
  151. {
  152. // If we're inside Safari in its trampoline state, it will very shortly relaunch itself.
  153. // We bail out here so that we'll be called again in the freshly-launched Safari process.
  154. if (insideSafari4OnTigerTrampoline())
  155. return;
  156. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  157. unsetenv("DYLD_INSERT_LIBRARIES");
  158. poseAsWebKitApp();
  159. extensionPaths = [[NSSet alloc] initWithObjects:@"~/Library/InputManagers/", @"/Library/InputManagers/",
  160. @"~/Library/Application Support/SIMBL/Plugins/", @"/Library/Application Support/SIMBL/Plugins/",
  161. @"~/Library/Application Enhancers/", @"/Library/Application Enhancers/",
  162. nil];
  163. // As of 2008-11 attempting to load Saft would cause a crash on launch, so prevent it from being loaded.
  164. NSArray *disabledInputManagers = [NSArray arrayWithObjects:@"Saft", nil];
  165. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  166. NSDictionary *defaultPrefs = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:RunStateShutDown], WKNERunState,
  167. [NSNumber numberWithBool:YES], WKNEShouldMonitorShutdowns,
  168. disabledInputManagers, @"NSDisabledInputManagers", nil];
  169. [userDefaults registerDefaults:defaultPrefs];
  170. if ([userDefaults boolForKey:WKNEShouldMonitorShutdowns]) {
  171. WKNERunStates savedState = (WKNERunStates)[userDefaults integerForKey:WKNERunState];
  172. if (savedState == RunStateInitializing) {
  173. // Use CoreFoundation here as AppKit hasn't been initialized at this stage of Safari's lifetime
  174. CFOptionFlags responseFlags;
  175. CFUserNotificationDisplayAlert(0, kCFUserNotificationCautionAlertLevel,
  176. NULL, NULL, NULL,
  177. CFSTR("WebKit failed to open correctly"),
  178. CFSTR("WebKit failed to open correctly on your previous attempt. Please disable any Safari extensions that you may have installed. If the problem continues to occur, please file a bug report at http://webkit.org/quality/reporting.html"),
  179. CFSTR("Continue"), NULL, NULL, &responseFlags);
  180. }
  181. else if (savedState == RunStateRunning) {
  182. NSLog(@"WebKit failed to shut down cleanly. Checking for Safari extensions.");
  183. CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myBundleDidLoad,
  184. myBundleDidLoad, (CFStringRef) NSBundleDidLoadNotification,
  185. NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  186. }
  187. }
  188. [userDefaults setInteger:RunStateInitializing forKey:WKNERunState];
  189. [userDefaults synchronize];
  190. CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillFinishLaunching,
  191. myApplicationWillFinishLaunching, (CFStringRef) NSApplicationWillFinishLaunchingNotification,
  192. NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  193. CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), &myApplicationWillTerminate,
  194. myApplicationWillTerminate, (CFStringRef) NSApplicationWillTerminateNotification,
  195. NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  196. NSLog(@"WebKit %@ initialized.", [webKitLauncherBundle() objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
  197. [pool release];
  198. }