cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject ios commit: [CB-3069] Fix InAppBrowser load events (for non-redirecting pages)
Date Thu, 18 Apr 2013 18:08:37 GMT
Updated Branches:
  refs/heads/master c6e711473 -> 037620604


[CB-3069] Fix InAppBrowser load events (for non-redirecting pages)

Also adds in some extra logging that is disable by default.


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/03762060
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/03762060
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/03762060

Branch: refs/heads/master
Commit: 037620604112f288292d8185e5285413ef508bef
Parents: c6e7114
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Thu Apr 18 14:07:34 2013 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu Apr 18 14:07:34 2013 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVInAppBrowser.h    |   15 +------
 CordovaLib/Classes/CDVInAppBrowser.m    |   54 ++++++++++++-------------
 CordovaLib/Classes/CDVWebViewDelegate.m |   45 +++++++++++++++++-----
 3 files changed, 64 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/03762060/CordovaLib/Classes/CDVInAppBrowser.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVInAppBrowser.h b/CordovaLib/Classes/CDVInAppBrowser.h
index cb16569..a79c5f9 100644
--- a/CordovaLib/Classes/CDVInAppBrowser.h
+++ b/CordovaLib/Classes/CDVInAppBrowser.h
@@ -24,16 +24,7 @@
 
 @class CDVInAppBrowserViewController;
 
-@protocol CDVInAppBrowserNavigationDelegate <NSObject>
-
-- (void)browserLoadStart:(NSURL*)url;
-- (void)browserLoadStop:(NSURL*)url;
-- (void)browserLoadError:(NSError*)error forUrl:(NSURL*)url;
-- (void)browserExit;
-
-@end
-
-@interface CDVInAppBrowser : CDVPlugin <CDVInAppBrowserNavigationDelegate>
+@interface CDVInAppBrowser : CDVPlugin
 
 @property (nonatomic, retain) CDVInAppBrowserViewController* inAppBrowserViewController;
 @property (nonatomic, copy) NSString* callbackId;
@@ -46,7 +37,6 @@
 
 @interface CDVInAppBrowserViewController : UIViewController <UIWebViewDelegate>{
     @private
-    NSURL* _requestedURL;
     NSString* _userAgent;
     NSString* _prevUserAgent;
     NSInteger _userAgentLockToken;
@@ -62,7 +52,8 @@
 @property (nonatomic, strong) IBOutlet UIToolbar* toolbar;
 
 @property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate;
-@property (nonatomic, weak) id <CDVInAppBrowserNavigationDelegate> navigationDelegate;
+@property (nonatomic, weak) CDVInAppBrowser* navigationDelegate;
+@property (nonatomic) NSURL* requestedURL;
 
 - (void)close;
 - (void)navigateTo:(NSURL*)url;

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/03762060/CordovaLib/Classes/CDVInAppBrowser.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVInAppBrowser.m b/CordovaLib/Classes/CDVInAppBrowser.m
index 1844a7f..e414d4b 100644
--- a/CordovaLib/Classes/CDVInAppBrowser.m
+++ b/CordovaLib/Classes/CDVInAppBrowser.m
@@ -229,8 +229,6 @@
     [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
 }
 
-#pragma mark CDVInAppBrowserNavigationDelegate
-
 /**
  * The iframe bridge provided for the InAppBrowser is capable of executing any oustanding
callback belonging
  * to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered,
and that no
@@ -246,14 +244,15 @@
  * value to pass to the callback. [NSURL path] should take care of the URL-unescaping, and
a JSON_EXCEPTION
  * is returned if the JSON is invalid.
  */
-- (void)browserLoadStart:(NSURL*)url
+- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType
 {
-    CDVPluginResult* pluginResult;
+    NSURL* url = request.URL;
 
     // See if the url uses the 'gap-iab' protocol. If so, the host should be the id of a
callback to execute,
     // and the path, if present, should be a JSON-encoded value to pass to the callback.
     if ([[url scheme] isEqualToString:@"gap-iab"]) {
         NSString* scriptCallbackId = [url host];
+        CDVPluginResult* pluginResult = nil;
 
         if ([scriptCallbackId hasPrefix:@"InAppBrowser"]) {
             NSString* scriptResult = [url path];
@@ -272,36 +271,45 @@
                 pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
             }
             [self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
-            return;
+            return NO;
         }
     }
+    return YES;
+}
+
+- (void)webViewDidStartLoad:(UIWebView*)theWebView
+{
     if (self.callbackId != nil) {
-        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                     messageAsDictionary:@{@"type":@"loadstart", @"url":[url
absoluteString]}];
+        NSString* url = [[self.inAppBrowserViewController requestedURL] absoluteString];
+        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
+                                                      messageAsDictionary:@{@"type":@"loadstart",
@"url":url}];
         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
 
         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
     }
 }
 
