Modern Apps git

← Modern-Apps.git

web: convert JNI surface to shared jni_fn! macro

Commit
ad6d3f74d0fd1cbfe211d4d14c76ccce8d78e559
Author
Vayun Mathur <[email protected]>
When
2026-12-01 19:37 UTC (4 days ago)
Parents
6971bd9
LOC Phase 5 rollout. Six of the seven web_shields JNI
functions repeated the EnvUnowned / with_env /
LogErrorAndDefault scaffold and convert to
jni_bridge::jni_fn!; nativeDestroy takes an unused _env
and stays hand-written. Bodies are verbatim (verified
token-for-token); handles and null checks stay in them.

Lines: lib.rs 363 -> 303 (-60, -16.5%); structure_check
web module 18,701 after, structure: OK. Cargo.lock gains
only the jni_bridge edge for web_shields.

Verified: nm -D on libweb_shields.so before/after -
exported symbol set identical (7/7 Java_ ShieldsNative
symbols); cargo test -p web_shields: 37 passed; JVM tests
:web:testDevUnitTest StoresAndSettingsHelpersTest: 7/7
passed (the only test class referencing ShieldsNative;
web has no testDevUnitTestNative split; run excludes the
pre-existing rustFileLength ratchet); cargo fmt --check
-p web_shields clean.

