]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.m
Added a dependency on apt7-key to test control.
[cydia.git] / UICaboodle / BrowserView.m
index 08dd698b131b76f7ff789a9d496ca2104b07859a..cb3709c23f4a2837847d6a32eb7385f3b3d3b56a 100644 (file)
@@ -1,7 +1,9 @@
 #include <BrowserView.h>
 
 #include <BrowserView.h>
 
+#include <WebCore/WebCoreThread.h>
+
 /* Indirect Delegate {{{ */
 /* Indirect Delegate {{{ */
-@interface IndirectDelegate : NSProxy {
+@interface IndirectDelegate : NSObject {
     _transient volatile id delegate_;
 }
 
     _transient volatile id delegate_;
 }
 
     return self;
 }
 
     return self;
 }
 
+- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didCommitLoadForFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didFinishLoadForFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
+}
+
+- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
+}
+
+- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
+    if (delegate_ != nil)
+        return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
+    return nil;
+}
+
+- (IMP) methodForSelector:(SEL)sel {
+    if (IMP method = [super methodForSelector:sel])
+        return method;
+    fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
+    return NULL;
+}
+
 - (BOOL) respondsToSelector:(SEL)sel {
 - (BOOL) respondsToSelector:(SEL)sel {
-    return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel];
+    if ([super respondsToSelector:sel])
+        return YES;
+    // XXX: WebThreadCreateNSInvocation returns nil
+    //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
+    return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
 }
 
 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
 }
 
 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
+    if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
+        return method;
+    //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
     if (delegate_ != nil)
         if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
             return sig;
     if (delegate_ != nil)
         if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
             return sig;
 - (void) _setLayoutInterval:(float)interval;
 @end
 
 - (void) _setLayoutInterval:(float)interval;
 @end
 
+@interface WebScriptObject (Cydia)
+
+- (unsigned) count;
+- (id) objectAtIndex:(unsigned)index;
+
+@end
+
+@implementation WebScriptObject (Cydia)
+
+- (unsigned) count {
+    id length([self valueForKey:@"length"]);
+    if ([length respondsToSelector:@selector(intValue)])
+        return [length intValue];
+    else
+        return 0;
+}
+
+- (id) objectAtIndex:(unsigned)index {
+    return [self webScriptValueAtIndex:index];
+}
+
+@end
+
 /* Web Scripting {{{ */
 @interface CydiaObject : NSObject {
     id indirect_;
 /* Web Scripting {{{ */
 @interface CydiaObject : NSObject {
     id indirect_;
     } return self;
 }
 
     } return self;
 }
 
++ (NSArray *) _attributeKeys {
+    return [NSArray arrayWithObjects:@"device", nil];
+}
+
+- (NSArray *) attributeKeys {
+    return [[self class] _attributeKeys];
+}
+
++ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
+    return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
+}
+
+- (NSString *) device {
+    return [[UIDevice currentDevice] uniqueIdentifier];
+}
+
 + (NSString *) webScriptNameForSelector:(SEL)selector {
 + (NSString *) webScriptNameForSelector:(SEL)selector {
-    if (selector == @selector(getPackageById:))
+    if (selector == @selector(close))
+        return @"close";
+    else if (selector == @selector(getPackageById:))
         return @"getPackageById";
     else if (selector == @selector(setAutoPopup:))
         return @"setAutoPopup";
         return @"getPackageById";
     else if (selector == @selector(setAutoPopup:))
         return @"setAutoPopup";
         return @"setButtonImage";
     else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
         return @"setButtonTitle";
         return @"setButtonImage";
     else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
         return @"setButtonTitle";
+    else if (selector == @selector(setFinishHook:))
+        return @"setFinishHook";
     else if (selector == @selector(setPopupHook:))
         return @"setPopupHook";
     else if (selector == @selector(setPopupHook:))
         return @"setPopupHook";
+    else if (selector == @selector(setSpecial:))
+        return @"setSpecial";
     else if (selector == @selector(setViewportWidth:))
         return @"setViewportWidth";
     else if (selector == @selector(supports:))
         return @"supports";
     else if (selector == @selector(setViewportWidth:))
         return @"setViewportWidth";
     else if (selector == @selector(supports:))
         return @"supports";
+    else if (selector == @selector(stringWithFormat:arguments:))
+        return @"format";
+    else if (selector == @selector(localizedStringForKey:value:table:))
+        return @"localize";
     else if (selector == @selector(du:))
         return @"du";
     else if (selector == @selector(statfs:))
     else if (selector == @selector(du:))
         return @"du";
     else if (selector == @selector(statfs:))
         _assert(dup2(fds[1], 1) != -1);
         _assert(close(fds[0]) != -1);
         _assert(close(fds[1]) != -1);
         _assert(dup2(fds[1], 1) != -1);
         _assert(close(fds[0]) != -1);
         _assert(close(fds[1]) != -1);
-        execlp("du", "du", "-s", [path UTF8String], NULL);
+        /* XXX: this should probably not use du */
+        execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL);
         exit(1);
         _assert(false);
     }
         exit(1);
         _assert(false);
     }
     return value;
 }
 
     return value;
 }
 
