From 9fe5e5f86da78ba617752610bcb70a09d0975d08 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 26 Oct 2008 20:03:40 +0000 Subject: [PATCH] Internalized JavaScript activatorial. --- Cydia.mm | 223 ++++++++++++++++++++++++++------------- UICaboodle/BrowserView.h | 5 + makefile | 2 +- 3 files changed, 155 insertions(+), 75 deletions(-) diff --git a/Cydia.mm b/Cydia.mm index fcc59bb9..1cecb660 100644 --- a/Cydia.mm +++ b/Cydia.mm @@ -66,6 +66,8 @@ #import #import +#import + #include #include @@ -1815,9 +1817,14 @@ class Progress : } - (NSString *) rating { - if (NSString *pattern = [Indices_ objectForKey:@"Rating"]) - return [pattern stringByReplacingOccurrencesOfString:@"%@" withString:[id_ stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; - else + if (NSString *rating = [Indices_ objectForKey:@"Rating"]) { + rating = [NSString stringWithFormat:@"%@?package=%@", rating, + [id_ stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + if (latest_ != nil) + rating = [NSString stringWithFormat:@"%@&version=%@", rating, + [latest_ stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + return rating; + } else return nil; } @@ -3955,6 +3962,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } } +- (bool) _loading { + return false; +} + - (void) reloadData { [self setPackage:[database_ packageWithName:name_]]; [self reloadButtons]; @@ -4756,6 +4767,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } #endif +- (bool) _loading { + return false; +} + @end /* }}} */ @@ -4844,7 +4859,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { [scroller_ setDelegate:nil]; - [background_ release]; + if (button_ != nil) + [button_ release]; + if (style_ != nil) + [style_ release]; + if (function_ != nil) + [function_ release]; + [scroller_ release]; [urls_ release]; [indicator_ release]; @@ -4968,12 +4989,57 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { return [confirm boolValue]; } +/* Web Scripting {{{ */ ++ (NSString *) webScriptNameForSelector:(SEL)selector { + if (selector == @selector(getPackageById:)) + return @"getPackageById"; + else if (selector == @selector(setButton:withStyle:toFunction:)) + return @"setButton"; + else if (selector == @selector(supports:)) + return @"supports"; + else + return nil; +} + ++ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { + return [self webScriptNameForSelector:selector] == nil; +} + +- (BOOL) supports:(NSString *)feature { + return [feature isEqualToString:@"window.open"]; +} + +- (Package *) getPackageById:(NSString *)id { + return [[Database sharedInstance] packageWithName:id]; +} + +- (void) setButton:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { + NSLog(@"b:%@", button); + NSLog(@"f:%@", [function class]); + + if (button_ != nil) + [button_ autorelease]; + button_ = button == nil ? nil : [button retain]; + + if (style_ != nil) + [style_ autorelease]; + style_ = style == nil ? nil : [style retain]; + + if (function_ != nil) + [function_ autorelease]; + function_ = function == nil ? nil : [function retain]; +} +/* }}} */ + - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { - [window setValue:delegate_ forKey:@"cydia"]; + [window setValue:self forKey:@"cydia"]; } - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id)listener { if (NSURL *url = [request URL]) { + if (name != nil && [name isEqualToString:@"_open"]) + [delegate_ openURL:url]; + NSLog(@"win:%@:%@", url, [action description]); if (![self getSpecial:url]) { NSString *scheme([[url scheme] lowercaseString]); @@ -5196,7 +5262,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { reloading_ = false; loading_ = true; - [indicator_ startAnimation]; [self reloadButtons]; if (title_ != nil) { @@ -5204,6 +5269,21 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { title_ = nil; } + if (button_ != nil) { + [button_ release]; + button_ = nil; + } + + if (style_ != nil) { + [style_ release]; + style_ = nil; + } + + if (function_ != nil) { + [function_ release]; + function_ = nil; + } + [book_ reloadTitleForPage:self]; WebView *webview = [webview_ webView]; @@ -5220,11 +5300,22 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (void) _finishLoading { if (!reloading_) { loading_ = false; - [indicator_ stopAnimation]; [self reloadButtons]; } } +- (bool) _loading { + return loading_; +} + +- (void) reloadButtons { + if ([self _loading]) + [indicator_ startAnimation]; + else + [indicator_ stopAnimation]; + [super reloadButtons]; +} + - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame { return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame]; } @@ -5245,18 +5336,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { return [webview_ webView:sender didReceiveDocTypeForFrame:frame]; } -- (void) _clearBackground { - [background_ setBackgroundColor:[UIColor pinStripeColor]]; - [background_ setImage:[UIImage applicationImageNamed:@"pinstripe.png"]]; - [scroller_ setShowBackgroundShadow:NO]; -} - - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { if ([frame parentFrame] == nil) { [self _finishLoading]; - [self _clearBackground]; - if (DOMDocument *document = [frame DOMDocument]) if (DOMNodeList *bodies = [document getElementsByTagName:@"body"]) for (DOMHTMLBodyElement *body in bodies) { @@ -5273,7 +5356,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (alpha != 0) { colored = true; - [background_ setBackgroundColor:[UIColor + [scroller_ setBackgroundColor:[UIColor colorWithRed:([[rgb red] getFloatValue:DOM_CSS_NUMBER] / 255) green:([[rgb green] getFloatValue:DOM_CSS_NUMBER] / 255) blue:([[rgb blue] getFloatValue:DOM_CSS_NUMBER] / 255) @@ -5282,14 +5365,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } } - if (DOMCSSPrimitiveValue *image = static_cast([style getPropertyCSSValue:@"background-image"])) { - NSString *src([image getStringValue]); - if ([src isEqualToString:@"none"]) - goto none; - NSLog(@"img:%@", [image getStringValue]); - } else none: if (colored) - [background_ setImage:nil]; - + if (!colored) + [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; break; } } @@ -5320,13 +5397,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { struct CGRect bounds = [self bounds]; - background_ = [[UIImageView alloc] initWithFrame:bounds]; - [self _clearBackground]; - [self addSubview:background_]; - scroller_ = [[UIScroller alloc] initWithFrame:bounds]; [self addSubview:scroller_]; + [scroller_ setShowBackgroundShadow:NO]; + [scroller_ setFixedBackgroundPattern:YES]; + [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; + [scroller_ setScrollingEnabled:YES]; [scroller_ setAdjustForContentSizeChange:YES]; [scroller_ setClipsSubviews:YES]; @@ -5419,7 +5496,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { urls_ = [[NSMutableArray alloc] initWithCapacity:16]; [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; - [background_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; } return self; } @@ -5429,16 +5505,36 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } - (void) _rightButtonClicked { - reloading_ = true; - [self reloadURL]; + if (function_ == nil) { + reloading_ = true; + [self reloadURL]; + } else { + JSObjectRef function([function_ JSObject]); + JSGlobalContextRef context([[[webview_ webView] mainFrame] globalContext]); + JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); + } } - (NSString *) _rightButtonTitle { - return @"Reload"; + return button_ != nil ? button_ : @"Reload"; } - (NSString *) rightButtonTitle { - return loading_ ? @"" : [self _rightButtonTitle]; + return [self _loading] ? @"" : [self _rightButtonTitle]; +} + +- (UINavigationButtonStyle) rightButtonStyle { + if (style_ == nil) normal: + return UINavigationButtonStyleNormal; + else if ([style_ isEqualToString:@"Normal"]) + return UINavigationButtonStyleNormal; + else if ([style_ isEqualToString:@"Back"]) + return UINavigationButtonStyleBack; + else if ([style_ isEqualToString:@"Highlighted"]) + return UINavigationButtonStyleHighlighted; + else if ([style_ isEqualToString:@"Destructive"]) + return UINavigationButtonStyleDestructive; + else goto normal; } - (NSString *) title { @@ -5742,6 +5838,16 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { return request; } +- (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { + id client([self client]); + NSData *data(UIImagePNGRepresentation(icon)); + + NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); + [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + [client URLProtocol:self didLoadData:data]; + [client URLProtocolDidFinishLoading:self]; +} + - (void) startLoading { id client([self client]); NSURLRequest *request([self request]); @@ -5770,47 +5876,32 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { Package *package([database packageWithName:path]); if (package == nil) goto fail; - UIImage *icon([package icon]); - - NSData *data(UIImagePNGRepresentation(icon)); - - NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); - [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - [client URLProtocol:self didLoadData:data]; - [client URLProtocolDidFinishLoading:self]; + [self _returnPNGWithImage:icon forRequest:request]; } else if ([command isEqualToString:@"source-icon"]) { if (path == nil) goto fail; path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *source(Simplify(path)); - UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]); if (icon == nil) icon = [UIImage applicationImageNamed:@"unknown.png"]; - - NSData *data(UIImagePNGRepresentation(icon)); - - NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); - [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - [client URLProtocol:self didLoadData:data]; - [client URLProtocolDidFinishLoading:self]; + [self _returnPNGWithImage:icon forRequest:request]; + } else if ([command isEqualToString:@"uikit-image"]) { + if (path == nil) + goto fail; + path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + UIImage *icon(_UIImageWithName(path)); + [self _returnPNGWithImage:icon forRequest:request]; } else if ([command isEqualToString:@"section-icon"]) { if (path == nil) goto fail; path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *section(Simplify(path)); - UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]); if (icon == nil) icon = [UIImage applicationImageNamed:@"unknown.png"]; - - NSData *data(UIImagePNGRepresentation(icon)); - - NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); - [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - [client URLProtocol:self didLoadData:data]; - [client URLProtocolDidFinishLoading:self]; + [self _returnPNGWithImage:icon forRequest:request]; } else fail: { [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; } @@ -7342,7 +7433,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { #if 0 [[[MailToView alloc] initWithView:underlay_ delegate:self url:url] autorelease]; #else - [UIApp openURL:url]; + [UIApp openURL:url];// asPanel:YES]; #endif } @@ -7473,22 +7564,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { [self finish]; } -/* Web Scripting {{{ */ -+ (NSString *) webScriptNameForSelector:(SEL)selector { - if (selector == @selector(supports:)) - return @"supports"; - return nil; -} - -+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { - return selector != @selector(supports:); -} - -- (BOOL) supports:(NSString *)feature { - return [feature isEqualToString:@"window.open"]; -} -/* }}} */ - - (void) showKeyboard:(BOOL)show { CGSize keysize = [UIKeyboard defaultSize]; CGRect keydown = {{0, [overlay_ bounds].size.height}, keysize}; diff --git a/UICaboodle/BrowserView.h b/UICaboodle/BrowserView.h index 5be825cc..0c4cbb04 100644 --- a/UICaboodle/BrowserView.h +++ b/UICaboodle/BrowserView.h @@ -22,10 +22,15 @@ IndirectDelegate *indirect_; NSURLAuthenticationChallenge *challenge_; + NSNumber *confirm_; NSString *title_; bool loading_; bool reloading_; + NSString *button_; + NSString *style_; + WebScriptObject *function_; + bool pushed_; } diff --git a/makefile b/makefile index 2169094d..a9d9c6f6 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,7 @@ exec: exec.mm makefile $(target)g++ -Wall -Werror -o $@ $< -framework Foundation -framework CoreFoundation -lobjc Cydia: Cydia.mm ../uicaboodle.m/*.mm *.h makefile - $(target)g++ -march=armv6 -mcpu=arm1176jzf-s -I../uicaboodle.m -fobjc-call-cxx-cdtors -g0 -O2 -Wall -Werror -o $@ $(filter %.mm,$^) -framework UIKit -framework IOKit -framework CoreFoundation -framework Foundation -framework CoreGraphics -framework GraphicsServices -framework MessageUI -framework QuartzCore -framework WebKit -lobjc -lapt-pkg -lpcre -fobjc-exceptions -F"$${PKG_ROOT}"/System/Library/PrivateFrameworks + $(target)g++ -march=armv6 -mcpu=arm1176jzf-s -I../uicaboodle.m -fobjc-call-cxx-cdtors -g0 -O2 -Wall -Werror -o $@ $(filter %.mm,$^) -framework UIKit -framework IOKit -framework CoreFoundation -framework Foundation -framework CoreGraphics -framework GraphicsServices -framework MessageUI -framework QuartzCore -framework WebKit -framework JavaScriptCore -lobjc -lapt-pkg -lpcre -fobjc-exceptions -F"$${PKG_ROOT}"/System/Library/PrivateFrameworks sign: Cydia CODESIGN_ALLOCATE=$$(which "$(target)codesign_allocate") /apl/tel/util/ldid -Slaunch.xml Cydia -- 2.45.2