From ca3111cbfd27abb960e8a2da03491d06ce3fe544 Mon Sep 17 00:00:00 2001 From: Leonardo Monteiro Fernandes Date: Mon, 13 Sep 2021 15:14:38 +1000 Subject: [PATCH 1/4] Fix memory leak https://github.com/apache/cordova-plugin-inappbrowser/issues/290 --- src/android/InAppBrowser.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 76613c789..0998f0ac0 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -541,6 +541,16 @@ public void onPageFinished(WebView view, String url) { dialog.dismiss(); dialog = null; } + + // Fix for webView window not being destroyed correctly causing memory leak + // (https://github.com/apache/cordova-plugin-inappbrowser/issues/290) + if (url.equals("about:blank")) { + inAppWebView.onPause(); + inAppWebView.removeAllViews(); + inAppWebView.destroyDrawingCache(); + inAppWebView.destroy(); + inAppWebView = null; + } } }); // NB: From SDK 19: "If you call methods on WebView from any thread From 440487e62b2baa06d2cb0e00b840f877fe7dfc79 Mon Sep 17 00:00:00 2001 From: Leonardo Monteiro Fernandes Date: Mon, 13 Sep 2021 18:57:04 +1000 Subject: [PATCH 2/4] Fix beforeLoad not being called in some requests --- src/android/InAppBrowser.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 0998f0ac0..9747b9d3c 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -265,12 +265,6 @@ else if (action.equals("loadAfterBeforeload")) { @SuppressLint("NewApi") @Override public void run() { - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) { - currentClient.waitForBeforeload = false; - inAppWebView.setWebViewClient(currentClient); - } else { - ((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false; - } inAppWebView.loadUrl(url); } }); @@ -1166,7 +1160,6 @@ public class InAppBrowserClient extends WebViewClient { EditText edittext; CordovaWebView webView; String beforeload; - boolean waitForBeforeload; /** * Constructor. @@ -1178,7 +1171,6 @@ public InAppBrowserClient(CordovaWebView webView, EditText mEditText, String bef this.webView = webView; this.edittext = mEditText; this.beforeload = beforeload; - this.waitForBeforeload = beforeload != null; } /** @@ -1239,7 +1231,7 @@ public boolean shouldOverrideUrlLoading(String url, String method) { } // On first URL change, initiate JS callback. Only after the beforeload event, continue. - if (useBeforeload && this.waitForBeforeload) { + if (useBeforeload) { if(sendBeforeLoad(url, method)) { return true; } @@ -1334,9 +1326,6 @@ else if (!url.startsWith("http:") && !url.startsWith("https:") && url.matches("^ } } - if (useBeforeload) { - this.waitForBeforeload = true; - } return override; } From e9771e91408307b4d5b9b44693c2d64535664e58 Mon Sep 17 00:00:00 2001 From: Leonardo Monteiro Fernandes Date: Fri, 17 Sep 2021 16:27:29 +1000 Subject: [PATCH 3/4] Allow InAppBrowser to keep working after opening a link with target="_system" --- src/android/InAppBrowser.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 9747b9d3c..b162ad669 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -160,7 +160,6 @@ public class InAppBrowser extends CordovaPlugin { */ public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException { if (action.equals("open")) { - this.callbackContext = callbackContext; final String url = args.getString(0); String t = args.optString(1); if (t == null || t.equals("") || t.equals(NULL)) { @@ -171,6 +170,10 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca LOG.d(LOG_TAG, "target = " + target); + if (!SYSTEM.equals(target)) { + this.callbackContext = callbackContext; + } + this.cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { From b4526da4eb3702d2fb4d76dd5cb591c269d81790 Mon Sep 17 00:00:00 2001 From: Leonardo Monteiro Fernandes Date: Fri, 17 Sep 2021 16:30:03 +1000 Subject: [PATCH 4/4] Allow InAppBrowser to keep working after opening a link with target="_system" --- src/android/InAppBrowser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index b162ad669..2f8380705 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -170,7 +170,8 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca LOG.d(LOG_TAG, "target = " + target); - if (!SYSTEM.equals(target)) { + final boolean keepCallback = !SYSTEM.equals(target); + if (keepCallback) { this.callbackContext = callbackContext; } @@ -251,7 +252,7 @@ else if (SYSTEM.equals(target)) { } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); - pluginResult.setKeepCallback(true); + pluginResult.setKeepCallback(keepCallback); callbackContext.sendPluginResult(pluginResult); } });