WebKitLauncherURLProtocol.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #import "WebKitLauncherURLProtocol.h"
  26. #import "WebKitNightlyEnabler.h"
  27. #if ENABLE_SPARKLE
  28. #import <AppKit/AppKit.h>
  29. #import <Sparkle/Sparkle.h>
  30. #endif
  31. @interface WebKitLauncherURLProtocol (ImplementationDetails)
  32. -(void)handleIsWebKitLauncherAvailableJS;
  33. -(void)handleCheckForUpdates;
  34. -(void)resourceNotFound;
  35. @end
  36. @implementation WebKitLauncherURLProtocol
  37. +(void)load
  38. {
  39. [NSURLProtocol registerClass:self];
  40. }
  41. +(BOOL)canInitWithRequest:(NSURLRequest *)request
  42. {
  43. if (![[[request URL] scheme] isEqualToString:@"x-webkit-launcher"])
  44. return NO;
  45. NSURL *mainDocumentURL = [request mainDocumentURL];
  46. if (!mainDocumentURL)
  47. return NO;
  48. NSString *mainDocumentHost = [mainDocumentURL host];
  49. if (![mainDocumentHost isEqualToString:@"webkit.org"] && ![mainDocumentHost hasSuffix:@".webkit.org"])
  50. return NO;
  51. return YES;
  52. }
  53. +(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
  54. {
  55. return request;
  56. }
  57. -(void)startLoading
  58. {
  59. NSURLRequest *request = [self request];
  60. NSString *resourceSpecifier = [[request URL] resourceSpecifier];
  61. if ([resourceSpecifier isEqualToString:@"is-x-webkit-launcher-available.js"]) {
  62. [self handleIsWebKitLauncherAvailableJS];
  63. return;
  64. }
  65. #if ENABLE_SPARKLE
  66. if ([resourceSpecifier isEqualToString:@"check-for-updates"]) {
  67. [self handleCheckForUpdates];
  68. return;
  69. }
  70. #endif
  71. [self resourceNotFound];
  72. }
  73. -(void)stopLoading
  74. {
  75. }
  76. -(void)handleIsWebKitLauncherAvailableJS
  77. {
  78. id client = [self client];
  79. NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/javascript" expectedContentLength:0 textEncodingName:@"utf-8"];
  80. [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
  81. [response release];
  82. NSData *data = [@"var isWebKitLauncherAvailable = true;" dataUsingEncoding:NSUTF8StringEncoding];
  83. [client URLProtocol:self didLoadData:data];
  84. [client URLProtocolDidFinishLoading:self];
  85. }
  86. #if ENABLE_SPARKLE
  87. -(void)handleCheckForUpdates
  88. {
  89. id client = [self client];
  90. NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/plain" expectedContentLength:0 textEncodingName:@"utf-8"];
  91. [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
  92. [response release];
  93. SUUpdater *updater = [SUUpdater updaterForBundle:webKitLauncherBundle()];
  94. [updater performSelectorOnMainThread:@selector(checkForUpdates:) withObject:self waitUntilDone:NO];
  95. [client URLProtocolDidFinishLoading:self];
  96. }
  97. #endif
  98. -(void)resourceNotFound
  99. {
  100. id client = [self client];
  101. NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:NSErrorFailingURLStringKey forKey:[[self request] URL]];
  102. NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:infoDictionary];
  103. [client URLProtocol:self didFailWithError:error];
  104. }
  105. @end