Repository: servo/servo
File: ports/servoshell/egl/ohos/mod.rs (Rust)

Code before:
    let xc_wrapper = XComponentWrapper(xcomponent);
    let window_wrapper = WindowWrapper(window);

    // TODO: This if will be removed once we add multi-window support in a follow-up PR.
    // This function will always be invoked on the UI thread, so there is no concurrency.
    if FIRST_WINDOW.load(Ordering::Relaxed) {
        FIRST_WINDOW.store(false, Ordering::Relaxed);
        // The servo event loop is initialized before the native window / xcomponent is created,
        // so if this fails, servo has crashed and we don't have a way to recover.
        call(ServoAction::CreatePlatformWindow(
            xc_wrapper,
            window_wrapper,
        ))
        .expect("Servo main thread channel not initialized");

        let native_vsync =
            ohos_vsync::NativeVsync::new("ServoVsync").expect("Failed to create NativeVsync");
        // get_period() returns an error - perhaps we need to wait until the first callback?
        // info!("Native vsync period is {} nanoseconds", native_vsync.get_period().unwrap());
        unsafe {
            native_vsync
                .request_raw_callback_with_self(Some(on_vsync_cb))
                .expect("Failed to request vsync callback")
        }
        info!("Enabled Vsync!");
    } else {
        call(ServoAction::NewWebview(xc_wrapper, window_wrapper))
            .expect("Servo main thread channel not initialized");
    }
    info!("Returning from on_surface_created_cb");
}

/// Returns the offset of the surface relative to its parent's top left corner
///
/// # Safety
///
/// `xcomponent` and `native_window` must be valid, non-null and aligned pointers to a
/// live xcomponent and associated native window surface.
unsafe fn get_xcomponent_offset(
    xcomponent: *mut OH_NativeXComponent,
    native_window: *mut c_void,
) -> Result<Point2D<i32, DevicePixel>, i32> {
    let mut x: f64 = 0.0;
    let mut y: f64 = 0.0;

    let result = unsafe {
        OH_NativeXComponent_GetXComponentOffset(xcomponent, native_window, &raw mut x, &raw mut y)
    };
    if result != 0 {
        error!("OH_NativeXComponent_GetXComponentOffset failed with {result}");
        return Err(result);

Review comment (suggestion):
```suggestion
            .expect("Could not create new webview");
```

Code after:
    let xc_wrapper = XComponentWrapper(xcomponent);
    let window_wrapper = WindowWrapper(window);

    // TODO: This if will be removed once we add multi-window support in a follow-up PR.
    // This function will always be invoked on the UI thread, so there is no concurrency.
    if FIRST_WINDOW.load(Ordering::Relaxed) {
        FIRST_WINDOW.store(false, Ordering::Relaxed);
        // The servo event loop is initialized before the native window / xcomponent is created,
        // so if this fails, servo has crashed and we don't have a way to recover.
        call(ServoAction::CreatePlatformWindow(
            xc_wrapper,
            window_wrapper,
        ))
        .expect("Servo main thread channel not initialized");

        let native_vsync =
            ohos_vsync::NativeVsync::new("ServoVsync").expect("Failed to create NativeVsync");
        unsafe {
            native_vsync
                .request_raw_callback_with_self(Some(on_vsync_cb))
                .expect("Failed to request vsync callback")
        }
        info!("Enabled Vsync!");
    } else {
        call(ServoAction::NewWebview(xc_wrapper, window_wrapper))
            .expect("Servo main thread channel not initialized");
    }
    info!("Returning from on_surface_created_cb");
}

/// Returns the offset of the surface relative to its parent's top left corner
///
/// # Safety
///
/// `xcomponent` and `native_window` must be valid, non-null and aligned pointers to a
/// live xcomponent and associated native window surface.
unsafe fn get_xcomponent_offset(
    xcomponent: *mut OH_NativeXComponent,
    native_window: *mut c_void,
) -> Result<Point2D<i32, DevicePixel>, i32> {
    let mut x: f64 = 0.0;
    let mut y: f64 = 0.0;

    let result = unsafe {
        OH_NativeXComponent_GetXComponentOffset(xcomponent, native_window, &raw mut x, &raw mut y)
    };
    if result != 0 {
        error!("OH_NativeXComponent_GetXComponentOffset failed with {result}");
        return Err(result);
    }
    let x = (x.round() as i64).try_into().expect("X offset too large");