-- (void)browserLoadStop:(NSURL*)url
+- (void)webViewDidFinishLoad:(UIWebView*)theWebView
 {
     // Create an iframe bridge in the new document to communicate with the CDVInAppBrowserViewController
     [self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:@"(function(d){var
e=d.createElement('iframe');e.id='cdv-iab-bridge';e.style.display='none';d.body.appendChild(e);})(document)"];
     if (self.callbackId != nil) {
+        // TODO: It would be more useful to return the URL the page is actually on (e.g.
if it's been redirected).
+        NSString* url = [[self.inAppBrowserViewController requestedURL] absoluteString];
         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
-                                                      messageAsDictionary:@{@"type":@"loadstop",
@"url":[url absoluteString]}];
+                                                      messageAsDictionary:@{@"type":@"loadstop",
@"url":url}];
         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
 
         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
     }
 }
 
-- (void)browserLoadError:(NSError*)error forUrl:(NSURL*)url
+- (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
 {
     if (self.callbackId != nil) {
+        NSString* url = [[self.inAppBrowserViewController requestedURL] absoluteString];
         CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
-                                                      messageAsDictionary:@{@"type":@"loaderror",
@"url":[url absoluteString], @"code": [NSNumber numberWithInt:error.code], @"message": error.localizedDescription}];
+                                                      messageAsDictionary:@{@"type":@"loaderror",
@"url":url, @"code": [NSNumber numberWithInt:error.code], @"message": error.localizedDescription}];
         [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
 
         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
@@ -328,6 +336,8 @@
 
 @implementation CDVInAppBrowserViewController
 
+@synthesize requestedURL = _requestedURL;
+
 - (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
 {
     self = [super init];
@@ -545,21 +555,13 @@
     self.forwardButton.enabled = theWebView.canGoForward;
 
     [self.spinner startAnimating];
+
+    return [self.navigationDelegate webViewDidStartLoad:theWebView];
 }
 
 - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType
 {
-    if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserLoadStart:)])
{
-        NSURL* url = request.URL;
-        if (url == nil) {
-            url = _requestedURL;
-        }
-        [self.navigationDelegate browserLoadStart:url];
-        // Return NO if the URL uses the 'gap-iab' protocol, as this is used for the
-        // IAB bridge, rather than any actual web content.
-        return ![[url scheme] isEqualToString:@"gap-iab"];
-    }
-    return YES;
+    return [self.navigationDelegate webView:theWebView shouldStartLoadWithRequest:request
navigationType:navigationType];
 }
 
 - (void)webViewDidFinishLoad:(UIWebView*)theWebView
@@ -588,9 +590,7 @@
         [CDVUserAgentUtil setUserAgent:_prevUserAgent lockToken:_userAgentLockToken];
     }
 
-    if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserLoadStop:)])
{
-        [self.navigationDelegate browserLoadStop:_requestedURL];
-    }
+    [self.navigationDelegate webViewDidFinishLoad:theWebView];
 }
 
 - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
@@ -604,9 +604,7 @@
 
     self.addressLabel.text = @"Load Error";
 
-    if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserLoadError:forUrl:)])
{
-        [self.navigationDelegate browserLoadError:error forUrl:_requestedURL];
-    }
+    [self.navigationDelegate webView:theWebView didFailLoadWithError:error];
 }
 
 #pragma mark CDVScreenOrientationDelegate

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/03762060/CordovaLib/Classes/CDVWebViewDelegate.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVWebViewDelegate.m b/CordovaLib/Classes/CDVWebViewDelegate.m
index a72bfb9..2a7cf0d 100644
--- a/CordovaLib/Classes/CDVWebViewDelegate.m
+++ b/CordovaLib/Classes/CDVWebViewDelegate.m
@@ -20,6 +20,9 @@
 #import "CDVWebViewDelegate.h"
 #import "CDVAvailability.h"
 