Diff

Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex dc5d29b689..0dfcaa0afc 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6496,6 +6496,7 @@ version = "0.1.0" dependencies = [  "adblock",  "jni",+ "jni_bridge",  "serde_json", ] 
web/src/main/rust/Cargo.toml
diff --git a/web/src/main/rust/Cargo.toml b/web/src/main/rust/Cargo.tomlindex 07733cc532..5ab5ebbcd5 100644--- a/web/src/main/rust/Cargo.toml+++ b/web/src/main/rust/Cargo.toml@@ -15,4 +15,5 @@ crate-type = ["cdylib", "rlib"] # the render thread (shouldInterceptRequest) and the UI thread. adblock.workspace = true jni.workspace = true+jni_bridge = { path = "../../../../library/jni-bridge/src/main/rust" } serde_json.workspace = true
web/src/main/rust/src/lib.rs
diff --git a/web/src/main/rust/src/lib.rs b/web/src/main/rust/src/lib.rsindex 39854076fb..5c922acd2f 100644--- a/web/src/main/rust/src/lib.rs+++ b/web/src/main/rust/src/lib.rs@@ -24,6 +24,7 @@ use adblock::Engine; use jni::objects::{JByteArray, JClass, JString}; use jni::sys::{jbyteArray, jlong, jstring}; use jni::{Env, EnvUnowned};+use jni_bridge::jni_fn; use serde_json::{json, Value}; use std::collections::HashSet; @@ -234,122 +235,61 @@ pub(crate) fn destroy_handle(handle: jlong) {     } } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeCreate<'l>(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    filters: JString<'l>,-    resources_json: JString<'l>,-) -> jlong {-    env.with_env(|env| -> jni::errors::Result<jlong> {-        Ok(create_handle(jstr(env, &filters), jstr(env, &resources_json)))-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeCreate -> jlong; (_class: JClass<'l>, filters: JString<'l>, resources_json: JString<'l>) |env| {+    Ok(create_handle(jstr(env, &filters), jstr(env, &resources_json)))+} } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeCreateFromCache<'l>(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    cache: JByteArray<'l>,-    resources_json: JString<'l>,-) -> jlong {-    env.with_env(|env| -> jni::errors::Result<jlong> {-        match env.convert_byte_array(&cache) {-            Ok(bytes) => Ok(create_cache_handle(bytes, jstr(env, &resources_json))),-            Err(_) => Ok(0),-        }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeCreateFromCache -> jlong; (_class: JClass<'l>, cache: JByteArray<'l>, resources_json: JString<'l>) |env| {+    match env.convert_byte_array(&cache) {+        Ok(bytes) => Ok(create_cache_handle(bytes, jstr(env, &resources_json))),+        Err(_) => Ok(0),+    }+} } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeSerialize<'l>(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    handle: jlong,-) -> jbyteArray {-    env.with_env(|env| -> jni::errors::Result<jbyteArray> {-        match serialize_handle(handle) {-            Some(bytes) => match env.byte_array_from_slice(&bytes) {-                Ok(a) => Ok(a.into_raw()),-                Err(_) => Ok(std::ptr::null_mut()),-            },-            None => Ok(std::ptr::null_mut()),-        }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeSerialize -> jbyteArray; (_class: JClass<'l>, handle: jlong) |env| {+    match serialize_handle(handle) {+        Some(bytes) => match env.byte_array_from_slice(&bytes) {+            Ok(a) => Ok(a.into_raw()),+            Err(_) => Ok(std::ptr::null_mut()),+        },+        None => Ok(std::ptr::null_mut()),+    }+} } -/// `{"blocked":bool,"important":bool,"exception":bool,"redirect":str?,"rewritten":str?}`-///-/// `redirect` carries the body of the uBO resource named by a `$redirect` rule, already-/// resolved by the engine. `rewritten` is the `$removeparam`-cleaned URL, which applies-/// only when the request is not blocked.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeCheck<'l>(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    handle: jlong,-    url: JString<'l>,-    source_url: JString<'l>,-    request_type: JString<'l>,-) -> jstring {-    env.with_env(|env| -> jni::errors::Result<jstring> {-        match check_handle(-            handle,-            jstr(env, &url),-            jstr(env, &source_url),-            jstr(env, &request_type),-        ) {-            Some(s) => Ok(out(env, s)),-            None => Ok(std::ptr::null_mut()),-        }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+// `{"blocked":bool,"important":bool,"exception":bool,"redirect":str?,"rewritten":str?}`+//+// `redirect` carries the body of the uBO resource named by a `$redirect` rule, already+// resolved by the engine. `rewritten` is the `$removeparam`-cleaned URL, which applies+// only when the request is not blocked.+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeCheck -> jstring; (_class: JClass<'l>, handle: jlong, url: JString<'l>, source_url: JString<'l>, request_type: JString<'l>) |env| {+    match check_handle(+        handle,+        jstr(env, &url),+        jstr(env, &source_url),+        jstr(env, &request_type),+    ) {+        Some(s) => Ok(out(env, s)),+        None => Ok(std::ptr::null_mut()),+    }+} } -/// `{"hide":[sel],"procedural":[json],"exceptions":[sel],"script":str,"generichide":bool}`-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeCosmeticResources<-    'l,->(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    handle: jlong,-    url: JString<'l>,-) -> jstring {-    env.with_env(|env| -> jni::errors::Result<jstring> {-        match cosmetic_handle(handle, jstr(env, &url)) {-            Some(s) => Ok(out(env, s)),-            None => Ok(std::ptr::null_mut()),-        }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+// `{"hide":[sel],"procedural":[json],"exceptions":[sel],"script":str,"generichide":bool}`+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeCosmeticResources -> jstring; (_class: JClass<'l>, handle: jlong, url: JString<'l>) |env| {+    match cosmetic_handle(handle, jstr(env, &url)) {+        Some(s) => Ok(out(env, s)),+        None => Ok(std::ptr::null_mut()),+    }+} } -/// Second cosmetic pass: classes and ids discovered by the page's `MutationObserver`-/// are mapped to any generic hide rules that reference them. All three arguments are-/// JSON string arrays; the result is a JSON array of CSS selectors.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_web_shields_ShieldsNative_nativeHiddenClassIdSelectors<-    'l,->(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    handle: jlong,-    classes: JString<'l>,-    ids: JString<'l>,-    exceptions: JString<'l>,-) -> jstring {-    env.with_env(|env| -> jni::errors::Result<jstring> {-        match hidden_handle(handle, jstr(env, &classes), jstr(env, &ids), jstr(env, &exceptions)) {-            Some(s) => Ok(out(env, s)),-            None => Ok(std::ptr::null_mut()),-        }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+// Second cosmetic pass: classes and ids discovered by the page's `MutationObserver`+// are mapped to any generic hide rules that reference them. All three arguments are+// JSON string arrays; the result is a JSON array of CSS selectors.+jni_fn! { Java_com_vayunmathur_web_shields_ShieldsNative_nativeHiddenClassIdSelectors -> jstring; (_class: JClass<'l>, handle: jlong, classes: JString<'l>, ids: JString<'l>, exceptions: JString<'l>) |env| {+    match hidden_handle(handle, jstr(env, &classes), jstr(env, &ids), jstr(env, &exceptions)) {+        Some(s) => Ok(out(env, s)),+        None => Ok(std::ptr::null_mut()),+    }+} }  /// # Safety /// `handle` must not be used again after this call.