|
1 /* |
|
2 * Copyright (C) 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 * 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. |
|
12 * |
|
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. |
|
24 */ |
|
25 |
|
26 #import "WebKitLauncherURLProtocol.h" |
|
27 #import "WebKitNightlyEnabler.h" |
|
28 |
|
29 #if ENABLE_SPARKLE |
|
30 #import <AppKit/AppKit.h> |
|
31 #import <Sparkle/Sparkle.h> |
|
32 #endif |
|
33 |
|
34 @interface WebKitLauncherURLProtocol (ImplementationDetails) |
|
35 -(void)handleIsWebKitLauncherAvailableJS; |
|
36 -(void)handleCheckForUpdates; |
|
37 -(void)resourceNotFound; |
|
38 @end |
|
39 |
|
40 @implementation WebKitLauncherURLProtocol |
|
41 |
|
42 +(void)load |
|
43 { |
|
44 [NSURLProtocol registerClass:self]; |
|
45 } |
|
46 |
|
47 +(BOOL)canInitWithRequest:(NSURLRequest *)request |
|
48 { |
|
49 if (![[[request URL] scheme] isEqualToString:@"x-webkit-launcher"]) |
|
50 return NO; |
|
51 |
|
52 NSURL *mainDocumentURL = [request mainDocumentURL]; |
|
53 if (!mainDocumentURL) |
|
54 return NO; |
|
55 |
|
56 NSString *mainDocumentHost = [mainDocumentURL host]; |
|
57 if (![mainDocumentHost isEqualToString:@"webkit.org"] && ![mainDocumentHost hasSuffix:@".webkit.org"]) |
|
58 return NO; |
|
59 |
|
60 return YES; |
|
61 } |
|
62 |
|
63 +(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request |
|
64 { |
|
65 return request; |
|
66 } |
|
67 |
|
68 -(void)startLoading |
|
69 { |
|
70 NSURLRequest *request = [self request]; |
|
71 NSString *resourceSpecifier = [[request URL] resourceSpecifier]; |
|
72 if ([resourceSpecifier isEqualToString:@"is-x-webkit-launcher-available.js"]) { |
|
73 [self handleIsWebKitLauncherAvailableJS]; |
|
74 return; |
|
75 } |
|
76 #if ENABLE_SPARKLE |
|
77 if ([resourceSpecifier isEqualToString:@"check-for-updates"]) { |
|
78 [self handleCheckForUpdates]; |
|
79 return; |
|
80 } |
|
81 #endif |
|
82 [self resourceNotFound]; |
|
83 } |
|
84 |
|
85 -(void)stopLoading |
|
86 { |
|
87 } |
|
88 |
|
89 -(void)handleIsWebKitLauncherAvailableJS |
|
90 { |
|
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]; |
|
94 [response release]; |
|
95 |
|
96 NSData *data = [@"var isWebKitLauncherAvailable = true;" dataUsingEncoding:NSUTF8StringEncoding]; |
|
97 [client URLProtocol:self didLoadData:data]; |
|
98 [client URLProtocolDidFinishLoading:self]; |
|
99 } |
|
100 |
|
101 #if ENABLE_SPARKLE |
|
102 -(void)handleCheckForUpdates |
|
103 { |
|
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]; |
|
107 [response release]; |
|
108 |
|
109 SUUpdater *updater = [SUUpdater updaterForBundle:webKitLauncherBundle()]; |
|
110 [updater performSelectorOnMainThread:@selector(checkForUpdates:) withObject:self waitUntilDone:NO]; |
|
111 [client URLProtocolDidFinishLoading:self]; |
|
112 } |
|
113 #endif |
|
114 |
|
115 -(void)resourceNotFound |
|
116 { |
|
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]; |
|
121 } |
|
122 |
|
123 @end |