Skip to content

Commit 82d3271

Browse files
feat
1 parent 7b724e5 commit 82d3271

File tree

3 files changed

+95
-72
lines changed

3 files changed

+95
-72
lines changed

src/lib/loadPlugin.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,69 @@ import Url from "utils/Url";
55
import actionStack from "./actionStack";
66

77
export default async function loadPlugin(pluginId, justInstalled = false) {
8-
const baseUrl = await helpers.toInternalUri(Url.join(PLUGIN_DIR, pluginId));
9-
const cacheFile = Url.join(CACHE_STORAGE, pluginId);
8+
const baseUrl = await helpers.toInternalUri(Url.join(PLUGIN_DIR, pluginId));
9+
const cacheFile = Url.join(CACHE_STORAGE, pluginId);
1010

11-
const pluginJson = await fsOperation(
12-
Url.join(PLUGIN_DIR, pluginId, "plugin.json"),
13-
).readFile("json");
11+
const pluginJson = await fsOperation(
12+
Url.join(PLUGIN_DIR, pluginId, "plugin.json"),
13+
).readFile("json");
1414

15-
let mainUrl;
16-
if (
17-
await fsOperation(Url.join(PLUGIN_DIR, pluginId, pluginJson.main)).exists()
18-
) {
19-
mainUrl = Url.join(baseUrl, pluginJson.main);
20-
} else {
21-
mainUrl = Url.join(baseUrl, "main.js");
22-
}
15+
let mainUrl;
16+
if (
17+
await fsOperation(Url.join(PLUGIN_DIR, pluginId, pluginJson.main)).exists()
18+
) {
19+
mainUrl = Url.join(baseUrl, pluginJson.main);
20+
} else {
21+
mainUrl = Url.join(baseUrl, "main.js");
22+
}
2323

24-
return new Promise((resolve, reject) => {
25-
const $script = <script src={mainUrl}></script>;
24+
return new Promise((resolve, reject) => {
25+
const $script = <script src={mainUrl}></script>;
2626

27-
$script.onerror = (error) => {
28-
reject(
29-
new Error(
30-
`Failed to load script for plugin ${pluginId}: ${error.message || error}`,
31-
),
32-
);
33-
};
27+
$script.onerror = (error) => {
28+
reject(
29+
new Error(
30+
`Failed to load script for plugin ${pluginId}: ${error.message || error}`,
31+
),
32+
);
33+
};
3434

35-
$script.onload = async () => {
36-
const $page = Page("Plugin");
37-
$page.show = () => {
38-
actionStack.push({
39-
id: pluginId,
40-
action: $page.hide,
41-
});
35+
$script.onload = async () => {
36+
const $page = Page("Plugin");
37+
$page.show = () => {
38+
actionStack.push({
39+
id: pluginId,
40+
action: $page.hide,
41+
});
4242

43-
app.append($page);
44-
};
43+
app.append($page);
44+
};
4545

46-
$page.onhide = function () {
47-
actionStack.remove(pluginId);
48-
};
46+
$page.onhide = function () {
47+
actionStack.remove(pluginId);
48+
};
4949

50-
try {
51-
if (!(await fsOperation(cacheFile).exists())) {
52-
await fsOperation(CACHE_STORAGE).createFile(pluginId);
53-
}
50+
try {
51+
if (!(await fsOperation(cacheFile).exists())) {
52+
await fsOperation(CACHE_STORAGE).createFile(pluginId);
53+
}
5454

55-
await acode.initPlugin(pluginId, baseUrl, $page, {
56-
cacheFileUrl: await helpers.toInternalUri(cacheFile),
57-
cacheFile: fsOperation(cacheFile),
58-
firstInit: justInstalled,
59-
ctx: await PluginContext.generate(
60-
pluginId,
61-
JSON.stringify(pluginJson),
62-
),
63-
});
55+
await acode.initPlugin(pluginId, baseUrl, $page, {
56+
cacheFileUrl: await helpers.toInternalUri(cacheFile),
57+
cacheFile: fsOperation(cacheFile),
58+
firstInit: justInstalled,
59+
ctx: await PluginContext.generate(
60+
pluginId,
61+
JSON.stringify(pluginJson),
62+
),
63+
});
6464

65-
resolve();
66-
} catch (error) {
67-
reject(error);
68-
}
69-
};
65+
resolve();
66+
} catch (error) {
67+
reject(error);
68+
}
69+
};
7070

71-
document.head.append($script);
72-
});
71+
document.head.append($script);
72+
});
7373
}

