mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
ohos: Bump napi-ohos (#34160)
Update napi-ohos and migrate away from deprecated `JsFunction` and use the new `Function` and `build_threadsafe_function()` methods. Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
This commit is contained in:
parent
5e2c7908d0
commit
dcb9058fe3
3 changed files with 39 additions and 46 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -4524,21 +4524,20 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "napi-ohos"
|
name = "napi-ohos"
|
||||||
version = "0.1.3"
|
version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ad5a3bbb2ae61f345b8c11776f2e79fc2bb71d1901af9a5f81f03c9238a05d86"
|
checksum = "e9f51da5368ed904ee9274332875955442a7dda4f0352d3eb7a29d238650fc96"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.6.0",
|
||||||
"ctor",
|
"ctor",
|
||||||
"napi-sys-ohos",
|
"napi-sys-ohos",
|
||||||
"once_cell",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "napi-sys-ohos"
|
name = "napi-sys-ohos"
|
||||||
version = "0.0.1"
|
version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f101404db01422d034db5afa63eefff6d9c8f66c0894278bc456b4c30954e166"
|
checksum = "7f7fcdef583bb4e03060b852a65d2d9ca3a781c43b97cf7e546d3937ea2b0612"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libloading",
|
"libloading",
|
||||||
]
|
]
|
||||||
|
|
|
@ -87,7 +87,7 @@ env_filter = "0.1.2"
|
||||||
ipc-channel = { workspace = true, features = ["force-inprocess"] }
|
ipc-channel = { workspace = true, features = ["force-inprocess"] }
|
||||||
hilog = "0.1.1"
|
hilog = "0.1.1"
|
||||||
napi-derive-ohos = "1.0.1"
|
napi-derive-ohos = "1.0.1"
|
||||||
napi-ohos = "0.1"
|
napi-ohos = "1.0.1"
|
||||||
ohos-sys = { version = "0.4.0", features = ["xcomponent"] }
|
ohos-sys = { version = "0.4.0", features = ["xcomponent"] }
|
||||||
ohos-vsync = "0.1.2"
|
ohos-vsync = "0.1.2"
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,9 @@ use std::time::Duration;
|
||||||
|
|
||||||
use log::{debug, error, info, trace, warn, LevelFilter};
|
use log::{debug, error, info, trace, warn, LevelFilter};
|
||||||
use napi_derive_ohos::{module_exports, napi};
|
use napi_derive_ohos::{module_exports, napi};
|
||||||
use napi_ohos::threadsafe_function::{
|
use napi_ohos::bindgen_prelude::Function;
|
||||||
ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode,
|
use napi_ohos::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
|
||||||
};
|
use napi_ohos::{Env, JsObject, JsString, NapiRaw};
|
||||||
use napi_ohos::{Env, JsFunction, JsObject, JsString, NapiRaw};
|
|
||||||
use ohos_sys::xcomponent::{
|
use ohos_sys::xcomponent::{
|
||||||
OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NativeXComponent_GetTouchEvent,
|
OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NativeXComponent_GetTouchEvent,
|
||||||
OH_NativeXComponent_RegisterCallback, OH_NativeXComponent_TouchEvent,
|
OH_NativeXComponent_RegisterCallback, OH_NativeXComponent_TouchEvent,
|
||||||
|
@ -96,10 +95,22 @@ enum ServoAction {
|
||||||
Vsync,
|
Vsync,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Queue length for the thread-safe function to submit URL updates to ArkTS
|
||||||
|
const UPDATE_URL_QUEUE_SIZE: usize = 1;
|
||||||
|
/// Queue length for the thread-safe function to submit prompts to ArkTS
|
||||||
|
///
|
||||||
|
/// We can submit alerts in a non-blocking fashion, but alerts will always come from the
|
||||||
|
/// embedder thread. Specifying 4 as a max queue size seems reasonable for now, and can
|
||||||
|
/// be adjusted later.
|
||||||
|
const PROMPT_QUEUE_SIZE: usize = 4;
|
||||||
// Todo: Need to check if OnceLock is suitable, or if the TS function can be destroyed, e.g.
|
// Todo: Need to check if OnceLock is suitable, or if the TS function can be destroyed, e.g.
|
||||||
// if the activity gets suspended.
|
// if the activity gets suspended.
|
||||||
static SET_URL_BAR_CB: OnceLock<ThreadsafeFunction<String, ErrorStrategy::Fatal>> = OnceLock::new();
|
static SET_URL_BAR_CB: OnceLock<
|
||||||
static PROMPT_TOAST: OnceLock<ThreadsafeFunction<String, ErrorStrategy::Fatal>> = OnceLock::new();
|
ThreadsafeFunction<String, (), String, false, false, UPDATE_URL_QUEUE_SIZE>,
|
||||||
|
> = OnceLock::new();
|
||||||
|
static PROMPT_TOAST: OnceLock<
|
||||||
|
ThreadsafeFunction<String, (), String, false, false, PROMPT_QUEUE_SIZE>,
|
||||||
|
> = OnceLock::new();
|
||||||
|
|
||||||
impl ServoAction {
|
impl ServoAction {
|
||||||
fn dispatch_touch_event(
|
fn dispatch_touch_event(
|
||||||
|
@ -423,46 +434,29 @@ pub fn go_forward() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(js_name = "registerURLcallback")]
|
#[napi(js_name = "registerURLcallback")]
|
||||||
pub fn register_url_callback(callback: JsFunction) -> napi_ohos::Result<()> {
|
pub fn register_url_callback(callback: Function<String, ()>) -> napi_ohos::Result<()> {
|
||||||
// Currently we call the callback in a blocking fashion, always from the embedder thread,
|
|
||||||
// so a queue size of 1 is sufficient.
|
|
||||||
const UPDATE_URL_QUEUE_SIZE: usize = 1;
|
|
||||||
debug!("register_url_callback called!");
|
debug!("register_url_callback called!");
|
||||||
let function = callback.create_threadsafe_function(UPDATE_URL_QUEUE_SIZE, |ctx| {
|
let mut tsfn_builder = callback.build_threadsafe_function();
|
||||||
Ok(vec![ctx
|
let function = tsfn_builder
|
||||||
.env
|
.max_queue_size::<UPDATE_URL_QUEUE_SIZE>()
|
||||||
.create_string_from_std(ctx.value)
|
.build()?;
|
||||||
.inspect_err(|e| {
|
|
||||||
error!("Failed to create JsString: {e:?}")
|
SET_URL_BAR_CB.set(function).map_err(|_e| {
|
||||||
})?])
|
napi_ohos::Error::from_reason(
|
||||||
})?;
|
"Failed to set URL callback - register_url_callback called twice?",
|
||||||
// We ignore any error for now - but probably we should propagate it back to the TS layer.
|
)
|
||||||
let _ = SET_URL_BAR_CB
|
})
|
||||||
.set(function)
|
|
||||||
.inspect_err(|_| warn!("Failed to set URL callback - register_url_callback called twice?"));
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub fn register_prompt_toast_callback(callback: JsFunction) -> napi_ohos::Result<()> {
|
pub fn register_prompt_toast_callback(callback: Function<String, ()>) -> napi_ohos::Result<()> {
|
||||||
// We can submit alerts in a non-blocking fashion, but alerts will always come from the
|
|
||||||
// embedder thread. Specifying 4 as a max queue size seems reasonable for now, and can
|
|
||||||
// be adjusted later.
|
|
||||||
const PROMPT_QUEUE_SIZE: usize = 4;
|
|
||||||
debug!("register_prompt_toast_callback called!");
|
debug!("register_prompt_toast_callback called!");
|
||||||
let function = callback.create_threadsafe_function(PROMPT_QUEUE_SIZE, |ctx| {
|
let mut tsfn_builder = callback.build_threadsafe_function();
|
||||||
Ok(vec![ctx
|
let function = tsfn_builder.max_queue_size::<PROMPT_QUEUE_SIZE>().build()?;
|
||||||
.env
|
|
||||||
.create_string_from_std(ctx.value)
|
PROMPT_TOAST
|
||||||
.inspect_err(|e| {
|
|
||||||
error!("Failed to create JsString: {e:?}")
|
|
||||||
})?])
|
|
||||||
})?;
|
|
||||||
// We ignore any error for now - but probably we should propagate it back to the TS layer.
|
|
||||||
let _ = PROMPT_TOAST
|
|
||||||
.set(function)
|
.set(function)
|
||||||
.inspect_err(|_| error!("Failed to set prompt toast callback."));
|
.map_err(|_e| napi_ohos::Error::from_reason("Failed to set prompt toast callback"))
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue