Skip to content

Commit

Permalink
Cleanup of the Android specific code.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricedesre committed Feb 17, 2017
1 parent 05623b3 commit 9f631b9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 245 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions ports/servo/Cargo.toml
Expand Up @@ -46,6 +46,4 @@ libservo = {path = "../../components/servo"}
sig = "0.1"

[target.'cfg(target_os = "android")'.dependencies]
libc = "0.2"
android_glue = "0.2"
android_injected_glue = {git = "https://github.com/mmatyas/android-rs-injected-glue"}
115 changes: 30 additions & 85 deletions ports/servo/main.rs
Expand Up @@ -17,16 +17,11 @@

#![feature(start, core_intrinsics)]

#[cfg(target_os = "android")]
#[macro_use]
extern crate android_glue;
#[cfg(target_os = "android")]
extern crate android_injected_glue;
extern crate backtrace;
// The window backed by glutin
extern crate glutin_app as app;
#[cfg(target_os = "android")]
extern crate libc;
#[macro_use]
extern crate log;
// The Servo engine
Expand Down Expand Up @@ -65,9 +60,10 @@ fn install_crash_handler() {
use std::thread;

fn handler(_sig: i32) {
let name = thread::current().name()
.map(|n| format!(" for thread \"{}\"", n))
.unwrap_or("".to_owned());
let name = thread::current()
.name()
.map(|n| format!(" for thread \"{}\"", n))
.unwrap_or("".to_owned());
println!("Stack trace{}\n{:?}", name, Backtrace::new());
unsafe {
abort();
Expand All @@ -81,8 +77,7 @@ fn install_crash_handler() {
}

#[cfg(target_os = "android")]
fn install_crash_handler() {
}
fn install_crash_handler() {}

fn main() {
install_crash_handler();
Expand All @@ -106,15 +101,21 @@ fn main() {
warn!("Panic hook called.");
let msg = match info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match info.payload().downcast_ref::<String>() {
Some(s) => &**s,
None => "Box<Any>",
None => {
match info.payload().downcast_ref::<String>() {
Some(s) => &**s,
None => "Box<Any>",
}
},
};
let current_thread = thread::current();
let name = current_thread.name().unwrap_or("<unnamed>");
if let Some(location) = info.location() {
println!("{} (thread {}, at {}:{})", msg, name, location.file(), location.line());
println!("{} (thread {}, at {}:{})",
msg,
name,
location.file(),
location.line());
} else {
println!("{} (thread {})", msg, name);
}
Expand All @@ -128,7 +129,7 @@ fn main() {
setup_logging();

if let Some(token) = content_process_token {
return servo::run_content_process(token)
return servo::run_content_process(token);
}

if opts::get().is_printing_version {
Expand All @@ -155,17 +156,16 @@ fn main() {
loop {
let should_continue = browser.browser.handle_events(window.wait_events());
if !should_continue {
break
break;
}
};
}

unregister_glutin_resize_handler(&window);

platform::deinit()
}

fn register_glutin_resize_handler(window: &Rc<app::window::Window>,
browser: &mut BrowserWrapper) {
fn register_glutin_resize_handler(window: &Rc<app::window::Window>, browser: &mut BrowserWrapper) {
unsafe {
window.set_nested_event_loop_listener(browser);
}
Expand All @@ -188,7 +188,7 @@ impl app::NestedEventLoopListener for BrowserWrapper {
_ => false,
};
if !self.browser.handle_events(vec![event]) {
return false
return false;
}
if is_resize {
self.browser.repaint_synchronously()
Expand All @@ -199,12 +199,14 @@ impl app::NestedEventLoopListener for BrowserWrapper {

#[cfg(target_os = "android")]
fn setup_logging() {
android::setup_logging();
// Piping logs from stdout/stderr to logcat happens in android_injected_glue.
::std::env::set_var("RUST_LOG", "debug");

unsafe { android_injected_glue::ffi::app_dummy() };
}

#[cfg(not(target_os = "android"))]
fn setup_logging() {
}
fn setup_logging() {}

#[cfg(target_os = "android")]
/// Attempt to read parameters from a file since they are not passed to us in Android environments.
Expand Down Expand Up @@ -233,11 +235,10 @@ fn args() -> Vec<String> {
vec
},
Err(e) => {
debug!("Failed to open params file '{}': {}", PARAMS_FILE, Error::description(&e));
vec![
"servo".to_owned(),
"http://en.wikipedia.org/wiki/Rust".to_owned()
]
debug!("Failed to open params file '{}': {}",
PARAMS_FILE,
Error::description(&e));
vec!["servo".to_owned(), "http://en.wikipedia.org/wiki/Rust".to_owned()]
},
}
}
Expand All @@ -254,61 +255,5 @@ fn args() -> Vec<String> {
#[inline(never)]
#[allow(non_snake_case)]
pub extern "C" fn android_main(app: *mut ()) {
android_injected_glue::android_main2(app as *mut _, move |_, _| { main() });
}


#[cfg(target_os = "android")]
mod android {
extern crate android_glue;
extern crate android_injected_glue;
extern crate libc;

use self::libc::c_int;
use std::borrow::ToOwned;

pub fn setup_logging() {
use self::libc::{STDERR_FILENO, STDOUT_FILENO};
//use std::env;

//env::set_var("RUST_LOG", "servo,gfx,msg,util,layers,js,std,rt,extra");
redirect_output(STDERR_FILENO);
redirect_output(STDOUT_FILENO);

unsafe { android_injected_glue::ffi::app_dummy() };
}

struct FilePtr(*mut self::libc::FILE);

unsafe impl Send for FilePtr {}

fn redirect_output(file_no: c_int) {
use self::libc::{pipe, dup2};
use self::libc::fdopen;
use self::libc::fgets;
use std::ffi::CStr;
use std::ffi::CString;
use std::str::from_utf8;
use std::thread;

unsafe {
let mut pipes: [c_int; 2] = [ 0, 0 ];
pipe(pipes.as_mut_ptr());
dup2(pipes[1], file_no);
let mode = CString::new("r").unwrap();
let input_file = FilePtr(fdopen(pipes[0], mode.as_ptr()));
thread::Builder::new().name("android-logger".to_owned()).spawn(move || {
static READ_SIZE: usize = 1024;
let mut read_buffer = vec![0; READ_SIZE];
let FilePtr(input_file) = input_file;
loop {
fgets(read_buffer.as_mut_ptr(), (read_buffer.len() as i32)-1, input_file);
let c_str = CStr::from_ptr(read_buffer.as_ptr());
let slice = from_utf8(c_str.to_bytes()).unwrap();
android_glue::write_log(slice);
}
});
}
}

android_injected_glue::android_main2(app as *mut _, move |_, _| main());
}
58 changes: 0 additions & 58 deletions support/android/apk/jni/main.c

This file was deleted.

4 changes: 3 additions & 1 deletion support/android/apk/src/com/mozilla/servo/MainActivity.java
Expand Up @@ -16,10 +16,12 @@


public class MainActivity extends android.app.NativeActivity {
private static final String LOGTAG="servo_wrapper";
private static final String LOGTAG="ServoWrapper";
static {
Log.i(LOGTAG, "Loading the NativeActivity");
// libmain.so contains all of Servo native code with the injected glue.
System.loadLibrary("main");
Log.i(LOGTAG, "libmain.so loaded");
}

private void set_url(String url) {
Expand Down

0 comments on commit 9f631b9

Please sign in to comment.