Something went wrong. Try again.
Rewild Your Web forked from me.webbeef.org/browser.html
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135// SPDX-License-Identifier: AGPL-3.0-or-later
//! Android entry point. The app's NativeActivity loads libbeaver_shell.so, and//! android-activity calls `android_main` on a thread of its own. Before that it//! has already sent stdout and stderr to logcat (tag `RustStdoutStderr`).
mod assets;mod choreographer;
use std::ffi::{CStr, c_char};use std::path::{Path, PathBuf};use std::sync::OnceLock;use std::sync::atomic::{AtomicBool, Ordering};
use android_logger::{Config, FilterBuilder};use log::{LevelFilter, error, warn};use winit::event_loop::{ActiveEventLoop, EventLoop};use winit::platform::android::activity::AndroidApp;use winit::platform::android::activity::ndk::configuration::UiModeType;use winit::platform::android::{ActiveEventLoopExtAndroid, EventLoopBuilderExtAndroid};
pub(crate) use self::choreographer::ChoreographerRefreshDriver;use crate::CliArgs;
/// The app's private files directory, `/data/user/0/<app id>/files`.static DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
/// Set by the first `android_main` of the process.static STARTED: AtomicBool = AtomicBool::new(false);
/// The app's private files directory, which holds `config_dir()`.pub(crate) fn data_dir() -> Option<&'static Path> { DATA_DIR.get().map(PathBuf::as_path)}
/// The value of a system property, empty if it isn't set. `adb shell setprop`/// sets the `debug.` ones.pub(crate) fn system_property(name: &CStr) -> String { let mut value = [0 as c_char; libc::PROP_VALUE_MAX as usize]; // SAFETY: the name is NUL-terminated, and `value` holds the longest value // a property can have, with its NUL. unsafe { libc::__system_property_get(name.as_ptr(), value.as_mut_ptr()) }; // SAFETY: the value is NUL-terminated. unsafe { CStr::from_ptr(value.as_ptr()) } .to_string_lossy() .into_owned()}
/// Whether the device is a TV (Android TV, Google TV).pub(crate) fn is_television(event_loop: &ActiveEventLoop) -> bool { event_loop.android_app().config().ui_mode_type() == UiModeType::Television}
#[unsafe(no_mangle)]fn android_main(app: AndroidApp) { android_logger::init_once( Config::default() .with_tag("beaver") .with_max_level(LevelFilter::Info) .with_filter( FilterBuilder::new() .parse("warn,beaver_shell=info,servo_media_android=info") .build(), ), );
// android-activity calls android_main again for each new Activity in the // process, but Servo, winit's event loop and our statics can only be set // up once per process. if STARTED.swap(true, Ordering::SeqCst) { error!("Beaver already ran in this process; exiting so the next launch starts afresh"); exit_process(); }
// Log panics straight to logcat: stderr only gets there through a pipe and // a thread, which may not run before the process goes down. std::panic::set_hook(Box::new(|info| { let thread = std::thread::current(); let backtrace = std::backtrace::Backtrace::force_capture(); error!( "thread '{}' {info}\n{backtrace}", thread.name().unwrap_or("<unnamed>") ); }));
let Some(files_dir) = app.internal_data_path() else { error!("The app has no internal data path"); exit_process(); }; let share_dir = match assets::ensure_share_dir(&app, &files_dir) { Ok(share_dir) => share_dir, Err(e) => { error!("Could not extract the UI into {}: {e}", files_dir.display()); exit_process(); }, }; let tmp_dir = files_dir.join("tmp"); let _ = std::fs::remove_dir_all(&tmp_dir); if let Err(e) = std::fs::create_dir_all(&tmp_dir) { warn!("Could not create {}: {e}", tmp_dir.display()); }
// SAFETY: this runs before Beaver starts any thread, and nothing else in // the process reads these variables. unsafe { // Read by resources::ancestor_dir_path(). std::env::set_var("BEAVER_SHARE_DIR", &share_dir); // std::env::temp_dir() and SQLite's temporary files otherwise go to // /data/local/tmp, which apps can't write to. std::env::set_var("TMPDIR", &tmp_dir); } let _ = DATA_DIR.set(files_dir);
crate::init_process(); let event_loop = match EventLoop::with_user_event().with_android_app(app).build() { Ok(event_loop) => event_loop, Err(e) => { error!("Failed to create the event loop: {e}"); exit_process(); }, }; if let Err(e) = crate::run(event_loop, CliArgs::default()) { error!("{e}"); } exit_process();}
/// Ends the process without running the C++ static destructors, which would/// race Servo's threads. Servo can't be shut down and set up again in the same/// process anyway.fn exit_process() -> ! { // SAFETY: _exit has no preconditions. unsafe { libc::_exit(0) }}