]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Merge commit 'f64f9f6255e43c48ba2b8e5cea6a5b23e5d14e26' into tab-backup
[cydia.git] / UICaboodle / BrowserView.mm
index 2c45468d3d953cca84e056834407021529b8b4ea..2abc02ecc607b3c968bbf2c656260fb490b10b70 100644 (file)
@@ -318,9 +318,8 @@ static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebVie
 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:)])
-        return [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
-    else
-        return request;
+        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 {
@@ -336,8 +335,10 @@ static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$
 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     id<CYWebViewDelegate> delegate([self delegate]);
     if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
-        // XXX: check delegate
-        if ([delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame])
+        if (
+            ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
+            [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
+        )
             [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
 }
 // }}}
@@ -345,8 +346,10 @@ static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$
 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
     id<CYWebViewDelegate> delegate([self delegate]);
     if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
-        // XXX: check delegate
-        if ([delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame])
+        if (
+            ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
+            [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
+        )
             return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
     return NO;
 }
@@ -355,8 +358,10 @@ static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$
 - (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:)])
-        // XXX: check delegate
-        if ([delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame])
+        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;
 }
@@ -377,6 +382,14 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 }
 // }}}
 
+- (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");
@@ -390,7 +403,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 @end
 
 #define ShowInternals 0
-#define LogBrowser 1
+#define LogBrowser 0
 
 #define lprintf(args...) fprintf(stderr, args)
 
@@ -422,7 +435,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if (challenge_ != nil)
         [challenge_ release];
 
-    //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+    if (request_ != nil)
+        [request_ release];
 
     if (custom_ != nil)
         [custom_ release];
@@ -434,11 +448,13 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     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];
 
@@ -494,15 +510,24 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 - (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];
 }
@@ -510,15 +535,24 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 - (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];
 }
@@ -526,12 +560,23 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 - (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 {
@@ -565,13 +610,15 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     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];
+        }
     }
 }
 
@@ -591,7 +638,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if ([scheme isEqualToString:@"mailto"])
         [self _openMailToURL:url];
 
-    CYViewController *page([delegate_ pageForURL:url hasTag:NULL]);
+    CYViewController *page([delegate_ pageForURL:url]);
 
     if (page == nil) {
         BrowserController *browser([[[class_ alloc] init] autorelease]);
@@ -606,13 +653,11 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
         [[self navigationController] pushViewController:page animated:YES];
     } else {
-        UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
+        UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
 
         [navigation setHook:indirect_];
         [navigation setDelegate:delegate_];
 
-        [navigation setViewControllers:[NSArray arrayWithObject:page]];
-
         [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
             initWithTitle:UCLocalize("CLOSE")
             style:UIBarButtonItemStylePlain
@@ -697,7 +742,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if ([frame parentFrame] != nil)
         return;
 
+    if (title_ != nil)
+        [title_ autorelease];
     title_ = [title retain];
+
     [[self navigationItem] setTitle:title_];
 }
 
@@ -787,19 +835,13 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
         [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;
-
-            _nodefault
+        if (button == [alert cancelButtonIndex]) {
+        } else if (button == [alert firstOtherButtonIndex]) {
+            if (request_ != nil) {
+                WebThreadLock();
+                [webview_ loadRequest:request_];
+                WebThreadUnlock();
+            }
         }
 
         [alert dismissWithClickedButtonIndex:-1 animated:YES];
@@ -833,21 +875,43 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [[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 {
+    // Overridden in subclasses.
+}
+
 - (void) _didStartLoading {
     [self applyRightButton];
+
+    if ([loading_ count] != 1)
+        return;
+
+    [delegate_ retainNetworkActivityIndicator];
+    [self didStartLoading];
+}
+
+- (void) didFinishLoading {
+    // Overridden in subclasses.
 }
 
 - (void) _didFinishLoading {
@@ -855,10 +919,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         return;
 
     [self applyRightButton];
+    [[self navigationItem] setTitle:title_];
 
-    // XXX: wtf?
-    if (![self isLoading])
-        [[self navigationItem] setTitle:title_];
+    [delegate_ releaseNetworkActivityIndicator];
+    [self didFinishLoading];
 }
 
 - (bool) isLoading {
@@ -889,6 +953,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         [document setTileSize:CGSizeMake(320, 500)];
 
         [document setBackgroundColor:[UIColor clearColor]];
+
+        // XXX: this is terribly (too?) expensive
         [document setDrawsBackground:NO];
 
         WebView *webview([document webView]);
@@ -953,11 +1019,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
             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];