Remove readfile callback

This commit is contained in:
Paul Rouget 2019-02-06 07:20:17 +01:00
parent b7e9bab267
commit 475756ec2f
6 changed files with 42 additions and 119 deletions

View file

@ -10,7 +10,7 @@ pub mod gl_glue;
use servo::compositing::windowing::{
AnimationState, EmbedderCoordinates, MouseWindowEvent, WindowEvent, WindowMethods,
};
use servo::embedder_traits::resources::{self, Resource};
use servo::embedder_traits::resources::{self, Resource, ResourceReaderMethods};
use servo::embedder_traits::EmbedderMsg;
use servo::euclid::{TypedPoint2D, TypedScale, TypedSize2D, TypedVector2D};
use servo::msg::constellation_msg::TraversalDirection;
@ -42,11 +42,6 @@ pub struct InitOptions {
pub enable_subpixel_text_antialiasing: bool,
}
/// Delegate resource file reading to the embedder.
pub trait ReadFileTrait {
fn readfile(&self, file: &str) -> Vec<u8>;
}
/// Callbacks. Implemented by embedder. Called by Servo.
pub trait HostTrait {
/// Will be called from the thread used for the init call.
@ -112,10 +107,9 @@ pub fn init(
init_opts: InitOptions,
gl: Rc<dyn gl::Gl>,
waker: Box<dyn EventLoopWaker>,
readfile: Box<dyn ReadFileTrait + Send + Sync>,
callbacks: Box<dyn HostTrait>,
) -> Result<(), &'static str> {
resources::set(Box::new(ResourceReader(readfile)));
resources::set(Box::new(ResourceReaderInstance::new()));
if let Some(args) = init_opts.args {
let mut args: Vec<String> = serde_json::from_str(&args)
@ -538,31 +532,43 @@ impl WindowMethods for ServoCallbacks {
}
}
struct ResourceReader(Box<dyn ReadFileTrait + Send + Sync>);
struct ResourceReaderInstance;
impl resources::ResourceReaderMethods for ResourceReader {
fn read(&self, file: Resource) -> Vec<u8> {
let file = match file {
Resource::Preferences => "prefs.json",
Resource::BluetoothBlocklist => "gatt_blocklist.txt",
Resource::DomainList => "public_domains.txt",
Resource::HstsPreloadList => "hsts_preload.json",
Resource::SSLCertificates => "certs",
Resource::BadCertHTML => "badcert.html",
Resource::NetErrorHTML => "neterror.html",
Resource::UserAgentCSS => "user-agent.css",
Resource::ServoCSS => "servo.css",
Resource::PresentationalHintsCSS => "presentational-hints.css",
Resource::QuirksModeCSS => "quirks-mode.css",
Resource::RippyPNG => "rippy.png",
};
info!("ResourceReader::read({})", file);
self.0.readfile(file)
impl ResourceReaderInstance {
fn new() -> ResourceReaderInstance {
ResourceReaderInstance
}
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
vec![]
}
impl ResourceReaderMethods for ResourceReaderInstance {
fn read(&self, res: Resource) -> Vec<u8> {
Vec::from(match res {
Resource::Preferences => &include_bytes!("../../../../resources/prefs.json")[..],
Resource::HstsPreloadList => {
&include_bytes!("../../../../resources/hsts_preload.json")[..]
},
Resource::SSLCertificates => &include_bytes!("../../../../resources/certs")[..],
Resource::BadCertHTML => &include_bytes!("../../../../resources/badcert.html")[..],
Resource::NetErrorHTML => &include_bytes!("../../../../resources/neterror.html")[..],
Resource::UserAgentCSS => &include_bytes!("../../../../resources/user-agent.css")[..],
Resource::ServoCSS => &include_bytes!("../../../../resources/servo.css")[..],
Resource::PresentationalHintsCSS => {
&include_bytes!("../../../../resources/presentational-hints.css")[..]
},
Resource::QuirksModeCSS => &include_bytes!("../../../../resources/quirks-mode.css")[..],
Resource::RippyPNG => &include_bytes!("../../../../resources/rippy.png")[..],
Resource::DomainList => &include_bytes!("../../../../resources/public_domains.txt")[..],
Resource::BluetoothBlocklist => {
&include_bytes!("../../../../resources/gatt_blocklist.txt")[..]
},
})
}
fn sandbox_access_files(&self) -> Vec<PathBuf> {
vec![]
}
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
vec![]
}
}

View file

@ -5,9 +5,7 @@
#[macro_use]
extern crate log;
use simpleservo::{
self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ReadFileTrait, ServoGlue, SERVO,
};
use simpleservo::{self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO};
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::c_char;
@ -68,7 +66,6 @@ fn init(
opts: CInitOptions,
gl: gl_glue::ServoGl,
wakeup: extern "C" fn(),
readfile: extern "C" fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks,
) {
let args = unsafe { CStr::from_ptr(opts.args) };
@ -87,10 +84,9 @@ fn init(
};
let wakeup = Box::new(WakeupCallback::new(wakeup));
let readfile = Box::new(ReadFileCallback::new(readfile));
let callbacks = Box::new(HostCallbacks::new(callbacks));
simpleservo::init(opts, gl, wakeup, readfile, callbacks).unwrap();
simpleservo::init(opts, gl, wakeup, callbacks).unwrap();
}
#[cfg(target_os = "windows")]
@ -98,11 +94,10 @@ fn init(
pub extern "C" fn init_with_egl(
opts: CInitOptions,
wakeup: extern "C" fn(),
readfile: extern "C" fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks,
) {
let gl = gl_glue::egl::init().unwrap();
init(opts, gl, wakeup, readfile, callbacks)
init(opts, gl, wakeup, callbacks)
}
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
@ -110,11 +105,10 @@ pub extern "C" fn init_with_egl(
pub extern "C" fn init_with_gl(
opts: CInitOptions,
wakeup: extern "C" fn(),
readfile: extern "C" fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks,
) {
let gl = gl_glue::gl::init().unwrap();
init(opts, gl, wakeup, readfile, callbacks)
init(opts, gl, wakeup, callbacks)
}
#[no_mangle]
@ -268,25 +262,6 @@ impl EventLoopWaker for WakeupCallback {
}
}
pub struct ReadFileCallback(extern "C" fn(*const c_char) -> *const c_char);
impl ReadFileCallback {
fn new(callback: extern "C" fn(*const c_char) -> *const c_char) -> ReadFileCallback {
ReadFileCallback(callback)
}
}
impl ReadFileTrait for ReadFileCallback {
fn readfile(&self, file: &str) -> Vec<u8> {
debug!("readfile: {}", file);
let file = CString::new(file).expect("Can't create string");
let file_ptr = file.as_ptr();
let content = (self.0)(file_ptr);
let content = unsafe { CStr::from_ptr(content) };
content.to_bytes().to_owned()
}
}
struct HostCallbacks(CHostCallbacks);
impl HostCallbacks {

View file

@ -13,11 +13,9 @@ use jni::sys::{jboolean, jfloat, jint, jstring, JNI_TRUE};
use jni::{errors, JNIEnv, JavaVM};
use libc::{dup2, pipe, read};
use log::Level;
use simpleservo::{
self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ReadFileTrait, ServoGlue, SERVO,
};
use simpleservo::{self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO};
use std::os::raw::{c_char, c_int, c_void};
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::thread;
struct HostCallbacks {
@ -101,11 +99,10 @@ pub fn Java_org_mozilla_servoview_JNIServo_init(
};
let wakeup = Box::new(WakeupCallback::new(callbacks_ref.clone(), &env));
let readfile = Box::new(ReadFileCallback::new(callbacks_ref.clone(), &env));
let callbacks = Box::new(HostCallbacks::new(callbacks_ref, &env));
if let Err(err) =
gl_glue::egl::init().and_then(|gl| simpleservo::init(opts, gl, wakeup, readfile, callbacks))
gl_glue::egl::init().and_then(|gl| simpleservo::init(opts, gl, wakeup, callbacks))
{
throw(&env, err)
};
@ -352,39 +349,6 @@ impl EventLoopWaker for WakeupCallback {
}
}
pub struct ReadFileCallback {
callback: Mutex<GlobalRef>,
jvm: JavaVM,
}
impl ReadFileCallback {
pub fn new(callback: GlobalRef, env: &JNIEnv) -> ReadFileCallback {
let jvm = env.get_java_vm().unwrap();
let callback = Mutex::new(callback);
ReadFileCallback { callback, jvm }
}
}
impl ReadFileTrait for ReadFileCallback {
fn readfile(&self, file: &str) -> Vec<u8> {
// FIXME: we'd rather use attach_current_thread but it detaches the VM too early.
let env = self.jvm.attach_current_thread_as_daemon().unwrap();
let s = match new_string(&env, &file) {
Ok(s) => s,
Err(_) => return vec![],
};
let s = JValue::from(JObject::from(s));
let array = env.call_method(
self.callback.lock().unwrap().as_obj(),
"readfile",
"(Ljava/lang/String;)[B",
&[s],
);
let array = array.unwrap().l().unwrap().into_inner();
env.convert_byte_array(array).unwrap()
}
}
impl HostCallbacks {
pub fn new(callbacks: GlobalRef, env: &JNIEnv) -> HostCallbacks {
let jvm = env.get_java_vm().unwrap();