+- (void) close {
+    [indirect_ close];
+}
+
 - (void) setAutoPopup:(BOOL)popup {
     [indirect_ setAutoPopup:popup];
 }
 - (void) setAutoPopup:(BOOL)popup {
     [indirect_ setAutoPopup:popup];
 }
     [indirect_ setButtonTitle:button withStyle:style toFunction:function];
 }
 
     [indirect_ setButtonTitle:button withStyle:style toFunction:function];
 }
 
+- (void) setSpecial:(id)function {
+    [indirect_ setSpecial:function];
+}
+
+- (void) setFinishHook:(id)function {
+    [indirect_ setFinishHook:function];
+}
+
 - (void) setPopupHook:(id)function {
     [indirect_ setPopupHook:function];
 }
 - (void) setPopupHook:(id)function {
     [indirect_ setPopupHook:function];
 }
     [indirect_ setViewportWidth:width];
 }
 
     [indirect_ setViewportWidth:width];
 }
 
+- (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments {
+    //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]);
+    unsigned count([arguments count]);
+    id values[count];
+    for (unsigned i(0); i != count; ++i)
+        values[i] = [arguments objectAtIndex:i];
+    return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
+}
+
+- (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table {
+    if (reinterpret_cast<id>(table) == [WebUndefined undefined])
+        table = nil;
+    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table];
+}
+
 @end
 /* }}} */
 
 @end
 /* }}} */
 
     if (challenge_ != nil)
         [challenge_ release];
 
     if (challenge_ != nil)
         [challenge_ release];
 
+    WebThreadLock();
+
     WebView *webview = [webview_ webView];
     [webview setFrameLoadDelegate:nil];
     [webview setResourceLoadDelegate:nil];
     WebView *webview = [webview_ webView];
     [webview setFrameLoadDelegate:nil];
     [webview setResourceLoadDelegate:nil];
 
     [webview setDownloadDelegate:nil];
 
 
     [webview setDownloadDelegate:nil];
 
+    /* XXX: these are set by UIWebDocumentView
     [webview _setFormDelegate:nil];
     [webview _setUIKitDelegate:nil];
     [webview _setFormDelegate:nil];
     [webview _setUIKitDelegate:nil];
-    [webview setWebMailDelegate:nil];
-    [webview setEditingDelegate:nil];
+    [webview setEditingDelegate:nil];*/
+
+    /* XXX: no one sets this, ever
+    [webview setWebMailDelegate:nil];*/
 
     [webview_ setDelegate:nil];
     [webview_ setGestureDelegate:nil];
     [webview_ setFormEditingDelegate:nil];
     [webview_ setInteractionDelegate:nil];
 
 
     [webview_ setDelegate:nil];
     [webview_ setGestureDelegate:nil];
     [webview_ setFormEditingDelegate:nil];
     [webview_ setInteractionDelegate:nil];
 
+    [indirect_ setDelegate:nil];
+
     //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 
     [webview close];
     //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 
     [webview close];
     [webview_ release];
 #endif
 
     [webview_ release];
 #endif
 
-    [indirect_ setDelegate:nil];
     [indirect_ release];
 
     [indirect_ release];
 
+    WebThreadUnlock();
+
     [cydia_ release];
 
     [scroller_ setDelegate:nil];
     [cydia_ release];
 
     [scroller_ setDelegate:nil];
         [style_ release];
     if (function_ != nil)
         [function_ release];
         [style_ release];
     if (function_ != nil)
         [function_ release];
+    if (finish_ != nil)
+        [finish_ release];
     if (closer_ != nil)
         [closer_ release];
     if (closer_ != nil)
         [closer_ release];
+    if (special_ != nil)
+        [special_ release];
 
     [scroller_ release];
     [indicator_ release];
 
     [scroller_ release];
     [indicator_ release];
 - (void) loadRequest:(NSURLRequest *)request {
     pushed_ = true;
     error_ = false;
 - (void) loadRequest:(NSURLRequest *)request {
     pushed_ = true;
     error_ = false;
+
+    WebThreadLock();
     [webview_ loadRequest:request];
     [webview_ loadRequest:request];
+    WebThreadUnlock();
 }
 
 - (void) reloadURL {
 }
 
 - (void) reloadURL {
         [self loadRequest:request_];
     else {
         UIActionSheet *sheet = [[[UIActionSheet alloc]
         [self loadRequest:request_];
     else {
         UIActionSheet *sheet = [[[UIActionSheet alloc]
-            initWithTitle:@"Are you sure you want to submit this form again?"
-            buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
+            initWithTitle:CYLocalize("RESUBMIT_FORM")
+            buttons:[NSArray arrayWithObjects:CYLocalize("CANCEL"), CYLocalize("SUBMIT"), nil]
             defaultButtonIndex:0
             delegate:self
             context:@"submit"
             defaultButtonIndex:0
             delegate:self
             context:@"submit"
     return webview_;
 }
 
     return webview_;
 }
 
-- (void) _fixScroller {
-    CGRect bounds([webview_ documentBounds]);
-#if LogBrowser
-    NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
-#endif
-
+/* XXX: WebThreadLock? */
+- (void) _fixScroller:(CGRect)bounds {
     float extra;
     if (!editing_)
         extra = 0;
     float extra;
     if (!editing_)
         extra = 0;
     [scroller_ releaseRubberBandIfNecessary];
 }
 
     [scroller_ releaseRubberBandIfNecessary];
 }
 
+- (void) fixScroller {
+    CGRect bounds([webview_ documentBounds]);
+#if TrackResize
+    NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
+#endif
+    [self _fixScroller:bounds];
+}
+
 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
     size_ = frame.size;
 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
     size_ = frame.size;
-#if LogBrowser
+#if TrackResize
     NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
 #endif
     NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
 #endif
-    [self _fixScroller];
+    [self _fixScroller:frame];
 }
 
 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
 }
 
 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
 - (void) _pushPage {
     if (pushed_)
         return;
 - (void) _pushPage {
     if (pushed_)
         return;
-    [self autorelease];
+    // WTR: [self autorelease];
     pushed_ = true;
     [book_ pushPage:self];
 }
     pushed_ = true;
     [book_ pushPage:self];
 }
 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     if (![self _allowJavaScriptPanel])
         return;
 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     if (![self _allowJavaScriptPanel])
         return;
-
     [self retain];
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:nil
     [self retain];
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:nil
-        buttons:[NSArray arrayWithObjects:@"OK", nil]
+        buttons:[NSArray arrayWithObjects:CYLocalize("OK"), nil]
         defaultButtonIndex:0
         delegate:self
         context:@"alert"
         defaultButtonIndex:0
         delegate:self
         context:@"alert"
 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     if (![self _allowJavaScriptPanel])
         return NO;
 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     if (![self _allowJavaScriptPanel])
         return NO;
+    [self retain];
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:nil
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:nil
-        buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
+        buttons:[NSArray arrayWithObjects:CYLocalize("OK"), CYLocalize("Cancel"), nil]
         defaultButtonIndex:0
         delegate:indirect_
         context:@"confirm"
         defaultButtonIndex:0
         delegate:indirect_
         context:@"confirm"
 
     NSNumber *confirm([confirm_ autorelease]);
     confirm_ = nil;
 
     NSNumber *confirm([confirm_ autorelease]);
     confirm_ = nil;
+
+    [self autorelease];
     return [confirm boolValue];
 }
 
     return [confirm boolValue];
 }
 
     popup_ = popup;
 }
 
     popup_ = popup;
 }
 
-- (void) setPopupHook:(id)function {
-    if (closer_ != nil)
-        [closer_ autorelease];
-    closer_ = function == nil ? nil : [function retain];
+- (void) setSpecial:(id)function {
+    if (special_ != nil)
+        [special_ autorelease];
+    special_ = function == nil ? nil : [function retain];
 }
 
 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
 }
 
 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
     [self reloadButtons];
 }
 
     [self reloadButtons];
 }
 
