Skip to content

Commit

Permalink
feat(android/cli): Allow to use the old addJavascriptInterface bridge (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Nov 15, 2022
1 parent ba1e770 commit a6e7c54
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
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()) {
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;
};

ios?: {
Expand Down

0 comments on commit a6e7c54

Please sign in to comment.