-
Notifications
You must be signed in to change notification settings - Fork 959
/
CapacitorCookies.java
122 lines (105 loc) · 4.09 KB
/
CapacitorCookies.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.getcapacitor.plugin;
import android.webkit.JavascriptInterface;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginConfig;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.HttpCookie;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
@CapacitorPlugin
public class CapacitorCookies extends Plugin {
CapacitorCookieManager cookieManager;
@Override
public void load() {
this.bridge.getWebView().addJavascriptInterface(this, "CapacitorCookiesAndroidInterface");
this.cookieManager = new CapacitorCookieManager(null, java.net.CookiePolicy.ACCEPT_ALL, this.bridge);
this.cookieManager.removeSessionCookies();
CookieHandler.setDefault(this.cookieManager);
super.load();
}
@Override
protected void handleOnDestroy() {
super.handleOnDestroy();
this.cookieManager.removeSessionCookies();
}
@JavascriptInterface
public boolean isEnabled() {
PluginConfig pluginConfig = getBridge().getConfig().getPluginConfiguration("CapacitorCookies");
return pluginConfig.getBoolean("enabled", false);
}
@JavascriptInterface
public void setCookie(String domain, String action) {
cookieManager.setCookie(domain, action);
}
@PluginMethod
public void getCookies(PluginCall call) {
this.bridge.eval(
"document.cookie",
value -> {
String cookies = value.substring(1, value.length() - 1);
String[] cookieArray = cookies.split(";");
JSObject cookieMap = new JSObject();
for (String cookie : cookieArray) {
if (cookie.length() > 0) {
String[] keyValue = cookie.split("=", 2);
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String val = keyValue[1].trim();
try {
key = URLDecoder.decode(keyValue[0].trim(), StandardCharsets.UTF_8.name());
val = URLDecoder.decode(keyValue[1].trim(), StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ignored) {}
cookieMap.put(key, val);
}
}
}
call.resolve(cookieMap);
}
);
}
@PluginMethod
public void setCookie(PluginCall call) {
String key = call.getString("key");
if (null == key) {
call.reject("Must provide key");
}
String value = call.getString("value");
if (null == value) {
call.reject("Must provide value");
}
String url = call.getString("url");
String expires = call.getString("expires", "");
String path = call.getString("path", "/");
cookieManager.setCookie(url, key, value, expires, path);
call.resolve();
}
@PluginMethod
public void deleteCookie(PluginCall call) {
String key = call.getString("key");
if (null == key) {
call.reject("Must provide key");
}
String url = call.getString("url");
cookieManager.setCookie(url, key + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
call.resolve();
}
@PluginMethod
public void clearCookies(PluginCall call) {
String url = call.getString("url");
HttpCookie[] cookies = cookieManager.getCookies(url);
for (HttpCookie cookie : cookies) {
cookieManager.setCookie(url, cookie.getName() + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
}
call.resolve();
}
@PluginMethod
public void clearAllCookies(PluginCall call) {
cookieManager.removeAllCookies();
call.resolve();
}
}