+- (void) setFinishHook:(id)function {
+    if (finish_ != nil)
+        [finish_ autorelease];
+    finish_ = function == nil ? nil : [function retain];
+}
+
+- (void) setPopupHook:(id)function {
+    if (closer_ != nil)
+        [closer_ autorelease];
+    closer_ = function == nil ? nil : [function retain];
+}
+
 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
     editing_ = true;
 }
 
 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
     editing_ = true;
 }
 
 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
-    [self _fixScroller];
+    [self fixScroller];
 }
 
 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
     editing_ = false;
 }
 
 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
     editing_ = false;
-    [self _fixScroller];
+    [self fixScroller];
 }
 
 - (void) webViewClose:(WebView *)sender {
     [book_ close];
 }
 
 }
 
 - (void) webViewClose:(WebView *)sender {
     [book_ close];
 }
 
+- (void) close {
+    [book_ close];
+}
+
 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
     [window setValue:cydia_ forKey:@"cydia"];
 }
 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
     [window setValue:cydia_ forKey:@"cydia"];
 }
         } else if ([name isEqualToString:@"_open"])
             [delegate_ openURL:url];
         else if ([name isEqualToString:@"_popup"]) {
         } else if ([name isEqualToString:@"_open"])
             [delegate_ openURL:url];
         else if ([name isEqualToString:@"_popup"]) {
-            RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
-            [book setHook:indirect_];
-
-            RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
-            if (page == nil) {
-                /* XXX: call createWebViewWithRequest instead? */
+            NSString *scheme([[url scheme] lowercaseString]);
+            if ([scheme isEqualToString:@"mailto"])
+                [delegate_ openMailToURL:url];
+            else {
+                RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
+                [book setHook:indirect_];
+
+                RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
+                if (page == nil) {
+                    /* XXX: call createWebViewWithRequest instead? */
+
+                    [self setBackButtonTitle:title_];
+
+                    BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
+                    [browser loadURL:url];
+                    page = browser;
+                }
 
 
-                [self setBackButtonTitle:title_];
+                [book setDelegate:delegate_];
+                [page setDelegate:delegate_];
 
 
-                BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
-                [browser loadURL:url];
-                page = browser;
+                [book setPage:page];
+                [book_ pushBook:book];
             }
             }
-
-            [book setDelegate:delegate_];
-            [page setDelegate:delegate_];
-
-            [book setPage:page];
-            [book_ pushBook:book];
         } else goto unknown;
 
         [listener ignore];
         } else goto unknown;
 
         [listener ignore];
     else NSLog(@"nav:%@:%@", url, [action description]);
 #endif
 
     else NSLog(@"nav:%@:%@", url, [action description]);
 #endif
 
