You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/01/18 20:48:36 UTC

[1/3] ios commit: Fix for CB-2225

Fix for CB-2225

This uses a work-around with caveats :( to prevent the User-Agent from
being changed when the InAppBrowser loads a PDF.

This also adds a User-Agent getter to CDVCommandDelegate.


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

Branch: refs/heads/master
Commit: 07a223ee65add7e5e6ff108a77fbc684dad79914
Parents: 92fa3e4
Author: Andrew Grieve <ag...@chromium.org>
Authored: Fri Jan 18 14:46:23 2013 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Fri Jan 18 14:48:07 2013 -0500

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCommandDelegate.h     |    2 +
 CordovaLib/Classes/CDVCommandDelegateImpl.m |    5 +++
 CordovaLib/Classes/CDVInAppBrowser.h        |    6 ++-
 CordovaLib/Classes/CDVInAppBrowser.m        |   36 ++++++++++++++++------
 4 files changed, 37 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/07a223ee/CordovaLib/Classes/CDVCommandDelegate.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegate.h b/CordovaLib/Classes/CDVCommandDelegate.h
index 4a53f98..6b1dedd 100644
--- a/CordovaLib/Classes/CDVCommandDelegate.h
+++ b/CordovaLib/Classes/CDVCommandDelegate.h
@@ -44,5 +44,7 @@
 - (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop;
 // Runs the given block on a background thread using a shared thread-pool.
 - (void)runInBackground:(void (^)())block;
+// Returns the User-Agent of the associated UIWebView.
+- (NSString*)userAgent;
 
 @end

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/07a223ee/CordovaLib/Classes/CDVCommandDelegateImpl.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegateImpl.m b/CordovaLib/Classes/CDVCommandDelegateImpl.m
index b6a493b..3447111 100644
--- a/CordovaLib/Classes/CDVCommandDelegateImpl.m
+++ b/CordovaLib/Classes/CDVCommandDelegateImpl.m
@@ -128,4 +128,9 @@
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
 }
 
+- (NSString*)userAgent
+{
+    return [_viewController userAgent];
+}
+
 @end

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/07a223ee/CordovaLib/Classes/CDVInAppBrowser.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVInAppBrowser.h b/CordovaLib/Classes/CDVInAppBrowser.h
index 1d44c58..0a3ef70 100644
--- a/CordovaLib/Classes/CDVInAppBrowser.h
+++ b/CordovaLib/Classes/CDVInAppBrowser.h
@@ -44,6 +44,9 @@
 @interface CDVInAppBrowserViewController : UIViewController <UIWebViewDelegate>{
     @private
     NSURL* _requestedURL;
+    NSString* _userAgent;
+    NSString* _prevUserAgent;
+    BOOL _isPDF;
 }
 
 @property (nonatomic, strong) IBOutlet UIWebView* webView;
@@ -56,13 +59,12 @@
 
 @property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate;
 @property (nonatomic, weak) id <CDVInAppBrowserNavigationDelegate> navigationDelegate;
-@property (nonatomic, strong) NSString* userAgent;
 
 - (void)close;
 - (void)navigateTo:(NSURL*)url;
 - (void)showLocationBar:(BOOL)show;
 
-- (id)initWithUserAgent:(NSString*)userAgent;
+- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent;
 
 @end
 

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/07a223ee/CordovaLib/Classes/CDVInAppBrowser.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVInAppBrowser.m b/CordovaLib/Classes/CDVInAppBrowser.m
index 6c87d45..3e1beea 100644
--- a/CordovaLib/Classes/CDVInAppBrowser.m
+++ b/CordovaLib/Classes/CDVInAppBrowser.m
@@ -93,7 +93,7 @@
 {
     if (self.inAppBrowserViewController == nil) {
         NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
-        self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA];
+        self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate userAgent]];
         self.inAppBrowserViewController.navigationDelegate = self;
 
         if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
@@ -202,6 +202,9 @@
 
         [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
     }
+    // Don't recycle the ViewController since it may be consuming a lot of memory.
+    // Also - this is required for the PDF/User-Agent bug work-around.
+    self.inAppBrowserViewController = nil;
 }
 
 @end
@@ -210,11 +213,12 @@
 
 @implementation CDVInAppBrowserViewController
 
-- (id)initWithUserAgent:(NSString*)userAgent
+- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
 {
     self = [super init];
     if (self != nil) {
-        self.userAgent = userAgent;
+        _userAgent = userAgent;
+        _prevUserAgent = prevUserAgent;
         [self createViews];
     }
 
@@ -230,7 +234,7 @@
     webViewBounds.size.height -= FOOTER_HEIGHT;
 
     if (!self.webView) {
-        [CDVUserAgentUtil setUserAgent:self.userAgent];
+        [CDVUserAgentUtil setUserAgent:_userAgent];
 
         self.webView = [[UIWebView alloc] initWithFrame:webViewBounds];
         self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
@@ -417,9 +421,12 @@
 
     [self.spinner startAnimating];
 
+    NSURL* url = theWebView.request.URL;
+    // This is probably a bug, but it works on iOS 5 and 6 to know when a PDF
+    // is being loaded.
+    _isPDF = [[url absoluteString] length] == 0;
+
     if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserLoadStart:)]) {
-        NSURL* url = theWebView.request.URL;
-        // URL is nil when opening PDFs.
         if (url == nil) {
             url = _requestedURL;
         }
@@ -437,12 +444,21 @@
 
     [self.spinner stopAnimating];
 
+    // Work around a bug where the first time a PDF is opened, all UIWebViews
+    // reload their User-Agent from NSUserDefaults.
+    // This work-around makes the following assumptions:
+    // 1. The app has only a single Cordova Webview. If not, then the app should
+    //    take it upon themselves to load a PDF in the background as a part of
+    //    their start-up flow.
+    // 2. That the PDF does not require any additional network requests. We change
+    //    the user-agent here back to that of the CDVViewController, so requests
+    //    from it must pass through its white-list.
+    if (_isPDF) {
+        [CDVUserAgentUtil setUserAgent:_prevUserAgent];
+    }
+
     if ((self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserLoadStop:)]) {
         NSURL* url = theWebView.request.URL;
-        // URL is nil when opening PDFs.
-        if (url == nil) {
-            url = _requestedURL;
-        }
         [self.navigationDelegate browserLoadStop:url];
     }
 }