+// #define VerboseLog NSLog
+#define VerboseLog(...) do {} while (0)
+
 typedef enum {
     STATE_NORMAL,
     STATE_SHOULD_LOAD_MISSING,
@@ -66,12 +69,16 @@ typedef enum {
         return;
     }
     if (![self isJsLoadTokenSet:webView]) {
+        VerboseLog(@"Polled for page load start. result = YES!");
         _state = STATE_WAITING_FOR_FINISH;
         [self setLoadToken:webView];
         if ([_delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
             [_delegate webViewDidStartLoad:webView];
         }
         [self pollForPageLoadFinish:webView];
+    } else {
+        VerboseLog(@"Polled for page load start. result = NO");
+        [self performSelector:@selector(pollForPageLoadStart) withObject:webView afterDelay:50];
     }
 }
 
@@ -81,12 +88,14 @@ typedef enum {
         return;
     }
     if ([self isPageLoaded:webView]) {
+        VerboseLog(@"Polled for page load finish. result = YES!");
         _state = STATE_SHOULD_LOAD_MISSING;
         if ([_delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
             [_delegate webViewDidFinishLoad:webView];
         }
     } else {
-        [self performSelector:@selector(pollForPageLoaded) withObject:webView afterDelay:50];
+        VerboseLog(@"Polled for page load finish. result = NO");
+        [self performSelector:@selector(pollForPageLoadFinish) withObject:webView afterDelay:50];
     }
 }
 
@@ -98,23 +107,26 @@ typedef enum {
         shouldLoad = [_delegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
     }
 
+    VerboseLog(@"webView shouldLoad=%d state=%d loadCount=%d URL=%@", shouldLoad, _state,
_loadCount, request.URL);
+
     if (shouldLoad) {
         BOOL isTopLevelNavigation = [request.URL isEqual:[request mainDocumentURL]];
         if (isTopLevelNavigation) {
             _loadCount = 0;
             _state = STATE_NORMAL;
         }
+        VerboseLog(@"webView shouldLoad isTopLevelNavigation=%d state=%d loadCount=%d", isTopLevelNavigation,
_state, _loadCount);
     }
     return shouldLoad;
 }
 
 - (void)webViewDidStartLoad:(UIWebView*)webView
 {
+    VerboseLog(@"webView didStartLoad (before). state=%d loadCount=%d", _state, _loadCount);
+    BOOL fireCallback = NO;
     if (_state == STATE_NORMAL) {
         if (_loadCount == 0) {
-            if ([_delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
-                [_delegate webViewDidStartLoad:webView];
-            }
+            fireCallback = [_delegate respondsToSelector:@selector(webViewDidStartLoad:)];
             _loadCount += 1;
         } else if (_loadCount > 0) {
             _loadCount += 1;
@@ -132,15 +144,19 @@ typedef enum {
         [self pollForPageLoadStart:webView];
         [self pollForPageLoadFinish:webView];
     }
+    VerboseLog(@"webView didStartLoad (after). state=%d loadCount=%d", _state, _loadCount);
+    if (fireCallback) {
+        [_delegate webViewDidStartLoad:webView];
+    }
 }
 
 - (void)webViewDidFinishLoad:(UIWebView*)webView
 {
+    VerboseLog(@"webView didFinishLoad (before). state=%d loadCount=%d", _state, _loadCount);
+    BOOL fireCallback = NO;
     if (_state == STATE_NORMAL) {
         if (_loadCount == 1) {
-            if ([_delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
-                [_delegate webViewDidFinishLoad:webView];
-            }
+            fireCallback = [_delegate respondsToSelector:@selector(webViewDidFinishLoad:)];
             _loadCount = -1;
         } else if (_loadCount > 1) {
             _loadCount -= 1;
@@ -149,15 +165,20 @@ typedef enum {
         [self pollForPageLoadStart:webView];
         [self pollForPageLoadFinish:webView];
     }
+    VerboseLog(@"webView didFinishLoad (after). state=%d loadCount=%d", _state, _loadCount);
+    if (fireCallback) {
+        [_delegate webViewDidFinishLoad:webView];
+    }
 }
 
 - (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
 {
+    VerboseLog(@"webView didFailLoad (before). state=%d loadCount=%d", _state, _loadCount);
+    BOOL fireCallback = NO;
+
     if (_state == STATE_NORMAL) {
         if (_loadCount == 1) {
-            if ([_delegate respondsToSelector:@selector(webView:didFailLoadWithError:)])
{
-                [_delegate webView:webView didFailLoadWithError:error];
-            }
+            fireCallback = [_delegate respondsToSelector:@selector(webView:didFailLoadWithError:)];
             _loadCount = -1;
         } else if (_loadCount > 1) {
             _loadCount -= 1;
@@ -166,6 +187,10 @@ typedef enum {
         [self pollForPageLoadStart:webView];
         [self pollForPageLoadFinish:webView];
     }
+    VerboseLog(@"webView didFailLoad (after). state=%d loadCount=%d", _state, _loadCount);
+    if (fireCallback) {
+        [_delegate webView:webView didFailLoadWithError:error];
+    }
 }
 
 @end


Mime
View raw message