Skip to content

Commit 64b8738

Browse files
alexgerardojacintoleonardo-fernandesnflsilva
authored
Prepare release of version 4.0.0-OS14 (#43)
* RMET-2119 InAppBrowser Plugin - Check for element before obtaining it (#40) * fix: check if there's a next token before trying to obtain it References: https://outsystemsrd.atlassian.net/browse/RMET-2119 * chore: update changelog * RMET-2802 InAppBrowser Plugin - Multiple minor fixes from the community (#42) * Fix memory leak - initially reported in apache#290 Whenever closing an InAppBrowser instance, a webview was left in memory with about:blank page. This change fixes the issue by destroying and freeing the inAppWebView object. * Fix beforeLoad not being called in some requests - initially reported in apache#686 The waitForBeforeload flag was preventing beforeLoad from being called on every GET request. * Do not lose callbackContext when opening a SYSTEM url This fixes a condition where it was not possible to open a SYSTEM url while an InAppBrowser instance is displayed. The callbackContext of the InAppBrowser instance was lost when the SYSTEM url was opened. This fixes the issue by not setting the callbackContext on SYSTEM urls. * Fix crash when pausing/resuming application after closing InAppBrowser * Fix _loadAfterBeforeload callback not working due to this.rootName not being defined. * Reset beforeload variable if a new instance of InAppBrowser is opened without beforeload setting * chore: update changelog References: https://outsystemsrd.atlassian.net/browse/RMET-2802 * refactor: remove empty lines * refactor: use ternary operator and remove unnecessary ones Why: We can refactor the if-else block that assigns a value to beforeload to use a ternary operator. On the other hand, we can remove the unnecessary ternary operators used to assign a boolean value. They are redundant. References: https://outsystemsrd.atlassian.net/browse/RMET-2802 --------- Co-authored-by: Leonardo Monteiro Fernandes <[email protected]> Co-authored-by: Nelson Lopes Silva <[email protected]> --------- Co-authored-by: Leonardo Monteiro Fernandes <[email protected]> Co-authored-by: Nelson Lopes Silva <[email protected]>
1 parent cb6ca43 commit 64b8738

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
### [Unreleased]
8+
9+
### Fixes
10+
- Do not lose callbackContext when opening a SYSTEM url (https://outsystemsrd.atlassian.net/browse/RMET-2802).
11+
- Fix beforeLoad not being called in some requests (https://outsystemsrd.atlassian.net/browse/RMET-2802).
12+
- Fix memory leak with webview (https://outsystemsrd.atlassian.net/browse/RMET-2802).
13+
814
### Fix
915
- Android - InAppBrowser not opening when no options are passed - check for next element before trying to obtain. (https://outsystemsrd.atlassian.net/browse/RMET-2119)
1016

src/android/InAppBrowser.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public class InAppBrowser extends CordovaPlugin {
166166
*/
167167
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
168168
if (action.equals("open")) {
169-
this.callbackContext = callbackContext;
170169
final String url = args.getString(0);
171170
String t = args.optString(1);
172171
if (t == null || t.equals("") || t.equals(NULL)) {
@@ -177,6 +176,11 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca
177176

178177
LOG.d(LOG_TAG, "target = " + target);
179178

179+
final boolean keepCallback = !SYSTEM.equals(target);
180+
if (keepCallback) {
181+
this.callbackContext = callbackContext;
182+
}
183+
180184
this.cordova.getActivity().runOnUiThread(new Runnable() {
181185
@Override
182186
public void run() {
@@ -254,7 +258,7 @@ else if (SYSTEM.equals(target)) {
254258
}
255259

256260
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
257-
pluginResult.setKeepCallback(true);
261+
pluginResult.setKeepCallback(keepCallback);
258262
callbackContext.sendPluginResult(pluginResult);
259263
}
260264
});
@@ -271,12 +275,6 @@ else if (action.equals("loadAfterBeforeload")) {
271275
@SuppressLint("NewApi")
272276
@Override
273277
public void run() {
274-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
275-
currentClient.waitForBeforeload = false;
276-
inAppWebView.setWebViewClient(currentClient);
277-
} else {
278-
((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
279-
}
280278
inAppWebView.loadUrl(url);
281279
}
282280
});
@@ -360,7 +358,7 @@ public void onReset() {
360358
*/
361359
@Override
362360
public void onPause(boolean multitasking) {
363-
if (shouldPauseInAppBrowser) {
361+
if (shouldPauseInAppBrowser && inAppWebView != null) {
364362
inAppWebView.onPause();
365363
}
366364
}
@@ -370,7 +368,7 @@ public void onPause(boolean multitasking) {
370368
*/
371369
@Override
372370
public void onResume(boolean multitasking) {
373-
if (shouldPauseInAppBrowser) {
371+
if (shouldPauseInAppBrowser && inAppWebView != null) {
374372
inAppWebView.onResume();
375373
}
376374
}
@@ -549,6 +547,16 @@ public void onPageFinished(WebView view, String url) {
549547
dialog.dismiss();
550548
dialog = null;
551549
}
550+
551+
// Fix for webView window not being destroyed correctly causing memory leak
552+
// (https://github.com/apache/cordova-plugin-inappbrowser/issues/290)
553+
if (url.equals("about:blank")) {
554+
inAppWebView.onPause();
555+
inAppWebView.removeAllViews();
556+
inAppWebView.destroyDrawingCache();
557+
inAppWebView.destroy();
558+
inAppWebView = null;
559+
}
552560
}
553561
});
554562
// NB: From SDK 19: "If you call methods on WebView from any thread
@@ -647,48 +655,48 @@ public String showWebPage(final String url, HashMap<String, String> features) {
647655
if (features != null) {
648656
String show = features.get(LOCATION);
649657
if (show != null) {
650-
showLocationBar = show.equals("yes") ? true : false;
658+
showLocationBar = show.equals("yes");
651659
}
652660
if(showLocationBar) {
653661
String hideNavigation = features.get(HIDE_NAVIGATION);
654662
String hideUrl = features.get(HIDE_URL);
655-
if(hideNavigation != null) hideNavigationButtons = hideNavigation.equals("yes") ? true : false;
656-
if(hideUrl != null) hideUrlBar = hideUrl.equals("yes") ? true : false;
663+
if(hideNavigation != null) hideNavigationButtons = hideNavigation.equals("yes");
664+
if(hideUrl != null) hideUrlBar = hideUrl.equals("yes");
657665
}
658666
String zoom = features.get(ZOOM);
659667
if (zoom != null) {
660-
showZoomControls = zoom.equals("yes") ? true : false;
668+
showZoomControls = zoom.equals("yes");
661669
}
662670
String hidden = features.get(HIDDEN);
663671
if (hidden != null) {
664-
openWindowHidden = hidden.equals("yes") ? true : false;
672+
openWindowHidden = hidden.equals("yes");
665673
}
666674
String hardwareBack = features.get(HARDWARE_BACK_BUTTON);
667675
if (hardwareBack != null) {
668-
hadwareBackButton = hardwareBack.equals("yes") ? true : false;
676+
hadwareBackButton = hardwareBack.equals("yes");
669677
} else {
670678
hadwareBackButton = DEFAULT_HARDWARE_BACK;
671679
}
672680
String mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION);
673681
if (mediaPlayback != null) {
674-
mediaPlaybackRequiresUserGesture = mediaPlayback.equals("yes") ? true : false;
682+
mediaPlaybackRequiresUserGesture = mediaPlayback.equals("yes");
675683
}
676684
String cache = features.get(CLEAR_ALL_CACHE);
677685
if (cache != null) {
678-
clearAllCache = cache.equals("yes") ? true : false;
686+
clearAllCache = cache.equals("yes");
679687
} else {
680688
cache = features.get(CLEAR_SESSION_CACHE);
681689
if (cache != null) {
682-
clearSessionCache = cache.equals("yes") ? true : false;
690+
clearSessionCache = cache.equals("yes");
683691
}
684692
}
685693
String shouldPause = features.get(SHOULD_PAUSE);
686694
if (shouldPause != null) {
687-
shouldPauseInAppBrowser = shouldPause.equals("yes") ? true : false;
695+
shouldPauseInAppBrowser = shouldPause.equals("yes");
688696
}
689697
String wideViewPort = features.get(USER_WIDE_VIEW_PORT);
690698
if (wideViewPort != null ) {
691-
useWideViewPort = wideViewPort.equals("yes") ? true : false;
699+
useWideViewPort = wideViewPort.equals("yes");
692700
}
693701
String closeButtonCaptionSet = features.get(CLOSE_BUTTON_CAPTION);
694702
if (closeButtonCaptionSet != null) {
@@ -713,18 +721,18 @@ public String showWebPage(final String url, HashMap<String, String> features) {
713721
}
714722
String showFooterSet = features.get(FOOTER);
715723
if (showFooterSet != null) {
716-
showFooter = showFooterSet.equals("yes") ? true : false;
724+
showFooter = showFooterSet.equals("yes");
717725
}
718726
String footerColorSet = features.get(FOOTER_COLOR);
719727
if (footerColorSet != null) {
720728
footerColor = footerColorSet;
721729
}
722-
if (features.get(BEFORELOAD) != null) {
723-
beforeload = features.get(BEFORELOAD);
724-
}
730+
731+
beforeload = features.get(BEFORELOAD) != null ? features.get(BEFORELOAD) : "";
732+
725733
String fullscreenSet = features.get(FULLSCREEN);
726734
if (fullscreenSet != null) {
727-
fullscreen = fullscreenSet.equals("yes") ? true : false;
735+
fullscreen = fullscreenSet.equals("yes");
728736
}
729737
}
730738

@@ -812,7 +820,6 @@ public class InAppBrowserClient extends WebViewClient {
812820
EditText edittext;
813821
CordovaWebView webView;
814822
String beforeload;
815-
boolean waitForBeforeload;
816823

817824
/**
818825
* Constructor.
@@ -824,7 +831,6 @@ public InAppBrowserClient(CordovaWebView webView, EditText mEditText, String bef
824831
this.webView = webView;
825832
this.edittext = mEditText;
826833
this.beforeload = beforeload;
827-
this.waitForBeforeload = beforeload != null;
828834
}
829835

830836
/**
@@ -885,7 +891,7 @@ public boolean shouldOverrideUrlLoading(String url, String method) {
885891
}
886892

887893
// On first URL change, initiate JS callback. Only after the beforeload event, continue.
888-
if (useBeforeload && this.waitForBeforeload) {
894+
if (useBeforeload) {
889895
if(sendBeforeLoad(url, method)) {
890896
return true;
891897
}
@@ -980,9 +986,6 @@ else if (!url.startsWith("http:") && !url.startsWith("https:") && url.matches("^
980986
}
981987
}
982988

983-
if (useBeforeload) {
984-
this.waitForBeforeload = true;
985-
}
986989
return override;
987990
}
988991

www/inappbrowser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
_eventHandler: function (event) {
4242
if (event && (event.type in this.channels)) {
4343
if (event.type === 'beforeload') {
44-
this.channels[event.type].fire(event, this._loadAfterBeforeload);
44+
this.channels[event.type].fire(event, this._loadAfterBeforeload.bind(this));
4545
} else {
4646
this.channels[event.type].fire(event);
4747
}

0 commit comments

Comments
 (0)