-    const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
+    const NSArray *capability;
+
+#if 0 // XXX:3:GSSystemCopyCapability
+    capability = reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability));
+#else
+    capability = nil;
+#endif
 
 
-    if (
+    if (capability != nil && (
         [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
         [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
         [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
         [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
-    ) {
+    )) {
       open:
         [UIApp openURL:url];
         goto ignore;
       open:
         [UIApp openURL:url];
         goto ignore;
         NSLog(@"itms#%@#%u#%@", url, store, itms);
 #endif
 
         NSLog(@"itms#%@#%u#%@", url, store, itms);
 #endif
 
-        if (
+        if (capability != nil && (
             store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
             store == 2 && [capability containsObject:@"com.apple.AppStore"]
             store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
             store == 2 && [capability containsObject:@"com.apple.AppStore"]
-        ) {
+        )) {
             url = itms;
             goto open;
         }
             url = itms;
             goto open;
         }
             break;
 
             case 2:
             break;
 
             case 2:
-                if (request_ != nil)
+                if (request_ != nil) {
+                    WebThreadLock();
                     [webview_ loadRequest:request_];
                     [webview_ loadRequest:request_];
+                    WebThreadUnlock();
+                }
             break;
 
             default:
             break;
 
             default:
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:realm
 
     UIActionSheet *sheet = [[[UIActionSheet alloc]
         initWithTitle:realm
-        buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
+        buttons:[NSArray arrayWithObjects:CYLocalize("LOGIN"), CYLocalize("CANCEL"), nil]
         defaultButtonIndex:0
         delegate:self
         context:@"challenge"
         defaultButtonIndex:0
         delegate:self
         context:@"challenge"
 
     [sheet setNumberOfRows:1];
 
 
     [sheet setNumberOfRows:1];
 
-    [sheet addTextFieldWithValue:@"" label:@"username"];
-    [sheet addTextFieldWithValue:@"" label:@"password"];
+    [sheet addTextFieldWithValue:@"" label:CYLocalize("USERNAME")];
+    [sheet addTextFieldWithValue:@"" label:CYLocalize("PASSWORD")];
 
     UITextField *username([sheet textFieldAtIndex:0]); {
         UITextInputTraits *traits([username textInputTraits]);
 
     UITextField *username([sheet textFieldAtIndex:0]); {
         UITextInputTraits *traits([username textInputTraits]);
 }
 
 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
 }
 
 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
-    if ([frame parentFrame] != nil)
-        return;
+    if ([loading_ count] == 0)
+        [self retain];
+    [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
 
 
-    [webview_ resignFirstResponder];
+    if ([frame parentFrame] == nil) {
+        [webview_ resignFirstResponder];
 
 
-    reloading_ = false;
-    loading_ = true;
-    [self reloadButtons];
+        reloading_ = false;
 
 
-    if (title_ != nil) {
-        [title_ release];
-        title_ = nil;
-    }
+        if (title_ != nil) {
+            [title_ release];
+            title_ = nil;
+        }
 
 
-    if (button_ != nil) {
-        [button_ release];
-        button_ = nil;
-    }
+        if (button_ != nil) {
+            [button_ release];
+            button_ = nil;
+        }
 
 
-    if (style_ != nil) {
-        [style_ release];
-        style_ = nil;
-    }
+        if (style_ != nil) {
+            [style_ release];
+            style_ = nil;
+        }
 
 
-    if (function_ != nil) {
-        [function_ release];
-        function_ = nil;
-    }
+        if (function_ != nil) {
+            [function_ release];
+            function_ = nil;
+        }
 
 
-    if (closer_ != nil) {
-        [closer_ release];
-        closer_ = nil;
-    }
+        if (finish_ != nil) {
+            [finish_ release];
+            finish_ = nil;
+        }
 
 
-    [book_ reloadTitleForPage:self];
+        if (closer_ != nil) {
+            [closer_ release];
+            closer_ = nil;
+        }
 
 
-    [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
-    [scroller_ setZoomScale:1 duration:0];
+        if (special_ != nil) {
+            [special_ release];
+            special_ = nil;
+        }
+
+        [book_ reloadTitleForPage:self];
+
+        [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
 
 
-    CGRect webrect = [scroller_ bounds];
-    webrect.size.height = 0;
-    [webview_ setFrame:webrect];
+        if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
+            [scroller_ setZoomScale:1 duration:0];
+        else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
+            [scroller_ _setZoomScale:1 duration:0];
+        /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
+            [scroller_ setZoomScale:1 animated:NO];*/
+
+        CGRect webrect = [scroller_ bounds];
+        webrect.size.height = 0;
+        [webview_ setFrame:webrect];
+    }
+
+    [self reloadButtons];
 }
 
 - (void) _finishLoading {
 }
 
 - (void) _finishLoading {
-    if (!reloading_) {
-        loading_ = false;
-        [self reloadButtons];
-    }
+    size_t count([loading_ count]);
+    if (count == 0)
+        [self autorelease];
+    if (reloading_ || count != 0)
+        return;
+    if (finish_ != nil)
+        [self callFunction:finish_];
+    [self reloadButtons];
 }
 
 - (bool) isLoading {
 }
 
 - (bool) isLoading {
-    return loading_;
+    return [loading_ count] != 0;
 }
 
 - (void) reloadButtons {
 }
 
 - (void) reloadButtons {
 }
 
 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
 }
 
 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
-    if ([frame parentFrame] == nil) {
-        [self _finishLoading];
+    [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
+    [self _finishLoading];
 
 
+    if ([frame parentFrame] == nil) {
         if (DOMDocument *document = [frame DOMDocument])
             if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
                 for (DOMHTMLBodyElement *body in bodies) {
         if (DOMDocument *document = [frame DOMDocument])
             if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
                 for (DOMHTMLBodyElement *body in bodies) {
     return [webview_ webView:sender didFinishLoadForFrame:frame];
 }
 
     return [webview_ webView:sender didFinishLoadForFrame:frame];
 }
 
-- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
-    if ([frame parentFrame] != nil)
-        return;
+- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    if ([frame parentFrame] == nil)
+        [self autorelease];
+
+    [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
+    [self _finishLoading];
+
     if (reloading_)
         return;
     if (reloading_)
         return;
-    [self _finishLoading];
 
 
-    [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
-        [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
-        [[error localizedDescription] stringByAddingPercentEscapes]
-    ]]];
+    if ([frame parentFrame] == nil) {
+        [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
+            [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
+            [[error localizedDescription] stringByAddingPercentEscapes]
+        ]]];
 
 
-    error_ = true;
+        error_ = true;
+    }
+}
+
+- (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    [self _didFailWithError:error forFrame:frame];
+}
+
+- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    [self _didFailWithError:error forFrame:frame];
 }
 
 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
 }
 
 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
 #endif
 }
 
 #endif
 }
 
+/* XXX: fix this stupid include file
+- (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
+    [origin setQuota:0x500000];
+}*/
+
+- (void) _setTileDrawingEnabled:(BOOL)enabled {
+    //[webview_ setTileDrawingEnabled:enabled];
+}
+
 - (void) setViewportWidth:(float)width {
     width_ = width ? width != 0 : [[self class] defaultWidth];
     [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
 }
 
 - (void) setViewportWidth:(float)width {
     width_ = width ? width != 0 : [[self class] defaultWidth];
     [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
 }
 
+- (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
+    [self _setTileDrawingEnabled:NO];
+}
+
+- (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
+    [self _setTileDrawingEnabled:YES];
+    [webview_ redrawScaledDocument];
+}
+
+- (void) scrollerWillStartDragging:(UIScroller *)scroller {
+    [self _setTileDrawingEnabled:NO];
+}
+
+- (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
+    [self _setTileDrawingEnabled:YES];
+}
+
+- (void) scrollerDidEndDragging:(UIScroller *)scroller {
+    [self _setTileDrawingEnabled:YES];
+}
+
 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
     if ((self = [super initWithBook:book]) != nil) {
 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
     if ((self = [super initWithBook:book]) != nil) {
-        loading_ = false;
+        loading_ = [[NSMutableSet alloc] initWithCapacity:3];
         popup_ = false;
 
         struct CGRect bounds = [self bounds];
         popup_ = false;
 
         struct CGRect bounds = [self bounds];
         scroller_ = [[UIScroller alloc] initWithFrame:bounds];
         [self addSubview:scroller_];
 
         scroller_ = [[UIScroller alloc] initWithFrame:bounds];
         [self addSubview:scroller_];
 
-        [scroller_ setShowBackgroundShadow:NO];
         [scroller_ setFixedBackgroundPattern:YES];
         [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
 
         [scroller_ setScrollingEnabled:YES];
         [scroller_ setFixedBackgroundPattern:YES];
         [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
 
         [scroller_ setScrollingEnabled:YES];
-        [scroller_ setAdjustForContentSizeChange:YES];
         [scroller_ setClipsSubviews:YES];
         [scroller_ setAllowsRubberBanding:YES];
         [scroller_ setClipsSubviews:YES];
         [scroller_ setAllowsRubberBanding:YES];
-        [scroller_ setScrollDecelerationFactor:0.99];
+
         [scroller_ setDelegate:self];
         [scroller_ setDelegate:self];
+        [scroller_ setBounces:YES];
+        [scroller_ setScrollHysteresis:8];
+        [scroller_ setThumbDetectionEnabled:NO];
+        [scroller_ setDirectionalScrolling:YES];
+        [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
+        [scroller_ setEventMode:YES];
+        [scroller_ setShowBackgroundShadow:NO]; /* YES */
+        [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
+        [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
 
         CGRect webrect = [scroller_ bounds];
         webrect.size.height = 0;
 
         WebView *webview;
 
 
         CGRect webrect = [scroller_ bounds];
         webrect.size.height = 0;
 
         WebView *webview;
 
+        WebThreadLock();
+
 #if RecycleWebViews
         webview_ = [Documents_ lastObject];
         if (webview_ != nil) {
 #if RecycleWebViews
         webview_ = [Documents_ lastObject];
         if (webview_ != nil) {
             [webview_ setDrawsGrid:NO];
             [webview_ setLogsTilingChanges:NO];
             [webview_ setTileMinificationFilter:kCAFilterNearest];
             [webview_ setDrawsGrid:NO];
             [webview_ setLogsTilingChanges:NO];
             [webview_ setTileMinificationFilter:kCAFilterNearest];
-            [webview_ setDetectsPhoneNumbers:NO];
+            if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
+                /* XXX: abstractify */
+                [webview_ setDataDetectorTypes:0x80000000];
+            else
+                [webview_ setDetectsPhoneNumbers:NO];
             [webview_ setAutoresizes:YES];
 
             [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
             [webview_ setAutoresizes:YES];
 
             [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
 
             [webview_ _setDocumentType:0x4];
 
 
             [webview_ _setDocumentType:0x4];
 
-            [webview_ setZoomsFocusedFormControl:YES];
+            if ([webview_ respondsToSelector:@selector(UIWebDocumentView:)])
+                [webview_ setZoomsFocusedFormControl:YES];
             [webview_ setContentsPosition:7];
             [webview_ setEnabledGestures:0xa];
             [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
             [webview_ setContentsPosition:7];
             [webview_ setEnabledGestures:0xa];
             [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
             [webview _setUsesLoaderCache:YES];
 
             [webview setGroupName:@"CydiaGroup"];
             [webview _setUsesLoaderCache:YES];
 
             [webview setGroupName:@"CydiaGroup"];
-            [webview _setLayoutInterval:0];
+            if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
+                [webview _setLayoutInterval:0];
         }
 
         [self setViewportWidth:width];
         }
 
         [self setViewportWidth:width];
 
         //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 
 
         //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 
-        CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
-        indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
-        [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
-
         Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
         NSString *application = package == nil ? @"Cydia" : [NSString
             stringWithFormat:@"Cydia/%@",
         Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
         NSString *application = package == nil ? @"Cydia" : [NSString
             stringWithFormat:@"Cydia/%@",
         if (Safari_ != nil)
             application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
 
         if (Safari_ != nil)
             application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
 
-        /* XXX: lookup application directory? */
-        /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
-            if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
-                application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
-
         [webview setApplicationNameForUserAgent:application];
 
         indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
         cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
 
         [webview setApplicationNameForUserAgent:application];
 
         indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
         cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
 
-        [webview setFrameLoadDelegate:self];
+        [webview setFrameLoadDelegate:indirect_];
         [webview setResourceLoadDelegate:indirect_];
         [webview setResourceLoadDelegate:indirect_];
-        [webview setUIDelegate:self];
-        [webview setScriptDebugDelegate:self];
-        [webview setPolicyDelegate:self];
+        [webview setUIDelegate:indirect_];
+        [webview setScriptDebugDelegate:indirect_];
+        [webview setPolicyDelegate:indirect_];
+
+        WebThreadUnlock();
+
+        CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
+        indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
+        [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
 
         [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
         [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
 
         [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
         [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
+
+        /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
+        [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
+        [self addSubview:test];*/
     } return self;
 }
 
     } return self;
 }
 
     return [self initWithBook:book forWidth:0];
 }
 
     return [self initWithBook:book forWidth:0];
 }
 
-- (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
-    [webview_ redrawScaledDocument];
+- (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
+    WebThreadLock();
+    WebView *webview([webview_ webView]);
+    NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
+    WebThreadUnlock();
+    return string;
 }
 
 - (void) callFunction:(WebScriptObject *)function {
 }
 
 - (void) callFunction:(WebScriptObject *)function {
+    WebThreadLock();
+
     WebView *webview([webview_ webView]);
     WebFrame *frame([webview mainFrame]);
 
     WebView *webview([webview_ webView]);
     WebFrame *frame([webview mainFrame]);
 
     JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
 
     if (settings != NULL)
     JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
 
     if (settings != NULL)
-            settings->setJavaScriptCanOpenWindowsAutomatically(no);
+        settings->setJavaScriptCanOpenWindowsAutomatically(no);
+
+    WebThreadUnlock();
 }
 
 - (void) didCloseBook:(RVBook *)book {
 }
 
 - (void) didCloseBook:(RVBook *)book {
 }
 
 - (void) _rightButtonClicked {
 }
 
 - (void) _rightButtonClicked {
-    if (function_ == nil)
-        [self __rightButtonClicked];
-    else
+#if !AlwaysReload
+    if (function_ != nil)
         [self callFunction:function_];
         [self callFunction:function_];
+    else
+#endif
+        [self __rightButtonClicked];
 }
 
 - (id) _rightButtonTitle {
 }
 
 - (id) _rightButtonTitle {
-    return @"Reload";
+    return CYLocalize("RELOAD");
 }
 
 - (id) rightButtonTitle {
 }
 
 - (id) rightButtonTitle {
 }
 
 - (NSString *) title {
 }
 
 - (NSString *) title {
-    return title_ == nil ? @"Loading" : title_;
+    return title_ == nil ? CYLocalize("LOADING") : title_;
 }
 
 - (NSString *) backButtonTitle {
 }
 
 - (NSString *) backButtonTitle {
-    return @"Browser";
+    return CYLocalize("BROWSER");
 }
 
 - (void) setPageActive:(BOOL)active {
 }
 
 - (void) setPageActive:(BOOL)active {