From 5a2459748ba350e22798afb5208485febb0e6b6a Mon Sep 17 00:00:00 2001 From: Connor Imes Date: Thu, 29 Oct 2015 15:16:21 -0500 Subject: [PATCH] Read params from a file on Android --- components/servo/main.rs | 38 ++++++++++++++++++++++++++++++++++---- resources/android_params | 9 +++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 resources/android_params diff --git a/components/servo/main.rs b/components/servo/main.rs index 34c9bfe3d90..b339babb972 100644 --- a/components/servo/main.rs +++ b/components/servo/main.rs @@ -23,6 +23,8 @@ extern crate android_glue; // The window backed by glutin extern crate glutin_app as app; extern crate env_logger; +#[macro_use] +extern crate log; // The Servo engine extern crate servo; extern crate time; @@ -128,11 +130,39 @@ fn setup_logging() { } #[cfg(target_os = "android")] +/// Attempt to read parameters from a file since they are not passed to us in Android environments. +/// The first line should be the "servo" argument and the last should be the URL to load. +/// Blank lines and those beginning with a '#' are ignored. +/// Each line should be a separate parameter as would be parsed by the shell. +/// For example, "servo -p 10 http://en.wikipedia.org/wiki/Rust" would take 4 lines. fn args() -> Vec { - vec![ - "servo".to_owned(), - "http://en.wikipedia.org/wiki/Rust".to_owned() - ] + use std::error::Error; + use std::fs::File; + use std::io::{BufRead, BufReader}; + + const PARAMS_FILE: &'static str = "/sdcard/servo/android_params"; + match File::open(PARAMS_FILE) { + Ok(f) => { + let mut vec = Vec::new(); + let file = BufReader::new(&f); + for line in file.lines() { + let l = line.unwrap().trim().to_owned(); + // ignore blank lines and those that start with a '#' + match l.is_empty() || l.as_bytes()[0] == b'#' { + true => (), + false => vec.push(l), + } + } + 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() + ] + }, + } } #[cfg(not(target_os = "android"))] diff --git a/resources/android_params b/resources/android_params new file mode 100644 index 00000000000..17775237c00 --- /dev/null +++ b/resources/android_params @@ -0,0 +1,9 @@ +# The first line here should be the "servo" argument (without quotes) and the +# last should be the URL to load. +# Blank lines and those beginning with a '#' are ignored. +# Each line should be a separate parameter as would be parsed by the shell. +# For example, "servo -p 10 http://en.wikipedia.org/wiki/Rust" would take 4 +# lines (the "-p" and "10" are separate even though they are related). + +servo +http://en.wikipedia.org/wiki/Rust