]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Filter out the more annoying console.log() messages.
[cydia.git] / UICaboodle / BrowserView.mm
index b4ca1a12fe6803daade5019c23453d7cfc1b3cf4..7a356428275b0a8e434b8e84ee895e085cb72a98 100644 (file)
@@ -4,6 +4,7 @@
 #include "UCPlatform.h"
 
 #include <UICaboodle/BrowserView.h>
+#include "UICaboodle/PerlCompatibleRegEx.hpp"
 #include <UICaboodle/UCLocalize.h>
 
 //#include <QuartzCore/CALayer.h>
@@ -69,9 +70,7 @@ float CYScrollViewDecelerationRateNormal;
 @end
 
 /* Indirect Delegate {{{ */
-@interface IndirectDelegate : NSObject <
-    HookProtocol
-> {
+@interface IndirectDelegate : NSObject {
     _transient volatile id delegate_;
 }
 
@@ -90,11 +89,6 @@ float CYScrollViewDecelerationRateNormal;
     return self;
 }
 
-- (void) didDismissModalViewController {
-    if (delegate_ != nil)
-        return [delegate_ didDismissModalViewController];
-}
-
 - (IMP) methodForSelector:(SEL)sel {
     if (IMP method = [super methodForSelector:sel])
         return method;
@@ -480,12 +474,6 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if (challenge_ != nil)
         [challenge_ release];
 
-    if (request_ != nil)
-        [request_ release];
-
-    if (closer_ != nil)
-        [closer_ release];
-
     if (title_ != nil)
         [title_ release];
 
@@ -501,22 +489,25 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [super dealloc];
 }
 
-- (void) setURL:(NSURL *)url {
-    _assert(request_ == nil);
+- (NSURL *) URLWithURL:(NSURL *)url {
+    return url;
+}
 
-    request_ = [[NSURLRequest
-        requestWithURL:url
-        cachePolicy:NSURLRequestUseProtocolCachePolicy
+- (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
+    return [NSURLRequest
+        requestWithURL:[self URLWithURL:url]
+        cachePolicy:policy
         timeoutInterval:DefaultTimeout_
-    ] retain];
+    ];
+}
+
+- (void) setURL:(NSURL *)url {
+    _assert(request_ == nil);
+    request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
 }
 
 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
-    [self loadRequest:[NSURLRequest
-        requestWithURL:url
-        cachePolicy:policy
-        timeoutInterval:DefaultTimeout_
-    ]];
+    [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
 }
 
 - (void) loadURL:(NSURL *)url {
@@ -541,8 +532,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     NSMutableURLRequest *request([request_ mutableCopy]);
     [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
 
-    [request_ autorelease];
-    request_ = [request retain];
+    request_ = request;
 
     if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
         [self loadRequest:request_];
@@ -592,15 +582,6 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
-- (void) setPopupHook:(id)function {
-    if (closer_ != nil)
-        [closer_ autorelease];
-    if (function == nil)
-        closer_ = nil;
-    else
-        closer_ = [function retain];
-}
-
 - (void) scrollToBottomAnimated:(NSNumber *)animated {
     CGSize size([scroller_ contentSize]);
     CGPoint offset([scroller_ contentOffset]);
@@ -641,6 +622,24 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     return true;
 }
 
+- (bool) allowsNavigationAction {
+    return allowsNavigationAction_;
+}
+
+- (void) setAllowsNavigationAction:(bool)value {
+    allowsNavigationAction_ = value;
+}
+
+- (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
+    [self setAllowsNavigationAction:[value boolValue]];
+}
+
+- (void) popViewControllerWithNumber:(NSNumber *)value {
+    UINavigationController *navigation([self navigationController]);
+    if ([navigation topViewController] == self)
+        [navigation popViewControllerAnimated:[value boolValue]];
+}
+
 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
     [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
     [self _didFinishLoading];
@@ -658,9 +657,50 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     }
 }
 
+- (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
+    NSURL *url([request URL]);
+
+    // XXX: filter to internal usage?
+    CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
+
+    if (page == nil) {
+        BrowserController *browser([[[class_ alloc] init] autorelease]);
+        [browser loadRequest:request];
+        page = browser;
+    }
+
+    [page setDelegate:delegate_];
+
+    if (!pop) {
+        [[self navigationItem] setTitle:title_];
+
+        [[self navigationController] pushViewController:page animated:YES];
+    } else {
+        UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
+
+        [navigation setDelegate:delegate_];
+
+        [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
+            initWithTitle:UCLocalize("CLOSE")
+            style:UIBarButtonItemStylePlain
+            target:page
+            action:@selector(close)
+        ] autorelease]];
+
+        [[self navigationController] presentModalViewController:navigation animated:YES];
+
+        [delegate_ unloadData];
+    }
+}
+
 // CYWebViewDelegate {{{
 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
 #if LogMessages
+    static Pcre irritating("^(?:The page at .* displayed insecure content from .*\\.|Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\.)\\n$");
+    if (NSString *data = [message objectForKey:@"message"])
+        if (irritating(data))
+            return;
+
     NSLog(@"addMessageToConsole:%@", message);
 #endif
 }
