diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index 2611ff2b5f6..50da03e8906 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -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; -} - /// 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, waker: Box, - readfile: Box, callbacks: Box, ) -> 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 = serde_json::from_str(&args) @@ -538,31 +532,43 @@ impl WindowMethods for ServoCallbacks { } } -struct ResourceReader(Box); +struct ResourceReaderInstance; -impl resources::ResourceReaderMethods for ResourceReader { - fn read(&self, file: Resource) -> Vec { - 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 { - vec![] +} + +impl ResourceReaderMethods for ResourceReaderInstance { + fn read(&self, res: Resource) -> Vec { + 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 { vec![] } + + fn sandbox_access_files_dirs(&self) -> Vec { + vec![] + } } diff --git a/ports/libsimpleservo/capi/src/lib.rs b/ports/libsimpleservo/capi/src/lib.rs index fec34752299..2ea3edab711 100644 --- a/ports/libsimpleservo/capi/src/lib.rs +++ b/ports/libsimpleservo/capi/src/lib.rs @@ -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 { - 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 { diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index 3bb0b30de1c..841d377e55a 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -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, - 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 { - // 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(); diff --git a/support/android/apk/servoview/build.gradle b/support/android/apk/servoview/build.gradle index 0e97838f816..51bab4b45e2 100644 --- a/support/android/apk/servoview/build.gradle +++ b/support/android/apk/servoview/build.gradle @@ -111,7 +111,6 @@ android { sourceSets { main { - assets.srcDirs = [rootDir.absolutePath + "/../../../target/android/resources"] } armDebug { jniLibs.srcDirs = [getJniLibsPath(true, 'arm')] diff --git a/support/android/apk/servoview/src/main/java/org/mozilla/servoview/JNIServo.java b/support/android/apk/servoview/src/main/java/org/mozilla/servoview/JNIServo.java index e0aaba72757..b10d963b500 100644 --- a/support/android/apk/servoview/src/main/java/org/mozilla/servoview/JNIServo.java +++ b/support/android/apk/servoview/src/main/java/org/mozilla/servoview/JNIServo.java @@ -97,8 +97,6 @@ public class JNIServo { void onHistoryChanged(boolean canGoBack, boolean canGoForward); void onShutdownComplete(); - - byte[] readfile(String file); } } diff --git a/support/android/apk/servoview/src/main/java/org/mozilla/servoview/Servo.java b/support/android/apk/servoview/src/main/java/org/mozilla/servoview/Servo.java index 3aef110434e..05a210dfa53 100644 --- a/support/android/apk/servoview/src/main/java/org/mozilla/servoview/Servo.java +++ b/support/android/apk/servoview/src/main/java/org/mozilla/servoview/Servo.java @@ -6,12 +6,9 @@ package org.mozilla.servoview; import android.app.Activity; -import android.content.res.AssetManager; import android.content.Context; import android.util.Log; -import java.io.IOException; -import java.io.InputStream; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; @@ -20,7 +17,6 @@ import org.mozilla.servoview.JNIServo.ServoOptions; public class Servo { private static final String LOGTAG = "Servo"; - private AssetManager mAssetMgr; private JNIServo mJNI = new JNIServo(); private RunCallback mRunCallback; private boolean mShuttingDown; @@ -37,8 +33,6 @@ public class Servo { mRunCallback = runCallback; - mAssetMgr = activity.getResources().getAssets(); - mServoCallbacks = new Callbacks(client, gfxcb); mRunCallback.inGLThread(() -> { @@ -262,18 +256,5 @@ public class Servo { public void onRedrawing(boolean redrawing) { mRunCallback.inUIThread(() -> mClient.onRedrawing(redrawing)); } - - public byte[] readfile(String file) { - try { - InputStream stream = mAssetMgr.open(file); - byte[] bytes = new byte[stream.available()]; - stream.read(bytes); - stream.close(); - return bytes; - } catch (IOException e) { - Log.e(LOGTAG, "readfile error: " + e.getMessage()); - return null; - } - } } }