From 374f3d18f0a8adefbb9eca5292ebef10935c09a5 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 28 Dec 2008 19:30:17 +0000 Subject: [PATCH] Fixed window.cydia. --- UICaboodle/BrowserView.h | 2 + UICaboodle/BrowserView.m | 209 ++++++++++++++++++++++----------------- control | 2 +- 3 files changed, 124 insertions(+), 89 deletions(-) diff --git a/UICaboodle/BrowserView.h b/UICaboodle/BrowserView.h index a37d326b..632528da 100644 --- a/UICaboodle/BrowserView.h +++ b/UICaboodle/BrowserView.h @@ -34,12 +34,14 @@ @class Database; @class IndirectDelegate; +@class CydiaObject; @interface BrowserView : RVPage { UIScroller *scroller_; UIWebDocumentView *webview_; UIProgressIndicator *indicator_; IndirectDelegate *indirect_; + CydiaObject *cydia_; NSURLAuthenticationChallenge *challenge_; bool error_; diff --git a/UICaboodle/BrowserView.m b/UICaboodle/BrowserView.m index 6fe74f41..5cf07c85 100644 --- a/UICaboodle/BrowserView.m +++ b/UICaboodle/BrowserView.m @@ -49,6 +49,123 @@ - (void) _setLayoutInterval:(float)interval; @end +/* Web Scripting {{{ */ +@interface CydiaObject : NSObject { + id indirect_; +} + +- (id) initWithDelegate:(IndirectDelegate *)indirect; +@end + +@implementation CydiaObject + +- (void) dealloc { + [indirect_ release]; + [super dealloc]; +} + +- (id) initWithDelegate:(IndirectDelegate *)indirect { + if ((self = [super init]) != nil) { + indirect_ = [indirect retain]; + } return self; +} + ++ (NSString *) webScriptNameForSelector:(SEL)selector { + if (selector == @selector(getPackageById:)) + return @"getPackageById"; + else if (selector == @selector(setButtonImage:withStyle:toFunction:)) + return @"setButtonImage"; + else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) + return @"setButtonTitle"; + else if (selector == @selector(supports:)) + return @"supports"; + else if (selector == @selector(du:)) + return @"du"; + else if (selector == @selector(statfs:)) + return @"statfs"; + 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]; +} + +- (NSArray *) statfs:(NSString *)path { + struct statfs stat; + + if (path == nil || statfs([path UTF8String], &stat) == -1) + return nil; + + return [NSArray arrayWithObjects: + [NSNumber numberWithUnsignedLong:stat.f_bsize], + [NSNumber numberWithUnsignedLong:stat.f_blocks], + [NSNumber numberWithUnsignedLong:stat.f_bfree], + nil]; +} + +- (NSNumber *) du:(NSString *)path { + NSNumber *value(nil); + + int fds[2]; + _assert(pipe(fds) != -1); + + pid_t pid(ExecFork()); + if (pid == 0) { + _assert(dup2(fds[1], 1) != -1); + _assert(close(fds[0]) != -1); + _assert(close(fds[1]) != -1); + execlp("du", "du", "-s", [path UTF8String], NULL); + exit(1); + _assert(false); + } + + _assert(close(fds[1]) != -1); + + if (FILE *du = fdopen(fds[0], "r")) { + char line[1024]; + while (fgets(line, sizeof(line), du) != NULL) { + size_t length(strlen(line)); + while (length != 0 && line[length - 1] == '\n') + line[--length] = '\0'; + if (char *tab = strchr(line, '\t')) { + *tab = '\0'; + value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; + } + } + + fclose(du); + } else _assert(close(fds[0])); + + int status; + wait: + if (waitpid(pid, &status, 0) == -1) + if (errno == EINTR) + goto wait; + else _assert(false); + + return value; +} + +- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { + [indirect_ setButtonImage:button withStyle:style toFunction:function]; +} + +- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { + [indirect_ setButtonTitle:button withStyle:style toFunction:function]; +} + +@end +/* }}} */ + @implementation BrowserView #if ForSaurik @@ -90,6 +207,8 @@ [indirect_ setDelegate:nil]; [indirect_ release]; + [cydia_ release]; + [scroller_ setDelegate:nil]; if (button_ != nil) @@ -235,92 +354,6 @@ return [confirm boolValue]; } -/* Web Scripting {{{ */ -+ (NSString *) webScriptNameForSelector:(SEL)selector { - if (selector == @selector(getPackageById:)) - return @"getPackageById"; - else if (selector == @selector(setButtonImage:withStyle:toFunction:)) - return @"setButtonImage"; - else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) - return @"setButtonTitle"; - else if (selector == @selector(supports:)) - return @"supports"; - else if (selector == @selector(du:)) - return @"du"; - else if (selector == @selector(statfs:)) - return @"statfs"; - 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]; -} - -- (NSArray *) statfs:(NSString *)path { - struct statfs stat; - - if (path == nil || statfs([path UTF8String], &stat) == -1) - return nil; - - return [NSArray arrayWithObjects: - [NSNumber numberWithUnsignedLong:stat.f_bsize], - [NSNumber numberWithUnsignedLong:stat.f_blocks], - [NSNumber numberWithUnsignedLong:stat.f_bfree], - nil]; -} - -- (NSNumber *) du:(NSString *)path { - NSNumber *value(nil); - - int fds[2]; - _assert(pipe(fds) != -1); - - pid_t pid(ExecFork()); - if (pid == 0) { - _assert(dup2(fds[1], 1) != -1); - _assert(close(fds[0]) != -1); - _assert(close(fds[1]) != -1); - execlp("du", "du", "-s", [path UTF8String], NULL); - exit(1); - _assert(false); - } - - _assert(close(fds[1]) != -1); - - if (FILE *du = fdopen(fds[0], "r")) { - char line[1024]; - while (fgets(line, sizeof(line), du) != NULL) { - size_t length(strlen(line)); - while (length != 0 && line[length - 1] == '\n') - line[--length] = '\0'; - if (char *tab = strchr(line, '\t')) { - *tab = '\0'; - value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; - } - } - - fclose(du); - } else _assert(close(fds[0])); - - int status; - wait: - if (waitpid(pid, &status, 0) == -1) - if (errno == EINTR) - goto wait; - else _assert(false); - - return value; -} - - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { if (button_ != nil) [button_ autorelease]; @@ -348,14 +381,13 @@ [function_ autorelease]; function_ = function == nil ? nil : [function retain]; } -/* }}} */ - (void) webViewClose:(WebView *)sender { [book_ close]; } - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { - [window setValue:indirect_ forKey:@"cydia"]; + [window setValue:cydia_ forKey:@"cydia"]; } - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame { @@ -877,6 +909,7 @@ [webview setApplicationNameForUserAgent:application]; indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; + cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_]; [webview setFrameLoadDelegate:self]; [webview setResourceLoadDelegate:indirect_]; diff --git a/control b/control index 79997931..6c73b1c6 100644 --- a/control +++ b/control @@ -4,7 +4,7 @@ Priority: required Section: Packaging Maintainer: Jay Freeman (saurik) Architecture: iphoneos-arm -Version: 1.0.2676-38 +Version: 1.0.2677-38 Replaces: com.sosiphone.addcydia Depends: apt, darwintools, pcre, shell-cmds Conflicts: com.sosiphone.addcydia -- 2.45.2