@@ -672,12 +712,15 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
     if ([frame parentFrame] == nil) {
         if (!error_) {
-            if (request_ != nil)
-                [request_ autorelease];
-            if (request == nil)
-                request_ = nil;
-            else
-                request_ = [request retain];
+            NSURL *url(request == nil ? nil : [request URL]);
+
+            if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
+                request_ = request;
+            else {
+                if (url != nil)
+                    [self pushRequest:request asPop:NO];
+                [listener ignore];
+            }
         }
     }
 }
@@ -693,40 +736,12 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
     if ([frame isEqualToString:@"_open"])
         [delegate_ openURL:url];
-
-    NSString *scheme([[url scheme] lowercaseString]);
-    if ([scheme isEqualToString:@"mailto"])
-        [self _openMailToURL:url];
-
-    // XXX: filter to internal usage?
-    CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
-
-    if (page == nil) {
-        BrowserController *browser([[[class_ alloc] init] autorelease]);
-        [browser loadRequest:request];
-        page = browser;
-    }
-
-    [page setDelegate:delegate_];
-
-    if (![frame isEqualToString:@"_popup"]) {
-        [[self navigationItem] setTitle:title_];
-
-        [[self navigationController] pushViewController:page animated:YES];
-    } else {
-        UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
-
-        [navigation setHook:indirect_];
-        [navigation setDelegate:delegate_];
-
-        [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
-            initWithTitle:UCLocalize("CLOSE")
-            style:UIBarButtonItemStylePlain
-            target:page
-            action:@selector(close)
-        ] autorelease]];
-
-        [[self navigationController] presentModalViewController:navigation animated:YES];
+    else {
+        NSString *scheme([[url scheme] lowercaseString]);
+        if ([scheme isEqualToString:@"mailto"])
+            [self _openMailToURL:url];
+        else
+            [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
     }
 
     [listener ignore];
@@ -760,7 +775,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                 for (DOMHTMLBodyElement *body in (id) bodies) {
                     DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
 
-                    bool colored(false);
+                    UIColor *uic(nil);
 
                     if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
                         if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
@@ -771,10 +786,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                             float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
                             float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
 
-                            UIColor *uic(nil);
-
                             if (red == 0xc7 && green == 0xce && blue == 0xd5)
-                                uic = [UIColor groupTableViewBackgroundColor];
+                                uic = [UIColor pinStripeColor];
                             else if (alpha != 0)
                                 uic = [UIColor
                                     colorWithRed:(red / 255)
@@ -782,16 +795,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
                                     blue:(blue / 255)
                                     alpha:alpha
                                 ];
-
-                            if (uic != nil) {
-                                colored = true;
-                                [scroller_ setBackgroundColor:uic];
-                            }
                         }
                     }
 
-                    if (!colored)
-                        [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
+                    [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
                     break;
                 }
     }
@@ -818,7 +825,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         custom_ = nil;
         style_ = nil;
         function_ = nil;
-        CYRelease(closer_);
+
+        [self setHidesNavigationBar:NO];
 
         // XXX: do we still need to do this?
         [[self navigationItem] setTitle:nil];
@@ -991,12 +999,16 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
 - (id) initWithWidth:(float)width ofClass:(Class)_class {
     if ((self = [super init]) != nil) {
+        allowsNavigationAction_ = true;
+
         class_ = _class;
         loading_ = [[NSMutableSet alloc] initWithCapacity:5];
 
         indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
 
-        webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
+        CGRect bounds([[self view] bounds]);
+
+        webview_ = [[[CYWebView alloc] initWithFrame:bounds] autorelease];
         [webview_ setDelegate:self];
         [self setView:webview_];
 
@@ -1063,7 +1075,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         }
 
         [scroller_ setFixedBackgroundPattern:YES];
-        [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
+        [scroller_ setBackgroundColor:[UIColor clearColor]];
         [scroller_ setClipsSubviews:YES];
 
         [scroller_ setBounces:YES];
@@ -1089,6 +1101,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
         [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
 
+        UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
+        [webview_ insertSubview:table atIndex:0];
+
+        [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
     } return self;
@@ -1108,11 +1124,6 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     } return self;
 }
 
-- (void) didDismissModalViewController {
-    if (closer_ != nil)
-        [self callFunction:closer_];
-}
-
 - (void) callFunction:(WebScriptObject *)function {
     WebThreadLocked lock;
 
@@ -1180,6 +1191,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     [[[self navigationController] navigationBar] setBarStyle:style];
 }
 
+- (void) setNavigationBarTintColor:(UIColor *)color {
+    [[[self navigationController] navigationBar] setTintColor:color];
+}
+
 - (void) setHidesBackButton:(bool)value {
     [[self navigationItem] setHidesBackButton:value];
 }
@@ -1211,24 +1226,54 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     }
 }
 
+- (bool) hidesNavigationBar {
+    return hidesNavigationBar_;
+}
+
+- (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
+    if (visible_)
+        [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
+}
+
+- (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 {
-    [self dispatchEvent:@"CydiaViewDidAppear"];
     [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 {
-    [self dispatchEvent:@"CydiaViewDidDisappear"];
     [super viewDidDisappear:animated];
+    [self dispatchEvent:@"CydiaViewDidDisappear"];
 }
 
 @end