Upstream WebKit Nightly’s WebKitLauncher
https://bugs.webkit.org/show_bug.cgi?id=117418

Reviewed by Mark Rowe.

Literal patch from Nightly's OpenSource repo.

* WebKitLauncher/Configurations/Base.xcconfig:
* WebKitLauncher/Configurations/WebKitLauncher.xcconfig:
* WebKitLauncher/Info.plist:
* WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj:
* WebKitLauncher/WebKitLauncherURLProtocol.m:
(+[WebKitLauncherURLProtocol load]):
(+[WebKitLauncherURLProtocol canInitWithRequest:]):
(-[WebKitLauncherURLProtocol startLoading]):
(-[WebKitLauncherURLProtocol handleWebKitVersionInformation]):
(-[WebKitLauncherURLProtocol resourceNotFound]):
* WebKitLauncher/WebKitNightlyEnablerSparkle.m:
(initializeSparkle):
* WebKitLauncher/main.m:
(determineExecutablePath):
(currentMacOSXVersion):
(main):
* WebKitLauncher/start.html:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@152186 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index d03fd21..fd85702 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,30 @@
+2013-06-28  David Farler  <dfarler@apple.com>
+
+        Upstream WebKit Nightly’s WebKitLauncher
+        https://bugs.webkit.org/show_bug.cgi?id=117418
+
+        Reviewed by Mark Rowe.
+
+        Literal patch from Nightly's OpenSource repo.
+
+        * WebKitLauncher/Configurations/Base.xcconfig:
+        * WebKitLauncher/Configurations/WebKitLauncher.xcconfig:
+        * WebKitLauncher/Info.plist:
+        * WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj:
+        * WebKitLauncher/WebKitLauncherURLProtocol.m:
+        (+[WebKitLauncherURLProtocol load]):
+        (+[WebKitLauncherURLProtocol canInitWithRequest:]):
+        (-[WebKitLauncherURLProtocol startLoading]):
+        (-[WebKitLauncherURLProtocol handleWebKitVersionInformation]):
+        (-[WebKitLauncherURLProtocol resourceNotFound]):
+        * WebKitLauncher/WebKitNightlyEnablerSparkle.m:
+        (initializeSparkle):
+        * WebKitLauncher/main.m:
+        (determineExecutablePath):
+        (currentMacOSXVersion):
+        (main):
+        * WebKitLauncher/start.html:
+
 2013-06-28  Balazs Kelemen  <b.kelemen@samsung.com>
 
         [EFL] back/forward keyboard shortcuts are non-intuitive
diff --git a/Tools/WebKitLauncher/Configurations/Base.xcconfig b/Tools/WebKitLauncher/Configurations/Base.xcconfig
index 149eb82..5aceb3b 100644
--- a/Tools/WebKitLauncher/Configurations/Base.xcconfig
+++ b/Tools/WebKitLauncher/Configurations/Base.xcconfig
@@ -21,19 +21,17 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
-ARCHS = $(ARCHS_STANDARD_32_64_BIT);
+ARCHS = i386 x86_64;
 DEAD_CODE_STRIPPING = YES;
 DEBUG_INFORMATION_FORMAT = dwarf-with-dsym;
 GCC_DEBUGGING_SYMBOLS = full;
 GCC_MODEL_TUNING = G5;
 GCC_PREPROCESSOR_DEFINITIONS = ENABLE_SPARKLE=$(ENABLE_SPARKLE);
 GCC_TREAT_WARNINGS_AS_ERRORS = YES;
-GCC_VERSION = 4.0;
-GCC_VERSION[arch=x86_64] = 4.2;
-MACOSX_DEPLOYMENT_TARGET = 10.4;
-MACOSX_DEPLOYMENT_TARGET[arch=x86_64] = 10.5;
+GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
+MACOSX_DEPLOYMENT_TARGET = 10.6;
 PREBINDING = NO;
-SDKROOT = macosx10.5;
+SDKROOT = macosx;
 WARNING_CFLAGS = -Wall;
 
 ENABLE_SPARKLE = 0;
diff --git a/Tools/WebKitLauncher/Configurations/WebKitLauncher.xcconfig b/Tools/WebKitLauncher/Configurations/WebKitLauncher.xcconfig
index c4dde36..fd0124e 100644
--- a/Tools/WebKitLauncher/Configurations/WebKitLauncher.xcconfig
+++ b/Tools/WebKitLauncher/Configurations/WebKitLauncher.xcconfig
@@ -24,4 +24,3 @@
 INFOPLIST_FILE = Info.plist;
 PRODUCT_NAME = WebKit;
 WRAPPER_EXTENSION = app;
