]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Move setToken: to main thread for Metadata_.
[cydia.git] / UICaboodle / BrowserView.mm
index 8bc0eff42f74246c76b42a54bb12228b5d696837..d5496403674f386fc327e2ed969bada9ce108388 100644 (file)
@@ -35,6 +35,18 @@ extern NSString * const kCAFilterNearest;
 
 #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) {
@@ -525,15 +537,20 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 
     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 {
@@ -542,7 +559,9 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
             message:nil
             delegate:self
             cancelButtonTitle:UCLocalize("CANCEL")
-            otherButtonTitles:UCLocalize("SUBMIT"), nil
+            otherButtonTitles:
+                UCLocalize("SUBMIT"),
+            nil
         ] autorelease];
 
         [alert setContext:@"submit"];
@@ -550,9 +569,13 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     }
 }
 
+- (void) reloadURL {
+    [self reloadURLWithCache:YES];
+}
+
 - (void) reloadData {
     [super reloadData];
-    [self reloadURL];
+    [self reloadURLWithCache:YES];
 }
 
 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
@@ -577,7 +600,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     else
         function_ = [function retain];
 
-    [self applyRightButton];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
@@ -602,7 +625,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     else
         function_ = [function retain];
 
-    [self applyRightButton];
+    [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
 }
 
 - (void) setPopupHook:(id)function {
@@ -692,7 +715,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     if ([scheme isEqualToString:@"mailto"])
         [self _openMailToURL:url];
 
-    CYViewController *page([delegate_ pageForURL:url]);
+    // XXX: filter to internal usage?
+    CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
 
     if (page == nil) {
         BrowserController *browser([[[class_ alloc] init] autorelease]);
@@ -892,9 +916,8 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         if (button == [alert cancelButtonIndex]) {
         } else if (button == [alert firstOtherButtonIndex]) {
             if (request_ != nil) {
-                WebThreadLock();
+                WebThreadLocked lock;
                 [webview_ loadRequest:request_];
-                WebThreadUnlock();
             }
         }
 
@@ -1108,7 +1131,7 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
 }
 
 - (void) callFunction:(WebScriptObject *)function {
-    WebThreadLock();
+    WebThreadLocked lock;
 
     WebView *webview([[webview_ _documentView] webView]);
     WebFrame *frame([webview mainFrame]);
@@ -1141,12 +1164,10 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
         settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
 
     [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
-
-    WebThreadUnlock();
 }
 
 - (void) reloadButtonClicked {
-    [self reloadURL];
+    [self reloadURLWithCache:YES];
 }
 
 - (void) _customButtonClicked {
@@ -1166,4 +1187,47 @@ static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *se
     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