cj-tauri 2.1.1 → 2.2.0
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 +41 -6
- package/cjpm.toml +1 -1
- package/cli/cjpm.toml +1 -1
- package/cli/src/project.cj +1 -1
- package/cli/src/sync.cj +105 -8
- package/native/core/bridge_core.c +34 -5
- package/native/core/bridge_core.h +27 -2
- package/native/platforms/linux/bridge_linux.c +253 -8
- package/native/platforms/windows/bridge_win.c +442 -9
- package/native/tests/test_bridge_core.c +76 -4
- package/package.json +1 -1
- package/src/cookies_bridge.cj +42 -0
- package/src/host.cj +14 -0
- package/src/host_webkit.cj +50 -4
- package/src/host_webview2.cj +51 -4
- package/src/tests/fake_host.cj +8 -0
- package/src/version.cj +1 -1
package/README.md
CHANGED
|
@@ -6,11 +6,12 @@ Tauri's trio to Cangjie: a Cangjie backend (statically compiled) plus a system W
|
|
|
6
6
|
(`invoke` / `resolve` / `event`), and a capability-based security model (capability allowlists).
|
|
7
7
|
|
|
8
8
|
- Repository and full documentation: <https://github.com/yangyongzhen/cj-tauri>
|
|
9
|
-
- Current version: **2.1.
|
|
10
|
-
the
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
- Current version: **2.1.3** (new in this release: `scripts/winbuild.sh` now pairs the Windows stdx
|
|
10
|
+
package with the installed `cjc` — a mismatched pair compiles and links fine but aborts at startup,
|
|
11
|
+
and under a GUI subsystem it leaves **no log at all** — plus the `cookie-export` round of Windows
|
|
12
|
+
fixes: `USERPROFILE` fallback for the home directory, an empty cookie filter that reads the whole
|
|
13
|
+
store instead of matching `about:blank`, a borrowed `ICoreWebView2CookieList` that was being
|
|
14
|
+
released, and a cookie reply buffer too small to stay valid JSON)
|
|
14
15
|
- Current status: **Windows (WebView2), Linux (WebKitGTK) and HarmonyOS PC (ArkWeb) all run on real
|
|
15
16
|
hardware**; on the HarmonyOS side the menu capability bit and `fs` / `mqtt` / `redis` / `sqlite` /
|
|
16
17
|
`store` / `http` have been verified on a device
|
|
@@ -165,6 +166,40 @@ Known limitation: the first `dev` / `build` builds the C bridge into the **frame
|
|
|
165
166
|
(that's where the app's `cjpm.toml` link options point), so the package install directory must be writable;
|
|
166
167
|
if you installed under a read-only path, run from inside the repository instead.
|
|
167
168
|
|
|
169
|
+
## Changes in 2.1.3
|
|
170
|
+
|
|
171
|
+
- **Windows single-file builds use the cjc-matched stdx**: `scripts/winbuild.sh` defaulted to the
|
|
172
|
+
`1.0.5.1` Windows stdx while `cjc` was `1.2.0`. That pair builds, links and ships, but the app
|
|
173
|
+
aborts at startup — and with `--subsystem=windows` stderr is not connected, so double-clicking a
|
|
174
|
+
broken exe produced **no log whatsoever**. The default now points at the matched package
|
|
175
|
+
(log line `[winbuild] cjc 1.2.0 + windows stdx 1.2.0.1`) and a guard refuses to build when the
|
|
176
|
+
major/minor versions differ.
|
|
177
|
+
- `examples/cookie-export` Windows fixes: `homeDir()` falls back to `USERPROFILE` (and only wins when
|
|
178
|
+
the directory really exists), an empty cookie filter reads the whole store instead of matching the
|
|
179
|
+
`about:blank` URL, the borrowed `ICoreWebView2CookieList` is no longer released (it crashed the
|
|
180
|
+
WebView2 process with `0xc0000005`), and the cookie reply buffer is large enough to stay valid JSON.
|
|
181
|
+
|
|
182
|
+
For the full list see `[2.1.3] - 2026-11-06` in the repository's `CHANGELOG.md`.
|
|
183
|
+
|
|
184
|
+
## Changes in 2.1.2
|
|
185
|
+
|
|
186
|
+
- **Native CookieManager bridge**: `WebViewHost.cookiesJson(filter)` (C side `cj_bridge_get_cookies`)
|
|
187
|
+
returns the whole cookie store as `{"ok":true,"cookies":[…]}` — `webkit_cookie_manager_get_all_cookies`
|
|
188
|
+
on Linux, `ICoreWebView2_2::getCookieManager` + `GetCookies` on Windows. Embedded page views share the
|
|
189
|
+
host's WebKit context / WebView2 environment, so **httpOnly** session cookies (`z_c0`, `sessionid`,
|
|
190
|
+
`UserToken`) are readable — the ones `document.cookie` can never see. Blocking on the outside,
|
|
191
|
+
asynchronous on the inside: the platform layer posts the read to the UI thread and the caller waits
|
|
192
|
+
with a timeout, reporting `ok=false` with a reason instead of hanging.
|
|
193
|
+
- New `examples/cookie-export` app: embedded login for 5 platforms (CSDN / Zhihu / Juejin / OSChina /
|
|
194
|
+
Baijiahao), field validation against cspub's hard requirements, and export in both formats
|
|
195
|
+
(`~/.cspub/config.json` merged — other platforms' keys are preserved — plus `~/.cspub/cookie_<platform>`
|
|
196
|
+
at mode 600). Ships with 8 unit tests covering bridge-reply parsing, domain filtering, header assembly,
|
|
197
|
+
required-field checks and the merge-write.
|
|
198
|
+
- **Fixed** the `GLib-GObject-CRITICAL: g_object_unref: assertion 'G_IS_OBJECT (object)' failed` printed
|
|
199
|
+
on every bridge round-trip on Linux: `on_js_done` used the deprecated `webkit_web_view_run_javascript`,
|
|
200
|
+
whose finished result must not be unreffed as a `GObject` on WebKitGTK 2.52. Switched to
|
|
201
|
+
`evaluate_javascript` (`JSCValue`, transfer full) — warning gone, bridge builds warning-free.
|
|
202
|
+
|
|
168
203
|
## Changes in 2.1.1
|
|
169
204
|
|
|
170
205
|
- **Serial command groups** (issue #6-4): `IpcHub.registerSerial(cmd, handler, group)` /
|
|
@@ -185,7 +220,7 @@ if you installed under a read-only path, run from inside the repository instead.
|
|
|
185
220
|
- New `examples/hello-world` (smallest runnable app, generated by this CLI) and
|
|
186
221
|
`docs/hello-world-入门教程.md` — a starter guide written from an actual npx-create-to-window run.
|
|
187
222
|
|
|
188
|
-
For the full list see `[2.1.
|
|
223
|
+
For the full list see `[2.1.2] - 2026-11-05` in the repository's `CHANGELOG.md`.
|
|
189
224
|
|
|
190
225
|
## Changes in 2.1.0
|
|
191
226
|
|
package/cjpm.toml
CHANGED
package/cli/cjpm.toml
CHANGED
package/cli/src/project.cj
CHANGED
package/cli/src/sync.cj
CHANGED
|
@@ -397,7 +397,7 @@ func readBizFrameworkPath(): Option<String> {
|
|
|
397
397
|
if (b == 10) { // '\n':行结束,处理这一行
|
|
398
398
|
let hit = pathFromCjTauriLine(line)
|
|
399
399
|
if (let Some(p) <- hit) {
|
|
400
|
-
return
|
|
400
|
+
return bizFrameworkPathOf(p)
|
|
401
401
|
}
|
|
402
402
|
line.clear()
|
|
403
403
|
} else if (b != 13) { // 跳过 '\r'
|
|
@@ -407,11 +407,82 @@ func readBizFrameworkPath(): Option<String> {
|
|
|
407
407
|
// 文件无结尾换行时的最后一行
|
|
408
408
|
let last = pathFromCjTauriLine(line)
|
|
409
409
|
if (let Some(p) <- last) {
|
|
410
|
-
return
|
|
410
|
+
return bizFrameworkPathOf(p)
|
|
411
411
|
}
|
|
412
412
|
return None
|
|
413
413
|
}
|
|
414
414
|
|
|
415
|
+
/**
|
|
416
|
+
* 业务包声明的框架路径 → 可用框架根(issue #7)。
|
|
417
|
+
* lingqu 0.13.1+ 起 `cjpm.toml` 依赖写 `path = "${CJ_TAURI_ROOT}"`——cjpm 对
|
|
418
|
+
* dependencies.path 与 link-option 都会展开环境变量,但 sync 这里先前拿到的是
|
|
419
|
+
* **字面量**(`${CJ_TAURI_ROOT}` 拼进 cwd 变成不存在的目录),导致 sync 必挂而
|
|
420
|
+
* 构建正常。修法:
|
|
421
|
+
* ① `${VAR}` 形式 → 用 std.env.getVariable 展开(未定义的变量保留原样);
|
|
422
|
+
* ② 绝对化后路径不存在 → 回退 resolveFrameworkRoot()(CJ_TAURI_ROOT / 仓内定位),
|
|
423
|
+
* 与「读不到声明回 None」的旧行为一致——字面量不是声明,别拿它继续走。
|
|
424
|
+
*/
|
|
425
|
+
func bizFrameworkPathOf(p: String): Option<String> {
|
|
426
|
+
let expanded = expandVarPlaceholders(p)
|
|
427
|
+
let abs = absolutizeBizPath(expanded)
|
|
428
|
+
if (exists(Path(abs))) {
|
|
429
|
+
return Some(abs)
|
|
430
|
+
}
|
|
431
|
+
return resolveFrameworkRoot()
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* 把字符串里的 `${VAR}` 逐个展开为环境变量值;未定义的变量保留原样(可诊断)。
|
|
436
|
+
*
|
|
437
|
+
* **只认 `${VAR}`,不认 `$VAR`——这是有意与 cjpm 对齐**:实测 cjpm 对
|
|
438
|
+
* `path = "$CJ_TAURI_ROOT"` 直接报 `the path '$CJ_TAURI_ROOT' … does not exist`,
|
|
439
|
+
* 若 sync 单独支持无花括号形式,反而会造成「sync 过了、`cjpm build` 挂」的分裂。
|
|
440
|
+
*
|
|
441
|
+
* 全程按**字节**收集、最后一次 `fromUtf8`:把每个字节单独 `String.fromUtf8([b])` 会在
|
|
442
|
+
* 多字节 UTF-8 上炸(实测 `path = "/tmp/框架本体"` → `IllegalArgumentException: Invalid
|
|
443
|
+
* utf8 byte sequence`,中文路径的 workspace 直接跑不了 sync)——汉字是 3 字节,逐字节
|
|
444
|
+
* 转字符串每段都不是合法序列。展开值同理按其字节拼回。
|
|
445
|
+
*/
|
|
446
|
+
func expandVarPlaceholders(p: String): String {
|
|
447
|
+
let bytes = p.toArray()
|
|
448
|
+
let n = bytes.size
|
|
449
|
+
let out = ArrayList<Byte>()
|
|
450
|
+
var i = 0
|
|
451
|
+
while (i < n) {
|
|
452
|
+
// '${'(0x24 0x7B)开头 → 读到 '}'(0x7D)
|
|
453
|
+
if (bytes[i] == 36 && i + 1 < n && bytes[i + 1] == 123) {
|
|
454
|
+
var j = i + 2
|
|
455
|
+
let name = ArrayList<Byte>()
|
|
456
|
+
var closed = false
|
|
457
|
+
while (j < n) {
|
|
458
|
+
if (bytes[j] == 125) { // '}'
|
|
459
|
+
closed = true
|
|
460
|
+
break
|
|
461
|
+
}
|
|
462
|
+
name.add(bytes[j])
|
|
463
|
+
j += 1
|
|
464
|
+
}
|
|
465
|
+
if (closed && name.size > 0) {
|
|
466
|
+
let varName = String.fromUtf8(name.toArray())
|
|
467
|
+
// 未定义时保留的原文形如 ${VAR}——"$" 与 "{" 分开拼,避免字面量
|
|
468
|
+
// "${" 被当成插值起始符(没有配对 "}" 就报 unterminated)
|
|
469
|
+
let orig = "$" + "{" + varName + "}"
|
|
470
|
+
for (b in valueOfVar(varName, orig)) {
|
|
471
|
+
out.add(b)
|
|
472
|
+
}
|
|
473
|
+
i = j + 1
|
|
474
|
+
} else {
|
|
475
|
+
out.add(bytes[i])
|
|
476
|
+
i += 1
|
|
477
|
+
}
|
|
478
|
+
} else {
|
|
479
|
+
out.add(bytes[i])
|
|
480
|
+
i += 1
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return String.fromUtf8(out.toArray())
|
|
484
|
+
}
|
|
485
|
+
|
|
415
486
|
/** 行里含 "cjTauri" 时,取首个引号对之间的内容(模板生成的行即 ` cjTauri = { path = "..." }`,
|
|
416
487
|
第一个引号就是路径值的开头);不含或内容为空回 None */
|
|
417
488
|
func pathFromCjTauriLine(line: ArrayList<Byte>): Option<String> {
|
|
@@ -466,19 +537,45 @@ func bytesFind(data: ArrayList<Byte>, pat: Array<Byte>, from: Int64): Int64 {
|
|
|
466
537
|
}
|
|
467
538
|
|
|
468
539
|
/** 业务包 toml 里的相对路径以业务包根(= sync 的 cwd)为基准绝对化;
|
|
469
|
-
launcher 在 targets/desktop
|
|
540
|
+
launcher 在 targets/desktop 下,相对基准不同,不能直接抄。
|
|
541
|
+
|
|
542
|
+
绝对路径的三种写法都要认——只判 `/` 开头会把 Windows 盘符当相对路径拼成
|
|
543
|
+
`<cwd>/D:/…`,于是 `create` 产物声明的框架(`toTomlPath` 写出的正是 `D:/…`)
|
|
544
|
+
永远被判不存在、只能靠 `resolveFrameworkRoot()` 回退;回退捡到另一份框架副本
|
|
545
|
+
就是 issue #6 那个「两份副本 → cjpm 模块冲突」:
|
|
546
|
+
① `/` 开头(POSIX);
|
|
547
|
+
② `X:/` 或 `X:\`(Windows 盘符,大小写都算);
|
|
548
|
+
③ `//` 或两个反斜杠开头(UNC 共享路径)。 */
|
|
470
549
|
func absolutizeBizPath(p: String): String {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
550
|
+
let b = p.toArray()
|
|
551
|
+
let n = b.size
|
|
552
|
+
if (n > 0 && b[0] == 47) { // '/'
|
|
553
|
+
return p
|
|
475
554
|
}
|
|
476
|
-
|
|
555
|
+
// 'X:' + 分隔符 —— 盘符绝对路径
|
|
556
|
+
if (n >= 3 && isAsciiLetter(b[0]) && b[1] == 58 && (b[2] == 47 || b[2] == 92)) {
|
|
557
|
+
return p
|
|
558
|
+
}
|
|
559
|
+
// 两个相同分隔符开头 —— UNC(Linux 上 '//x' 也是绝对路径,判成绝对同样正确)
|
|
560
|
+
if (n >= 2 && (b[0] == 47 || b[0] == 92) && b[0] == b[1]) {
|
|
477
561
|
return p
|
|
478
562
|
}
|
|
479
563
|
return currentDir() + "/" + p
|
|
480
564
|
}
|
|
481
565
|
|
|
566
|
+
/** ASCII 字母——盘符只可能是 A-Z / a-z,别用字符字面量(本仓只用字节常量) */
|
|
567
|
+
func isAsciiLetter(c: UInt8): Bool {
|
|
568
|
+
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122)
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** 环境变量取值,未定义回原样(保留 ${VAR} 原文,好诊断)——case 块里只放 return */
|
|
572
|
+
func valueOfVar(name: String, orig: String): String {
|
|
573
|
+
match (getVariable(name)) {
|
|
574
|
+
case Some(v) => return v
|
|
575
|
+
case None => return orig
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
482
579
|
// ---------- ohos 骨架(批次 3-PR3,RFC §7.3) ----------
|
|
483
580
|
|
|
484
581
|
/** 能力清单文件名单:[capabilities] 目录下 .json 的 basename(sync 生成与 check 漂移共用) */
|
|
@@ -97,14 +97,16 @@ cj_host *cj_bridge_create(void) {
|
|
|
97
97
|
return h;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
int cj_bridge_destroy(cj_host *h) {
|
|
101
101
|
int i;
|
|
102
|
-
if (!h) return;
|
|
102
|
+
if (!h) return 1;
|
|
103
103
|
/* 平台资源先走:COM 环境引用 / 模块句柄等(幂等)。返回 0=宿主线程还在收尾,
|
|
104
|
-
它可能仍在用 h(GTK/WebView2 回调的 user_data 就是它)——这时连句柄一起留着不释放。
|
|
104
|
+
它可能仍在用 h(GTK/WebView2 回调的 user_data 就是它)——这时连句柄一起留着不释放。
|
|
105
|
+
回报 0 是**如实**:调用方(仓颉 dispose)据此保留句柄并可再试,别当成「已经释放」
|
|
106
|
+
把字段置空——那正是 issue #8 里「窗口还在屏幕上、页面 IPC 全哑」被静默放过去的地方。 */
|
|
105
107
|
if (!cj_plat_fini(h)) {
|
|
106
|
-
cj_logf("[cj-bridge] destroy: host thread still running, handle kept\n");
|
|
107
|
-
return;
|
|
108
|
+
cj_logf("[cj-bridge] destroy: host thread still running, handle kept (retryable)\n");
|
|
109
|
+
return 0;
|
|
108
110
|
}
|
|
109
111
|
if (h->js_lock) cj_plat_sync_destroy(h->js_lock);
|
|
110
112
|
if (h->dlg_lock) cj_plat_sync_destroy(h->dlg_lock);
|
|
@@ -124,6 +126,7 @@ void cj_bridge_destroy(cj_host *h) {
|
|
|
124
126
|
free(h->pending_url);
|
|
125
127
|
cj_logf("[cj-bridge] host destroyed: %p\n", (void *)h);
|
|
126
128
|
free(h);
|
|
129
|
+
return 1;
|
|
127
130
|
}
|
|
128
131
|
|
|
129
132
|
void cj_bridge_init(cj_host *h, cj_on_message_fn m, cj_on_destroy_fn d) {
|
|
@@ -1191,6 +1194,32 @@ void cj_core_dialog_tick(cj_host *h, dlg_req *req) {
|
|
|
1191
1194
|
cj_plat_sync_unlock(h->dlg_lock);
|
|
1192
1195
|
}
|
|
1193
1196
|
|
|
1197
|
+
/* =========================================================================
|
|
1198
|
+
* Cookie 读取:core 只做入口校验与日志,投递 / 等待 / 解析全在平台层
|
|
1199
|
+
*
|
|
1200
|
+
* 为什么不在 core 建状态机(对比 dialog):dialog 是模态单槽,必须由 core 串行化;
|
|
1201
|
+
* cookie 读取是只读快照,可以并发,平台各自把请求包在栈上结构里即可,core 不持状态。
|
|
1202
|
+
* ========================================================================= */
|
|
1203
|
+
|
|
1204
|
+
char *cj_bridge_get_cookies(cj_host *h, const char *filter) {
|
|
1205
|
+
char *json;
|
|
1206
|
+
if (!h || !h->ready) {
|
|
1207
|
+
cj_logf("[cj-bridge] get_cookies ignored: host not ready\n");
|
|
1208
|
+
return cj_strdup("{\"ok\":false,\"error\":\"host not ready\"}");
|
|
1209
|
+
}
|
|
1210
|
+
json = cj_plat_get_cookies(h, filter ? filter : "");
|
|
1211
|
+
if (!json) {
|
|
1212
|
+
return cj_strdup("{\"ok\":false,\"error\":\"platform returned null\"}");
|
|
1213
|
+
}
|
|
1214
|
+
cj_logf("[cj-bridge] get_cookies: filter=%s -> %d bytes\n",
|
|
1215
|
+
(filter && *filter) ? filter : "(all)", (int)strlen(json));
|
|
1216
|
+
return json;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
void cj_bridge_cookies_free(char *s) {
|
|
1220
|
+
free(s);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1194
1223
|
/* =========================================================================
|
|
1195
1224
|
* 串口(plugin_serial):路径白名单 + 参数校验 + 导出转发
|
|
1196
1225
|
*
|
|
@@ -297,6 +297,14 @@ void cj_plat_view_set_bounds(cj_host *h, long long view, int x, int y, int w, in
|
|
|
297
297
|
void cj_plat_view_navigate(cj_host *h, long long view, const char *url);
|
|
298
298
|
void cj_plat_view_destroy(cj_host *h, long long view);
|
|
299
299
|
|
|
300
|
+
/* ---- Cookie 读取(「一键导出登录态」类工具)----
|
|
301
|
+
阻塞式:平台把请求投到宿主 UI 线程发起异步读(Linux = webkit_cookie_manager_get_all_cookies,
|
|
302
|
+
Windows = ICoreWebView2CookieManager::GetCookies),拿到结果后回填并唤醒调用方。
|
|
303
|
+
返回 malloc 的 JSON(**调用方 free**),失败也返回 JSON。filter = 域名后缀,空串 = 全部。
|
|
304
|
+
平台必须在 UI 线程退出/窗口先关时也唤醒等待方(否则 worker 线程永远吊在条件变量上):
|
|
305
|
+
等待方自带超时,但别指望它——超时只是最后一道防线。 */
|
|
306
|
+
char *cj_plat_get_cookies(cj_host *h, const char *filter);
|
|
307
|
+
|
|
300
308
|
/* 宿主能力位(CJ_CAP_* 的按位或):纯静态事实,cj_plat_init 里由平台给出。 */
|
|
301
309
|
int cj_plat_host_capabilities(cj_host *h);
|
|
302
310
|
|
|
@@ -393,8 +401,11 @@ void cj_core_drop_files(cj_host *h, const char *uri_list);
|
|
|
393
401
|
CJ_BRIDGE_API cj_host *cj_bridge_create(void);
|
|
394
402
|
|
|
395
403
|
/* 释放宿主编:先 cj_plat_fini,再回收公共状态。对 NULL 安全。
|
|
396
|
-
|
|
397
|
-
|
|
404
|
+
返回值:1 = 已回收(句柄随即失效,调用方必须把字段置空);
|
|
405
|
+
0 = 平台宿主线程还没收工,**平台状态与句柄一起留着**,调用方可以稍后再调一次重试。
|
|
406
|
+
契约(issue #8):这是可重试的操作,不是「调过就算数」——早退时不能把句柄丢掉,
|
|
407
|
+
否则「没回收」这件事既看不见也再没有第二次机会(表象是窗口留在屏幕上、页面 IPC 全哑)。 */
|
|
408
|
+
CJ_BRIDGE_API int cj_bridge_destroy(cj_host *h);
|
|
398
409
|
|
|
399
410
|
/* 注入回调(JS 消息到达 / 窗口销毁)+ 平台一次性初始化 */
|
|
400
411
|
CJ_BRIDGE_API void cj_bridge_init(cj_host *h, cj_on_message_fn m, cj_on_destroy_fn d);
|
|
@@ -446,6 +457,20 @@ CJ_BRIDGE_API void cj_bridge_set_dialog_callback(cj_host *h, cj_on_dialog_fn cb)
|
|
|
446
457
|
CJ_BRIDGE_API int cj_bridge_show_dialog(cj_host *h, int kind, const char *title,
|
|
447
458
|
const char *message, const char *filter);
|
|
448
459
|
|
|
460
|
+
/* ===== Cookie 读取(「一键导出各平台登录态」类工具用)=====
|
|
461
|
+
从宿主 WebView 的 cookie 仓库整份读出,**含 httpOnly**——这正是它相对
|
|
462
|
+
document.cookie 的全部意义:各平台的会话令牌(知乎 z_c0 / 掘金 sessionid …)
|
|
463
|
+
基本都带 httpOnly,注入 JS 读 document.cookie 拿不到它们。
|
|
464
|
+
filter 是域名后缀(如 "csdn.net";空串 / NULL = 全部)。
|
|
465
|
+
返回 malloc 的 JSON,**调用方负责 free**;失败也返回 JSON({"ok":false,"error":…})。
|
|
466
|
+
线程口径:请在命令 worker 线程调用(内部投 UI 线程发起异步读 + 带超时等待);
|
|
467
|
+
调用方已站在 UI 线程时直接返回错误 JSON——等待的事件得由 UI 线程处理,阻塞即自死锁。 */
|
|
468
|
+
CJ_BRIDGE_API char *cj_bridge_get_cookies(cj_host *h, const char *filter);
|
|
469
|
+
|
|
470
|
+
/* 释放 cj_bridge_get_cookies 返回的缓冲(NULL 安全,幂等)。失败结果也是 malloc 的 JSON,
|
|
471
|
+
同样要走这里。 */
|
|
472
|
+
CJ_BRIDGE_API void cj_bridge_cookies_free(char *s);
|
|
473
|
+
|
|
449
474
|
/* 菜单栏:wire 是 MenuModel.toWire() 的线路文本(空串 = 移除菜单栏)。
|
|
450
475
|
未 start 时只记下、建窗时生效;start 之后调用=运行期换菜单(平台投到 UI 线程重建)。 */
|
|
451
476
|
CJ_BRIDGE_API void cj_bridge_set_menu(cj_host *h, const char *wire);
|
|
@@ -337,16 +337,19 @@ int cj_plat_fini(cj_host *h) {
|
|
|
337
337
|
/* 收尾顺序同样要命:窗口销毁是 GTK/WebKit 一整串多线程拆解,若它与仓颉运行时的收尾
|
|
338
338
|
并发(run() 返回 → 运行时关停),实测几乎必然以 SIGABRT 收尾(`schd-worker` unhandled
|
|
339
339
|
SIGABRT from runtime frame)。所以给宿主线程一个有界的等待窗口——纯 usleep 轮询标记,
|
|
340
|
-
不碰 join(join 会与运行时握手互等,见上);等不到也不阻塞进程退出,照旧留着状态。
|
|
340
|
+
不碰 join(join 会与运行时握手互等,见上);等不到也不阻塞进程退出,照旧留着状态。
|
|
341
|
+
预算与 Windows 侧同一口径(5s):GTK/WebKit 的拆解是整串多线程收尾,2s 实测不够宽。
|
|
342
|
+
早退时**保留 p 与 thread_done 标记**,调用方(仓颉 dispose)可以再调一次重试——
|
|
343
|
+
issue #8 里那次「窗口还在、IPC 全哑」就是早退之后句柄被置空、再没有第二次机会。 */
|
|
341
344
|
if (!p->thread_done) {
|
|
342
345
|
int spin = 0;
|
|
343
|
-
while (!p->thread_done && spin <
|
|
346
|
+
while (!p->thread_done && spin < 5000) { /* 最多约 5s */
|
|
344
347
|
usleep(1000);
|
|
345
348
|
spin++;
|
|
346
349
|
}
|
|
347
350
|
}
|
|
348
351
|
if (!p->thread_done) {
|
|
349
|
-
fprintf(stderr, "[cj-bridge] fini: host thread still running, platform state kept\n");
|
|
352
|
+
fprintf(stderr, "[cj-bridge] fini: host thread still running, platform state kept (retryable)\n");
|
|
350
353
|
return 0;
|
|
351
354
|
}
|
|
352
355
|
h->plat = NULL;
|
|
@@ -370,13 +373,18 @@ void cj_plat_wake_ui(cj_host *h) {
|
|
|
370
373
|
/* ===== 平台原语:投递 / 运行时操作 ===== */
|
|
371
374
|
|
|
372
375
|
/* 回投结果校验:这条 eval 是「仓颉 → 前端」的唯一出口,静默失败会让前端 promise 永久 pending。
|
|
373
|
-
Windows 侧有 log_hr("ExecuteScript", …) 兜同一件事(两平台对称)。
|
|
376
|
+
Windows 侧有 log_hr("ExecuteScript", …) 兜同一件事(两平台对称)。
|
|
377
|
+
|
|
378
|
+
用 evaluate_javascript 而不是弃用的 run_javascript:后者的 finish 返回 WebKitJavascriptResult,
|
|
379
|
+
在本机 WebKitGTK 2.52 上对它 g_object_unref 会触发 G_IS_OBJECT 断言失败(gdb 栈:
|
|
380
|
+
on_js_done → g_log → g_logv),每条命令刷一条 GLib-CRITICAL。新 API 返回 JSCValue(transfer full),
|
|
381
|
+
所有权语义明确,顺带消掉编译期的 deprecated 警告。world_name / source_uri 传 NULL = 用页面主世界。 */
|
|
374
382
|
static void on_js_done(GObject *src, GAsyncResult *res, gpointer ud) {
|
|
375
383
|
GError *err = NULL;
|
|
376
|
-
|
|
377
|
-
|
|
384
|
+
JSCValue *r =
|
|
385
|
+
webkit_web_view_evaluate_javascript_finish(WEBKIT_WEB_VIEW(src), res, &err);
|
|
378
386
|
if (r) {
|
|
379
|
-
g_object_unref(r); /*
|
|
387
|
+
g_object_unref(r); /* 每次回投都会新建一个结果值,不看也要释放 */
|
|
380
388
|
}
|
|
381
389
|
if (err) {
|
|
382
390
|
fprintf(stderr, "[cj-bridge] run js failed: %s\n", err->message);
|
|
@@ -387,7 +395,8 @@ static void on_js_done(GObject *src, GAsyncResult *res, gpointer ud) {
|
|
|
387
395
|
void cj_plat_post_js(cj_host *h, const char *script_utf8) {
|
|
388
396
|
plat_host *p = h ? (plat_host *)h->plat : NULL;
|
|
389
397
|
if (!p || !p->view || !script_utf8) return;
|
|
390
|
-
|
|
398
|
+
webkit_web_view_evaluate_javascript(WEBKIT_WEB_VIEW(p->view), script_utf8, -1, NULL, NULL, NULL,
|
|
399
|
+
on_js_done, NULL);
|
|
391
400
|
}
|
|
392
401
|
|
|
393
402
|
void cj_plat_open_devtools(cj_host *h) {
|
|
@@ -1612,6 +1621,242 @@ void cj_plat_view_destroy(cj_host *h, long long view) {
|
|
|
1612
1621
|
view_post(h, CJ_VIEW_OP_DESTROY, view, 0, 0, 0, 0, 0, NULL);
|
|
1613
1622
|
}
|
|
1614
1623
|
|
|
1624
|
+
/* =========================================================================
|
|
1625
|
+
* Cookie 读取(「一键导出各平台登录态」类工具)
|
|
1626
|
+
*
|
|
1627
|
+
* 形态:命令 worker 线程调 cj_plat_get_cookies → 投 GTK 线程 → webkit 异步读 cookie store
|
|
1628
|
+
* → 回调序列化成 JSON → 唤醒等待方(带 5s 超时)。
|
|
1629
|
+
*
|
|
1630
|
+
* 为什么必须走原生(而不是注入 JS 读 document.cookie):httpOnly。知乎 z_c0、掘金 sessionid
|
|
1631
|
+
* 这类会话令牌都带 httpOnly,JS 侧看不见——导出一个「字段齐全但发文 401」的文件比导不出来
|
|
1632
|
+
* 更难查。cookie store 才是全量快照。
|
|
1633
|
+
*
|
|
1634
|
+
* 为什么不用 core 的 cj_sync:那套谓词等待没有超时,窗口在等待期间销毁时 worker 会永远吊在
|
|
1635
|
+
* 条件变量上;这里自带 pthread mutex + cond + timedwait,超时后放弃等待(结构交给回调回收)。
|
|
1636
|
+
* ========================================================================= */
|
|
1637
|
+
|
|
1638
|
+
typedef struct cookie_call {
|
|
1639
|
+
cj_host *h;
|
|
1640
|
+
char *filter; /* 域名后缀;空串 = 全部 */
|
|
1641
|
+
char *json; /* 结果 JSON(GTK 线程 malloc,等待方取走后自己 free)*/
|
|
1642
|
+
int ready;
|
|
1643
|
+
int abandoned; /* 等待方已超时放弃:结构由异步回调自己回收 */
|
|
1644
|
+
pthread_mutex_t mtx;
|
|
1645
|
+
pthread_cond_t cond;
|
|
1646
|
+
} cookie_call;
|
|
1647
|
+
|
|
1648
|
+
/* JSON 字符串转义:只处理 JSON 必需的字符,>=0x20 的字节原样输出
|
|
1649
|
+
(cookie 值多是 ASCII;UTF-8 多字节序列逐字节拷贝不会被破坏)*/
|
|
1650
|
+
static void cookie_json_escaped(GString *out, const char *s) {
|
|
1651
|
+
const unsigned char *p = (const unsigned char *)(s ? s : "");
|
|
1652
|
+
g_string_append_c(out, '"');
|
|
1653
|
+
for (; *p; p++) {
|
|
1654
|
+
switch (*p) {
|
|
1655
|
+
case '"': g_string_append(out, "\\\""); break;
|
|
1656
|
+
case '\\': g_string_append(out, "\\\\"); break;
|
|
1657
|
+
case '\n': g_string_append(out, "\\n"); break;
|
|
1658
|
+
case '\r': g_string_append(out, "\\r"); break;
|
|
1659
|
+
case '\t': g_string_append(out, "\\t"); break;
|
|
1660
|
+
default:
|
|
1661
|
+
if (*p < 0x20) {
|
|
1662
|
+
g_string_append_printf(out, "\\u%04x", (unsigned)*p);
|
|
1663
|
+
} else {
|
|
1664
|
+
g_string_append_c(out, (char)*p);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
g_string_append_c(out, '"');
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
/* filter 为空 → 全收;否则按域名后缀匹配(".csdn.net" 命中 "csdn.net")*/
|
|
1672
|
+
static int cookie_domain_matches(const char *domain, const char *filter) {
|
|
1673
|
+
size_t dl, fl;
|
|
1674
|
+
if (!filter || !*filter) return 1;
|
|
1675
|
+
if (!domain) return 0;
|
|
1676
|
+
dl = strlen(domain);
|
|
1677
|
+
fl = strlen(filter);
|
|
1678
|
+
if (dl < fl) return 0;
|
|
1679
|
+
if (g_ascii_strncasecmp(domain + dl - fl, filter, fl) != 0) return 0;
|
|
1680
|
+
return dl == fl || domain[dl - fl - 1] == '.';
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
static void cookie_call_free(cookie_call *c) {
|
|
1684
|
+
if (!c) return;
|
|
1685
|
+
free(c->filter);
|
|
1686
|
+
free(c->json);
|
|
1687
|
+
pthread_mutex_destroy(&c->mtx);
|
|
1688
|
+
pthread_cond_destroy(&c->cond);
|
|
1689
|
+
free(c);
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/* 释放 get_all_cookies_finish 给的 GList:元素是 SoupCookie(普通结构体,不是 GObject),
|
|
1693
|
+
逐个 soup_cookie_free 再 g_list_free。不用 g_list_free_full 是为了让释放路径显式可读,
|
|
1694
|
+
也避免 GDestroyNotify 的函数指针强转在严格编译选项下被质疑。*/
|
|
1695
|
+
static void cookie_list_free(GList *list) {
|
|
1696
|
+
GList *it;
|
|
1697
|
+
for (it = list; it; it = it->next) {
|
|
1698
|
+
if (it->data) soup_cookie_free((SoupCookie *)it->data);
|
|
1699
|
+
}
|
|
1700
|
+
g_list_free(list);
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
/* 结果回填:等待方还在就交出去;已超时放弃就由这里回收结构 */
|
|
1704
|
+
static void cookie_call_set_result(cookie_call *c, char *json) {
|
|
1705
|
+
int abandon;
|
|
1706
|
+
pthread_mutex_lock(&c->mtx);
|
|
1707
|
+
abandon = c->abandoned;
|
|
1708
|
+
if (!abandon) {
|
|
1709
|
+
c->json = json;
|
|
1710
|
+
c->ready = 1;
|
|
1711
|
+
pthread_cond_signal(&c->cond);
|
|
1712
|
+
}
|
|
1713
|
+
pthread_mutex_unlock(&c->mtx);
|
|
1714
|
+
if (abandon) {
|
|
1715
|
+
free(json);
|
|
1716
|
+
cookie_call_free(c);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
static void cookie_call_set_error(cookie_call *c, const char *msg) {
|
|
1721
|
+
GString *s = g_string_new("{\"ok\":false,\"error\":");
|
|
1722
|
+
cookie_json_escaped(s, msg);
|
|
1723
|
+
g_string_append_c(s, '}');
|
|
1724
|
+
cookie_call_set_result(c, g_string_free(s, FALSE));
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
/* webkit 异步读完成(GTK 线程)*/
|
|
1728
|
+
static void on_all_cookies(GObject *src, GAsyncResult *res, gpointer ud) {
|
|
1729
|
+
cookie_call *c = (cookie_call *)ud;
|
|
1730
|
+
GError *err = NULL;
|
|
1731
|
+
GList *list;
|
|
1732
|
+
GList *it;
|
|
1733
|
+
GString *out;
|
|
1734
|
+
int n = 0;
|
|
1735
|
+
int first = 1;
|
|
1736
|
+
|
|
1737
|
+
list = webkit_cookie_manager_get_all_cookies_finish(WEBKIT_COOKIE_MANAGER(src), res, &err);
|
|
1738
|
+
if (err) {
|
|
1739
|
+
GString *msg = g_string_new("cookie store read failed: ");
|
|
1740
|
+
g_string_append(msg, err->message ? err->message : "unknown");
|
|
1741
|
+
cookie_call_set_error(c, msg->str);
|
|
1742
|
+
g_string_free(msg, TRUE);
|
|
1743
|
+
g_error_free(err);
|
|
1744
|
+
cookie_list_free(list);
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
/* 先数一遍匹配项(JSON 里的 count 得写准;cookie 数量级很小,两遍遍历无所谓)*/
|
|
1749
|
+
for (it = list; it; it = it->next) {
|
|
1750
|
+
SoupCookie *ck = (SoupCookie *)it->data;
|
|
1751
|
+
if (ck && cookie_domain_matches(soup_cookie_get_domain(ck), c->filter)) n++;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
out = g_string_new("");
|
|
1755
|
+
g_string_append_printf(out, "{\"ok\":true,\"count\":%d,\"cookies\":[", n);
|
|
1756
|
+
for (it = list; it; it = it->next) {
|
|
1757
|
+
SoupCookie *ck = (SoupCookie *)it->data;
|
|
1758
|
+
GDateTime *exp;
|
|
1759
|
+
if (!ck || !cookie_domain_matches(soup_cookie_get_domain(ck), c->filter)) continue;
|
|
1760
|
+
if (!first) g_string_append_c(out, ',');
|
|
1761
|
+
first = 0;
|
|
1762
|
+
g_string_append(out, "{\"name\":");
|
|
1763
|
+
cookie_json_escaped(out, soup_cookie_get_name(ck));
|
|
1764
|
+
g_string_append(out, ",\"value\":");
|
|
1765
|
+
cookie_json_escaped(out, soup_cookie_get_value(ck));
|
|
1766
|
+
g_string_append(out, ",\"domain\":");
|
|
1767
|
+
cookie_json_escaped(out, soup_cookie_get_domain(ck));
|
|
1768
|
+
g_string_append(out, ",\"path\":");
|
|
1769
|
+
cookie_json_escaped(out, soup_cookie_get_path(ck));
|
|
1770
|
+
g_string_append_printf(out, ",\"secure\":%s,\"httpOnly\":%s",
|
|
1771
|
+
soup_cookie_get_secure(ck) ? "true" : "false",
|
|
1772
|
+
soup_cookie_get_http_only(ck) ? "true" : "false");
|
|
1773
|
+
exp = soup_cookie_get_expires(ck);
|
|
1774
|
+
if (exp) {
|
|
1775
|
+
g_string_append_printf(out, ",\"expires\":%" G_GINT64_FORMAT,
|
|
1776
|
+
(gint64)g_date_time_to_unix(exp));
|
|
1777
|
+
} else {
|
|
1778
|
+
g_string_append(out, ",\"expires\":0,\"session\":true");
|
|
1779
|
+
}
|
|
1780
|
+
g_string_append_c(out, '}');
|
|
1781
|
+
}
|
|
1782
|
+
g_string_append(out, "]}");
|
|
1783
|
+
|
|
1784
|
+
cookie_list_free(list);
|
|
1785
|
+
cookie_call_set_result(c, g_string_free(out, FALSE));
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
/* GTK 线程:发起异步读 */
|
|
1789
|
+
static gboolean cookie_idle(gpointer ud) {
|
|
1790
|
+
cookie_call *c = (cookie_call *)ud;
|
|
1791
|
+
plat_host *p = (c && c->h) ? (plat_host *)c->h->plat : NULL;
|
|
1792
|
+
WebKitWebView *wv;
|
|
1793
|
+
WebKitWebContext *ctx;
|
|
1794
|
+
|
|
1795
|
+
if (!p || !p->view) {
|
|
1796
|
+
cookie_call_set_error(c, "webview not ready");
|
|
1797
|
+
return G_SOURCE_REMOVE;
|
|
1798
|
+
}
|
|
1799
|
+
wv = WEBKIT_WEB_VIEW(p->view);
|
|
1800
|
+
ctx = webkit_web_view_get_context(wv);
|
|
1801
|
+
if (!ctx) {
|
|
1802
|
+
cookie_call_set_error(c, "web context unavailable");
|
|
1803
|
+
return G_SOURCE_REMOVE;
|
|
1804
|
+
}
|
|
1805
|
+
webkit_cookie_manager_get_all_cookies(
|
|
1806
|
+
webkit_web_context_get_cookie_manager(ctx), NULL, on_all_cookies, c);
|
|
1807
|
+
return G_SOURCE_REMOVE;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
char *cj_plat_get_cookies(cj_host *h, const char *filter) {
|
|
1811
|
+
plat_host *p = h ? (plat_host *)h->plat : NULL;
|
|
1812
|
+
cookie_call *c;
|
|
1813
|
+
struct timespec ts;
|
|
1814
|
+
char *json = NULL;
|
|
1815
|
+
|
|
1816
|
+
if (!p || !p->view) {
|
|
1817
|
+
return strdup("{\"ok\":false,\"error\":\"webview not ready\"}");
|
|
1818
|
+
}
|
|
1819
|
+
if (cj_plat_is_ui_thread(h)) {
|
|
1820
|
+
/* 等待的事件得由 UI 线程处理:站在这里阻塞 = 自己把自己锁死 */
|
|
1821
|
+
return strdup("{\"ok\":false,\"error\":\"get_cookies must be called from a worker thread\"}");
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
c = (cookie_call *)calloc(1, sizeof(*c));
|
|
1825
|
+
if (!c) return strdup("{\"ok\":false,\"error\":\"out of memory\"}");
|
|
1826
|
+
c->h = h;
|
|
1827
|
+
c->filter = strdup(filter ? filter : "");
|
|
1828
|
+
if (!c->filter) {
|
|
1829
|
+
free(c);
|
|
1830
|
+
return strdup("{\"ok\":false,\"error\":\"out of memory\"}");
|
|
1831
|
+
}
|
|
1832
|
+
pthread_mutex_init(&c->mtx, NULL);
|
|
1833
|
+
pthread_cond_init(&c->cond, NULL);
|
|
1834
|
+
|
|
1835
|
+
if (!g_idle_add(cookie_idle, c)) {
|
|
1836
|
+
cookie_call_free(c);
|
|
1837
|
+
return strdup("{\"ok\":false,\"error\":\"ui thread unavailable\"}");
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
pthread_mutex_lock(&c->mtx);
|
|
1841
|
+
clock_gettime(CLOCK_REALTIME, &ts);
|
|
1842
|
+
ts.tv_sec += 5;
|
|
1843
|
+
while (!c->ready) {
|
|
1844
|
+
if (pthread_cond_timedwait(&c->cond, &c->mtx, &ts) == ETIMEDOUT) break;
|
|
1845
|
+
}
|
|
1846
|
+
if (c->ready) {
|
|
1847
|
+
json = c->json;
|
|
1848
|
+
c->json = NULL;
|
|
1849
|
+
pthread_mutex_unlock(&c->mtx);
|
|
1850
|
+
cookie_call_free(c);
|
|
1851
|
+
} else {
|
|
1852
|
+
/* 超时:放弃等待但**不能**释放结构——异步回调可能还在路上,交给它回收 */
|
|
1853
|
+
c->abandoned = 1;
|
|
1854
|
+
pthread_mutex_unlock(&c->mtx);
|
|
1855
|
+
json = strdup("{\"ok\":false,\"error\":\"timeout waiting for cookie store\"}");
|
|
1856
|
+
}
|
|
1857
|
+
return json;
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1615
1860
|
/* 最小化到托盘这里只**记一条策略**(并复位提醒位):真正的拦截发生在用户按下最小化的那一刻
|
|
1616
1861
|
(window-state-event),此刻窗口层没有可做的事——与 Windows 侧同形。 */
|
|
1617
1862
|
void cj_plat_set_minimize_to_tray(cj_host *h, int enabled) {
|