-MACOSX_DEPLOYMENT_TARGET[arch=ppc] = 10.2;
diff --git a/Tools/WebKitLauncher/Info.plist b/Tools/WebKitLauncher/Info.plist
index 83564a7..7141ec8 100644
--- a/Tools/WebKitLauncher/Info.plist
+++ b/Tools/WebKitLauncher/Info.plist
@@ -428,13 +428,9 @@
 	<array>
 		<string>x86_64</string>
 		<string>i386</string>
-		<string>ppc</string>
 	</array>
-	<key>LSMinimumSystemVersionByArchitecture</key>
-	<dict>
-		<key>x86_64</key>
-		<string>10.6</string>
-	</dict>
+	<key>LSMinimumSystemVersion</key>
+	<string>10.6</string>
 	<key>SUFeedURL</key>
 	<string>http://nightly.webkit.org/builds/trunk/mac/rss</string>
 	<key>SUShowReleaseNotes</key>
diff --git a/Tools/WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj b/Tools/WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj
index 4cf2f12e..71fc111 100644
--- a/Tools/WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj
+++ b/Tools/WebKitLauncher/WebKitLauncher.xcodeproj/project.pbxproj
@@ -33,6 +33,7 @@
 		5DA88F6E0FC8136000AB2F62 /* WebKitNightlyEnablerSparkle.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA88F6C0FC8136000AB2F62 /* WebKitNightlyEnablerSparkle.m */; };
 		5DA88F7A0FC813EB00AB2F62 /* WebKitNightlyEnabler.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA88F790FC813EB00AB2F62 /* WebKitNightlyEnabler.h */; };
 		5DB70525097B94CD009875EC /* webkit.icns in Resources */ = {isa = PBXBuildFile; fileRef = 5DB70524097B94CD009875EC /* webkit.icns */; };
+		5DE0853815AE0AA0000A27B4 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5DE0853715AE0AA0000A27B4 /* Security.framework */; };
 		8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
 		8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
 /* End PBXBuildFile section */
