Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android/cli): Allow to use the old addJavascriptInterface bridge #6043

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CapConfig {
private boolean webContentsDebuggingEnabled = false;
private boolean loggingEnabled = true;
private boolean initialFocus = true;
private boolean useLegacyBridge = false;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;
private String errorPath;

Expand Down Expand Up @@ -128,6 +129,7 @@ private CapConfig(Builder builder) {
this.webContentsDebuggingEnabled = builder.webContentsDebuggingEnabled;
this.loggingEnabled = builder.loggingEnabled;
this.initialFocus = builder.initialFocus;
this.useLegacyBridge = builder.useLegacyBridge;
this.minWebViewVersion = builder.minWebViewVersion;
this.errorPath = builder.errorPath;

Expand Down Expand Up @@ -186,6 +188,7 @@ private void deserializeConfig(@Nullable Context context) {
);
minWebViewVersion = JSONUtils.getInt(configJSON, "android.minWebViewVersion", DEFAULT_ANDROID_WEBVIEW_VERSION);
captureInput = JSONUtils.getBoolean(configJSON, "android.captureInput", captureInput);
useLegacyBridge = JSONUtils.getBoolean(configJSON, "android.useLegacyBridge", useLegacyBridge);
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);

String logBehavior = JSONUtils.getString(
Expand Down Expand Up @@ -284,6 +287,10 @@ public boolean isInitialFocus() {
return initialFocus;
}

public boolean isUsingLegacyBridge() {
return useLegacyBridge;
}

public int getMinWebViewVersion() {
if (minWebViewVersion < MINIMUM_ANDROID_WEBVIEW_VERSION) {
Logger.warn("Specified minimum webview version is too low, defaulting to " + MINIMUM_ANDROID_WEBVIEW_VERSION);
Expand Down Expand Up @@ -450,6 +457,7 @@ public static class Builder {
private Boolean webContentsDebuggingEnabled = null;
private boolean loggingEnabled = true;
private boolean initialFocus = false;
private boolean useLegacyBridge = false;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;

// Embedded
Expand Down Expand Up @@ -545,6 +553,11 @@ public Builder setCaptureInput(boolean captureInput) {
return this;
}

public Builder setUseLegacyBridge(boolean useLegacyBridge) {
this.useLegacyBridge = useLegacyBridge;
return this;
}

public Builder setWebContentsDebuggingEnabled(boolean webContentsDebuggingEnabled) {
this.webContentsDebuggingEnabled = webContentsDebuggingEnabled;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ public MessageHandler(Bridge bridge, WebView webView, PluginManager cordovaPlugi
this.webView = webView;
this.cordovaPluginManager = cordovaPluginManager;

WebViewCompat.WebMessageListener capListener = (view, message, sourceOrigin, isMainFrame, replyProxy) -> {
if (isMainFrame) {
postMessage(message.getData());
javaScriptReplyProxy = replyProxy;
} else {
Logger.warn("Plugin execution is allowed in Main Frame only");
}
};

if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) && !bridge.getConfig().isUsingLegacyBridge()) {
WebViewCompat.WebMessageListener capListener = (view, message, sourceOrigin, isMainFrame, replyProxy) -> {
if (isMainFrame) {
postMessage(message.getData());
javaScriptReplyProxy = replyProxy;
} else {
Logger.warn("Plugin execution is allowed in Main Frame only");
}
};
try {
WebViewCompat.addWebMessageListener(webView, "androidBridge", bridge.getAllowedOriginRules(), capListener);
} catch (Exception ex) {
Expand Down Expand Up @@ -123,12 +122,12 @@ public void sendResponseMessage(PluginCall call, PluginResult successResult, Plu

boolean isValidCallbackId = !call.getCallbackId().equals(PluginCall.CALLBACK_ID_DANGLING);
if (isValidCallbackId) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) && javaScriptReplyProxy != null) {
if (bridge.getConfig().isUsingLegacyBridge()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use an extra if because Android Studio would complain if I put this check with ! on the isFeatureSupported check on the postMessage line

legacySendResponseMessage(data);
} else if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) && javaScriptReplyProxy != null) {
javaScriptReplyProxy.postMessage(data.toString());
} else {
final String runScript = "window.Capacitor.fromNative(" + data.toString() + ")";
final WebView webView = this.webView;
webView.post(() -> webView.evaluateJavascript(runScript, null));
legacySendResponseMessage(data);
}
} else {
bridge.getApp().fireRestoredResult(data);
Expand All @@ -141,6 +140,12 @@ public void sendResponseMessage(PluginCall call, PluginResult successResult, Plu
}
}

private void legacySendResponseMessage(PluginResult data) {
final String runScript = "window.Capacitor.fromNative(" + data.toString() + ")";
final WebView webView = this.webView;
webView.post(() -> webView.evaluateJavascript(runScript, null));
}

private void callPluginMethod(String callbackId, String pluginId, String methodName, JSObject methodData) {
PluginCall call = new PluginCall(this, pluginId, callbackId, methodName, methodData);
bridge.callPluginMethod(pluginId, methodName, call);
Expand Down
9 changes: 9 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ export interface CapacitorConfig {
*/
releaseType?: 'AAB' | 'APK';
};

/**
* Use legacy [addJavascriptInterface](https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String))
* instead of the new and more secure [addWebMessageListener](https://developer.android.com/reference/androidx/webkit/WebViewCompat#addWebMessageListener(android.webkit.WebView,java.lang.String,java.util.Set%3Cjava.lang.String%3E,androidx.webkit.WebViewCompat.WebMessageListener))
*
* @since 4.5.0
* @default false
*/
useLegacyBridge: boolean;
jcesarmobile marked this conversation as resolved.
Show resolved Hide resolved
};

ios?: {
Expand Down