]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Implement a flexible cydia object whitelist.
[cydia.git] / UICaboodle / BrowserView.mm
index fee3cf0c2e8960b19bc7c9e419d9f0f968b61f11..d1450671cf7a35afb868828f4f05d0905a2aed61 100644 (file)
@@ -1,20 +1,77 @@
+#include <UIKit/UIKit.h>
+#include "iPhonePrivate.h"
+
+#include "UCPlatform.h"
+
 #include <UICaboodle/BrowserView.h>
 #include <UICaboodle/UCLocalize.h>
 
-#import <QuartzCore/CALayer.h>
+//#include <QuartzCore/CALayer.h>
 // XXX: fix the minimum requirement
 extern NSString * const kCAFilterNearest;
 
 #include <WebCore/WebCoreThread.h>
 
+#include <WebKit/WebPolicyDelegate.h>
+#include <WebKit/WebPreferences.h>
+
+#include <WebKit/DOMCSSPrimitiveValue.h>
+#include <WebKit/DOMCSSStyleDeclaration.h>
+#include <WebKit/DOMDocument.h>
+#include <WebKit/DOMHTMLBodyElement.h>
+#include <WebKit/DOMRGBColor.h>
+
+//#include <WebCore/Page.h>
+//#include <WebCore/Settings.h>
+
 #include "substrate.h"
 
