WebKitNightlyEnablerSparkle.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if !ENABLE_SPARKLE
  26. void initializeSparkle()
  27. {
  28. // No-op.
  29. }
  30. #else // ENABLE_SPARKLE
  31. #import <Cocoa/Cocoa.h>
  32. #import <Sparkle/SUUpdater.h>
  33. #import <objc/objc-runtime.h>
  34. #import "WebKitNightlyEnabler.h"
  35. // We need to tweak the wording of the prompt to make sense in the context of WebKit and Safari.
  36. static NSString* updatePermissionPromptDescription(id self, SEL _cmd)
  37. {
  38. return @"Should WebKit automatically check for updates? You can always check for updates manually from the Safari menu.";
  39. }
  40. static NSPanel *updateAlertPanel(id updateItem, id host)
  41. {
  42. NSString *hostName = objc_msgSend(host, @selector(name));
  43. NSPanel *panel = NSGetInformationalAlertPanel([NSString stringWithFormat:@"Would you like to download and install %@ %@ now?", hostName, objc_msgSend(updateItem, @selector(displayVersionString))],
  44. [NSString stringWithFormat:@"You are currently running %@ %@.", hostName, objc_msgSend(host, @selector(displayVersion))],
  45. @"Install Update", @"Skip This Version", @"Remind Me Later");
  46. NSArray *subviews = [[panel contentView] subviews];
  47. NSEnumerator *e = [subviews objectEnumerator];
  48. NSView *view;
  49. while ((view = [e nextObject])) {
  50. if (![view isKindOfClass:[NSButton class]])
  51. continue;
  52. NSButton *button = (NSButton *)view;
  53. [button setAction:@selector(webKitHandleButtonPress:)];
  54. if ([button tag] == NSAlertOtherReturn)
  55. [button setKeyEquivalent:@"\033"];
  56. }
  57. [panel center];
  58. return panel;
  59. }
  60. // Sparkle's udpate alert panel looks odd with the release notes hidden, so we
  61. // swap it out with a standard NSAlert-style panel instead.
  62. static id updateAlertInitForAlertPanel(id self, SEL _cmd, id updateItem, id host)
  63. {
  64. NSPanel *panel = updateAlertPanel(updateItem, host);
  65. [panel setDelegate:self];
  66. self = [self initWithWindow:panel];
  67. if (!self)
  68. return nil;
  69. [updateItem retain];
  70. [host retain];
  71. object_setInstanceVariable(self, "updateItem", (void*)updateItem);
  72. object_setInstanceVariable(self, "host", (void*)host);
  73. [self setShouldCascadeWindows:NO];
  74. return self;
  75. }
  76. @implementation NSAlert (WebKitLauncherExtensions)
  77. - (void)webKitHandleButtonPress:(id)sender
  78. {
  79. // We rely on the fact that NSAlertOtherReturn == -1, NSAlertAlternateReturn == 0 and NSAlertDefaultReturn == 1
  80. // to map the button tag to the corresponding selector
  81. SEL selectors[] = { @selector(remindMeLater:), @selector(skipThisVersion:), @selector(installUpdate:) };
  82. SEL selector = selectors[[sender tag] + 1];
  83. id delegate = [[sender window] delegate];
  84. objc_msgSend(delegate, selector, sender);
  85. }
  86. @end
  87. #if __LP64__
  88. #define setMethodImplementation method_setImplementation
  89. #else
  90. static void setMethodImplementation(Method m, IMP imp)
  91. {
  92. m->method_imp = imp;
  93. }
  94. #endif
  95. static NSString *userAgentStringForSparkle()
  96. {
  97. NSBundle *safariBundle = [NSBundle mainBundle];
  98. NSString *safariVersion = [[safariBundle localizedInfoDictionary] valueForKey:@"CFBundleShortVersionString"];
  99. NSString *safariBuild = [[[safariBundle infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey] substringFromIndex:1];
  100. NSString *webKitRevision = [[webKitLauncherBundle() infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey];
  101. NSString *applicationName = [NSString stringWithFormat:@"Version/%@ Safari/%@ WebKitRevision/%@", safariVersion, safariBuild, webKitRevision];
  102. Class WebView = objc_lookUpClass("WebView");
  103. return objc_msgSend(WebView, @selector(_standardUserAgentWithApplicationName:), applicationName);
  104. }
  105. void initializeSparkle()
  106. {
  107. // Override some Sparkle behaviour
  108. Method methodToPatch = class_getInstanceMethod(objc_getRequiredClass("SUUpdatePermissionPrompt"), @selector(promptDescription));
  109. setMethodImplementation(methodToPatch, (IMP)updatePermissionPromptDescription);
  110. methodToPatch = class_getInstanceMethod(objc_getRequiredClass("SUUpdateAlert"), @selector(initWithAppcastItem:host:));
  111. setMethodImplementation(methodToPatch, (IMP)updateAlertInitForAlertPanel);
  112. SUUpdater *updater = [SUUpdater updaterForBundle:webKitLauncherBundle()];
  113. [updater setUserAgentString:userAgentStringForSparkle()];
  114. // Find the first separator on the Safari menu…
  115. NSMenu *applicationSubmenu = [[[NSApp mainMenu] itemAtIndex:0] submenu];
  116. int i = 0;
  117. for (; i < [applicationSubmenu numberOfItems]; i++) {
  118. if ([[applicationSubmenu itemAtIndex:i] isSeparatorItem])
  119. break;
  120. }
  121. // … and insert a menu item that can be used to manually trigger update checks.
  122. NSMenuItem *updateMenuItem = [[NSMenuItem alloc] initWithTitle:@"Check for WebKit Updates…" action:@selector(checkForUpdates:) keyEquivalent:@""];
  123. [updateMenuItem setTarget:updater];
  124. [applicationSubmenu insertItem:updateMenuItem atIndex:i];
  125. [updateMenuItem release];
  126. }
  127. #endif // ENABLE_SPARKLE