#include "substrate.h"
#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) {
@interface WebView (Apple)
- (void) _setLayoutInterval:(float)interval;
+- (void) _setAllowsMessaging:(BOOL)allows;
@end
@interface WebPreferences (Apple)
- (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@:"];
}
@implementation CYWebView : UIWebView
+#if ShowInternals
+#include "UICaboodle/UCInternal.h"
+#endif
+
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]) != nil) {
} return self;
return created;
}*/
+// 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]);
- (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];
}
// }}}
- (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;
}
- (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;
}
+ (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");
@end
-#define ShowInternals 0
-#define LogBrowser 0
-
-#define lprintf(args...) fprintf(stderr, args)
-
@implementation BrowserController
#if ShowInternals
if (title_ != nil)
[title_ release];
+ if ([loading_ count] != 0)
+ [delegate_ releaseNetworkActivityIndicator];
[loading_ release];
[reloaditem_ release];
[super dealloc];
}
+- (void) setURL:(NSURL *)url {
+ _assert(request_ == nil);
+
+ request_ = [[NSURLRequest
+ requestWithURL:url
+ cachePolicy:NSURLRequestUseProtocolCachePolicy
+ timeoutInterval:DefaultTimeout_
+ ] retain];
+}
+
- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
[self loadRequest:[NSURLRequest
requestWithURL:url
cachePolicy:policy
- timeoutInterval:120.0
+ timeoutInterval:DefaultTimeout_
]];
}
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_ autorelease];
+ request_ = [request retain];
+
if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
[self loadRequest:request_];
else {
message:nil
delegate:self
cancelButtonTitle:UCLocalize("CANCEL")
- otherButtonTitles:UCLocalize("SUBMIT"), nil
+ otherButtonTitles:
+ UCLocalize("SUBMIT"),
+ nil
] autorelease];
[alert setContext:@"submit"];
}
}
+- (void) reloadURL {
+ [self reloadURLWithCache:YES];
+}
+
+- (void) reloadData {
+ [super reloadData];
+ [self reloadURLWithCache:YES];
+}
+
- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
if (custom_ != nil)
[custom_ autorelease];
else
function_ = [function retain];
- [self applyRightButton];
+ [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
}
- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
else
function_ = [function retain];
- [self applyRightButton];
+ [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
}
- (void) setPopupHook:(id)function {
[self _setViewportWidth];
}
+- (void) _setViewportWidthOnMainThread:(NSNumber *)width {
+ [self setViewportWidth:[width floatValue]];
+}
+
+- (void) setViewportWidthOnMainThread:(float)width {
+ [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
+}
+
- (void) webViewUpdateViewSettings:(UIWebView *)view {
[self _setViewportWidth];
}
}
// CYWebViewDelegate {{{
+- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
+#if LogMessages
+ NSLog(@"addMessageToConsole:%@", message);
+#endif
+}
+
- (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);
if ([scheme isEqualToString:@"mailto"])
[self _openMailToURL:url];
- CYViewController *page([delegate_ pageForURL:url hasTag:NULL]);
+ // XXX: filter to internal usage?
+ CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
if (page == nil) {
BrowserController *browser([[[class_ alloc] init] autorelease]);
[[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
[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) {
+ WebThreadLocked lock;
+ [webview_ loadRequest:request_];
+ }
}
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[[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? (if we don't, just remove indicator_)
- [[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 {
return;
[self applyRightButton];
+ [[self navigationItem] setTitle:title_];
- // XXX: wtf?
- if (![self isLoading])
- [[self navigationItem] setTitle:title_];
+ [delegate_ releaseNetworkActivityIndicator];
+ [self didFinishLoading];
}
- (bool) isLoading {
[preferences setCacheModel:WebCacheModelDocumentBrowser];
[preferences setOfflineWebApplicationCacheEnabled:YES];
+#if LogMessages
+ if ([document respondsToSelector:@selector(setAllowsMessaging:)])
+ [document setAllowsMessaging:YES];
+ if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
+ [webview _setAllowsMessaging:YES];
+#endif
+
if ([webview_ respondsToSelector:@selector(_scrollView)]) {
scroller_ = [webview_ _scrollView];
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];
return [self initWithWidth:0];
}
+- (id) initWithURL:(NSURL *)url {
+ if ((self = [self init]) != nil) {
+ [self setURL:url];
+ } return self;
+}
+
- (void) didDismissModalViewController {
if (closer_ != nil)
[self callFunction:closer_];
}
- (void) callFunction:(WebScriptObject *)function {
- WebThreadLock();
+ WebThreadLocked lock;
WebView *webview([[webview_ _documentView] webView]);
WebFrame *frame([webview mainFrame]);
settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
[preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
-
- WebThreadUnlock();
}
- (void) reloadButtonClicked {
- [self reloadURL];
+ [self reloadURLWithCache:YES];
}
- (void) _customButtonClicked {
return 980;
}
+- (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) viewWillAppear:(BOOL)animated {
+ [self dispatchEvent:@"CydiaViewWillAppear"];
+ [super viewWillAppear:animated];
+}
+
+- (void) viewDidAppear:(BOOL)animated {
+ [self dispatchEvent:@"CydiaViewDidAppear"];
+ [super viewDidAppear:animated];
+}
+
+- (void) viewWillDisappear:(BOOL)animated {
+ [self dispatchEvent:@"CydiaViewWillDisappear"];
+ [super viewWillDisappear:animated];
+}
+
+- (void) viewDidDisappear:(BOOL)animated {
+ [self dispatchEvent:@"CydiaViewDidDisappear"];
+ [super viewDidDisappear:animated];
+}
+
@end