@@ -78,6 +79,7 @@
 		5DA88F7F0FC8176100AB2F62 /* WebKitLauncher.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = WebKitLauncher.xcconfig; sourceTree = "<group>"; };
 		5DA88F800FC8176100AB2F62 /* WebKitNightlyEnabler.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = WebKitNightlyEnabler.xcconfig; sourceTree = "<group>"; };
 		5DB70524097B94CD009875EC /* webkit.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = webkit.icns; sourceTree = "<group>"; };
+		5DE0853715AE0AA0000A27B4 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
 		5DE79BEB0F1C63CC0067BE08 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
 		8D1107320486CEB800E47090 /* WebKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WebKit.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -96,6 +98,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				5DE0853815AE0AA0000A27B4 /* Security.framework in Frameworks */,
 				8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -124,6 +127,7 @@
 		29B97314FDCFA39411CA2CEA /* WebKit */ = {
 			isa = PBXGroup;
 			children = (
+				5DE0853715AE0AA0000A27B4 /* Security.framework */,
 				29B97315FDCFA39411CA2CEA /* Source */,
 				5DA88F7D0FC8174E00AB2F62 /* Configurations */,
 				29B97317FDCFA39411CA2CEA /* Resources */,
@@ -237,6 +241,9 @@
 /* Begin PBXProject section */
 		29B97313FDCFA39411CA2CEA /* Project object */ = {
 			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0420;
+			};
 			buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "WebKitLauncher" */;
 			compatibilityVersion = "Xcode 3.1";
 			developmentRegion = English;
diff --git a/Tools/WebKitLauncher/WebKitLauncherURLProtocol.m b/Tools/WebKitLauncher/WebKitLauncherURLProtocol.m
index 9e56857..f1a945f 100644
--- a/Tools/WebKitLauncher/WebKitLauncherURLProtocol.m
+++ b/Tools/WebKitLauncher/WebKitLauncherURLProtocol.m
@@ -31,6 +31,10 @@
 #import <Sparkle/Sparkle.h>
 #endif
 
+@interface NSObject (WKBrowsingContextControllerMethods)
++ (void)registerSchemeForCustomProtocol:(NSString *)scheme;
+@end
+
 @interface WebKitLauncherURLProtocol (ImplementationDetails)
 -(void)handleIsWebKitLauncherAvailableJS;
 -(void)handleCheckForUpdates;
@@ -41,6 +45,7 @@
 
 +(void)load
 {
+    [NSClassFromString(@"WKBrowsingContextController") registerSchemeForCustomProtocol:@"x-webkit-launcher"];
     [NSURLProtocol registerClass:self];
 }
 
@@ -53,6 +58,9 @@
     if (!mainDocumentURL)
         return NO;
 
+    if ([[mainDocumentURL scheme] isEqualToString:@"file"])
+        return YES;
+
     NSString *mainDocumentHost = [mainDocumentURL host];
     if (![mainDocumentHost isEqualToString:@"webkit.org"] && ![mainDocumentHost hasSuffix:@".webkit.org"])
         return NO;
@@ -73,6 +81,10 @@
         [self handleIsWebKitLauncherAvailableJS];
         return;
     }
+    if ([resourceSpecifier isEqualToString:@"webkit-version-information.js"]) {
+        [self handleWebKitVersionInformation];
+        return;
+    }
 #if ENABLE_SPARKLE
     if ([resourceSpecifier isEqualToString:@"check-for-updates"]) {
         [self handleCheckForUpdates];
@@ -98,6 +110,26 @@
     [client URLProtocolDidFinishLoading:self];
 }
 
+-(void)handleWebKitVersionInformation
+{
+    id client = [self client];
+    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/javascript" expectedContentLength:0 textEncodingName:@"utf-8"];
+    [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
+    [response release];
+
+    NSBundle *bundle = webKitLauncherBundle();
+    int revision = [[[bundle infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey] intValue];
+    NSString *branch = [NSString stringWithContentsOfURL:[bundle URLForResource:@"BRANCH" withExtension:nil] encoding:NSUTF8StringEncoding error:0];
+    branch = [branch stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
+    if (!branch)
+        branch = @"trunk";
+
+    NSString *script = [NSString stringWithFormat:@"var webKitRevision = %d; var webKitBranch = \"%@\";", revision, branch];
+    NSData *data = [script dataUsingEncoding:NSUTF8StringEncoding];
+    [client URLProtocol:self didLoadData:data];
+    [client URLProtocolDidFinishLoading:self];
+}
+
 #if ENABLE_SPARKLE
 -(void)handleCheckForUpdates
 {
@@ -115,7 +147,7 @@
 -(void)resourceNotFound
 {
     id client = [self client];
-    NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:NSErrorFailingURLStringKey forKey:[[self request] URL]];
+    NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:NSURLErrorFailingURLStringErrorKey forKey:[[self request] URL]];
     NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:infoDictionary];
     [client URLProtocol:self didFailWithError:error];
 }
diff --git a/Tools/WebKitLauncher/WebKitNightlyEnablerSparkle.m b/Tools/WebKitLauncher/WebKitNightlyEnablerSparkle.m
index 8b13ab8..d604072 100644
--- a/Tools/WebKitLauncher/WebKitNightlyEnablerSparkle.m
+++ b/Tools/WebKitLauncher/WebKitNightlyEnablerSparkle.m
@@ -102,19 +102,6 @@
 
 @end
 
-#if __LP64__
-
-#define setMethodImplementation method_setImplementation
-
-#else
-
-static void setMethodImplementation(Method m, IMP imp)
-{
-    m->method_imp = imp;
-}
-
-#endif
-
 static NSString *userAgentStringForSparkle()
 {
     NSBundle *safariBundle = [NSBundle mainBundle];
@@ -130,10 +117,10 @@
 {
     // Override some Sparkle behaviour
     Method methodToPatch = class_getInstanceMethod(objc_getRequiredClass("SUUpdatePermissionPrompt"), @selector(promptDescription));
-    setMethodImplementation(methodToPatch, (IMP)updatePermissionPromptDescription);
+    method_setImplementation(methodToPatch, (IMP)updatePermissionPromptDescription);
 
     methodToPatch = class_getInstanceMethod(objc_getRequiredClass("SUUpdateAlert"), @selector(initWithAppcastItem:host:));
-    setMethodImplementation(methodToPatch, (IMP)updateAlertInitForAlertPanel);
+    method_setImplementation(methodToPatch, (IMP)updateAlertInitForAlertPanel);
 
     SUUpdater *updater = [SUUpdater updaterForBundle:webKitLauncherBundle()];
     [updater setUserAgentString:userAgentStringForSparkle()];
diff --git a/Tools/WebKitLauncher/main.m b/Tools/WebKitLauncher/main.m
index 4fee68a..45e4a0d 100644
--- a/Tools/WebKitLauncher/main.m
+++ b/Tools/WebKitLauncher/main.m
@@ -157,13 +157,39 @@
     return safariBundle;
 }
 
+static NSString *determineExecutablePath(NSBundle *bundle)
+{
+    NSString *safariExecutablePath = [bundle executablePath];
+
+    NSString *safariForWebKitDevelopmentExecutablePath = [bundle pathForAuxiliaryExecutable:@"SafariForWebKitDevelopment"];
+    if (![[NSFileManager defaultManager] fileExistsAtPath:safariForWebKitDevelopmentExecutablePath])
+        return safariExecutablePath;
+
+    SecStaticCodeRef staticCode;
+    if (SecStaticCodeCreateWithPath((CFURLRef)[bundle executableURL], kSecCSDefaultFlags, &staticCode) != noErr)
+        return [bundle executablePath];
+
+    NSDictionary *codeInformation;
+    if (SecCodeCopySigningInformation(staticCode, kSecCSRequirementInformation, (CFDictionaryRef*)&codeInformation) != noErr) {
+        CFRelease(staticCode);
+        return safariExecutablePath;
+    }
+    CFRelease(staticCode);
+    [codeInformation autorelease];
+
+    if ([codeInformation objectForKey:(id)kSecCodeInfoEntitlements])
+        return safariForWebKitDevelopmentExecutablePath;
+
+    return safariExecutablePath;
+}
+
 static NSString *currentMacOSXVersion()
 {
     SInt32 version;
     if (Gestalt(gestaltSystemVersion, &version) != noErr)
         return @"10.4";
 
-    return [NSString stringWithFormat:@"%x.%x", (version & 0xFF00) >> 8, (version & 0x00F0) >> 4];
+    return [NSString stringWithFormat:@"%lx.%lx", (long)(version & 0xFF00) >> 8, (long)(version & 0x00F0) >> 4l];
 }
 
 static NSString *fallbackMacOSXVersion(NSString *systemVersion)
@@ -209,13 +235,13 @@
     }
 
     if (!frameworkPathIsUsable)
-        displayErrorAndQuit([NSString stringWithFormat:@"Mac OS X %@ is not supported", systemVersion],
-                            [NSString stringWithFormat:@"Nightly builds of WebKit are not supported on Mac OS X %@ at this time.", systemVersion]);
+        displayErrorAndQuit([NSString stringWithFormat:@"OS X %@ is not supported", systemVersion],
+                            [NSString stringWithFormat:@"Nightly builds of WebKit are not supported on OS X %@ at this time.", systemVersion]);
 
     NSString *pathToEnablerLib = [[NSBundle mainBundle] pathForResource:@"WebKitNightlyEnabler" ofType:@"dylib"];
 
     NSBundle *safariBundle = locateSafariBundle();
-    NSString *executablePath = [safariBundle executablePath];
+    NSString *executablePath = determineExecutablePath(safariBundle);
 
     if (!checkSafariVersion(safariBundle)) {
         NSString *safariVersion = [[safariBundle localizedInfoDictionary] objectForKey:@"CFBundleShortVersionString"];
diff --git a/Tools/WebKitLauncher/start.html b/Tools/WebKitLauncher/start.html
index 2c94e79..bdb593e 100644
--- a/Tools/WebKitLauncher/start.html
+++ b/Tools/WebKitLauncher/start.html
@@ -1,31 +1,15 @@
 <html>
     <head>
         <title>Loading WebKit...</title>
-        <meta http-equiv="refresh" content="1;URL=http://nightly.webkit.org/start/" />
+        <meta http-equiv="refresh" content="1;URL=http://nightly.webkit.org/start/">
         <script type="text/javascript">
-            function getWebKitRevision()
-            {
-                var request = new XMLHttpRequest();
-                request.open("GET", "VERSION", false);
-                request.send();
-                var revision = parseInt(request.responseText);
-                if (isNaN(revision))
-                    return "";
-
-                return revision;
+            var script = document.createElement('script');
+            script.src = "x-webkit-launcher:webkit-version-information.js";
+            script.onload = function() {
+                if (webKitBranch && webKitRevision)
+                    document.location = "http://nightly.webkit.org/start/" + webKitBranch + "/" + webKitRevision;
             }
-
-            function getWebKitBranch()
-            {
-                var request = new XMLHttpRequest();
-                request.open("GET", "BRANCH", false);
-                request.send();
-                return (request.responseText || "trunk").replace(/\s/g, '')
-            }
-
-            var revision = getWebKitRevision();
-            var branch = getWebKitBranch();
-            document.location = "http://nightly.webkit.org/start/" + branch + "/" + revision;
+            document.documentElement.appendChild(script);
         </script>
     </head>
     <body>