2 * Copyright (C) 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #import "WebKitLauncherURLProtocol.h"
27 #import "WebKitNightlyEnabler.h"
30 #import <AppKit/AppKit.h>
31 #import <Sparkle/Sparkle.h>
34 @interface WebKitLauncherURLProtocol (ImplementationDetails)
35 -(void)handleIsWebKitLauncherAvailableJS;
36 -(void)handleCheckForUpdates;
37 -(void)resourceNotFound;
40 @implementation WebKitLauncherURLProtocol
44 [NSURLProtocol registerClass:self];
47 +(BOOL)canInitWithRequest:(NSURLRequest *)request
49 if (![[[request URL] scheme] isEqualToString:@"x-webkit-launcher"])
52 NSURL *mainDocumentURL = [request mainDocumentURL];
56 NSString *mainDocumentHost = [mainDocumentURL host];
57 if (![mainDocumentHost isEqualToString:@"webkit.org"] && ![mainDocumentHost hasSuffix:@".webkit.org"])
63 +(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
70 NSURLRequest *request = [self request];
71 NSString *resourceSpecifier = [[request URL] resourceSpecifier];
72 if ([resourceSpecifier isEqualToString:@"is-x-webkit-launcher-available.js"]) {
73 [self handleIsWebKitLauncherAvailableJS];
77 if ([resourceSpecifier isEqualToString:@"check-for-updates"]) {
78 [self handleCheckForUpdates];
82 [self resourceNotFound];
89 -(void)handleIsWebKitLauncherAvailableJS
91 id client = [self client];
92 NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/javascript" expectedContentLength:0 textEncodingName:@"utf-8"];
93 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
96 NSData *data = [@"var isWebKitLauncherAvailable = true;" dataUsingEncoding:NSUTF8StringEncoding];
97 [client URLProtocol:self didLoadData:data];
98 [client URLProtocolDidFinishLoading:self];
102 -(void)handleCheckForUpdates
104 id client = [self client];
105 NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/plain" expectedContentLength:0 textEncodingName:@"utf-8"];
106 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
109 SUUpdater *updater = [SUUpdater updaterForBundle:webKitLauncherBundle()];
110 [updater performSelectorOnMainThread:@selector(checkForUpdates:) withObject:self waitUntilDone:NO];
111 [client URLProtocolDidFinishLoading:self];
115 -(void)resourceNotFound
117 id client = [self client];
118 NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:NSErrorFailingURLStringKey forKey:[[self request] URL]];
119 NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:infoDictionary];
120 [client URLProtocol:self didFailWithError:error];