M2(V2 阻塞性验证通过): setPathAllowingUniversalAccess 解决 file:// 跨域

T-M2-08 V2:真机验证 file:// 加载下 H5 fetch/XHR 取本地资源——首测被 CORS 拦截
(origin 'null',file 协议不在允许跨域 scheme 列表,正是框架 §7.3 预判风险)。
启用官方方案 WebviewController.setPathAllowingUniversalAccess([filesDir/tsgames]),
对 H5 完全透明(仍 file:// 加载)。复测:V2 fetch/XHR OK。

真机端到端链路全通(Pura 90 模拟器):启动编排→远程失败降级本地→解压内置包→
注入 app_data.js→file:// 加载大厅→跨域 fetch OK→桥握手→getTime 回执→nativeEcho
特殊字符往返一致。app_data 全局变量、Launchtype=0、appservice/setPostUrl 下发均正确。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
lanterngamescn
2026-06-25 18:41:42 +08:00
co-authored by Claude Opus 4.8
parent 2b0bd1f392
commit 2100faaef7
@@ -64,6 +64,17 @@ export struct BridgeGameContainer {
/** onControllerAttached:建桥 + 经组装根注册全部能力(框架 §A.1:能力注册放这里)。 */ /** onControllerAttached:建桥 + 经组装根注册全部能力(框架 §A.1:能力注册放这里)。 */
private setupBridge(): void { private setupBridge(): void {
const hostCtx: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; const hostCtx: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
// 🔴 V2 file:// 跨域(框架 §7.3):file:// 页面的 XHR/fetch 默认被 CORS 拦截(origin 'null')。
// 官方方案:把资源根加入"允许跨域访问"白名单,file:// 访问该路径下资源即放开同源限制。
// 对 H5 完全透明(仍 file:// 加载)。路径须在 filesDir 子目录且与用户文件隔离。
try {
this.controller.setPathAllowingUniversalAccess([`${hostCtx.filesDir}/tsgames`]);
} catch (e) {
const err = e as Error;
Logger.tag('BridgeGameContainer').e(`setPathAllowingUniversalAccess failed: ${err.message}`);
}
const adapter = new WebviewControllerAdapter(this.controller); const adapter = new WebviewControllerAdapter(this.controller);
const bridge = new BridgeController(adapter); const bridge = new BridgeController(adapter);
bridge.setBridgeJs(BridgeJsLoader.load(hostCtx)); bridge.setBridgeJs(BridgeJsLoader.load(hostCtx));