-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add WebView Plugin API #2525
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
Draft
RohitKushvaha01
wants to merge
4
commits into
Acode-Foundation:main
Choose a base branch
from
RohitKushvaha01:feat/webview-plugin-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,071
−2
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6bb00b5
feat: add WebView Plugin API
RohitKushvaha01 fdbca22
format
RohitKushvaha01 d58b26c
fix: remove title bar and close button from fullscreen WebView
RohitKushvaha01 f30398a
fix: lifecycle events, hidden show, fullscreen disconnect, and security
RohitKushvaha01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import nativeBridge from "../plugins/webview/www/webview"; | ||
|
|
||
| let initialized = false; | ||
| const instances = new Map(); | ||
| const eventCallbacks = new Map(); | ||
|
|
||
| function ensureInit() { | ||
| if (!initialized) { | ||
| nativeBridge.setMessageCallback((payload) => { | ||
| const { id, message, event, data } = payload; | ||
|
|
||
| if (event) { | ||
| const callbacks = eventCallbacks.get(id); | ||
| if (callbacks) { | ||
| callbacks.forEach((entry) => { | ||
| if (entry.event === event) { | ||
| try { | ||
| entry.callback(event, data); | ||
| } catch (e) { | ||
| console.error("WebView event callback error:", e); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (message !== undefined) { | ||
| const instance = instances.get(id); | ||
| if (instance && instance._messageCallbacks) { | ||
| let parsed = message; | ||
| try { | ||
| parsed = JSON.parse(message); | ||
| } catch (_) {} | ||
| instance._messageCallbacks.forEach((cb) => { | ||
| try { | ||
| cb(parsed); | ||
| } catch (e) { | ||
| console.error("WebView message callback error:", e); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| initialized = true; | ||
| } | ||
| } | ||
|
|
||
| class WebView { | ||
| constructor(id, options = {}) { | ||
| this.id = id; | ||
| this.options = options; | ||
| this._messageCallbacks = []; | ||
| this._eventCallbacks = []; | ||
| this._destroyed = false; | ||
|
|
||
| instances.set(id, this); | ||
| eventCallbacks.set(id, this._eventCallbacks); | ||
| } | ||
|
|
||
| async loadURL(url) { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.loadURL(this.id, url); | ||
| } | ||
|
|
||
| async loadHTML(html) { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.loadHTML(this.id, html); | ||
| } | ||
|
|
||
| async evaluate(js) { | ||
| this._checkDestroyed(); | ||
| return await nativeBridge.evaluate(this.id, js); | ||
| } | ||
|
|
||
| onMessage(callback) { | ||
| this._checkDestroyed(); | ||
| if (typeof callback === "function") { | ||
| this._messageCallbacks.push(callback); | ||
| } | ||
| } | ||
|
|
||
| offMessage(callback) { | ||
| this._messageCallbacks = this._messageCallbacks.filter( | ||
| (cb) => cb !== callback, | ||
| ); | ||
| } | ||
|
|
||
| on(event, callback) { | ||
| this._checkDestroyed(); | ||
| if (typeof callback === "function") { | ||
| this._eventCallbacks.push({ event, callback }); | ||
| } | ||
| } | ||
|
|
||
| off(event, callback) { | ||
| this._eventCallbacks = this._eventCallbacks.filter( | ||
| (entry) => !(entry.event === event && entry.callback === callback), | ||
| ); | ||
| } | ||
|
|
||
| async postMessage(message) { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.postMessage(this.id, message); | ||
| } | ||
|
|
||
| async show() { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.show(this.id); | ||
| } | ||
|
|
||
| async hide() { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.hide(this.id); | ||
| } | ||
|
|
||
| async reload() { | ||
| this._checkDestroyed(); | ||
| await nativeBridge.reload(this.id); | ||
| } | ||
|
|
||
| async destroy() { | ||
| this._checkDestroyed(); | ||
| this._destroyed = true; | ||
| await nativeBridge.destroy(this.id); | ||
| instances.delete(this.id); | ||
| eventCallbacks.delete(this.id); | ||
| this._messageCallbacks = []; | ||
| this._eventCallbacks = []; | ||
| } | ||
|
|
||
| _checkDestroyed() { | ||
| if (this._destroyed) { | ||
| throw new Error("WebView has been destroyed"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const webviewAPI = { | ||
| async create(options = {}) { | ||
| ensureInit(); | ||
|
|
||
| const id = await nativeBridge.create({ | ||
| title: options.title || "", | ||
| mode: options.mode || "hidden", | ||
| width: options.width || 0, | ||
| height: options.height || 0, | ||
| x: options.x || 0, | ||
| y: options.y || 0, | ||
| allowNavigation: options.allowNavigation !== false, | ||
| allowDownloads: options.allowDownloads === true, | ||
| visible: options.visible !== false, | ||
| }); | ||
|
|
||
| return new WebView(id, options); | ||
| }, | ||
| }; | ||
|
|
||
| export default webviewAPI; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "cordova-plugin-acode-webview", | ||
| "version": "1.0.0", | ||
| "description": "Acode WebView Plugin API", | ||
| "main": "", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "Apache-2.0" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" | ||
| xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-acode-webview" version="1.0.0"> | ||
| <name>cordova-plugin-acode-webview</name> | ||
| <description>Acode WebView Plugin API - Create and manage isolated WebView instances</description> | ||
| <license>Apache 2.0</license> | ||
|
|
||
| <platform name="android"> | ||
|
|
||
| <config-file target="res/xml/config.xml" parent="/*"> | ||
| <feature name="AcodeWebView"> | ||
| <param name="android-package" value="com.foxdebug.webview.WebViewPlugin"/> | ||
| </feature> | ||
| </config-file> | ||
|
|
||
| <config-file parent="./application" target="AndroidManifest.xml"> | ||
| <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:name="com.foxdebug.webview.WebViewActivity" android:theme="@style/Theme.App.Activity" android:windowSoftInputMode="adjustResize" android:resizeableActivity="true"> | ||
| </activity> | ||
| </config-file> | ||
|
|
||
| <source-file src="src/android/com/foxdebug/webview/WebViewPlugin.java" target-dir="src/com/foxdebug/webview"/> | ||
| <source-file src="src/android/com/foxdebug/webview/WebViewInstance.java" target-dir="src/com/foxdebug/webview"/> | ||
| <source-file src="src/android/com/foxdebug/webview/WebViewActivity.java" target-dir="src/com/foxdebug/webview"/> | ||
|
|
||
| </platform> | ||
| </plugin> |
67 changes: 67 additions & 0 deletions
67
src/plugins/webview/src/android/com/foxdebug/webview/WebViewActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.foxdebug.webview; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.os.Build; | ||
| import android.os.Bundle; | ||
| import android.view.ViewGroup; | ||
| import android.webkit.WebView; | ||
| import android.widget.FrameLayout; | ||
|
|
||
| public class WebViewActivity extends Activity { | ||
|
|
||
| private static WebViewPlugin plugin; | ||
|
|
||
| private WebView webView; | ||
| private String webviewId; | ||
|
|
||
| public static void setPlugin(WebViewPlugin p) { | ||
| plugin = p; | ||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
|
|
||
| Intent intent = getIntent(); | ||
| webviewId = intent.getStringExtra("webviewId"); | ||
|
|
||
| WebViewInstance instance = plugin != null ? plugin.getInstance(webviewId) : null; | ||
| if (instance == null) { | ||
| finish(); | ||
| return; | ||
| } | ||
|
|
||
| instance.createWebView(this); | ||
| webView = instance.getWebView(); | ||
|
|
||
| FrameLayout container = new FrameLayout(this); | ||
| container.addView(webView, new FrameLayout.LayoutParams( | ||
| ViewGroup.LayoutParams.MATCH_PARENT, | ||
| ViewGroup.LayoutParams.MATCH_PARENT | ||
| )); | ||
| setContentView(container); | ||
|
|
||
| if (Build.VERSION.SDK_INT >= 30) { | ||
| getWindow().setDecorFitsSystemWindows(false); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onBackPressed() { | ||
| if (webView != null && webView.canGoBack()) { | ||
| webView.goBack(); | ||
| } else { | ||
| finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void onDestroy() { | ||
| super.onDestroy(); | ||
| if (plugin != null) { | ||
| plugin.sendEventToCordova(webviewId, "closed", null); | ||
| plugin.removeInstance(webviewId); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebViewActivityreads its private staticpluginfield, but the fullscreen launch path never callssetPlugin(). The field remains null, so creating a fullscreen WebView makesinstancenull and immediately finishes the activity without displaying any content. Initialize this reference before launching the activity or retrieve the initializedWebViewPluginsingleton.