|
| 1 | +use core::fmt; |
| 2 | +use css_inline::{CSSInliner, StylesheetCache}; |
| 3 | +use jni::{ |
| 4 | + JNIEnv, |
| 5 | + errors::Result as JNIResult, |
| 6 | + objects::{JClass, JObject, JString}, |
| 7 | + sys::jstring, |
| 8 | +}; |
| 9 | +use std::{borrow::Cow, num::NonZeroUsize}; |
| 10 | + |
| 11 | +trait JNIExt { |
| 12 | + fn get_bool_field(&mut self, obj: &JObject, name: &str) -> JNIResult<bool>; |
| 13 | + fn get_int_field(&mut self, obj: &JObject, name: &str) -> JNIResult<i32>; |
| 14 | + fn get_string_field_opt(&mut self, obj: &JObject, name: &str) -> JNIResult<Option<String>>; |
| 15 | +} |
| 16 | + |
| 17 | +impl<'a> JNIExt for JNIEnv<'a> { |
| 18 | + fn get_bool_field(&mut self, obj: &JObject, name: &str) -> JNIResult<bool> { |
| 19 | + self.get_field(obj, name, "Z")?.z() |
| 20 | + } |
| 21 | + |
| 22 | + fn get_int_field(&mut self, obj: &JObject, name: &str) -> JNIResult<i32> { |
| 23 | + self.get_field(obj, name, "I")?.i() |
| 24 | + } |
| 25 | + |
| 26 | + fn get_string_field_opt(&mut self, cfg: &JObject, name: &str) -> JNIResult<Option<String>> { |
| 27 | + let value = self.get_field(cfg, name, "Ljava/lang/String;")?.l()?; |
| 28 | + if value.is_null() { |
| 29 | + Ok(None) |
| 30 | + } else { |
| 31 | + Ok(Some(self.get_string(&JString::from(value))?.into())) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +enum Error<E> { |
| 37 | + Jni(jni::errors::Error), |
| 38 | + Other(E), |
| 39 | +} |
| 40 | + |
| 41 | +impl<E> From<jni::errors::Error> for Error<E> { |
| 42 | + fn from(value: jni::errors::Error) -> Self { |
| 43 | + Error::Jni(value) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<E: fmt::Display> fmt::Display for Error<E> { |
| 48 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 49 | + match self { |
| 50 | + Error::Jni(error) => error.fmt(f), |
| 51 | + Error::Other(error) => error.fmt(f), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn build_inliner( |
| 57 | + env: &mut JNIEnv, |
| 58 | + cfg: JObject, |
| 59 | +) -> Result<CSSInliner<'static>, Error<css_inline::ParseError>> { |
| 60 | + let inline_style_tags = env.get_bool_field(&cfg, "inlineStyleTags")?; |
| 61 | + let keep_style_tags = env.get_bool_field(&cfg, "keepStyleTags")?; |
| 62 | + let keep_link_tags = env.get_bool_field(&cfg, "keepLinkTags")?; |
| 63 | + let load_remote_stylesheets = env.get_bool_field(&cfg, "loadRemoteStylesheets")?; |
| 64 | + let cache_size = env.get_int_field(&cfg, "cacheSize")?; |
| 65 | + let preallocate_node_capacity = env.get_int_field(&cfg, "preallocateNodeCapacity")?; |
| 66 | + |
| 67 | + let extra_css = env.get_string_field_opt(&cfg, "extraCss")?; |
| 68 | + let base_url = env.get_string_field_opt(&cfg, "baseUrl")?; |
| 69 | + let mut builder = CSSInliner::options() |
| 70 | + .inline_style_tags(inline_style_tags) |
| 71 | + .keep_style_tags(keep_style_tags) |
| 72 | + .keep_link_tags(keep_link_tags) |
| 73 | + .load_remote_stylesheets(load_remote_stylesheets) |
| 74 | + .extra_css(extra_css.map(Cow::Owned)) |
| 75 | + .preallocate_node_capacity(preallocate_node_capacity as usize); |
| 76 | + |
| 77 | + if let Some(url) = base_url { |
| 78 | + match css_inline::Url::parse(&url) { |
| 79 | + Ok(url) => { |
| 80 | + builder = builder.base_url(Some(url)); |
| 81 | + } |
| 82 | + Err(error) => return Err(Error::Other(error)), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if cache_size > 0 { |
| 87 | + builder = builder.cache(StylesheetCache::new( |
| 88 | + NonZeroUsize::new(cache_size as usize).expect("Cache size is not null"), |
| 89 | + )); |
| 90 | + } |
| 91 | + |
| 92 | + Ok(builder.build()) |
| 93 | +} |
| 94 | + |
| 95 | +fn throw(mut env: JNIEnv, message: String) -> jstring { |
| 96 | + let exception = env |
| 97 | + .find_class("org/cssinline/CssInlineException") |
| 98 | + .expect("CssInlineException class not found"); |
| 99 | + env.throw_new(exception, message) |
| 100 | + .expect("Failed to throw CssInlineException"); |
| 101 | + std::ptr::null_mut() |
| 102 | +} |
| 103 | + |
| 104 | +#[unsafe(no_mangle)] |
| 105 | +pub extern "system" fn Java_org_cssinline_CssInline_nativeInline( |
| 106 | + mut env: JNIEnv, |
| 107 | + _class: JClass, |
| 108 | + input: JString, |
| 109 | + cfg: JObject, |
| 110 | +) -> jstring { |
| 111 | + let html: String = env |
| 112 | + .get_string(&input) |
| 113 | + .expect("Failed to get Java String") |
| 114 | + .into(); |
| 115 | + let inliner = match build_inliner(&mut env, cfg) { |
| 116 | + Ok(inliner) => inliner, |
| 117 | + Err(error) => return throw(env, error.to_string()), |
| 118 | + }; |
| 119 | + match inliner.inline(&html) { |
| 120 | + Ok(out) => env |
| 121 | + .new_string(out) |
| 122 | + .expect("Failed to get Java String") |
| 123 | + .into_raw(), |
| 124 | + Err(error) => throw(env, error.to_string()), |
| 125 | + } |
| 126 | +} |
0 commit comments