src/plugins/pluginContext/src/android/Tee.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515

1616
public class Tee extends CordovaPlugin {
1717

18-
private static final Map<String, String> tokenStore = new HashMap<>();
19-
private static final HashSet<String> disclosed = new HashSet<>();
20-
private static final Map<String, List<String>> permissionStore = new HashMap<>();
18+
// pluginId : token
19+
private /*static*/ final Map<String, String> tokenStore = new HashMap<>();
20+
21+
//assined tokens
22+
private /*static*/ final HashSet<String> disclosed = new HashSet<>();
23+
24+
// token : list of permissions
25+
private /*static*/ final Map<String, List<String>> permissionStore = new HashMap<>();
2126

2227
@Override
2328
public boolean execute(String action, JSONArray args, CallbackContext callback)
@@ -62,12 +67,20 @@ public boolean execute(String action, JSONArray args, CallbackContext callback)
6267
return false;
6368
}
6469

65-
private boolean grantedPermission(String token, String permission) {
70+
//============================================================
71+
//do not change function signatures
72+
public boolean isTokenValid(String token, String pluginId) {
73+
String storedToken = tokenStore.get(pluginId);
74+
return storedToken != null && token.equals(storedToken);
75+
}
76+
77+
78+
public boolean grantedPermission(String token, String permission) {
6679
List<String> permissions = permissionStore.get(token);
6780
return permissions != null && permissions.contains(permission);
6881
}
6982

70-
private List<String> listAllPermissions(String token) {
83+
public List<String> listAllPermissions(String token) {
7184
List<String> permissions = permissionStore.get(token);
7285

7386
if (permissions == null) {
@@ -76,6 +89,7 @@ private List<String> listAllPermissions(String token) {
7689

7790
return new ArrayList<>(permissions); // return copy (safe)
7891
}
92+
//============================================================
7993

8094

8195
private synchronized void handleTokenRequest(

src/plugins/pluginContext/www/PluginContext.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
var exec = require("cordova/exec");
22

33
const PluginContext = (function () {
4-
const _state = new WeakMap();
5-
4+
//=============================
65
class _PluginContext {
7-
time = Date.now();
8-
constructor(pluginId, uuid) {
9-
_state.set(this, { pluginId, uuid });
6+
constructor(uuid) {
7+
this.created_at = Date.now();
8+
this.uuid = uuid;
109
Object.freeze(this);
1110
}
11+
12+
toString() {
13+
return this.uuid;
14+
}
15+
16+
[Symbol.toPrimitive](hint) {
17+
if (hint === "number") {
18+
return NaN; // prevent numeric coercion
19+
}
20+
return this.uuid;
21+
}
22+
1223
grantedPermission(permission) {
13-
const state = _state.get(this);
1424
return new Promise((resolve, reject) => {
1525
exec(resolve, reject, "Tee", "grantedPermission", [
16-
state.uuid,
26+
this.uuid,
1727
permission,
1828
]);
1929
});
2030
}
2131

2232
listAllPermissions() {
23-
const state = _state.get(this);
2433
return new Promise((resolve, reject) => {
25-
exec(resolve, reject, "Tee", "listAllPermissions", [state.uuid]);
34+
exec(resolve, reject, "Tee", "listAllPermissions", [this.uuid]);
2635
});
2736
}
2837
}
2938

3039
//Object.freeze(this);
3140

41+
//===============================
42+
3243
return {
3344
generate: async function (pluginId, pluginJson) {
3445
try {
@@ -42,11 +53,9 @@ const PluginContext = (function () {
4253
}
4354

4455
const uuid = await requestToken(pluginId);
45-
return new _PluginContext(pluginId, uuid);
56+
return new _PluginContext(uuid);
4657
} catch (err) {
47-
console.warn(
48-
`PluginContext generation failed for pluginId ${pluginId}:`,
49-
);
58+
console.warn(`PluginContext creation failed for pluginId ${pluginId}:`);
5059
return null;
5160
}
5261
},

0 commit comments

Comments
 (0)