Skip to content

Commit 5418259

Browse files
lybvincijianliang00
authored andcommitted
[BugFix][Harmony] Bind NativeFacade actor to UI runner and clarify callback threads
Fix Harmony standalone runtime initialization by binding the NativeFacade actor to the UI task runner, matching Harmony's single UI thread model for native callbacks. Also document callback thread expectations across platforms: - Android: mark background runtime callbacks as invoked on the JS thread - iOS: update header comments to state callbacks are on the JS thread - Harmony: annotate ETS callbacks as invoked on the UI thread AutoSubmit: True issue: m-7305562623 AutoLand: release/3.9, release/3.8, release_3.8_weekly_20260522 SkipChecks:macro
1 parent 047242e commit 5418259

4 files changed

Lines changed: 35 additions & 10 deletions

File tree

core/shell/runtime/bts/bts_runtime_standalone_helper.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <utility>
99
#include <vector>
1010

11+
#include "core/base/threading/task_runner_manufactor.h"
1112
#include "core/base/threading/vsync_monitor.h"
1213
#include "core/runtime/js/jsi/jsi.h"
1314
#include "core/services/event_report/event_tracker_platform_impl.h"
@@ -42,7 +43,15 @@ BTSRuntimeStandalone::InitRuntimeStandalone(
4243
lynx::base::TaskRunnerManufactor::GetJSRunner(group_name);
4344
auto native_runtime_facade =
4445
std::make_shared<lynx::shell::LynxActor<lynx::shell::NativeFacade>>(
45-
std::move(native_facade_runtime), js_task_runner, instance_id, true);
46+
std::move(native_facade_runtime),
47+
#if OS_HARMONY
48+
// Harmony only exposes a single UI thread for native callbacks,
49+
// so the NativeFacade actor must be bound to the UI runner.
50+
base::UIThread::GetRunner(),
51+
#else
52+
js_task_runner,
53+
#endif
54+
instance_id, true);
4655
std::shared_ptr<base::VSyncMonitor> vsync_monitor =
4756
base::VSyncMonitor::Create();
4857

platform/android/lynx_android/src/main/java/com/lynx/tasm/LynxBackgroundRuntime.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.lynx.tasm.base.LLog;
2424
import com.lynx.tasm.core.JSProxy;
2525
import com.lynx.tasm.core.resource.LynxResourceLoader;
26+
import com.lynx.tasm.utils.UIThreadUtils;
2627
import java.lang.ref.WeakReference;
2728
import java.nio.charset.Charset;
2829
import java.util.HashMap;
@@ -352,27 +353,36 @@ public void unsubscribeSessionStorage(String key, double id) {
352353
}
353354
}
354355

356+
// Invoked on the JS thread, dispatched to the UI thread before notifying clients.
355357
@RestrictTo(RestrictTo.Scope.LIBRARY)
356358
@Override
357359
@CalledByNative
358360
public void onErrorOccurred(LynxError error) {
359-
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
360-
client.onReceivedError(error);
361-
}
361+
UIThreadUtils.runOnUiThread(() -> {
362+
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
363+
client.onReceivedError(error);
364+
}
365+
});
362366
}
363367

368+
// Invoked on the JS thread, dispatched to the UI thread before notifying clients.
364369
@CalledByNative
365370
public void onModuleMethodInvoked(String module, String method, int error_code) {
366-
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
367-
client.onModuleMethodInvoked(module, method, error_code);
368-
}
371+
UIThreadUtils.runOnUiThread(() -> {
372+
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
373+
client.onModuleMethodInvoked(module, method, error_code);
374+
}
375+
});
369376
}
370377

378+
// Invoked on the JS thread, dispatched to the UI thread before notifying clients.
371379
@CalledByNative
372380
public void onEvaluateJavaScriptEnd(String url) {
373-
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
374-
client.onEvaluateJavaScriptEnd(url);
375-
}
381+
UIThreadUtils.runOnUiThread(() -> {
382+
for (LynxBackgroundRuntimeClient client : mRuntimeClients) {
383+
client.onEvaluateJavaScriptEnd(url);
384+
}
385+
});
376386
}
377387

378388
public boolean attachToLynxView() {

platform/android/lynx_android/src/main/java/com/lynx/tasm/LynxBackgroundRuntimeClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package com.lynx.tasm;
66

77
public abstract class LynxBackgroundRuntimeClient {
8+
// Invoked on the UI thread.
89
public void onReceivedError(LynxError error) {}
10+
// Invoked on the UI thread.
911
public void onModuleMethodInvoked(String module, String method, int error_code) {}
12+
// Invoked on the UI thread.
1013
public void onEvaluateJavaScriptEnd(String url) {}
1114
}

platform/harmony/lynx_harmony/src/main/ets/tasm/LynxBackgroundRuntime.ets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class LynxBackgroundRuntime extends lynx.LynxRuntimeWrapper {
6767
}
6868

6969
// client callback
70+
// Invoked on the UI thread.
7071
onErrorOccurred(level: string, errorCode: number, message: string, suggestion: string,
7172
customInfo: Record<string, string>, logboxOnly: boolean) {
7273
let err = new LynxError(LynxErrorLevel[level], errorCode, message, suggestion, LynxError.NATIVE_ERROR, customInfo);
@@ -81,12 +82,14 @@ export class LynxBackgroundRuntime extends lynx.LynxRuntimeWrapper {
8182
});
8283
}
8384

85+
// Invoked on the UI thread.
8486
onModuleMethodInvoked(module: string, method: string, error_code: number) {
8587
this.clients.forEach(client => {
8688
client.onModuleMethodInvoked(module, method, error_code);
8789
});
8890
}
8991

92+
// Invoked on the UI thread.
9093
onEvaluateJavaScriptEnd(url: string) {
9194
this.clients.forEach(client => {
9295
client.onEvaluateJavaScriptEnd(url);

0 commit comments

Comments
 (0)