cj-tauri 2.1.0 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -115
- package/cjpm.toml +1 -1
- package/cli/cjpm.toml +1 -1
- package/cli/src/manifest.cj +13 -2
- package/cli/src/project.cj +1 -1
- package/cli/src/sync.cj +213 -4
- package/native/core/bridge_core.c +26 -0
- package/native/core/bridge_core.h +22 -0
- package/native/platforms/linux/bridge_linux.c +247 -5
- package/native/platforms/windows/bridge_win.c +405 -2
- package/native/tests/test_bridge_core.c +49 -1
- package/package.json +4 -4
- package/src/app.cj +10 -0
- package/src/cookies_bridge.cj +42 -0
- package/src/host.cj +14 -0
- package/src/host_webkit.cj +25 -0
- package/src/host_webview2.cj +25 -0
- package/src/ipc_hub.cj +66 -2
- package/src/tests/app_spec_test.cj +5 -1
- package/src/tests/fake_host.cj +8 -0
- package/src/tests/ipc_hub_test.cj +76 -0
- package/src/tests/platform_contract_test.cj +15 -2
- package/src/version.cj +1 -1
- package/prebuilt/linux-x64/cj-tauri +0 -0
|
@@ -1077,6 +1077,404 @@ int cj_plat_post_ui(cj_host *h, cj_ui_fn fn, void *ud) {
|
|
|
1077
1077
|
return 1;
|
|
1078
1078
|
}
|
|
1079
1079
|
|
|
1080
|
+
/* =========================================================================
|
|
1081
|
+
* Cookie 读取(「一键导出各平台登录态」类工具)
|
|
1082
|
+
*
|
|
1083
|
+
* 为什么必须走原生:httpOnly。知乎 z_c0、掘金 sessionid 这类会话令牌都带 httpOnly,
|
|
1084
|
+
* 注入 JS 读 document.cookie 拿不到——导出「字段看着齐全、发文却 401」的文件比导不出来更难查。
|
|
1085
|
+
* WebView2 的 ICoreWebView2CookieManager::GetCookies 给的是全量快照(含 httpOnly 属性)。
|
|
1086
|
+
*
|
|
1087
|
+
* 形态与 Linux 侧同形:worker 线程 → 投 UI 线程 → 异步读 → 回填 JSON → 唤醒(带 5s 超时)。
|
|
1088
|
+
* GetCookies 按 uri 匹配 cookie 域,所以 filter 拼成 https://<filter>/;空 filter 传**空 uri**
|
|
1089
|
+
* (SDK 契约:uri 为空串/空指针时返回同 profile 下全部 cookie),不按 uri 过滤。
|
|
1090
|
+
* ========================================================================= */
|
|
1091
|
+
|
|
1092
|
+
typedef struct cookie_call {
|
|
1093
|
+
cj_host *h;
|
|
1094
|
+
char *filter;
|
|
1095
|
+
char *json;
|
|
1096
|
+
int ready;
|
|
1097
|
+
int abandoned; /* 等待方已超时放弃:结构由异步回调自己回收 */
|
|
1098
|
+
CRITICAL_SECTION cs;
|
|
1099
|
+
CONDITION_VARIABLE cv;
|
|
1100
|
+
} cookie_call;
|
|
1101
|
+
|
|
1102
|
+
/* 极简动态字符串:Windows 侧没有 glib,为几段 JSON 引一个库不值当 */
|
|
1103
|
+
typedef struct cookie_sbuf {
|
|
1104
|
+
char *p;
|
|
1105
|
+
size_t len;
|
|
1106
|
+
size_t cap;
|
|
1107
|
+
} cookie_sbuf;
|
|
1108
|
+
|
|
1109
|
+
static int cookie_sb_add(cookie_sbuf *b, const char *s, size_t n) {
|
|
1110
|
+
if (b->len + n + 1 > b->cap) {
|
|
1111
|
+
size_t cap = b->cap ? b->cap : 1024;
|
|
1112
|
+
char *np;
|
|
1113
|
+
while (cap < b->len + n + 1) cap *= 2;
|
|
1114
|
+
np = (char *)realloc(b->p, cap);
|
|
1115
|
+
if (!np) return 0;
|
|
1116
|
+
b->p = np;
|
|
1117
|
+
b->cap = cap;
|
|
1118
|
+
}
|
|
1119
|
+
memcpy(b->p + b->len, s, n);
|
|
1120
|
+
b->len += n;
|
|
1121
|
+
b->p[b->len] = '\0';
|
|
1122
|
+
return 1;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
static int cookie_sb_puts(cookie_sbuf *b, const char *s) {
|
|
1126
|
+
return cookie_sb_add(b, s ? s : "", s ? strlen(s) : 0);
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/* JSON 转义写入(>=0x20 的字节原样输出,UTF-8 多字节序列不会被破坏)*/
|
|
1130
|
+
static int cookie_sb_json(cookie_sbuf *b, const char *s) {
|
|
1131
|
+
const unsigned char *p = (const unsigned char *)(s ? s : "");
|
|
1132
|
+
if (!cookie_sb_add(b, "\"", 1)) return 0;
|
|
1133
|
+
for (; *p; p++) {
|
|
1134
|
+
char esc[8];
|
|
1135
|
+
switch (*p) {
|
|
1136
|
+
case '"': if (!cookie_sb_add(b, "\\\"", 2)) return 0; break;
|
|
1137
|
+
case '\\': if (!cookie_sb_add(b, "\\\\", 2)) return 0; break;
|
|
1138
|
+
case '\n': if (!cookie_sb_add(b, "\\n", 2)) return 0; break;
|
|
1139
|
+
case '\r': if (!cookie_sb_add(b, "\\r", 2)) return 0; break;
|
|
1140
|
+
case '\t': if (!cookie_sb_add(b, "\\t", 2)) return 0; break;
|
|
1141
|
+
default:
|
|
1142
|
+
if (*p < 0x20) {
|
|
1143
|
+
int n = snprintf(esc, sizeof(esc), "\\u%04x", (unsigned)*p);
|
|
1144
|
+
if (n > 0 && !cookie_sb_add(b, esc, (size_t)n)) return 0;
|
|
1145
|
+
} else if (!cookie_sb_add(b, (const char *)p, 1)) {
|
|
1146
|
+
return 0;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
return cookie_sb_add(b, "\"", 1);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
static char *cookie_dup(const char *s) {
|
|
1154
|
+
size_t n;
|
|
1155
|
+
char *p;
|
|
1156
|
+
if (!s) s = "";
|
|
1157
|
+
n = strlen(s);
|
|
1158
|
+
p = (char *)malloc(n + 1);
|
|
1159
|
+
if (!p) return NULL;
|
|
1160
|
+
memcpy(p, s, n + 1);
|
|
1161
|
+
return p;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
static void cookie_call_free(cookie_call *c) {
|
|
1165
|
+
if (!c) return;
|
|
1166
|
+
free(c->filter);
|
|
1167
|
+
free(c->json);
|
|
1168
|
+
DeleteCriticalSection(&c->cs);
|
|
1169
|
+
free(c);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/* 结果回填:等待方还在就交出去;已超时放弃就由这里回收结构 */
|
|
1173
|
+
static void cookie_call_set_result(cookie_call *c, char *json) {
|
|
1174
|
+
int abandon;
|
|
1175
|
+
EnterCriticalSection(&c->cs);
|
|
1176
|
+
abandon = c->abandoned;
|
|
1177
|
+
if (!abandon) {
|
|
1178
|
+
c->json = json;
|
|
1179
|
+
c->ready = 1;
|
|
1180
|
+
WakeConditionVariable(&c->cv);
|
|
1181
|
+
}
|
|
1182
|
+
LeaveCriticalSection(&c->cs);
|
|
1183
|
+
if (abandon) {
|
|
1184
|
+
free(json);
|
|
1185
|
+
cookie_call_free(c);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
static void cookie_call_set_error(cookie_call *c, const char *msg) {
|
|
1190
|
+
cookie_sbuf b;
|
|
1191
|
+
memset(&b, 0, sizeof(b));
|
|
1192
|
+
if (!cookie_sb_puts(&b, "{\"ok\":false,\"error\":") || !cookie_sb_json(&b, msg) ||
|
|
1193
|
+
!cookie_sb_puts(&b, "}")) {
|
|
1194
|
+
free(b.p);
|
|
1195
|
+
cookie_call_set_result(c, cookie_dup("{\"ok\":false,\"error\":\"out of memory\"}"));
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
cookie_call_set_result(c, b.p);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
/* --- 完成 handler(真引用计数,最后一个 Release 才释放)---
|
|
1202
|
+
⚠ 不要改回「Invoke 里 free(self)」的一次性写法:WebView2 在 Invoke 返回后仍会
|
|
1203
|
+
AddRef/Release 这个对象,而对象已经 free 掉——实测(2026-11-05)表现是
|
|
1204
|
+
`0xc0000005` 崩在 EmbeddedBrowserWebView.dll 里,随后所有 GetCookies 都不再回调
|
|
1205
|
+
(worker 侧只看到 5s 超时)。uri 也挂在 handler 上,让它和对象同生共死。 */
|
|
1206
|
+
|
|
1207
|
+
typedef struct cookies_hr {
|
|
1208
|
+
ICoreWebView2GetCookiesCompletedHandler iface;
|
|
1209
|
+
cookie_call *call;
|
|
1210
|
+
wchar_t *uri;
|
|
1211
|
+
LONG refs;
|
|
1212
|
+
} cookies_hr;
|
|
1213
|
+
|
|
1214
|
+
static HRESULT STDMETHODCALLTYPE cookies_QueryInterface(
|
|
1215
|
+
ICoreWebView2GetCookiesCompletedHandler *This, REFIID riid, void **ppvObject) {
|
|
1216
|
+
(void)riid;
|
|
1217
|
+
if (ppvObject) *ppvObject = This;
|
|
1218
|
+
return S_OK;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
static ULONG STDMETHODCALLTYPE cookies_AddRef(ICoreWebView2GetCookiesCompletedHandler *This) {
|
|
1222
|
+
cookies_hr *self = (cookies_hr *)This;
|
|
1223
|
+
LONG n = InterlockedIncrement(&self->refs);
|
|
1224
|
+
fprintf(stderr, "[cj-bridge] cookies: handler addref -> %ld\n", (long)n);
|
|
1225
|
+
return (ULONG)n;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
static ULONG STDMETHODCALLTYPE cookies_Release(ICoreWebView2GetCookiesCompletedHandler *This) {
|
|
1229
|
+
cookies_hr *self = (cookies_hr *)This;
|
|
1230
|
+
LONG left = InterlockedDecrement(&self->refs);
|
|
1231
|
+
fprintf(stderr, "[cj-bridge] cookies: handler release -> %ld\n", (long)left);
|
|
1232
|
+
if (left == 0) {
|
|
1233
|
+
free(self->uri);
|
|
1234
|
+
free(self);
|
|
1235
|
+
}
|
|
1236
|
+
return (ULONG)left;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
static HRESULT STDMETHODCALLTYPE cookies_Invoke(
|
|
1240
|
+
ICoreWebView2GetCookiesCompletedHandler *This, HRESULT errorCode,
|
|
1241
|
+
ICoreWebView2CookieList *result) {
|
|
1242
|
+
cookies_hr *self = (cookies_hr *)This;
|
|
1243
|
+
cookie_call *c = self ? self->call : NULL;
|
|
1244
|
+
cookie_sbuf b;
|
|
1245
|
+
UINT count = 0;
|
|
1246
|
+
UINT i;
|
|
1247
|
+
int first = 1;
|
|
1248
|
+
int ok = 1;
|
|
1249
|
+
|
|
1250
|
+
if (!c) {
|
|
1251
|
+
This->lpVtbl->Release(This);
|
|
1252
|
+
return S_OK;
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
if (FAILED(errorCode) || !result) {
|
|
1256
|
+
char msg[96];
|
|
1257
|
+
snprintf(msg, sizeof(msg), "cookie read failed: hr=0x%08lx", (unsigned long)errorCode);
|
|
1258
|
+
fprintf(stderr, "[cj-bridge] cookies: complete failed hr=0x%08lx\n",
|
|
1259
|
+
(unsigned long)errorCode);
|
|
1260
|
+
cookie_call_set_error(c, msg);
|
|
1261
|
+
/* 不要 Release(result):回调参数是**借用**给 handler 的,运行时自己管它的引用。
|
|
1262
|
+
释放它会让 WebView2 随后用到已销毁的 cookie list——实测崩在
|
|
1263
|
+
EmbeddedBrowserWebView.dll 里 0xc0000005(2026-11-05,两次同偏移)。 */
|
|
1264
|
+
This->lpVtbl->Release(This);
|
|
1265
|
+
return S_OK;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
if (FAILED(result->lpVtbl->get_Count(result, &count))) count = 0;
|
|
1269
|
+
fprintf(stderr, "[cj-bridge] cookies: complete count=%u\n", (unsigned)count);
|
|
1270
|
+
|
|
1271
|
+
memset(&b, 0, sizeof(b));
|
|
1272
|
+
{
|
|
1273
|
+
/* 前缀固定 31 字节 + 最多 10 位计数:32 会截断掉 "cookies": [,得到非法 JSON */
|
|
1274
|
+
char num[64];
|
|
1275
|
+
snprintf(num, sizeof(num), "{\"ok\":true,\"count\":%u,\"cookies\":[", (unsigned)count);
|
|
1276
|
+
ok = cookie_sb_puts(&b, num);
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
for (i = 0; ok && i < count; i++) {
|
|
1280
|
+
ICoreWebView2Cookie *ck = NULL;
|
|
1281
|
+
LPWSTR wn = NULL, wv = NULL, wd = NULL, wp = NULL;
|
|
1282
|
+
char *n, *v, *d, *pa;
|
|
1283
|
+
BOOL http_only = FALSE, secure = FALSE, is_session = FALSE;
|
|
1284
|
+
DOUBLE expires = 0.0;
|
|
1285
|
+
char num[80];
|
|
1286
|
+
|
|
1287
|
+
if (FAILED(result->lpVtbl->GetValueAtIndex(result, i, &ck)) || !ck) continue;
|
|
1288
|
+
|
|
1289
|
+
ck->lpVtbl->get_Name(ck, &wn);
|
|
1290
|
+
ck->lpVtbl->get_Value(ck, &wv);
|
|
1291
|
+
ck->lpVtbl->get_Domain(ck, &wd);
|
|
1292
|
+
ck->lpVtbl->get_Path(ck, &wp);
|
|
1293
|
+
ck->lpVtbl->get_IsHttpOnly(ck, &http_only);
|
|
1294
|
+
ck->lpVtbl->get_IsSecure(ck, &secure);
|
|
1295
|
+
ck->lpVtbl->get_IsSession(ck, &is_session);
|
|
1296
|
+
ck->lpVtbl->get_Expires(ck, &expires);
|
|
1297
|
+
|
|
1298
|
+
n = wide_to_utf8(wn);
|
|
1299
|
+
v = wide_to_utf8(wv);
|
|
1300
|
+
d = wide_to_utf8(wd);
|
|
1301
|
+
pa = wide_to_utf8(wp);
|
|
1302
|
+
|
|
1303
|
+
if (!first) ok = cookie_sb_add(&b, ",", 1);
|
|
1304
|
+
first = 0;
|
|
1305
|
+
ok = ok && cookie_sb_puts(&b, "{\"name\":") && cookie_sb_json(&b, n)
|
|
1306
|
+
&& cookie_sb_puts(&b, ",\"value\":") && cookie_sb_json(&b, v)
|
|
1307
|
+
&& cookie_sb_puts(&b, ",\"domain\":") && cookie_sb_json(&b, d)
|
|
1308
|
+
&& cookie_sb_puts(&b, ",\"path\":") && cookie_sb_json(&b, pa)
|
|
1309
|
+
&& cookie_sb_puts(&b, secure ? ",\"secure\":true" : ",\"secure\":false")
|
|
1310
|
+
&& cookie_sb_puts(&b, http_only ? ",\"httpOnly\":true" : ",\"httpOnly\":false");
|
|
1311
|
+
if (ok) {
|
|
1312
|
+
if (is_session || expires <= 0.0) {
|
|
1313
|
+
snprintf(num, sizeof(num), ",\"expires\":0,\"session\":true");
|
|
1314
|
+
} else {
|
|
1315
|
+
snprintf(num, sizeof(num), ",\"expires\":%lld", (long long)expires);
|
|
1316
|
+
}
|
|
1317
|
+
ok = cookie_sb_puts(&b, num);
|
|
1318
|
+
}
|
|
1319
|
+
ok = ok && cookie_sb_add(&b, "}", 1);
|
|
1320
|
+
|
|
1321
|
+
if (wn) CoTaskMemFree(wn);
|
|
1322
|
+
if (wv) CoTaskMemFree(wv);
|
|
1323
|
+
if (wd) CoTaskMemFree(wd);
|
|
1324
|
+
if (wp) CoTaskMemFree(wp);
|
|
1325
|
+
free(n);
|
|
1326
|
+
free(v);
|
|
1327
|
+
free(d);
|
|
1328
|
+
free(pa);
|
|
1329
|
+
ck->lpVtbl->Release(ck);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
ok = ok && cookie_sb_puts(&b, "]}");
|
|
1333
|
+
/* 同上:result 是借用的,不在这里 Release */
|
|
1334
|
+
|
|
1335
|
+
if (!ok) {
|
|
1336
|
+
free(b.p);
|
|
1337
|
+
cookie_call_set_error(c, "out of memory building cookie json");
|
|
1338
|
+
This->lpVtbl->Release(This);
|
|
1339
|
+
return S_OK;
|
|
1340
|
+
}
|
|
1341
|
+
cookie_call_set_result(c, b.p);
|
|
1342
|
+
This->lpVtbl->Release(This);
|
|
1343
|
+
return S_OK;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/* 与 g_ctrl_vtbl / g_drop_vtbl 同款:非 const,直接挂到 iface.lpVtbl 上 */
|
|
1347
|
+
static ICoreWebView2GetCookiesCompletedHandlerVtbl cookies_handler_vtbl = {
|
|
1348
|
+
cookies_QueryInterface,
|
|
1349
|
+
cookies_AddRef,
|
|
1350
|
+
cookies_Release,
|
|
1351
|
+
cookies_Invoke
|
|
1352
|
+
};
|
|
1353
|
+
|
|
1354
|
+
/* UI 线程:发起 GetCookies */
|
|
1355
|
+
static void cookie_ui_start(void *ud) {
|
|
1356
|
+
cookie_call *c = (cookie_call *)ud;
|
|
1357
|
+
plat_host *p = (c && c->h) ? (plat_host *)c->h->plat : NULL;
|
|
1358
|
+
ICoreWebView2_2 *wv2 = NULL;
|
|
1359
|
+
ICoreWebView2CookieManager *cm = NULL;
|
|
1360
|
+
wchar_t *uri = NULL;
|
|
1361
|
+
cookies_hr *hr;
|
|
1362
|
+
|
|
1363
|
+
if (!c) return;
|
|
1364
|
+
if (!p || !p->webview) {
|
|
1365
|
+
cookie_call_set_error(c, "webview not ready");
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
fprintf(stderr, "[cj-bridge] cookies: ui thread running read\n");
|
|
1369
|
+
if (FAILED(p->webview->lpVtbl->QueryInterface(
|
|
1370
|
+
p->webview, &IID_ICoreWebView2_2, (void **)&wv2)) || !wv2) {
|
|
1371
|
+
cookie_call_set_error(c, "ICoreWebView2_2 unavailable (cookie store needs WebView2 88+)");
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
if (FAILED(wv2->lpVtbl->get_CookieManager(wv2, &cm)) || !cm) {
|
|
1375
|
+
wv2->lpVtbl->Release(wv2);
|
|
1376
|
+
cookie_call_set_error(c, "cookie manager unavailable");
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
if (c->filter && *c->filter) {
|
|
1381
|
+
cookie_sbuf b;
|
|
1382
|
+
memset(&b, 0, sizeof(b));
|
|
1383
|
+
if (cookie_sb_puts(&b, "https://") && cookie_sb_puts(&b, c->filter) &&
|
|
1384
|
+
cookie_sb_puts(&b, "/")) {
|
|
1385
|
+
uri = utf8_to_wide(b.p);
|
|
1386
|
+
}
|
|
1387
|
+
free(b.p);
|
|
1388
|
+
} else {
|
|
1389
|
+
/* 空 filter = 「同 profile 下的全部 cookie」,**uri 必须给空串**:SDK 文档明写
|
|
1390
|
+
「If uri is empty string or null, all cookies under the same profile are returned」。
|
|
1391
|
+
别回落到当前页 URL——本类工具的主页面是 NavigateToString 出来的 about:blank,
|
|
1392
|
+
拿它去匹配一个站点都命不中,已登录也读出 count=0(2026-11-05 实机复现:
|
|
1393
|
+
内嵌视图登录 CSDN 后 probe 仍报「还没有 cookie」)。Linux 侧走 get_all_cookies,
|
|
1394
|
+
本来就不按 uri 过滤,两平台在此口径一致。 */
|
|
1395
|
+
uri = utf8_to_wide("");
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
hr = (cookies_hr *)calloc(1, sizeof(*hr));
|
|
1399
|
+
if (!hr || !uri) {
|
|
1400
|
+
free(hr);
|
|
1401
|
+
free(uri);
|
|
1402
|
+
cm->lpVtbl->Release(cm);
|
|
1403
|
+
wv2->lpVtbl->Release(wv2);
|
|
1404
|
+
cookie_call_set_error(c, "out of memory building cookie request");
|
|
1405
|
+
return;
|
|
1406
|
+
}
|
|
1407
|
+
/* uri 交给 handler 持有:异步读期间它得活着,随 handler 的最后一个 Release 释放 */
|
|
1408
|
+
hr->iface.lpVtbl = &cookies_handler_vtbl;
|
|
1409
|
+
hr->call = c;
|
|
1410
|
+
hr->uri = uri;
|
|
1411
|
+
hr->refs = 1;
|
|
1412
|
+
{
|
|
1413
|
+
HRESULT ghr = cm->lpVtbl->GetCookies(cm, uri, &hr->iface);
|
|
1414
|
+
cm->lpVtbl->Release(cm);
|
|
1415
|
+
wv2->lpVtbl->Release(wv2);
|
|
1416
|
+
if (FAILED(ghr)) {
|
|
1417
|
+
fprintf(stderr, "[cj-bridge] cookies: GetCookies failed hr=0x%08lx\n",
|
|
1418
|
+
(unsigned long)ghr);
|
|
1419
|
+
cookie_call_set_error(c, "GetCookies call failed");
|
|
1420
|
+
hr->iface.lpVtbl->Release(&hr->iface);
|
|
1421
|
+
return;
|
|
1422
|
+
}
|
|
1423
|
+
/* 我们这一份引用留到 Invoke 里交还:先不放。放早了(若运行时其实没 AddRef)
|
|
1424
|
+
就是「回调里还被指着的对象已经销毁」,与 free(self) 那条同一种崩法。 */
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
char *cj_plat_get_cookies(cj_host *h, const char *filter) {
|
|
1429
|
+
plat_host *p = h ? (plat_host *)h->plat : NULL;
|
|
1430
|
+
cookie_call *c;
|
|
1431
|
+
char *json = NULL;
|
|
1432
|
+
|
|
1433
|
+
if (!p || !p->webview) {
|
|
1434
|
+
return cookie_dup("{\"ok\":false,\"error\":\"webview not ready\"}");
|
|
1435
|
+
}
|
|
1436
|
+
if (p->thread_set && GetCurrentThreadId() == p->thread_id) {
|
|
1437
|
+
/* 等待的事件得由 UI 线程处理:站在这里阻塞 = 自己把自己锁死 */
|
|
1438
|
+
return cookie_dup("{\"ok\":false,\"error\":\"get_cookies must be called from a worker thread\"}");
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
c = (cookie_call *)calloc(1, sizeof(*c));
|
|
1442
|
+
if (!c) return cookie_dup("{\"ok\":false,\"error\":\"out of memory\"}");
|
|
1443
|
+
c->h = h;
|
|
1444
|
+
c->filter = cookie_dup(filter ? filter : "");
|
|
1445
|
+
if (!c->filter) {
|
|
1446
|
+
free(c);
|
|
1447
|
+
return cookie_dup("{\"ok\":false,\"error\":\"out of memory\"}");
|
|
1448
|
+
}
|
|
1449
|
+
InitializeCriticalSection(&c->cs);
|
|
1450
|
+
InitializeConditionVariable(&c->cv);
|
|
1451
|
+
|
|
1452
|
+
if (!cj_plat_post_ui(h, cookie_ui_start, c)) {
|
|
1453
|
+
cookie_call_free(c);
|
|
1454
|
+
return cookie_dup("{\"ok\":false,\"error\":\"ui thread unavailable\"}");
|
|
1455
|
+
}
|
|
1456
|
+
fprintf(stderr, "[cj-bridge] cookies: posted read filter=\"%s\"\n",
|
|
1457
|
+
(c->filter && *c->filter) ? c->filter : "(all)");
|
|
1458
|
+
|
|
1459
|
+
EnterCriticalSection(&c->cs);
|
|
1460
|
+
while (!c->ready) {
|
|
1461
|
+
if (!SleepConditionVariableCS(&c->cv, &c->cs, 5000)) break; /* 超时:放弃等待 */
|
|
1462
|
+
}
|
|
1463
|
+
if (c->ready) {
|
|
1464
|
+
json = c->json;
|
|
1465
|
+
c->json = NULL;
|
|
1466
|
+
LeaveCriticalSection(&c->cs);
|
|
1467
|
+
cookie_call_free(c);
|
|
1468
|
+
} else {
|
|
1469
|
+
/* 超时不能释放结构:异步回调可能还在路上,交给它回收 */
|
|
1470
|
+
c->abandoned = 1;
|
|
1471
|
+
LeaveCriticalSection(&c->cs);
|
|
1472
|
+
fprintf(stderr, "[cj-bridge] cookies: timeout after 5000ms, abandoning\n");
|
|
1473
|
+
json = cookie_dup("{\"ok\":false,\"error\":\"timeout waiting for cookie store\"}");
|
|
1474
|
+
}
|
|
1475
|
+
return json;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1080
1478
|
/* ===== 剪贴板(plugin_clipboard 的平台层):Win32 clipboard =====
|
|
1081
1479
|
调用方保证已在宿主 UI 线程上(core 的 UI 线程分流负责)。OpenClipboard 带重试:
|
|
1082
1480
|
剪贴板是系统单例,别的进程可能正占着(MSDN 建议的做法)。 */
|
|
@@ -1549,6 +1947,13 @@ static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
|
|
|
1549
1947
|
return 0;
|
|
1550
1948
|
}
|
|
1551
1949
|
case WM_DESTROY:
|
|
1950
|
+
/* 先置 should_quit + 触发 onDestroy(每个宿主恰好一次)+ 作废进行中的对话框。
|
|
1951
|
+
必须排在下面的 view_close 清理**之前**:view_close 是同步 COM 调用,某个内嵌视图
|
|
1952
|
+
的网页无响应(Edge 渲染进程挂起)时它会卡在宿主线程上——窗口已经没了(视觉上
|
|
1953
|
+
「关闭了」),但 should_quit 没置位,仓颉侧 waitForExit 的轮询就永远等下去,
|
|
1954
|
+
表象是「界面关了进程还在」(2026-10-26 偶发实测)。提前置位后,清理慢就慢,
|
|
1955
|
+
仓颉 main 返回时进程退出会终止全部线程,不再永久残留。 */
|
|
1956
|
+
cj_core_window_destroyed(h);
|
|
1552
1957
|
if (p) {
|
|
1553
1958
|
/* 托盘图标先摘:不摘的话进程退出后系统托盘里会留一个点不动的幽灵图标 */
|
|
1554
1959
|
tray_remove(p);
|
|
@@ -1569,8 +1974,6 @@ static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
|
|
|
1569
1974
|
}
|
|
1570
1975
|
p->hwnd = NULL; /* 窗口没了:别再往它 PostMessage */
|
|
1571
1976
|
}
|
|
1572
|
-
/* 置 should_quit + 触发 onDestroy(每个宿主恰好一次)+ 作废进行中的对话框 */
|
|
1573
|
-
cj_core_window_destroyed(h);
|
|
1574
1977
|
PostQuitMessage(0);
|
|
1575
1978
|
return 0;
|
|
1576
1979
|
default:
|
|
@@ -476,6 +476,21 @@ int cj_plat_pid_alive(long long pid) {
|
|
|
476
476
|
return 0; /* 桩平台没有真进程 */
|
|
477
477
|
}
|
|
478
478
|
|
|
479
|
+
/* ===== Cookie 读取桩:桩平台没有真 cookie store,只为让 core 的**入口校验与转发**可调可断言 =====
|
|
480
|
+
「投 UI 线程 → 异步读 → 回填唤醒」那条路由 + 超时兜底由平台实机验证(Linux / Windows 各一份)。 */
|
|
481
|
+
static int g_cookie_calls = 0;
|
|
482
|
+
static char *g_cookie_last_filter = NULL;
|
|
483
|
+
static int g_cookie_null = 0; /* 扳成 1 时模拟「平台回 NULL」*/
|
|
484
|
+
|
|
485
|
+
char *cj_plat_get_cookies(cj_host *h, const char *filter) {
|
|
486
|
+
(void)h;
|
|
487
|
+
g_cookie_calls++;
|
|
488
|
+
free(g_cookie_last_filter);
|
|
489
|
+
g_cookie_last_filter = strdup(filter ? filter : "");
|
|
490
|
+
if (g_cookie_null) return NULL;
|
|
491
|
+
return strdup("{\"ok\":true,\"count\":0,\"cookies\":[]}");
|
|
492
|
+
}
|
|
493
|
+
|
|
479
494
|
/* ===== 内嵌资源桩:桩平台没有 PE 资源表,如实返回 0 或留一句原因,
|
|
480
495
|
让 core 的三条分支(参数校验 / 无资源 / 失败)都能在无 SDK 的机器上被断言。 ===== */
|
|
481
496
|
int cj_plat_extract_assets(const char *dir, char *err, int err_len) {
|
|
@@ -1267,7 +1282,40 @@ int main(void) {
|
|
|
1267
1282
|
CHECK(cj_bridge_pid_alive(0) == 0, "pid_alive(0) 恒为 0(不落到平台原语)");
|
|
1268
1283
|
}
|
|
1269
1284
|
|
|
1270
|
-
/* =====
|
|
1285
|
+
/* ===== Cookie 读取:core 只做入口校验与转发(投递 / 等待 / 超时在平台层,实机验证) ===== */
|
|
1286
|
+
{
|
|
1287
|
+
char *j;
|
|
1288
|
+
case_begin("get cookies");
|
|
1289
|
+
g_cookie_calls = 0;
|
|
1290
|
+
j = cj_bridge_get_cookies(NULL, "");
|
|
1291
|
+
CHECK(j != NULL && strstr(j, "host not ready") != NULL,
|
|
1292
|
+
"host 为 NULL 时回失败 JSON(不落平台原语)");
|
|
1293
|
+
CHECK(g_cookie_calls == 0, "host 不可用时不触达平台原语");
|
|
1294
|
+
cj_bridge_cookies_free(j);
|
|
1295
|
+
|
|
1296
|
+
h->ready = 1;
|
|
1297
|
+
j = cj_bridge_get_cookies(h, NULL);
|
|
1298
|
+
CHECK(j != NULL && strstr(j, "cookies") != NULL, "NULL filter 原样转成空串并转发");
|
|
1299
|
+
CHECK(g_cookie_calls == 1, "就绪后转交平台原语恰好一次");
|
|
1300
|
+
CHECK(g_cookie_last_filter && g_cookie_last_filter[0] == '\0',
|
|
1301
|
+
"NULL filter 落到平台层是空串(= 全量)");
|
|
1302
|
+
cj_bridge_cookies_free(j);
|
|
1303
|
+
|
|
1304
|
+
j = cj_bridge_get_cookies(h, "zhihu.com");
|
|
1305
|
+
CHECK(g_cookie_last_filter && strcmp(g_cookie_last_filter, "zhihu.com") == 0,
|
|
1306
|
+
"filter 原样转发给平台(不做改写)");
|
|
1307
|
+
CHECK(j != NULL && strstr(j, "\"ok\":true") != NULL, "平台 JSON 原样回给上层");
|
|
1308
|
+
cj_bridge_cookies_free(j);
|
|
1309
|
+
|
|
1310
|
+
g_cookie_null = 1;
|
|
1311
|
+
j = cj_bridge_get_cookies(h, "");
|
|
1312
|
+
CHECK(j != NULL && strstr(j, "platform returned null") != NULL,
|
|
1313
|
+
"平台回 NULL 时 core 自己补失败 JSON(绝不给上层空指针)");
|
|
1314
|
+
cj_bridge_cookies_free(j);
|
|
1315
|
+
g_cookie_null = 0;
|
|
1316
|
+
h->ready = 0;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1271
1319
|
{
|
|
1272
1320
|
case_begin("extract assets");
|
|
1273
1321
|
CHECK(cj_bridge_extract_assets(NULL) == -1, "extract_assets(NULL) 被拒");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cj-tauri",
|
|
3
|
-
"version": "2.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.3",
|
|
4
|
+
"description": "Tauri for the Cangjie language: a scaffold CLI for hybrid desktop apps — Cangjie backend (statically compiled) plus a system WebView frontend (HTML/CSS/JS), with a bidirectional IPC bridge and a capability-based security model. Windows, Linux and HarmonyOS PC.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cangjie",
|
|
7
7
|
"cangjie-lang",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"type": "git",
|
|
44
44
|
"url": "git+https://github.com/yangyongzhen/cj-tauri.git"
|
|
45
45
|
},
|
|
46
|
-
"homepage": "https://
|
|
46
|
+
"homepage": "https://github.com/yangyongzhen/cj-tauri#readme",
|
|
47
47
|
"bugs": {
|
|
48
|
-
"url": "https://
|
|
48
|
+
"url": "https://github.com/yangyongzhen/cj-tauri/issues"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public"
|
package/src/app.cj
CHANGED
|
@@ -238,6 +238,16 @@ public class TauriApp <: AppHost {
|
|
|
238
238
|
return this
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
+
/**
|
|
242
|
+
* 注册进串行组(issue #6-④):同 group 的命令严格按到达序执行,不同组 / 普通命令
|
|
243
|
+
* 的并发语义不变。适用于共享一份可变宿主状态的命令族——webtab 的 show/hide/close/bounds
|
|
244
|
+
* 这类「页面快速连点时乱序会让原生状态与 DOM 错位」的形状。链式返回 this,与 register 一致。
|
|
245
|
+
*/
|
|
246
|
+
public func registerSerial(cmd: String, handler: CommandHandler, group: String): TauriApp {
|
|
247
|
+
this.hub.registerSerial(cmd, handler, group)
|
|
248
|
+
return this
|
|
249
|
+
}
|
|
250
|
+
|
|
241
251
|
/**
|
|
242
252
|
* 装配插件(对标 Tauri v2 的 .plugin(...))
|
|
243
253
|
*
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
package cjTauri
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cookie 读取桥(「一键导出各平台登录态」类工具的平台面)。
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么必须走原生,而不是注入 JS 读 `document.cookie`
|
|
7
|
+
*
|
|
8
|
+
* **httpOnly**。各平台的会话令牌(知乎 `z_c0`、掘金 `sessionid`、开源中国 `oscid`、
|
|
9
|
+
* 百家号 `BDUSS` …)基本都是 httpOnly Cookie,JS 侧完全看不见。用 `document.cookie`
|
|
10
|
+
* 那条路导出来的文件「字段看着齐全」,发文却 401——比导不出来更难查。cookie store 才是全量快照。
|
|
11
|
+
*
|
|
12
|
+
* ## 平台差异在哪
|
|
13
|
+
*
|
|
14
|
+
* 两平台导出同名同签名(AGENTS §2 的硬约束),差异全在 `native/platforms/` 下的平台文件:
|
|
15
|
+
* Linux = `webkit_cookie_manager_get_all_cookies`(异步,GList<SoupCookie>),
|
|
16
|
+
* Windows = `ICoreWebView2CookieManager::GetCookies`(异步,ICoreWebView2CookieList)。
|
|
17
|
+
*
|
|
18
|
+
* ## 鸿蒙侧为什么整段门禁掉
|
|
19
|
+
*
|
|
20
|
+
* 鸿蒙没有 C 桥产物,这两个符号无处解析;留着的风险不是编译报错,而是**模块加载期
|
|
21
|
+
* SIGSEGV**(同 `clipboard_bridge.cj` 那次白屏,AGENTS §4 最后一条)。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 读宿主 WebView 的 cookie 仓库。返回 **malloc 的 JSON**,必须经 `cj_bridge_cookies_free` 释放。
|
|
26
|
+
*
|
|
27
|
+
* 成功:`{"ok":true,"count":N,"cookies":[{"name","value","domain","path","secure","httpOnly","expires","session"}]}`
|
|
28
|
+
* 失败:`{"ok":false,"error":"…"}` —— 失败也返回 JSON,调用方永远不必判空指针。
|
|
29
|
+
*
|
|
30
|
+
* `filter` = 域名后缀(`"csdn.net"` 命中 `.csdn.net` / `www.csdn.net`),空串 = 全部。
|
|
31
|
+
*
|
|
32
|
+
* **线程口径**:必须在命令 worker 线程调用——内部投 UI 线程发起异步读并带 5s 超时;
|
|
33
|
+
* 站在 UI 线程上调用会直接拿到错误 JSON(等待的事件得由 UI 线程处理,阻塞即自死锁)。
|
|
34
|
+
*/
|
|
35
|
+
@When[env != "ohos"]
|
|
36
|
+
@C
|
|
37
|
+
foreign func cj_bridge_get_cookies(h: CPointer<Unit>, filter: CString): CString
|
|
38
|
+
|
|
39
|
+
/** 释放 get_cookies 返回的缓冲(NULL 安全,幂等) */
|
|
40
|
+
@When[env != "ohos"]
|
|
41
|
+
@C
|
|
42
|
+
foreign func cj_bridge_cookies_free(s: CString): Unit
|
package/src/host.cj
CHANGED
|
@@ -402,6 +402,20 @@ public interface WebViewHost {
|
|
|
402
402
|
*/
|
|
403
403
|
func clipboardReadText(): Option<String>
|
|
404
404
|
|
|
405
|
+
/**
|
|
406
|
+
* 读宿主 WebView 的 cookie 仓库,返回 JSON 文本(**含 httpOnly**——这正是它存在的理由,
|
|
407
|
+
* 会话令牌基本都在 httpOnly 里,`document.cookie` 看不见)。
|
|
408
|
+
*
|
|
409
|
+
* 成功:`{"ok":true,"count":N,"cookies":[{"name","value","domain","path","secure","httpOnly","expires","session"}]}`
|
|
410
|
+
* 失败:`{"ok":false,"error":"…"}` —— 失败也返回合法 JSON,调用方不必判 null/异常。
|
|
411
|
+
*
|
|
412
|
+
* `filter` 是域名后缀(`"csdn.net"` 命中 `.csdn.net`/`www.csdn.net`),空串 = 全部。
|
|
413
|
+
*
|
|
414
|
+
* **线程口径**:在命令 worker 线程调用——内部投 UI 线程发起异步读取并带超时等待;
|
|
415
|
+
* 站在 UI 线程上调用会直接拿到错误 JSON(等待的事件得由 UI 线程自己处理,阻塞即自死锁)。
|
|
416
|
+
*/
|
|
417
|
+
func cookiesJson(filter: String): String
|
|
418
|
+
|
|
405
419
|
/**
|
|
406
420
|
* 发一条系统通知(发后即忘:提交即返回,显示成败不回传)。
|
|
407
421
|
* 本平台未实现时返回 false(`system:host` 的能力位会同口径如实上报)。
|
package/src/host_webkit.cj
CHANGED
|
@@ -523,6 +523,31 @@ public class WebKitHost <: WebViewHost {
|
|
|
523
523
|
return Some(text)
|
|
524
524
|
}
|
|
525
525
|
|
|
526
|
+
/**
|
|
527
|
+
* 读宿主 WebView 的 cookie 仓库(JSON 文本,**含 httpOnly**)。
|
|
528
|
+
*
|
|
529
|
+
* filter = 域名后缀(如 `"csdn.net"`;`".csdn.net"` 也命中),空串 = 全部。
|
|
530
|
+
* 失败也返回 JSON(`{"ok":false,"error":…}`),调用方永远不必判 null。
|
|
531
|
+
*
|
|
532
|
+
* **线程口径**:请在命令 worker 线程调用——桥内部投 UI 线程发起异步读并带 5s 超时;
|
|
533
|
+
* 站在 UI 线程上调用会直接拿到错误 JSON(等待的事件得由 UI 线程处理,阻塞即自死锁)。
|
|
534
|
+
*/
|
|
535
|
+
public func cookiesJson(filter: String): String {
|
|
536
|
+
let f = unsafe { LibC.mallocCString(filter) }
|
|
537
|
+
let cs = unsafe { cj_bridge_get_cookies(this.handle, f) }
|
|
538
|
+
unsafe {
|
|
539
|
+
LibC.free(f) // C 侧只拷贝,不接管入参所有权
|
|
540
|
+
}
|
|
541
|
+
if (cs.isNull()) {
|
|
542
|
+
return "{\"ok\":false,\"error\":\"bridge returned null\"}"
|
|
543
|
+
}
|
|
544
|
+
let json = cs.toString()
|
|
545
|
+
unsafe {
|
|
546
|
+
cj_bridge_cookies_free(cs) // 归还缓冲:malloc 家族分配,必须走桥的 free
|
|
547
|
+
}
|
|
548
|
+
return json
|
|
549
|
+
}
|
|
550
|
+
|
|
526
551
|
public func notify(title: String, body: String): Bool {
|
|
527
552
|
var rc: Int32 = -1
|
|
528
553
|
unsafe {
|
package/src/host_webview2.cj
CHANGED
|
@@ -523,6 +523,31 @@ public class WebView2Host <: WebViewHost {
|
|
|
523
523
|
return Some(text)
|
|
524
524
|
}
|
|
525
525
|
|
|
526
|
+
/**
|
|
527
|
+
* 读宿主 WebView 的 cookie 仓库(JSON 文本,**含 httpOnly**)。
|
|
528
|
+
*
|
|
529
|
+
* filter = 域名后缀(如 `"csdn.net"`;`".csdn.net"` 也命中),空串 = 全部。
|
|
530
|
+
* 失败也返回 JSON(`{"ok":false,"error":…}`),调用方永远不必判 null。
|
|
531
|
+
*
|
|
532
|
+
* **线程口径**:请在命令 worker 线程调用——桥内部投 UI 线程发起异步读并带 5s 超时;
|
|
533
|
+
* 站在 UI 线程上调用会直接拿到错误 JSON(等待的事件得由 UI 线程处理,阻塞即自死锁)。
|
|
534
|
+
*/
|
|
535
|
+
public func cookiesJson(filter: String): String {
|
|
536
|
+
let f = unsafe { LibC.mallocCString(filter) }
|
|
537
|
+
let cs = unsafe { cj_bridge_get_cookies(this.handle, f) }
|
|
538
|
+
unsafe {
|
|
539
|
+
LibC.free(f) // C 侧只拷贝,不接管入参所有权
|
|
540
|
+
}
|
|
541
|
+
if (cs.isNull()) {
|
|
542
|
+
return "{\"ok\":false,\"error\":\"bridge returned null\"}"
|
|
543
|
+
}
|
|
544
|
+
let json = cs.toString()
|
|
545
|
+
unsafe {
|
|
546
|
+
cj_bridge_cookies_free(cs) // 归还缓冲:malloc 家族分配,必须走桥的 free
|
|
547
|
+
}
|
|
548
|
+
return json
|
|
549
|
+
}
|
|
550
|
+
|
|
526
551
|
public func notify(title: String, body: String): Bool {
|
|
527
552
|
var rc: Int32 = -1
|
|
528
553
|
unsafe {
|