-
Notifications
You must be signed in to change notification settings - Fork 814
PluginContext #1867
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
Merged
+260
−0
Merged
PluginContext #1867
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c183fd8
feat: pluginContext
RohitKushvaha01 4588b20
Merge branch 'Acode-Foundation:main' into main
RohitKushvaha01 86e8103
format
RohitKushvaha01 7b724e5
fix: issues
RohitKushvaha01 82d3271
feat
RohitKushvaha01 b2f13e4
fix: name
RohitKushvaha01 e0076ea
fix: pacvkage-lock.json
RohitKushvaha01 b4992a4
Update src/plugins/pluginContext/src/android/Tee.java
RohitKushvaha01 88c40ff
Update src/plugins/pluginContext/www/PluginContext.js
RohitKushvaha01 104d5e8
Update src/plugins/pluginContext/src/android/Tee.java
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
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 @@ | ||
| {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "com.foxdebug.acode.rk.plugin.plugincontext", | ||
| "version": "1.0.0", | ||
| "description": "PluginContext", | ||
| "cordova": { | ||
| "id": "com.foxdebug.acode.rk.plugin.plugincontext", | ||
| "platforms": [ | ||
| "android" | ||
| ] | ||
| }, | ||
| "keywords": [ | ||
| "ecosystem:cordova", | ||
| "cordova-android" | ||
| ], | ||
| "author": "@RohitKushvaha01", | ||
| "license": "MIT" | ||
| } |
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,22 @@ | ||
| <?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="com.foxdebug.acode.rk.plugin.plugincontext" version="1.0.0"> | ||
| <name>PluginContext</name> | ||
|
|
||
|
|
||
| <js-module name="PluginContext" src="www/PluginContext.js"> | ||
| <clobbers target="window.PluginContext" /> | ||
| </js-module> | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| <platform name="android"> | ||
| <config-file parent="/*" target="res/xml/config.xml"> | ||
| <feature name="Tee"> | ||
| <param name="android-package" value="com.foxdebug.acode.rk.plugin.Tee" /> | ||
| </feature> | ||
| </config-file> | ||
|
|
||
| <source-file src="src/android/Tee.java" target-dir="src/com/foxdebug/acode/rk/plugin" /> | ||
|
|
||
|
|
||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </platform> | ||
| </plugin> | ||
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,138 @@ | ||
| package com.foxdebug.acode.rk.plugin; | ||
|
|
||
| import org.apache.cordova.CallbackContext; | ||
| import org.apache.cordova.CordovaPlugin; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONException; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class Tee extends CordovaPlugin { | ||
|
|
||
| // pluginId : token | ||
| private /*static*/ final Map<String, String> tokenStore = new ConcurrentHashMap<>(); | ||
|
|
||
| //assigned tokens | ||
| private /*static*/ final Set<String> disclosed = ConcurrentHashMap.newKeySet(); | ||
|
|
||
| // token : list of permissions | ||
| private /*static*/ final Map<String, List<String>> permissionStore = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public boolean execute(String action, JSONArray args, CallbackContext callback) | ||
| throws JSONException { | ||
|
|
||
| if ("requestToken".equals(action)) { | ||
| String pluginId = args.getString(0); | ||
| String pluginJson = args.getString(1); | ||
| handleTokenRequest(pluginId, pluginJson, callback); | ||
| return true; | ||
| } | ||
|
|
||
| if ("grantedPermission".equals(action)) { | ||
| String token = args.getString(0); | ||
| String permission = args.getString(1); | ||
|
|
||
| if (!permissionStore.containsKey(token)) { | ||
| callback.error("INVALID_TOKEN"); | ||
| return true; | ||
| } | ||
|
|
||
| boolean granted = grantedPermission(token, permission); | ||
| callback.success(granted ? 1 : 0); | ||
| return true; | ||
| } | ||
|
|
||
| if ("listAllPermissions".equals(action)) { | ||
| String token = args.getString(0); | ||
|
|
||
| if (!permissionStore.containsKey(token)) { | ||
| callback.error("INVALID_TOKEN"); | ||
| return true; | ||
| } | ||
|
|
||
| List<String> permissions = listAllPermissions(token); | ||
| JSONArray result = new JSONArray(permissions); | ||
|
|
||
| callback.success(result); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| //============================================================ | ||
| //do not change function signatures | ||
| public boolean isTokenValid(String token, String pluginId) { | ||
| String storedToken = tokenStore.get(pluginId); | ||
| return storedToken != null && token.equals(storedToken); | ||
| } | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| public boolean grantedPermission(String token, String permission) { | ||
| List<String> permissions = permissionStore.get(token); | ||
| return permissions != null && permissions.contains(permission); | ||
| } | ||
|
|
||
| public List<String> listAllPermissions(String token) { | ||
| List<String> permissions = permissionStore.get(token); | ||
|
|
||
| if (permissions == null) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| return new ArrayList<>(permissions); // return copy (safe) | ||
| } | ||
| //============================================================ | ||
|
|
||
|
|
||
| private synchronized void handleTokenRequest( | ||
| String pluginId, | ||
| String pluginJson, | ||
| CallbackContext callback | ||
| ) { | ||
|
|
||
| if (disclosed.contains(pluginId)) { | ||
| callback.error("TOKEN_ALREADY_ISSUED"); | ||
| return; | ||
| } | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| String token = tokenStore.get(pluginId); | ||
|
|
||
| if (token == null) { | ||
| token = UUID.randomUUID().toString(); | ||
| tokenStore.put(pluginId, token); | ||
| } | ||
|
|
||
| try { | ||
| JSONObject json = new JSONObject(pluginJson); | ||
| JSONArray permissions = json.optJSONArray("permissions"); | ||
|
|
||
| List<String> permissionList = new ArrayList<>(); | ||
|
|
||
| if (permissions != null) { | ||
| for (int i = 0; i < permissions.length(); i++) { | ||
| permissionList.add(permissions.getString(i)); | ||
| } | ||
| } | ||
|
|
||
| // Bind permissions to token | ||
| permissionStore.put(token, permissionList); | ||
|
|
||
| } catch (JSONException e) { | ||
| callback.error("INVALID_PLUGIN_JSON"); | ||
| return; | ||
| } | ||
|
|
||
| disclosed.add(pluginId); | ||
| callback.success(token); | ||
| } | ||
| } | ||
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,65 @@ | ||
| var exec = require("cordova/exec"); | ||
|
|
||
| const PluginContext = (function () { | ||
| //============================= | ||
| class _PluginContext { | ||
| constructor(uuid) { | ||
| this.created_at = Date.now(); | ||
| this.uuid = uuid; | ||
| Object.freeze(this); | ||
| } | ||
|
|
||
| toString() { | ||
| return this.uuid; | ||
| } | ||
|
|
||
| [Symbol.toPrimitive](hint) { | ||
| if (hint === "number") { | ||
| return NaN; // prevent numeric coercion | ||
| } | ||
| return this.uuid; | ||
| } | ||
|
|
||
| grantedPermission(permission) { | ||
| return new Promise((resolve, reject) => { | ||
| exec(resolve, reject, "Tee", "grantedPermission", [ | ||
| this.uuid, | ||
| permission, | ||
| ]); | ||
| }); | ||
| } | ||
|
|
||
| listAllPermissions() { | ||
| return new Promise((resolve, reject) => { | ||
| exec(resolve, reject, "Tee", "listAllPermissions", [this.uuid]); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| //Object.freeze(this); | ||
|
|
||
| //=============================== | ||
|
|
||
| return { | ||
| generate: async function (pluginId, pluginJson) { | ||
| try { | ||
| function requestToken(pluginId) { | ||
| return new Promise((resolve, reject) => { | ||
| exec(resolve, reject, "Tee", "requestToken", [ | ||
| pluginId, | ||
| pluginJson, | ||
| ]); | ||
| }); | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const uuid = await requestToken(pluginId); | ||
| return new _PluginContext(uuid); | ||
| } catch (err) { | ||
| console.warn(`PluginContext creation failed for pluginId ${pluginId}:`, err); | ||
| return null; | ||
| } | ||
| }, | ||
| }; | ||
| })(); | ||
|
|
||
| module.exports = PluginContext; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.