Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ bitwarden-state = { workspace = true, features = ["uniffi"] }
bitwarden-uniffi-error = { workspace = true }
bitwarden-vault = { workspace = true, features = ["uniffi"] }
chrono = { workspace = true, features = ["std"] }
env_logger = "0.11.1"
log = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
uniffi = { workspace = true }
uuid = { workspace = true }

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.15"
tracing-android = "0.2.0"

# The use of rustls-platform-verifier requires some extra support to communicate with the Android platform
jni = ">=0.21, <0.22"
libloading = ">=0.8.1, <0.9"
rustls-platform-verifier = "0.6.0"

[target.'cfg(target_os = "ios")'.dependencies]
oslog = "0.2.0"
tracing-oslog = "0.3.0"

[build-dependencies]
uniffi = { workspace = true, features = ["build"] }
Expand Down
15 changes: 8 additions & 7 deletions crates/bitwarden-uniffi/src/android_support.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, sync::OnceLock};

use jni::sys::{JavaVM, jint, jsize};
use tracing::{error, info};

pub static JAVA_VM: OnceLock<jni::JavaVM> = OnceLock::new();

Expand All @@ -9,7 +10,7 @@ pub static JAVA_VM: OnceLock<jni::JavaVM> = OnceLock::new();
#[allow(non_snake_case)]
#[unsafe(no_mangle)]
pub extern "system" fn JNI_OnLoad(vm_ptr: jni::JavaVM, _reserved: *mut std::ffi::c_void) -> jint {
log::info!("JNI_OnLoad initializing");
info!("JNI_OnLoad initializing");
JAVA_VM.get_or_init(|| vm_ptr);
jni::sys::JNI_VERSION_1_6
}
Expand All @@ -18,25 +19,25 @@ pub fn init() {
fn init_inner() -> Result<(), Box<dyn Error>> {
let jvm = match JAVA_VM.get() {
Some(jvm) => {
log::info!("JavaVM already initialized");
info!("JavaVM already initialized");
jvm
}
None => {
log::info!("JavaVM not initialized, initializing now");
info!("JavaVM not initialized, initializing now");
let jvm = java_vm()?;
JAVA_VM.get_or_init(|| jvm)
}
};

let mut env = jvm.attach_current_thread_permanently()?;
log::info!("Initializing Android verifier");
info!("Initializing Android verifier");
init_verifier(&mut env)?;
log::info!("SDK Android support initialized");
info!("SDK Android support initialized");
Ok(())
}

if let Err(e) = init_inner() {
log::error!("Failed to initialize Android support: {:#?}", e);
if let Err(error) = init_inner() {
error!(%error, "Failed to initialize Android support");
}
}

Expand Down
75 changes: 59 additions & 16 deletions crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

uniffi::setup_scaffolding!();

use std::sync::Arc;
use std::sync::{Arc, Once};

use auth::AuthClient;
use bitwarden_core::{ClientSettings, client::internal::ClientManagedTokens};
Expand Down Expand Up @@ -111,22 +111,65 @@ impl Client {
}
}

static INIT: Once = Once::new();

fn init_logger() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let _ = env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.try_init();

#[cfg(target_os = "ios")]
let _ = oslog::OsLogger::new("com.8bit.bitwarden")
.level_filter(log::LevelFilter::Info)
.init();

#[cfg(target_os = "android")]
android_logger::init_once(
android_logger::Config::default()
.with_tag("com.bitwarden.sdk")
.with_max_level(log::LevelFilter::Info),
);
use tracing_subscriber::{EnvFilter, layer::SubscriberExt as _, util::SubscriberInitExt as _};

INIT.call_once(|| {
// the log level prioritization is determined by:
// 1. if RUST_LOG is detected at runtime
// 2. if RUST_LOG is provided at compile time
// 3. default to INFO
let filter = EnvFilter::builder()
.with_default_directive(
option_env!("RUST_LOG")
.unwrap_or("info")
.parse()
.expect("should provide valid log level at compile time."),
)
.from_env_lossy();

let fmtlayer = tracing_subscriber::fmt::layer()
.with_ansi(true)
.with_file(true)
.with_line_number(true)
.with_target(true)
.pretty();

#[cfg(target_os = "ios")]
{
const TAG: &str = "com.8bit.bitwarden";

tracing_subscriber::registry()
.with(fmtlayer)
.with(filter)
.with(tracing_oslog::OsLogger::new(TAG, "default"))
.init();
}

#[cfg(target_os = "android")]
{
const TAG: &str = "com.bitwarden.sdk";

tracing_subscriber::registry()
.with(fmtlayer)
.with(filter)
.with(
tracing_android::layer(TAG)
.expect("initialization of android logcat tracing layer"),
)
.init();
}

#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
tracing_subscriber::registry()
.with(fmtlayer)
.with(filter)
.init();
}
});
}

/// Setup the error converter to ensure conversion errors don't cause panics
Expand Down
Loading