-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from breadrock1/feat/multiple-instances
Feature: Impled multiple instances
- Loading branch information
Showing
11 changed files
with
181 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
[package] | ||
name = "adblock-coffee" | ||
version = "0.1.4" | ||
version = "1.1.7" | ||
edition = "2021" | ||
authors = ["Bread White <breadrock1@gmail.com>"] | ||
|
||
[lib] | ||
crate_type = ["cdylib"] | ||
|
||
[dependencies] | ||
anyhow = "^1.0" | ||
adblock = "^0.8" | ||
anyhow = "1.0.95" | ||
adblock = "0.9.4" | ||
env_logger = "0.11.3" | ||
jni = "^0.21" | ||
jni = "0.21.1" | ||
lazy_static = "1.5.0" | ||
log = "0.4.22" | ||
once_cell = "1.19.0" | ||
thiserror = "^1.0" | ||
thiserror = "2.0.9" | ||
|
||
[target.'cfg(target_os = "android")'.dependencies] | ||
android_logger = "^0.14" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,98 @@ | ||
use jni::objects::{JObjectArray, JString}; | ||
use jni::sys::{jboolean, jlong}; | ||
use jni::objects::{JList, JObjectArray, JString}; | ||
use jni::sys::{jboolean, jint, jlong}; | ||
use jni::JNIEnv; | ||
use lazy_static::lazy_static; | ||
use once_cell::sync::Lazy; | ||
use std::collections::HashMap; | ||
use std::sync::Mutex; | ||
|
||
use crate::adblock::AdvtBlocker; | ||
use crate::errors::RustException; | ||
use crate::errors::{Result, RustException}; | ||
|
||
lazy_static! { | ||
static ref INSTANCE: Lazy<Mutex<AdvtBlocker>> = Lazy::new(|| { | ||
let init: Mutex<AdvtBlocker> = Mutex::new(AdvtBlocker::default()); | ||
init | ||
}); | ||
static ref INSTANCE_POOL: Lazy<Mutex<HashMap<jlong, AdvtBlocker>>> = | ||
Lazy::new(|| { Mutex::new(HashMap::default()) }); | ||
} | ||
|
||
pub(crate) fn init_object_wrapped( | ||
env: &mut JNIEnv, | ||
rules: &JObjectArray, | ||
) -> Result<jlong, RustException> { | ||
pub(crate) fn init_object_wrapped(env: &mut JNIEnv, rules: &JObjectArray) -> Result<jlong> { | ||
let conv_rules = extract_list_str(env, rules)?; | ||
let mut instance_lock = INSTANCE.lock().map_err(RustException::from)?; | ||
|
||
instance_lock.recreate(conv_rules); | ||
let advt_instance = AdvtBlocker::new(conv_rules); | ||
let ptr = Box::into_raw(Box::new(&advt_instance)) as jlong; | ||
|
||
let mut instance_lock = INSTANCE_POOL.lock()?; | ||
instance_lock.insert(ptr, advt_instance); | ||
|
||
let ptr = Box::into_raw(Box::new(&INSTANCE)); | ||
Ok(ptr as jlong) | ||
Ok(ptr) | ||
} | ||
|
||
pub(crate) fn destroy_object_wrapped( | ||
_env: &mut JNIEnv, | ||
_ptr: jlong, | ||
) -> Result<jboolean, RustException> { | ||
let instance_lock = INSTANCE.lock().map_err(RustException::from)?; | ||
pub(crate) fn destroy_object_wrapped(_env: &mut JNIEnv, ptr: jlong) -> Result<jboolean> { | ||
let mut instance_lock = INSTANCE_POOL.lock()?; | ||
|
||
let Some(instance) = instance_lock.remove(&ptr) else { | ||
let msg = format!("failed to remove instance: {ptr:?}"); | ||
return Err(RustException::InstanceAccess(msg)); | ||
}; | ||
|
||
drop(instance_lock); | ||
drop(instance); | ||
Ok(true as jboolean) | ||
} | ||
|
||
pub(crate) fn check_net_urls_wrapped( | ||
env: &mut JNIEnv, | ||
_ptr: jlong, | ||
ptr: jlong, | ||
url: &JString, | ||
src_url: &JString, | ||
req_type: &JString, | ||
) -> Result<jboolean, RustException> { | ||
let advt_blocker = INSTANCE.lock().map_err(RustException::from)?; | ||
) -> Result<jboolean> { | ||
let instance_lock = INSTANCE_POOL.lock()?; | ||
let Some(advt_blocker) = instance_lock.get(&ptr) else { | ||
let msg = format!("failed to get instance: {ptr:?}"); | ||
return Err(RustException::InstanceAccess(msg)); | ||
}; | ||
|
||
let url_str = extract_str(env, url)?; | ||
let src_url_str = extract_str(env, src_url)?; | ||
let req_type_str = extract_str(env, req_type)?; | ||
|
||
let check_result = advt_blocker.check_network_urls( | ||
url_str.as_str(), | ||
src_url_str.as_str(), | ||
req_type_str.as_str(), | ||
)?; | ||
|
||
Ok(check_result as jboolean) | ||
} | ||
|
||
fn extract_str<'a>(env: &'a mut JNIEnv, j_obj: &'a JString) -> Result<String, RustException> { | ||
let j_str = env.get_string(&j_obj).map_err(RustException::from)?; | ||
|
||
let str_obj = j_str | ||
.to_str() | ||
.map_err(|err| RustException::ExtractParameter(err.to_string()))?; | ||
|
||
Ok(str_obj.to_string()) | ||
advt_blocker | ||
.check_network_urls(&url_str, &src_url_str, &req_type_str) | ||
.map(|result| result as jboolean) | ||
} | ||
|
||
fn extract_list_str<'a>( | ||
env: &'a mut JNIEnv, | ||
j_obj_arr: &'a JObjectArray, | ||
) -> Result<Vec<String>, RustException> { | ||
let j_list = env.get_list(&j_obj_arr).map_err(RustException::from)?; | ||
|
||
let j_list_size = j_list.size(env).map_err(RustException::from)?; | ||
fn extract_list_str<'a>(env: &'a mut JNIEnv, j_obj_arr: &'a JObjectArray) -> Result<Vec<String>> { | ||
let j_list = env.get_list(j_obj_arr)?; | ||
let j_list_size = j_list.size(env)?; | ||
|
||
let mut list_data = Vec::with_capacity(j_list_size as usize); | ||
for index in 0..j_list_size { | ||
let j_obj_get_result = j_list.get(env, index); | ||
if j_obj_get_result.is_err() { | ||
let err = j_obj_get_result.err().unwrap(); | ||
log::warn!("failed to parse rules: {:?}", err); | ||
continue; | ||
match extract_entity(env, &j_list, index) { | ||
Ok(data) => list_data.push(data), | ||
Err(err) => { | ||
log::error!("failed to extract str from java object: {err:#?}"); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
Ok(list_data) | ||
} | ||
|
||
let j_obj_opt = j_obj_get_result?; | ||
if j_obj_opt.is_none() { | ||
log::warn!("parsed rule is none. skipped..."); | ||
continue; | ||
fn extract_entity(env: &mut JNIEnv, j_list: &JList, index: jint) -> Result<String> { | ||
let j_obj_opt = j_list.get(env, index)?; | ||
let j_str = match j_obj_opt { | ||
Some(j_obj) => JString::from(j_obj), | ||
None => { | ||
let msg = format!("parsed rule is none: {j_obj_opt:?}. skipped..."); | ||
return Err(RustException::ParseJavaObject(msg)); | ||
} | ||
}; | ||
|
||
let j_str = JString::from(j_obj_opt.unwrap()); | ||
let str_data = extract_str(env, &j_str)?; | ||
list_data.push(str_data); | ||
} | ||
extract_str(env, &j_str) | ||
} | ||
|
||
Ok(list_data) | ||
fn extract_str<'a>(env: &'a mut JNIEnv, j_obj: &'a JString) -> Result<String> { | ||
let j_str = env.get_string(j_obj)?; | ||
let str_obj = j_str.to_str()?; | ||
Ok(str_obj.to_string()) | ||
} |
Oops, something went wrong.