-@interface NSString (UIKit)
-- (NSString *) stringByAddingPercentEscapes;
+#define ForSaurik 0
+#define DefaultTimeout_ 120.0
+
+#define ShowInternals 0
+#define LogBrowser 0
+#define LogMessages 0
+
+#define lprintf(args...) fprintf(stderr, args)
+
+// WebThreadLocked {{{
+struct WebThreadLocked {
+    _finline WebThreadLocked() {
+        WebThreadLock();
+    }
+
+    _finline ~WebThreadLocked() {
+        WebThreadUnlock();
+    }
+};
+// }}}
+
+template <typename Type_>
+static inline void CYRelease(Type_ &value) {
+    if (value != nil) {
+        [value release];
+        value = nil;
+    }
+}
+
+float CYScrollViewDecelerationRateNormal;
+
+@interface WebView (Apple)
+- (void) _setLayoutInterval:(float)interval;
+- (void) _setAllowsMessaging:(BOOL)allows;
+@end
+
+@interface WebPreferences (Apple)
++ (void) _setInitialDefaultTextEncodingToSystemEncoding;
+- (void) _setLayoutInterval:(NSInteger)interval;
+- (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
 @end
 
 /* Indirect Delegate {{{ */
-@interface IndirectDelegate : NSObject {
+@interface IndirectDelegate : NSObject <
+    HookProtocol
+> {
     _transient volatile id delegate_;
 }
 
@@ -33,50 +90,9 @@ extern NSString * const kCAFilterNearest;
     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 {
+- (void) didDismissModalViewController {
     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;
+        return [delegate_ didDismissModalViewController];
 }
 
 - (IMP) methodForSelector:(SEL)sel {
@@ -89,18 +105,28 @@ extern NSString * const kCAFilterNearest;
 - (BOOL) respondsToSelector:(SEL)sel {
     if ([super respondsToSelector:sel])
         return YES;
+
     // XXX: WebThreadCreateNSInvocation returns nil
-    //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
+
+#if ShowInternals
+    fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
+#endif
+
     return delegate_ == nil ? NO : [delegate_ respondsToSelector: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 ShowInternals
+    fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
+#endif
+
     if (delegate_ != nil)
         if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
             return sig;
+
     // XXX: I fucking hate Apple so very very bad
     return [NSMethodSignature signatureWithObjCTypes:"v@:"];
 }
@@ -114,17 +140,9 @@ extern NSString * const kCAFilterNearest;
 @end
 /* }}} */
 
-@interface WebView (UICaboodle)
-- (void) setScriptDebugDelegate:(id)delegate;
-- (void) _setFormDelegate:(id)delegate;
-- (void) _setUIKitDelegate:(id)delegate;
-- (void) setWebMailDelegate:(id)delegate;
-- (void) _setLayoutInterval:(float)interval;
-@end
-
 @implementation WebScriptObject (UICaboodle)
 
-- (unsigned) count {
+- (NSUInteger) count {
     id length([self valueForKey:@"length"]);
     if ([length respondsToSelector:@selector(intValue)])
         return [length intValue];
@@ -138,163 +156,363 @@ extern NSString * const kCAFilterNearest;
 
 @end
 
-#if 0
-/* Mail Composition {{{ */
-@interface MailToView : PopUpView {
-    MailComposeController *controller_;
+// CYWebPolicyDecision* {{{
+enum CYWebPolicyDecision {
+    CYWebPolicyDecisionUnknown,
+    CYWebPolicyDecisionDownload,
+    CYWebPolicyDecisionIgnore,
+    CYWebPolicyDecisionUse,
+};
+
+@interface CYWebPolicyDecisionMediator : NSObject <
+    WebPolicyDecisionListener
+> {
+    id<WebPolicyDecisionListener> listener_;
+    CYWebPolicyDecision decision_;
 }
 
-- (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url;
+- (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
+
+- (CYWebPolicyDecision) decision;
+- (bool) decided;
+- (bool) decide;
 
 @end
 
-@implementation MailToView
+@implementation CYWebPolicyDecisionMediator
 
-- (void) dealloc {
-    [controller_ release];
-    [super dealloc];
+- (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
+    if ((self = [super init]) != nil) {
+        listener_ = listener;
+    } return self;
 }
 
-- (void) mailComposeControllerWillAttemptToSend:(MailComposeController *)controller {
-    NSLog(@"will");
+- (CYWebPolicyDecision) decision {
+    return decision_;
 }
 
-- (void) mailComposeControllerDidAttemptToSend:(MailComposeController *)controller mailDelivery:(id)delivery {
-    NSLog(@"did:%@", delivery);
-// [UIApp setStatusBarShowsProgress:NO];
-if ([controller error]){
-NSArray *buttons = [NSArray arrayWithObjects:UCLocalize("OK"), nil];
-UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize("ERROR") buttons:buttons defaultButtonIndex:0 delegate:self context:self];
-[mailAlertSheet setBodyText:[controller error]];
-[mailAlertSheet popupAlertAnimated:YES];
-}
+- (bool) decided {
+    return decision_ != CYWebPolicyDecisionUnknown;
 }
 
-- (void) showError {
-    NSLog(@"%@", [controller_ error]);
-    NSArray *buttons = [NSArray arrayWithObjects:UCLocalize("OK"), nil];
-    UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize("ERROR") buttons:buttons defaultButtonIndex:0 delegate:self context:self];
-    [mailAlertSheet setBodyText:[controller_ error]];
-    [mailAlertSheet popupAlertAnimated:YES];
-}
+- (bool) decide {
+    switch (decision_) {
+        case CYWebPolicyDecisionUnknown:
+        default:
+            NSLog(@"CYWebPolicyDecisionUnknown");
+            return false;
 
-- (void) deliverMessage { _pooled
-    setuid(501);
-    setgid(501);
+        case CYWebPolicyDecisionDownload: [listener_ download]; break;
+        case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
+        case CYWebPolicyDecisionUse: [listener_ use]; break;
+    }
 
-    if (![controller_ deliverMessage])
-        [self performSelectorOnMainThread:@selector(showError) withObject:nil waitUntilDone:NO];
+    return true;
 }
 
-- (void) mailComposeControllerCompositionFinished:(MailComposeController *)controller {
-    if ([controller_ needsDelivery])
-        [NSThread detachNewThreadSelector:@selector(deliverMessage) toTarget:self withObject:nil];
-    else
-        [self cancel];
+- (void) download {
+    decision_ = CYWebPolicyDecisionDownload;
 }
 
-- (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url {
-    if ((self = [super initWithView:view delegate:delegate]) != nil) {
-        controller_ = [[MailComposeController alloc] initForContentSize:[overlay_ bounds].size];
-        [controller_ setDelegate:self];
-        [controller_ initializeUI];
-        [controller_ setupForURL:url];
+- (void) ignore {
+    decision_ = CYWebPolicyDecisionIgnore;
+}
 
-        UIView *view([controller_ view]);
-        [overlay_ addSubview:view];
-    } return self;
+- (void) use {
+    decision_ = CYWebPolicyDecisionUse;
 }
 
 @end
-/* }}} */
-#endif
+// }}}
 
-@implementation BrowserView
+@implementation CYWebView : UIWebView
 
 #if ShowInternals
-#include "Internals.h"
+#include "UICaboodle/UCInternal.h"
 #endif
 
+- (id) initWithFrame:(CGRect)frame {
+    if ((self = [super initWithFrame:frame]) != nil) {
+    } return self;
+}
+
 - (void) dealloc {
-#if LogBrowser
-    NSLog(@"[BrowserView dealloc]");
-#endif
+    [super dealloc];
+}
 
-    if (challenge_ != nil)
-        [challenge_ release];
+- (id<CYWebViewDelegate>) delegate {
+    return (id<CYWebViewDelegate>) [super delegate];
+}
 
-    WebThreadLock();
+/*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    WebView *created(nil);
+    if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
+        created = [delegate webView:view createWebViewWithRequest:request];
+    if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
+        created = [super webView:view createWebViewWithRequest:request];
+    return created;
+}*/
 
-    WebView *webview = [webview_ webView];
-    [webview setFrameLoadDelegate:nil];
-    [webview setResourceLoadDelegate:nil];
-    [webview setUIDelegate:nil];
-    [webview setScriptDebugDelegate:nil];
-    [webview setPolicyDelegate:nil];
+// webView:addMessageToConsole: (X.Xx) {{{
+static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
+        [uiWebView webView:view addMessageToConsole:message];
+}
+
+- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
+        [delegate webView:view addMessageToConsole:message];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
+        [super webView:view addMessageToConsole:message];
+}
+// }}}
+// webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
+- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
+    if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
+        [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
+    if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
+        [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
+    [mediator decide];
+}
+// }}}
+// webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
+static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
+        [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
+}
+
+- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
+    if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
+        [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
+    if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
+        [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
+    [mediator decide];
+}
+// }}}
+// webView:didClearWindowObject:forFrame: (3.2+) {{{
+static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
+        [uiWebView webView:view didClearWindowObject:window forFrame:frame];
+}
+
+- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
+        [delegate webView:view didClearWindowObject:window forFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
+        [super webView:view didClearWindowObject:window forFrame:frame];
+}
+// }}}
+// webView:didFailLoadWithError:forFrame: (2.0+) {{{
+- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
+        [delegate webView:view didFailLoadWithError:error forFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
+        [super webView:view didFailLoadWithError:error forFrame:frame];
+}
+// }}}
+// webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
+- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
+        [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
+        [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
+}
+// }}}
+// webView:didFinishLoadForFrame: (2.0+) {{{
+- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
+        [delegate webView:view didFinishLoadForFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
+        [super webView:view didFinishLoadForFrame:frame];
+}
+// }}}
+// webView:didReceiveTitle:forFrame: (3.2+) {{{
+static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
+        [uiWebView webView:view didReceiveTitle:title forFrame:frame];
+}
+
+- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
+        [delegate webView:view didReceiveTitle:title forFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
+        [super webView:view didReceiveTitle:title forFrame:frame];
+}
+// }}}
+// webView:didStartProvisionalLoadForFrame: (2.0+) {{{
+- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
+        [delegate webView:view didStartProvisionalLoadForFrame:frame];
+    if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
+        [super webView:view didStartProvisionalLoadForFrame:frame];
+}
+// }}}
+// webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
+static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
+        request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
+    return request;
+}
 
-    [webview setDownloadDelegate:nil];
+- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
+        request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
+    if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
+        request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
+    return request;
+}
+// }}}
+// webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
+- (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
+        if (
+            ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
+            [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
+        )
+            [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
+}
+// }}}
+// webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
+- (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
+        if (
+            ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
+            [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
+        )
+            return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
+    return NO;
+}
+// }}}
+// webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
+- (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
+        if (
+            ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
+            [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
+        )
+            return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
+    return nil;
+}
+// }}}
+// webViewClose: (3.2+) {{{
+static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
+    UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
+    if ([uiWebView respondsToSelector:@selector(webViewClose:)])
+        [uiWebView webViewClose:view];
+}
 
-    /* XXX: these are set by UIWebDocumentView
-    [webview _setFormDelegate:nil];
-    [webview _setUIKitDelegate:nil];
-    [webview setEditingDelegate:nil];*/
+- (void) webViewClose:(WebView *)view {
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webViewClose:)])
+        [delegate webViewClose:view];
+    if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
+        [super webViewClose:view];
+}
+// }}}
 
-    /* XXX: no one sets this, ever
-    [webview setWebMailDelegate:nil];*/
+- (void) _updateViewSettings {
+    [super _updateViewSettings];
 
-    [webview_ setDelegate:nil];
-    [webview_ setGestureDelegate:nil];
-    [webview_ setFormEditingDelegate:nil];
-    [webview_ setInteractionDelegate:nil];
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
+        [delegate webViewUpdateViewSettings:self];
+}
 
-    [indirect_ setDelegate:nil];
++ (void) initialize {
+    if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
+        class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
+    }
+}
 
-    //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+@end
 
-    [webview close];
+@implementation BrowserController
 
-#if RecycleWebViews
-    [webview_ removeFromSuperview];
-    [Documents_ addObject:[webview_ autorelease]];
-#else
-    [webview_ release];
+#if ShowInternals
+#include "UICaboodle/UCInternal.h"
 #endif
 
-    [indirect_ release];
++ (void) _initialize {
+    [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
+
+    if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
+        CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
+    else // XXX: this actually might be fast on some older systems: we should look into this
+        CYScrollViewDecelerationRateNormal = 0.998;
+}
+
+- (void) dealloc {
+#if LogBrowser
+    NSLog(@"[BrowserController dealloc]");
+#endif
 
-    WebThreadUnlock();
+    [webview_ setDelegate:nil];
 
-    [scroller_ setDelegate:nil];
+    [indirect_ setDelegate:nil];
+    [indirect_ release];
+
+    if (challenge_ != nil)
+        [challenge_ release];
 
-    if (button_ != nil)
-        [button_ release];
-    if (style_ != nil)
-        [style_ release];
-    if (function_ != nil)
-        [function_ release];
-    if (finish_ != nil)
-        [finish_ release];
     if (closer_ != nil)
         [closer_ release];
-    if (special_ != nil)
-        [special_ release];
 
-    [scroller_ release];
-    [indicator_ release];
-    if (confirm_ != nil)
-        [confirm_ release];
-    if (sensitive_ != nil)
-        [sensitive_ release];
     if (title_ != nil)
         [title_ release];
+
+    if ([loading_ count] != 0)
+        [delegate_ releaseNetworkActivityIndicator];
+    [loading_ release];
+
+    [reloaditem_ release];
+    [loadingitem_ release];
+
+    [indicator_ release];
+
     [super dealloc];
 }
 
+- (void) setURL:(NSURL *)url {
+    _assert(request_ == nil);
+
+    request_ = [NSURLRequest
+        requestWithURL:url
+        cachePolicy:NSURLRequestUseProtocolCachePolicy
+        timeoutInterval:DefaultTimeout_
+    ];
+}
+
 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
     [self loadRequest:[NSURLRequest
         requestWithURL:url
         cachePolicy:policy
-        timeoutInterval:30.0
+        timeoutInterval:DefaultTimeout_
     ]];
 }
 
@@ -303,483 +521,364 @@ UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize(
 }
 
 - (void) loadRequest:(NSURLRequest *)request {
-    pushed_ = true;
+#if LogBrowser
+    NSLog(@"loadRequest:%@", request);
+#endif
+
     error_ = false;
 
-    WebThreadLock();
+    WebThreadLocked lock;
     [webview_ loadRequest:request];
-    WebThreadUnlock();
 }
 
-- (void) reloadURL {
+- (void) reloadURLWithCache:(BOOL)cache {
     if (request_ == nil)
         return;
 
+    NSMutableURLRequest *request([request_ mutableCopy]);
+    [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
+
+    request_ = request;
+
     if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
         [self loadRequest:request_];
     else {
-        UIActionSheet *sheet = [[[UIActionSheet alloc]
+        UIAlertView *alert = [[[UIAlertView alloc]
             initWithTitle:UCLocalize("RESUBMIT_FORM")
-            buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
-            defaultButtonIndex:0
+            message:nil
             delegate:self
-            context:@"submit"
+            cancelButtonTitle:UCLocalize("CANCEL")
+            otherButtonTitles:
+                UCLocalize("SUBMIT"),
+            nil
         ] autorelease];
 
-        [sheet setNumberOfRows:1];
-        [sheet popupAlertAnimated:YES];
+        [alert setContext:@"submit"];
+        [alert show];
     }
 }
 
-- (WebView *) webView {
-    return [webview_ webView];
+- (void) reloadURL {
+    [self reloadURLWithCache:YES];
 }
 
-- (UIWebDocumentView *) documentView {
-    return webview_;
+- (void) reloadData {
+    [super reloadData];
+    [self reloadURLWithCache:YES];
 }
 
-/* XXX: WebThreadLock? */
-- (void) _fixScroller:(CGRect)bounds {
-    float extra;
-    if (!editing_)
-        extra = 0;
-    else {
-        UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
-        CGRect peripheral([assistant peripheralFrame]);
-#if LogBrowser
-        NSLog(@"per:%f", peripheral.size.height);
-#endif
-        extra = peripheral.size.height;
-    }
-
-    CGRect subrect([scroller_ frame]);
-    subrect.size.height -= extra;
-    [scroller_ setScrollerIndicatorSubrect:subrect];
+- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
+    custom_ = button;
+    style_ = style;
+    function_ = function;
 
-    NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
-    [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
+}
 
-    CGSize size(size_);
-    size.height += extra;
-    [scroller_ setContentSize:size];
+- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
+    custom_ = button;
+    style_ = style;
+    function_ = function;
 
-    [scroller_ releaseRubberBandIfNecessary];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
-- (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) removeButton {
+    custom_ = [NSNull null];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
-- (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
-    size_ = frame.size;
-#if TrackResize
-    NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
-#endif
-    [self _fixScroller:frame];
+- (void) setPopupHook:(id)function {
+    if (closer_ != nil)
+        [closer_ autorelease];
+    if (function == nil)
+        closer_ = nil;
+    else
+        closer_ = [function retain];
 }
 
-- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
-    [self view:sender didSetFrame:frame];
-}
+- (void) scrollToBottomAnimated:(NSNumber *)animated {
+    CGSize size([scroller_ contentSize]);
+    CGPoint offset([scroller_ contentOffset]);
+    CGRect frame([scroller_ frame]);
 
-- (void) pushPage:(RVPage *)page {
-    [page setDelegate:delegate_];
-    [self setBackButtonTitle:title_];
-    [book_ pushPage:page];
+    if (size.height - offset.y < frame.size.height + 20.f) {
+        CGRect rect = {{0, size.height-1}, {size.width, 1}};
+        [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
+    }
 }
 
-- (void) _pushPage {
-    if (pushed_)
-        return;
-    // WTR: [self autorelease];
-    pushed_ = true;
-    [book_ pushPage:self];
+- (void) _setViewportWidth {
+    [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
 }
 
-- (void) swapPage:(RVPage *)page {
-    [page setDelegate:delegate_];
-    if (pushed_)
-        [book_ swapPage:page];
-    else
-        [book_ pushPage:page];
+- (void) setViewportWidth:(float)width {
+    width_ = width != 0 ? width : [[self class] defaultWidth];
+    [self _setViewportWidth];
 }
 
-- (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
-#if LogBrowser
-    NSLog(@"getSpecial:%@", url);
-#endif
+- (void) _setViewportWidthOnMainThread:(NSNumber *)width {
+    [self setViewportWidth:[width floatValue]];
+}
 
-    if (RVPage *page = [delegate_ pageForURL:url hasTag:NULL]) {
-        if (swap)
-            [self swapPage:page];
-        else
-            [self pushPage:page];
+- (void) setViewportWidthOnMainThread:(float)width {
+    [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
+}
 
-        return true;
-    } else
-        return false;
+- (void) webViewUpdateViewSettings:(UIWebView *)view {
+    [self _setViewportWidth];
 }
 
-- (void) webViewShow:(WebView *)sender {
-    /* XXX: this is where I cry myself to sleep */
+- (void) _openMailToURL:(NSURL *)url {
+    [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
 }
 
 - (bool) _allowJavaScriptPanel {
     return true;
 }
 
-- (bool) allowSensitiveRequests {
-    return [self _allowJavaScriptPanel];
+- (bool) allowsNavigationAction {
+    return allowsNavigationAction_;
 }
 
-- (void) _promptForSensitive:(NSMutableArray *)array {
-    NSString *name([array objectAtIndex:0]);
-
-    UIActionSheet *sheet = [[[UIActionSheet alloc]
-        initWithTitle:nil
-        buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
-        defaultButtonIndex:0
-        delegate:indirect_
-        context:@"sensitive"
-    ] autorelease];
-
-    NSString *host(@"XXX");
-
-    [sheet setNumberOfRows:1];
-    [sheet setBodyText:[NSString stringWithFormat:@"The website at %@ is requesting your phone's %@. This is almost certainly for product licensing purposes. Will you allow this?", host, name]];
-    [sheet popupAlertAnimated:YES];
-
-    NSRunLoop *loop([NSRunLoop currentRunLoop]);
-    NSDate *future([NSDate distantFuture]);
-
-    while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
-
-    NSNumber *sensitive([sensitive_ autorelease]);
-    sensitive_ = nil;
-
-    [self autorelease];
-    [array replaceObjectAtIndex:0 withObject:sensitive];
+- (void) setAllowsNavigationAction:(bool)value {
+    allowsNavigationAction_ = value;
 }
 
-- (bool) promptForSensitive:(NSString *)name {
-    if (![self allowSensitiveRequests])
-        return false;
-
-    NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
-    [array addObject:name];
-
-    [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
-    return [[array lastObject] boolValue];
+- (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
+    [self setAllowsNavigationAction:[value boolValue]];
 }
 
-- (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
-    if (![self _allowJavaScriptPanel])
+- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
+    [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
+    [self _didFinishLoading];
+
+    if ([error code] == NSURLErrorCancelled)
         return;
-    [self retain];
-
-    UIActionSheet *sheet = [[[UIActionSheet alloc]
-        initWithTitle:nil
-        buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
-        defaultButtonIndex:0
-        delegate:self
-        context:@"alert"
-    ] autorelease];
 
-    [sheet setBodyText:message];
-    [sheet popupAlertAnimated:YES];
+    if ([frame parentFrame] == nil) {
+        [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
+            [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
+            [[error localizedDescription] stringByAddingPercentEscapes]
+        ]]];
+
+        error_ = true;
+    }
 }
 
-- (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
-    if (![self _allowJavaScriptPanel])
-        return NO;
-    [self retain];
+- (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
+    NSURL *url([request URL]);
 
-    UIActionSheet *sheet = [[[UIActionSheet alloc]
-        initWithTitle:nil
-        buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
-        defaultButtonIndex:0
-        delegate:indirect_
-        context:@"confirm"
-    ] autorelease];
+    // XXX: filter to internal usage?
+    CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
 
-    [sheet setNumberOfRows:1];
-    [sheet setBodyText:message];
-    [sheet popupAlertAnimated:YES];
+    if (page == nil) {
+        BrowserController *browser([[[class_ alloc] init] autorelease]);
+        [browser loadRequest:request];
+        page = browser;
+    }
 
-    NSRunLoop *loop([NSRunLoop currentRunLoop]);
-    NSDate *future([NSDate distantFuture]);
+    [page setDelegate:delegate_];
 
-    while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
+    if (!pop) {
+        [[self navigationItem] setTitle:title_];
 
-    NSNumber *confirm([confirm_ autorelease]);
-    confirm_ = nil;
+        [[self navigationController] pushViewController:page animated:YES];
+    } else {
+        UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
 
-    [self autorelease];
-    return [confirm boolValue];
-}
+        [navigation setHook:indirect_];
+        [navigation setDelegate:delegate_];
 
-- (void) setAutoPopup:(BOOL)popup {
-    popup_ = popup;
-}
+        [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
+            initWithTitle:UCLocalize("CLOSE")
+            style:UIBarButtonItemStylePlain
+            target:page
+            action:@selector(close)
+        ] autorelease]];
 
-- (void) setSpecial:(id)function {
-    if (special_ != nil)
-        [special_ autorelease];
-    special_ = function == nil ? nil : [function retain];
+        [[self navigationController] presentModalViewController:navigation animated:YES];
+    }
 }
 
-- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
-    if (button_ != nil)
-        [button_ autorelease];
-    button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
-
-    if (style_ != nil)
-        [style_ autorelease];
-    style_ = style == nil ? nil : [style retain];
-
-    if (function_ != nil)
-        [function_ autorelease];
-    function_ = function == nil ? nil : [function retain];
-
-    [self reloadButtons];
+// CYWebViewDelegate {{{
+- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
+#if LogMessages
+    NSLog(@"addMessageToConsole:%@", message);
+#endif
 }
 
-- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
-    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];
-
-    [self reloadButtons];
-}
+- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+#if LogBrowser
+    NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
+#endif
 
-- (void) setFinishHook:(id)function {
-    if (finish_ != nil)
-        [finish_ autorelease];
-    finish_ = function == nil ? nil : [function retain];
-}
+    if ([frame parentFrame] == nil) {
+        if (!error_) {
+            NSURL *url(request == nil ? nil : [request URL]);
 
-- (void) setPopupHook:(id)function {
-    if (closer_ != nil)
-        [closer_ autorelease];
-    closer_ = function == nil ? nil : [function retain];
+            if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
+                request_ = request;
+            else {
+                if (url != nil)
+                    [self pushRequest:request asPop:NO];
+                [listener ignore];
+            }
+        }
+    }
 }
 
-- (void) _openMailToURL:(NSURL *)url {
-// XXX: this makes me sad
-#if 0
-    [[[MailToView alloc] initWithView:underlay_ delegate:self url:url] autorelease];
-#else
-    [UIApp openURL:url];// asPanel:YES];
+- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+#if LogBrowser
+    NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
 #endif
-}
-
-- (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
-    editing_ = true;
-}
 
-- (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
-    [self fixScroller];
-}
+    NSURL *url([request URL]);
+    if (url == nil)
+        return;
 
-- (void) webViewDidEndEditingFormElements:(WebView *)sender {
-    editing_ = false;
-    [self fixScroller];
-}
+    if ([frame isEqualToString:@"_open"])
+        [delegate_ openURL:url];
+    else {
+        NSString *scheme([[url scheme] lowercaseString]);
+        if ([scheme isEqualToString:@"mailto"])
+            [self _openMailToURL:url];
+        else
+            [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
+    }
 
-- (void) webViewClose:(WebView *)sender {
-    [book_ close];
+    [listener ignore];
 }
 
-- (void) close {
-    [book_ close];
+- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
 }
 
-- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
-}
+- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
+#if LogBrowser
+    NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
+#endif
 
-- (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
-    NSLog(@"err:%@", error);
+    [self _didFailWithError:error forFrame:frame];
 }
 
-- (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
+- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
 #if LogBrowser
-    NSLog(@"nwa:%@", name);
+    NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
 #endif
 
-    if (NSURL *url = [request URL]) {
-        if (name == nil) unknown: {
-            if (![self getSpecial:url swap:NO]) {
-                NSString *scheme([[url scheme] lowercaseString]);
-                if ([scheme isEqualToString:@"mailto"])
-                    [self _openMailToURL:url];
-                else goto use;
-            }
-        } else if ([name isEqualToString:@"_open"])
-            [delegate_ openURL:url];
-        else if ([name isEqualToString:@"_popup"]) {
-            NSString *scheme([[url scheme] lowercaseString]);
-            if ([scheme isEqualToString:@"mailto"])
-                [self _openMailToURL:url];
-            else {
-                RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
-                [book setHook:indirect_];
+    [self _didFailWithError:error forFrame:frame];
+}
 
-                RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
-                if (page == nil) {
-                    /* XXX: call createWebViewWithRequest instead? */
+// XXX: factor this out somewhere
+- (UIColor *) groupTableViewBackgroundColor {
+    UIDevice *device([UIDevice currentDevice]);
+    bool iPad([device respondsToSelector:@selector(userInterfaceIdiom)] && [device userInterfaceIdiom] == UIUserInterfaceIdiomPad);
+    return iPad ? [UIColor colorWithRed:0.821 green:0.834 blue:0.860 alpha:1] : [UIColor groupTableViewBackgroundColor];
+}
 
-                    [self setBackButtonTitle:title_];
+- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
+    [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
 
-                    BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
-                    [browser loadURL:url];
-                    page = browser;
-                }
+    if ([frame parentFrame] == nil) {
+        if (DOMDocument *document = [frame DOMDocument])
+            if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
+                for (DOMHTMLBodyElement *body in (id) bodies) {
+                    DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
 
-                [book setDelegate:delegate_];
-                [page setDelegate:delegate_];
+                    UIColor *uic([self groupTableViewBackgroundColor]);
 
-                [book setPage:page];
-                [book_ pushBook:book];
-            }
-        } else goto unknown;
+                    if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
+                        if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
+                            DOMRGBColor *rgb([color getRGBColorValue]);
 
-        [listener ignore];
-    } else use:
-        [listener use];
-}
+                            float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
+                            float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
+                            float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
+                            float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
 
-- (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
-    if ([WebView canShowMIMEType:type])
-        [listener use];
-    else {
-        // XXX: handle more mime types!
-        [listener ignore];
+                            if (red == 0xc7 && green == 0xce && blue == 0xd5)
+                                uic = [UIColor pinStripeColor];
+                            else if (alpha != 0)
+                                uic = [UIColor
+                                    colorWithRed:(red / 255)
+                                    green:(green / 255)
+                                    blue:(blue / 255)
+                                    alpha:alpha
+                                ];
+                        }
+                    }
 
-        WebView *webview([webview_ webView]);
-        if (frame == [webview mainFrame])
-            [UIApp openURL:[request URL]];
+                    [scroller_ setBackgroundColor:uic];
+                    break;
+                }
     }
+
+    [self _didFinishLoading];
 }
 
-- (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
-    if (request == nil) ignore: {
-        [listener ignore];
+- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
+    if ([frame parentFrame] != nil)
         return;
-    }
-
-    NSURL *url([request URL]);
 
-    if (url == nil) use: {
-        if (!error_ && [frame parentFrame] == nil) {
-            if (request_ != nil)
-                [request_ autorelease];
-            request_ = [request retain];
-#if LogBrowser
-            NSLog(@"dpn:%@", request_);
-#endif
-        }
+    if (title_ != nil)
+        [title_ autorelease];
+    title_ = [title retain];
 
-        [listener use];
+    [[self navigationItem] setTitle:title_];
+}
 
-        WebView *webview([webview_ webView]);
-        if (frame == [webview mainFrame])
-            [self _pushPage];
-        return;
-    }
-#if LogBrowser
-    else NSLog(@"nav:%@:%@", url, [action description]);
-#endif
+- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
+    [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
 
-    const NSArray *capability;
+    if ([frame parentFrame] == nil) {
+        CYRelease(title_);
+        custom_ = nil;
+        style_ = nil;
+        function_ = nil;
+        CYRelease(closer_);
 
-#if 0 // XXX:3:GSSystemCopyCapability
-    capability = reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability));
-#else
-    capability = nil;
-#endif
+        [self setHidesNavigationBar:NO];
 
-    if (capability != nil && (
-        [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
-        [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
-    )) {
-      open:
-        [UIApp openURL:url];
-        goto ignore;
+        // XXX: do we still need to do this?
+        [[self navigationItem] setTitle:nil];
     }
 
-    int store(_not(int));
-    if (NSURL *itms = [url itmsURL:&store]) {
+    [self _didStartLoading];
+}
+
+- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
 #if LogBrowser
-        NSLog(@"itms#%@#%u#%@", url, store, itms);
+    NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
 #endif
 
-        if (capability != nil && (
-            store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
-            store == 2 && [capability containsObject:@"com.apple.AppStore"]
-        )) {
-            url = itms;
-            goto open;
-        }
-    }
-
-    NSString *scheme([[url scheme] lowercaseString]);
-
-    if ([scheme isEqualToString:@"tel"]) {
-        // XXX: intelligence
-        goto open;
-    }
+    return request;
+}
 
-    if ([scheme isEqualToString:@"mailto"]) {
-        [self _openMailToURL:url];
-        goto ignore;
-    }
+- (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
+    return [self _allowJavaScriptPanel];
+}
 
-    if ([self getSpecial:url swap:YES])
-        goto ignore;
-    else if ([WebView _canHandleRequest:request])
-        goto use;
-    else if ([url isSpringboardHandledURL])
-        goto open;
-    else
-        goto use;
+- (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
+    return [self _allowJavaScriptPanel];
 }
 
-- (void) webView:(WebView *)sender setStatusText:(NSString *)text {
-    //lprintf("Status:%s\n", [text UTF8String]);
+- (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
+    return [self _allowJavaScriptPanel];
 }
 
-- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
-    NSString *context([sheet context]);
+- (void) webViewClose:(WebView *)view {
+    [self close];
+}
+// }}}
 
-    if ([context isEqualToString:@"alert"]) {
-        [self autorelease];
-        [sheet dismiss];
-    } else if ([context isEqualToString:@"confirm"]) {
-        switch (button) {
-            case 1:
-                confirm_ = [NSNumber numberWithBool:YES];
-            break;
+- (void) close {
+    [[self navigationController] dismissModalViewControllerAnimated:YES];
+}
 
-            case 2:
-                confirm_ = [NSNumber numberWithBool:NO];
-            break;
-        }
+- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
+    NSString *context([alert context]);
 
-        [sheet dismiss];
-    } else if ([context isEqualToString:@"sensitive"]) {
+    if ([context isEqualToString:@"sensitive"]) {
         switch (button) {
             case 1:
                 sensitive_ = [NSNumber numberWithBool:YES];
@@ -790,14 +889,14 @@ UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize(
             break;
         }
 
-        [sheet dismiss];
+        [alert dismissWithClickedButtonIndex:-1 animated:YES];
     } else if ([context isEqualToString:@"challenge"]) {
         id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
 
         switch (button) {
             case 1: {
-                NSString *username([[sheet textFieldAtIndex:0] text]);
-                NSString *password([[sheet textFieldAtIndex:1] text]);
+                NSString *username([[alert textFieldAtIndex:0] text]);
+                NSString *password([[alert textFieldAtIndex:1] text]);
 
                 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
 
@@ -808,506 +907,244 @@ UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize(
                 [sender cancelAuthenticationChallenge:challenge_];
             break;
 
-            default:
-                _assert(false);
+            _nodefault
         }
 
         [challenge_ release];
         challenge_ = nil;
 
-        [sheet dismiss];
+        [alert dismissWithClickedButtonIndex:-1 animated:YES];
     } else if ([context isEqualToString:@"submit"]) {
-        switch (button) {
-            case 1:
-            break;
-
-            case 2:
-                if (request_ != nil) {
-                    WebThreadLock();
-                    [webview_ loadRequest:request_];
-                    WebThreadUnlock();
-                }
-            break;
-
-            default:
-                _assert(false);
+        if (button == [alert cancelButtonIndex]) {
+        } else if (button == [alert firstOtherButtonIndex]) {
+            if (request_ != nil) {
+                WebThreadLocked lock;
+                [webview_ loadRequest:request_];
+            }
         }
 
-        [sheet dismiss];
+        [alert dismissWithClickedButtonIndex:-1 animated:YES];
     }
 }
 
-- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
-    challenge_ = [challenge retain];
-
-    NSURLProtectionSpace *space([challenge protectionSpace]);
-    NSString *realm([space realm]);
-    if (realm == nil)
-        realm = @"";
-
-    UIActionSheet *sheet = [[[UIActionSheet alloc]
-        initWithTitle:realm
-        buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
-        defaultButtonIndex:0
-        delegate:self
-        context:@"challenge"
-    ] autorelease];
-
-    [sheet setNumberOfRows:1];
-
-    [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
-    [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
-
-    UITextField *username([sheet textFieldAtIndex:0]); {
-        UITextInputTraits *traits([username textInputTraits]);
-        [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
-        [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
-        [traits setKeyboardType:UIKeyboardTypeASCIICapable];
-        [traits setReturnKeyType:UIReturnKeyNext];
-    }
-
-    UITextField *password([sheet textFieldAtIndex:1]); {
-        UITextInputTraits *traits([password textInputTraits]);
-        [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
-        [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
-        [traits setKeyboardType:UIKeyboardTypeASCIICapable];
-        // XXX: UIReturnKeyDone
-        [traits setReturnKeyType:UIReturnKeyNext];
-        [traits setSecureTextEntry:YES];
-    }
-
-    [sheet popupAlertAnimated:YES];
+- (UIBarButtonItemStyle) rightButtonStyle {
+    if (style_ == nil) normal:
+        return UIBarButtonItemStylePlain;
+    else if ([style_ isEqualToString:@"Normal"])
+        return UIBarButtonItemStylePlain;
+    else if ([style_ isEqualToString:@"Highlighted"])
+        return UIBarButtonItemStyleDone;
+    else goto normal;
 }
 
-- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
-    return request;
+- (UIBarButtonItem *) customButton {
+    return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
+        initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
+        style:[self rightButtonStyle]
+        target:self
+        action:@selector(customButtonClicked)
+    ] autorelease];
 }
 
-- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
-//- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
-#if LogBrowser
-    NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
-    //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
-#endif
-
-    NSNumber *value([features objectForKey:@"width"]);
-    float width(value == nil ? 0 : [value floatValue]);
-
-    RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
-
-    /* XXX: deal with cydia:// pages */
-    BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
-
-    if (features != nil && popup_) {
-        [book setDelegate:delegate_];
-        [book setHook:indirect_];
-        [browser setDelegate:delegate_];
-
-        [browser loadRequest:request];
-
-        [book setPage:browser];
-        [book_ pushBook:book];
-    } else if (request == nil) {
-        [self setBackButtonTitle:title_];
-        [browser setDelegate:delegate_];
-        [browser retain];
-    } else {
-        [self pushPage:browser];
-        [browser loadRequest:request];
-    }
-
-    return [browser webView];
+- (UIBarButtonItem *) rightButton {
+    return reloaditem_;
 }
 
-- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
-    return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
-    //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
+- (void) applyLoadingTitle {
+    [[self navigationItem] setTitle:UCLocalize("LOADING")];
 }
 
-- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
-    if ([frame parentFrame] != nil)
-        return;
-
-    title_ = [title retain];
-    [book_ reloadTitleForPage:self];
+- (void) layoutRightButton {
+    [[loadingitem_ view] addSubview:indicator_];
+    [[loadingitem_ view] bringSubviewToFront:indicator_];
 }
 
-- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
-    if ([loading_ count] == 0)
-        [self retain];
-    [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
+- (void) applyRightButton {
+    if ([self isLoading]) {
+        [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
+        [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
 
-    if ([frame parentFrame] == nil) {
-        [webview_ resignFirstResponder];
-
-        reloading_ = false;
-
-        if (title_ != nil) {
-            [title_ release];
-            title_ = nil;
-        }
-
-        if (button_ != nil) {
-            [button_ release];
-            button_ = nil;
-        }
-
-        if (style_ != nil) {
-            [style_ release];
-            style_ = nil;
-        }
-
-        if (function_ != nil) {
-            [function_ release];
-            function_ = nil;
-        }
-
-        if (finish_ != nil) {
-            [finish_ release];
-            finish_ = nil;
-        }
-
-        if (closer_ != nil) {
-            [closer_ release];
-            closer_ = nil;
-        }
-
-        if (special_ != nil) {
-            [special_ release];
-            special_ = nil;
-        }
-
-        [book_ reloadTitleForPage:self];
-
-        [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
-
-        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];*/
+        [indicator_ startAnimating];
+        [self applyLoadingTitle];
+    } else {
+        [indicator_ stopAnimating];
 
-        CGRect webrect = [scroller_ bounds];
-        webrect.size.height = 0;
-        [webview_ setFrame:webrect];
+        [[self navigationItem] setRightBarButtonItem:(
+            custom_ != nil ? [self customButton] : [self rightButton]
+        ) animated:YES];
     }
-
-    [self reloadButtons];
 }
 
-- (void) _finishLoading {
-    size_t count([loading_ count]);
-    if (count == 0)
-        [self autorelease];
-    if (reloading_ || count != 0)
-        return;
-    if (finish_ != nil)
-        [self callFunction:finish_];
-    [self reloadButtons];
+- (void) didStartLoading {
+    // Overridden in subclasses.
 }
 
-- (bool) isLoading {
-    return [loading_ count] != 0;
-}
+- (void) _didStartLoading {
+    [self applyRightButton];
 
-- (void) reloadButtons {
-    if ([self isLoading])
-        [indicator_ startAnimation];
-    else
-        [indicator_ stopAnimation];
-    [super reloadButtons];
-}
+    if ([loading_ count] != 1)
+        return;
 
-- (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
-    return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
+    [delegate_ retainNetworkActivityIndicator];
+    [self didStartLoading];
 }
 
-- (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
-    return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
+- (void) didFinishLoading {
+    // Overridden in subclasses.
 }
 
-- (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
-    return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
-}
+- (void) _didFinishLoading {
+    if ([loading_ count] != 0)
+        return;
 
-- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
-    [self _pushPage];
-    return [webview_ webView:sender didCommitLoadForFrame:frame];
-}
+    [self applyRightButton];
+    [[self navigationItem] setTitle:title_];
 
-- (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
-    return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
+    [delegate_ releaseNetworkActivityIndicator];
+    [self didFinishLoading];
 }
 
-- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
-    [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) {
-                    DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
-
-                    bool colored(false);
-
-                    if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
-                        if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
-                            DOMRGBColor *rgb([color getRGBColorValue]);
+- (bool) isLoading {
+    return [loading_ count] != 0;
+}
 
-                            float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
-                            float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
-                            float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
-                            float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
+- (id) initWithWidth:(float)width ofClass:(Class)_class {
+    if ((self = [super init]) != nil) {
+        allowsNavigationAction_ = true;
 
-                            UIColor *uic(nil);
+        class_ = _class;
+        loading_ = [[NSMutableSet alloc] initWithCapacity:5];
 
-                            if (red == 0xc7 && green == 0xce && blue == 0xd5)
-                                uic = [UIColor pinStripeColor];
-                            else if (alpha != 0)
-                                uic = [UIColor
-                                    colorWithRed:(red / 255)
-                                    green:(green / 255)
-                                    blue:(blue / 255)
-                                    alpha:alpha
-                                ];
+        indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
 
-                            if (uic != nil) {
-                                colored = true;
-                                [scroller_ setBackgroundColor:uic];
-                            }
-                        }
-                    }
+        webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
+        [webview_ setDelegate:self];
+        [self setView:webview_];
 
-                    if (!colored)
-                        [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
-                    break;
-                }
-    }
+        if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
+            [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
+        else
+            [webview_ setDetectsPhoneNumbers:NO];
 
-    return [webview_ webView:sender didFinishLoadForFrame:frame];
-}
+        [webview_ setScalesPageToFit:YES];
 
-- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
-    if ([frame parentFrame] == nil)
-        [self autorelease];
+        UIWebDocumentView *document([webview_ _documentView]);
 
-    [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
-    [self _finishLoading];
+        // XXX: I think this improves scrolling; the hardcoded-ness sucks
+        [document setTileSize:CGSizeMake(320, 500)];
 
-    if (reloading_)
-        return;
+        [document setBackgroundColor:[UIColor clearColor]];
 
-    if ([frame parentFrame] == nil) {
-        [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
-            [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
-            [[error localizedDescription] stringByAddingPercentEscapes]
-        ]]];
+        // XXX: this is terribly (too?) expensive
+        [document setDrawsBackground:NO];
 
-        error_ = true;
-    }
-}
+        WebView *webview([document webView]);
+        WebPreferences *preferences([webview preferences]);
 
-- (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
-    [self _didFailWithError:error forFrame:frame];
-}
+        // XXX: I have no clue if I actually /want/ this modification
+        if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
+            [webview _setLayoutInterval:0];
+        else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
+            [preferences _setLayoutInterval:0];
 
-- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
-    [self _didFailWithError:error forFrame:frame];
-}
+        [preferences setCacheModel:WebCacheModelDocumentBrowser];
+        [preferences setOfflineWebApplicationCacheEnabled:YES];
 
-- (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
-#if LogBrowser || ForSaurik
-    lprintf("Console:%s\n", [[dictionary description] UTF8String]);
+#if LogMessages
+        if ([document respondsToSelector:@selector(setAllowsMessaging:)])
+            [document setAllowsMessaging:YES];
+        if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
+            [webview _setAllowsMessaging:YES];
 #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];
-}
+        if ([webview_ respondsToSelector:@selector(_scrollView)]) {
+            scroller_ = [webview_ _scrollView];
 
-- (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];
-}
+            [scroller_ setDirectionalLockEnabled:YES];
+            [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
+            [scroller_ setDelaysContentTouches:NO];
 
-- (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
-    [self _setTileDrawingEnabled:YES];
-}
+            [scroller_ setCanCancelContentTouches:YES];
+        } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
+            UIScroller *scroller([webview_ _scroller]);
+            scroller_ = (UIScrollView *) scroller;
 
-- (void) scrollerDidEndDragging:(UIScroller *)scroller {
-    [self _setTileDrawingEnabled:YES];
-}
+            [scroller setDirectionalScrolling:YES];
+            // XXX: we might be better off /not/ setting this on older systems
+            [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
+            [scroller setScrollHysteresis:0]; /* 8 */
 
-- (id) initWithBook:(RVBook *)book forWidth:(float)width {
-    if ((self = [super initWithBook:book]) != nil) {
-        loading_ = [[NSMutableSet alloc] initWithCapacity:3];
-        popup_ = false;
+            [scroller setThumbDetectionEnabled:NO];
 
-        struct CGRect bounds = [self bounds];
+            // use NO with UIApplicationUseLegacyEvents(YES)
+            [scroller setEventMode:YES];
 
-        scroller_ = [[UIScroller alloc] initWithFrame:bounds];
-        [self addSubview:scroller_];
+            // XXX: this is handled by setBounces, right?
+            //[scroller setAllowsRubberBanding:YES];
+        }
 
         [scroller_ setFixedBackgroundPattern:YES];
-        [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
-
-        [scroller_ setScrollingEnabled:YES];
+        [scroller_ setBackgroundColor:[self groupTableViewBackgroundColor]];
         [scroller_ setClipsSubviews:YES];
-        [scroller_ setAllowsRubberBanding:YES];
 
-        [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;
-
-        WebThreadLock();
-
-#if RecycleWebViews
-        webview_ = [Documents_ lastObject];
-        if (webview_ != nil) {
-            webview_ = [webview_ retain];
-            webview = [webview_ webView];
-            [Documents_ removeLastObject];
-            [webview_ setFrame:webrect];
-        } else {
-#else
-        if (true) {
-#endif
-            webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
-            webview = [webview_ webView];
-
-            // XXX: this is terribly (too?) expensive
-            //[webview_ setDrawsBackground:NO];
-            [webview setPreferencesIdentifier:@"Cydia"];
-
-            [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
-
-            [webview_ setAllowsMessaging:YES];
-
-            [webview_ setTilingEnabled:YES];
-            [webview_ setDrawsGrid:NO];
-            [webview_ setLogsTilingChanges:NO];
-            [webview_ setTileMinificationFilter:kCAFilterNearest];
-            if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
-                /* XXX: abstractify */
-                [webview_ setDataDetectorTypes:0x80000000];
-            else
-                [webview_ setDetectsPhoneNumbers:NO];
-            [webview_ setAutoresizes:YES];
-
-            [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
-            [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
-            [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
-            //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
-
-            [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
-
-            [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
-            [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
-            [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
-
-            [webview_ _setDocumentType:0x4];
-
-            if ([webview_ respondsToSelector:@selector(UIWebDocumentView:)])
-                [webview_ setZoomsFocusedFormControl:YES];
-            [webview_ setContentsPosition:7];
-            [webview_ setEnabledGestures:0xa];
-            [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
-            [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
-
-            [webview_ setSmoothsFonts:YES];
-            [webview_ setAllowsImageSheet:YES];
-            [webview _setUsesLoaderCache:YES];
-
-            [webview setGroupName:@"CydiaGroup"];
-            if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
-                [webview _setLayoutInterval:0];
-        }
+        [scroller_ setScrollingEnabled:YES];
+        [scroller_ setShowBackgroundShadow:NO];
 
         [self setViewportWidth:width];
 
-        [webview_ setDelegate:self];
-        [webview_ setGestureDelegate:self];
-        [webview_ setFormEditingDelegate:self];
-        [webview_ setInteractionDelegate:self];
-
-        [scroller_ addSubview:webview_];
-
-        //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
-
-        indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
-
-        [webview setFrameLoadDelegate:indirect_];
-        [webview setResourceLoadDelegate:indirect_];
-        [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];
+        reloaditem_ = [[UIBarButtonItem alloc]
+            initWithTitle:UCLocalize("RELOAD")
+            style:[self rightButtonStyle]
+            target:self
+            action:@selector(reloadButtonClicked)
+        ];
+
+        loadingitem_ = [[UIBarButtonItem alloc]
+            initWithTitle:@" "
+            style:UIBarButtonItemStylePlain
+            target:self
+            action:@selector(reloadButtonClicked)
+        ];
+
+        indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
+        [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
+
+        [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
+        [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
+    } return self;
+}
 
-        [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
-        [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
+- (id) initWithWidth:(float)width {
+    return [self initWithWidth:width ofClass:[self class]];
+}
 
-        /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
-        [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
-        [self addSubview:test];*/
-    } return self;
+- (id) init {
+    return [self initWithWidth:0];
 }
 
-- (id) initWithBook:(RVBook *)book {
-    return [self initWithBook:book forWidth:0];
+- (id) initWithURL:(NSURL *)url {
+    if ((self = [self init]) != nil) {
+        [self setURL:url];
+    } return self;
 }
 
-- (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
-    WebThreadLock();
-    WebView *webview([webview_ webView]);
-    NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
-    WebThreadUnlock();
-    return string;
+- (void) didDismissModalViewController {
+    if (closer_ != nil)
+        [self callFunction:closer_];
 }
 
 - (void) callFunction:(WebScriptObject *)function {
-    WebThreadLock();
+    WebThreadLocked lock;
 
-    WebView *webview([webview_ webView]);
+    WebView *webview([[webview_ _documentView] webView]);
     WebFrame *frame([webview mainFrame]);
+    WebPreferences *preferences([webview preferences]);
+
+    bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
+    [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
 
-    id _private(MSHookIvar<id>(webview, "_private"));
+    /*id _private(MSHookIvar<id>(webview, "_private"));
     WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
     WebCore::Settings *settings(page == NULL ? NULL : page->settings());
 
@@ -1317,9 +1154,9 @@ UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize(
     else {
         no = settings->JavaScriptCanOpenWindowsAutomatically();
         settings->setJavaScriptCanOpenWindowsAutomatically(true);
-    }
+    }*/
 
-    if (UIWindow *window = [self window])
+    if (UIWindow *window = [[self view] window])
         if (UIResponder *responder = [window firstResponder])
             [responder resignFirstResponder];
 
@@ -1327,77 +1164,126 @@ UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize(
     JSGlobalContextRef context([frame globalContext]);
     JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
 
-    if (settings != NULL)
-        settings->setJavaScriptCanOpenWindowsAutomatically(no);
+    /*if (settings != NULL)
+        settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
 
-    WebThreadUnlock();
+    [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
 }
 
-- (void) didCloseBook:(RVBook *)book {
-    if (closer_ != nil)
-        [self callFunction:closer_];
+- (void) reloadButtonClicked {
+    [self reloadURLWithCache:YES];
 }
 
-- (void) __rightButtonClicked {
-    reloading_ = true;
-    [self reloadURL];
+- (void) _customButtonClicked {
+    [self reloadButtonClicked];
 }
 
-- (void) _rightButtonClicked {
+- (void) customButtonClicked {
 #if !AlwaysReload
     if (function_ != nil)
         [self callFunction:function_];
     else
 #endif
-        [self __rightButtonClicked];
+    [self _customButtonClicked];
 }
 
-- (id) _rightButtonTitle {
-    return UCLocalize("RELOAD");
++ (float) defaultWidth {
+    return 980;
 }
 
-- (id) rightButtonTitle {
-    return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
+- (void) setNavigationBarStyle:(NSString *)name {
+    UIBarStyle style;
+    if ([name isEqualToString:@"Black"])
+        style = UIBarStyleBlack;
+    else
+        style = UIBarStyleDefault;
+
+    [[[self navigationController] navigationBar] setBarStyle:style];
 }
 
-- (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;
+- (void) setNavigationBarTintColor:(UIColor *)color {
+    [[[self navigationController] navigationBar] setTintColor:color];
 }
 
-- (NSString *) title {
-    return title_ == nil ? UCLocalize("LOADING") : title_;
+- (void) setHidesBackButton:(bool)value {
+    [[self navigationItem] setHidesBackButton:value];
 }
 
-- (NSString *) backButtonTitle {
-    return UCLocalize("BROWSER");
+- (void) setHidesBackButtonByNumber:(NSNumber *)value {
+    [self setHidesBackButton:[value boolValue]];
 }
 
-- (void) setPageActive:(BOOL)active {
-    if (!active)
-        [indicator_ removeFromSuperview];
-    else
-        [[book_ navigationBar] addSubview:indicator_];
+- (void) dispatchEvent:(NSString *)event {
+    WebThreadLocked lock;
+
+    NSString *script([NSString stringWithFormat:@
+        "(function() {"
+            "var event = this.document.createEvent('Events');"
+            "event.initEvent('%@', false, false);"
+            "this.document.dispatchEvent(event);"
+        "})();"
+    , event]);
+
+    NSMutableArray *frames([NSMutableArray arrayWithObjects:
+        [[[webview_ _documentView] webView] mainFrame]
+    , nil]);
+
+    while (WebFrame *frame = [frames lastObject]) {
+        WebScriptObject *object([frame windowObject]);
+        [object evaluateWebScript:script];
+        [frames removeLastObject];
+        [frames addObjectsFromArray:[frame childFrames]];
+    }
 }
 
-- (void) resetViewAnimated:(BOOL)animated {
+- (bool) hidesNavigationBar {
+    return hidesNavigationBar_;
 }
 
-- (void) setPushed:(bool)pushed {
-    pushed_ = pushed;
+- (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
+    if (visible_)
+        [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
 }
 
-+ (float) defaultWidth {
-    return 980;
+- (void) setHidesNavigationBar:(bool)value {
+    if (hidesNavigationBar_ != value) {
+        hidesNavigationBar_ = value;
+        [self _setHidesNavigationBar:YES animated:YES];
+    }
+}
+
+- (void) setHidesNavigationBarByNumber:(NSNumber *)value {
+    [self setHidesNavigationBar:[value boolValue]];
+}
+
+- (void) viewWillAppear:(BOOL)animated {
+    visible_ = true;
+
+    if ([self hidesNavigationBar])
+        [self _setHidesNavigationBar:YES animated:animated];
+
+    [self dispatchEvent:@"CydiaViewWillAppear"];
+    [super viewWillAppear:animated];
+}
+
+- (void) viewDidAppear:(BOOL)animated {
+    [super viewDidAppear:animated];
+    [self dispatchEvent:@"CydiaViewDidAppear"];
+}
+
+- (void) viewWillDisappear:(BOOL)animated {
+    [self dispatchEvent:@"CydiaViewWillDisappear"];
+    [super viewWillDisappear:animated];
+
+    if ([self hidesNavigationBar])
+        [self _setHidesNavigationBar:NO animated:animated];
+
+    visible_ = false;
+}
+
+- (void) viewDidDisappear:(BOOL)animated {
+    [super viewDidDisappear:animated];
+    [self dispatchEvent:@"CydiaViewDidDisappear"];
 }
 
 @end