]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Use UIApplication::networkActivityIndicator to indicate network activity.
[cydia.git] / UICaboodle / BrowserView.mm
index 8b361f5ecaa680641992e8874a687f9572241ab4..f1053f85e65f5c5fa76185f6c4900ca510adb104 100644 (file)
@@ -36,6 +36,8 @@ static inline void CYRelease(Type_ &value) {
     }
 }
 
+float CYScrollViewDecelerationRateNormal;
+
 @interface WebView (Apple)
 - (void) _setLayoutInterval:(float)interval;
 @end
@@ -167,6 +169,7 @@ enum CYWebPolicyDecision {
     switch (decision_) {
         case CYWebPolicyDecisionUnknown:
         default:
+            NSLog(@"CYWebPolicyDecisionUnknown");
             return false;
 
         case CYWebPolicyDecisionDownload: [listener_ download]; break;
@@ -208,102 +211,199 @@ enum CYWebPolicyDecision {
 }
 
 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
-    NSLog(@"createWebViewWithRequest:%@", request);
-    WebView *created(nil); // XXX
-    if (created == nil && [super respondsToSelector:@selector(webView:createWebViewWithRequest:)])
-        return [super webView:view createWebViewWithRequest:request];
-    else
-        return created;
+    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: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]);
-    [[self delegate] webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
-    if (![mediator decided] && [super respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
+    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]);
-    [[self delegate] webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
-    if (![mediator decided] && [super respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
+    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 {
-    [[self delegate] webView:view didClearWindowObject:window forFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
+    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 {
-    [[self delegate] webView:view didFailLoadWithError:error forFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
+    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 {
-    [[self delegate] webView:view didFailProvisionalLoadWithError:error forFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
+    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 {
-    [[self delegate] webView:view didFinishLoadForFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
+    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 {
-    [[self delegate] webView:view didReceiveTitle:title forFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
+    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 {
-    [[self delegate] webView:view didStartProvisionalLoadForFrame:frame];
-    if ([super respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
+    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;
+}
 
 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
-    if ([super respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:)])
+    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];
-    return [[self delegate] 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 {
-    if ([super respondsToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
-        if ([[self delegate] webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame: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 {
-    if ([super respondsToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
-        if ([[self delegate] webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame: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 {
-    if ([super respondsToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
-        if ([[self delegate] webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame: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];
+}
 
 - (void) webViewClose:(WebView *)view {
-    [[self delegate] webViewClose:view];
-    if ([super respondsToSelector:@selector(webViewClose:)])
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webViewClose:)])
+        [delegate webViewClose:view];
+    if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
         [super webViewClose:view];
 }
+// }}}
+
+- (void) _updateViewSettings {
+    [super _updateViewSettings];
+
+    id<CYWebViewDelegate> delegate([self delegate]);
+    if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
+        [delegate webViewUpdateViewSettings:self];
+}
+
++ (void) initialize {
+    if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
+        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");
+    }
+}
 
 @end
 
 #define ShowInternals 0
-#define LogBrowser 1
+#define LogBrowser 0
 
 #define lprintf(args...) fprintf(stderr, args)
 
@@ -315,6 +415,11 @@ enum CYWebPolicyDecision {
 
 + (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 {
@@ -330,7 +435,8 @@ enum CYWebPolicyDecision {
     if (challenge_ != nil)
         [challenge_ release];
 
-    //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+    if (request_ != nil)
+        [request_ release];
 
     if (custom_ != nil)
         [custom_ release];
@@ -342,11 +448,13 @@ enum CYWebPolicyDecision {
     if (closer_ != nil)
         [closer_ release];
 
-    if (sensitive_ != nil)
-        [sensitive_ release];
     if (title_ != nil)
         [title_ release];
 
+    if ([loading_ count] != 0)
+        [delegate_ releaseNetworkActivityIndicator];
+    [loading_ release];
+
     [reloaditem_ release];
     [loadingitem_ release];
 
@@ -368,6 +476,10 @@ enum CYWebPolicyDecision {
 }
 
 - (void) loadRequest:(NSURLRequest *)request {
+#if LogBrowser
+    NSLog(@"loadRequest:%@", request);
+#endif
+
     error_ = false;
 
     WebThreadLock();
@@ -398,15 +510,24 @@ enum CYWebPolicyDecision {
 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
     if (custom_ != nil)
         [custom_ autorelease];
-    custom_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
+    if (button == nil)
+        custom_ = nil;
+    else
+        custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
 
     if (style_ != nil)
         [style_ autorelease];
-    style_ = style == nil ? nil : [style retain];
+    if (style == nil)
+        style_ = nil;
+    else
+        style_ = [style retain];
 
     if (function_ != nil)
         [function_ autorelease];
-    function_ = function == nil ? nil : [function retain];
+    if (function == nil)
+        function_ = nil;
+    else
+        function_ = [function retain];
 
     [self applyRightButton];
 }
@@ -414,15 +535,24 @@ enum CYWebPolicyDecision {
 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
     if (custom_ != nil)
         [custom_ autorelease];
-    custom_ = button == nil ? nil : [button retain];
+    if (button == nil)
+        custom_ = nil;
+    else
+        custom_ = [button retain];
 
     if (style_ != nil)
         [style_ autorelease];
-    style_ = style == nil ? nil : [style retain];
+    if (style == nil)
+        style_ = nil;
+    else
+        style_ = [style retain];
 
     if (function_ != nil)
         [function_ autorelease];
-    function_ = function == nil ? nil : [function retain];
+    if (function == nil)
+        function_ = nil;
+    else
+        function_ = [function retain];
 
     [self applyRightButton];
 }
@@ -430,12 +560,23 @@ enum CYWebPolicyDecision {
 - (void) setPopupHook:(id)function {
     if (closer_ != nil)
         [closer_ autorelease];
-    closer_ = function == nil ? nil : [function retain];
+    if (function == nil)
+        closer_ = nil;
+    else
+        closer_ = [function retain];
+}
+
+- (void) _setViewportWidth {
+    [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
 }
 
 - (void) setViewportWidth:(float)width {
     width_ = width != 0 ? width : [[self class] defaultWidth];
-    [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
+    [self _setViewportWidth];
+}
+
+- (void) webViewUpdateViewSettings:(UIWebView *)view {
+    [self _setViewportWidth];
 }
 
 - (void) _openMailToURL:(NSURL *)url {
@@ -469,13 +610,15 @@ enum CYWebPolicyDecision {
     NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
 #endif
 
-    if (!error_ && [frame parentFrame] == nil) {
-        if (request_ != nil)
-            [request_ autorelease];
-        if (request == nil)
-            request_ = nil;
-        else
-            request_ = [request retain];
+    if ([frame parentFrame] == nil) {
+        if (!error_) {
+            if (request_ != nil)
+                [request_ autorelease];
+            if (request == nil)
+                request_ = nil;
+            else
+                request_ = [request retain];
+        }
     }
 }
 
@@ -601,7 +744,10 @@ enum CYWebPolicyDecision {
     if ([frame parentFrame] != nil)
         return;
 
+    if (title_ != nil)
+        [title_ autorelease];
     title_ = [title retain];
+
     [[self navigationItem] setTitle:title_];
 }
 
@@ -623,6 +769,10 @@ enum CYWebPolicyDecision {
 }
 
 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
+#if LogBrowser
+    NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
+#endif
+
     return request;
 }
 
@@ -733,26 +883,39 @@ enum CYWebPolicyDecision {
     [[self navigationItem] setTitle:UCLocalize("LOADING")];
 }
 
+- (void) layoutRightButton {
+    [[loadingitem_ view] addSubview:indicator_];
+    [[loadingitem_ view] bringSubviewToFront:indicator_];
+}
+
 - (void) applyRightButton {
     if ([self isLoading]) {
         [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
-        // XXX: why do we do this again here?
-        [[loadingitem_ view] addSubview:indicator_];
+        [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
+
+        [indicator_ startAnimating];
         [self applyLoadingTitle];
-    } else if (custom_ != nil) {
-        [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
     } else {
-        [[self navigationItem] setRightBarButtonItem:[self rightButton] animated:YES];
+        [indicator_ stopAnimating];
+
+        [[self navigationItem] setRightBarButtonItem:(
+            custom_ != nil ? [self customButton] : [self rightButton]
+        ) animated:YES];
     }
 }
 
 - (void) _didStartLoading {
     [self applyRightButton];
+
+    if ([loading_ count] != 1)
+        return;
+    [delegate_ retainNetworkActivityIndicator];
 }
 
 - (void) _didFinishLoading {
     if ([loading_ count] != 0)
         return;
+    [delegate_ releaseNetworkActivityIndicator];
 
     [self applyRightButton];
 
@@ -789,6 +952,8 @@ enum CYWebPolicyDecision {
         [document setTileSize:CGSizeMake(320, 500)];
 
         [document setBackgroundColor:[UIColor clearColor]];
+
+        // XXX: this is terribly (too?) expensive
         [document setDrawsBackground:NO];
 
         WebView *webview([document webView]);
@@ -807,7 +972,7 @@ enum CYWebPolicyDecision {
             scroller_ = [webview_ _scrollView];
 
             [scroller_ setDirectionalLockEnabled:YES];
-            [scroller_ setDecelerationRate:UIScrollViewDecelerationRateNormal];
+            [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
             [scroller_ setDelaysContentTouches:NO];
 
             [scroller_ setCanCancelContentTouches:YES];
@@ -816,7 +981,8 @@ enum CYWebPolicyDecision {
             scroller_ = (UIScrollView *) scroller;
 
             [scroller setDirectionalScrolling:YES];
-            [scroller setScrollDecelerationFactor:UIScrollViewDecelerationRateNormal]; /* 0.989324 */
+            // XXX: we might be better off /not/ setting this on older systems
+            [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
             [scroller setScrollHysteresis:0]; /* 8 */
 
             [scroller setThumbDetectionEnabled:NO];
@@ -852,11 +1018,8 @@ enum CYWebPolicyDecision {
             action:@selector(reloadButtonClicked)
         ];
 
-        CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
-        indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
-        [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
-        [indicator_ startAnimation];
-        [[loadingitem_ view] addSubview:indicator_];
+        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];