ohos: Bundle resource files in hap (#33513)

Bundle resource files into the .hap, so they are available
as files in the application sandbox, instead of included
into the shared library.
This should slightly reduce the binary size in debug and
release mode.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com>
This commit is contained in:
Jonathan Schwender 2024-09-24 06:29:53 +02:00 committed by GitHub
parent ff86771b48
commit 88ffe9f7a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 77 additions and 21 deletions

View file

@ -4,6 +4,7 @@
#![allow(non_snake_case)]
mod resources;
mod simpleservo;
use std::collections::HashMap;

View file

@ -16,32 +16,32 @@ impl ResourceReaderInstance {
impl ResourceReaderMethods for ResourceReaderInstance {
fn read(&self, res: Resource) -> Vec<u8> {
Vec::from(match res {
Resource::Preferences => &include_bytes!("../../../resources/prefs.json")[..],
Resource::Preferences => &include_bytes!("../../../../resources/prefs.json")[..],
Resource::HstsPreloadList => {
&include_bytes!("../../../resources/hsts_preload.json")[..]
&include_bytes!("../../../../resources/hsts_preload.json")[..]
},
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::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")[..]
&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::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")[..]
&include_bytes!("../../../../resources/gatt_blocklist.txt")[..]
},
Resource::MediaControlsCSS => {
&include_bytes!("../../../resources/media-controls.css")[..]
&include_bytes!("../../../../resources/media-controls.css")[..]
},
Resource::MediaControlsJS => {
&include_bytes!("../../../resources/media-controls.js")[..]
&include_bytes!("../../../../resources/media-controls.js")[..]
},
Resource::CrashHTML => &include_bytes!("../../../resources/crash.html")[..],
Resource::CrashHTML => &include_bytes!("../../../../resources/crash.html")[..],
Resource::DirectoryListingHTML => {
&include_bytes!("../../../resources/directory-listing.html")[..]
&include_bytes!("../../../../resources/directory-listing.html")[..]
},
})
}

View file

@ -27,8 +27,8 @@ use servo::webrender_traits::RenderingContext;
use servo::{self, gl, Servo};
use surfman::{Connection, SurfaceType};
use crate::egl::android::resources::ResourceReaderInstance;
use crate::egl::host_trait::HostTrait;
use crate::egl::resources::ResourceReaderInstance;
use crate::egl::servo_glue::{
Coordinates, ServoEmbedderCallbacks, ServoGlue, ServoWindowCallbacks,
};
@ -173,7 +173,7 @@ pub fn init(
);
SERVO.with(|s| {
let mut servo_glue = ServoGlue::new(rendering_context, servo.servo, window_callbacks);
let mut servo_glue = ServoGlue::new(rendering_context, servo.servo, window_callbacks, None);
let _ = servo_glue.process_event(EmbedderEvent::NewWebView(url, servo.browser_id));
*s.borrow_mut() = Some(servo_glue);
});

View file

@ -14,5 +14,4 @@ mod ohos;
mod log;
mod host_trait;
mod resources;
mod servo_glue;

View file

@ -31,6 +31,7 @@ use super::gl_glue;
use super::host_trait::HostTrait;
use super::servo_glue::ServoGlue;
mod resources;
mod simpleservo;
// Todo: in the future these libraries should be added by Rust sys-crates
@ -47,6 +48,8 @@ pub struct InitOpts {
pub url: String,
pub device_type: String,
pub os_full_name: String,
/// Path to application data bundled with the servo app, e.g. web-pages.
pub resource_dir: String,
pub display_density: f64,
}

View file

@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::fs;
use std::path::PathBuf;
use servo::embedder_traits::resources::{Resource, ResourceReaderMethods};
pub(crate) struct ResourceReaderInstance {
resource_dir: PathBuf,
}
impl ResourceReaderInstance {
pub(crate) fn new(resource_dir: PathBuf) -> Self {
assert!(resource_dir.is_dir());
Self { resource_dir }
}
}
impl ResourceReaderMethods for ResourceReaderInstance {
fn read(&self, res: Resource) -> Vec<u8> {
let file_path = self.resource_dir.join(res.filename());
fs::read(&file_path).expect("failed to read resource file")
}
fn sandbox_access_files(&self) -> Vec<PathBuf> {
vec![]
}
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
vec![]
}
}

View file

@ -23,8 +23,8 @@ use servo::{self, gl, Servo};
use surfman::{Connection, SurfaceType};
use crate::egl::host_trait::HostTrait;
use crate::egl::ohos::resources::ResourceReaderInstance;
use crate::egl::ohos::InitOpts;
use crate::egl::resources::ResourceReaderInstance;
use crate::egl::servo_glue::{
Coordinates, ServoEmbedderCallbacks, ServoGlue, ServoWindowCallbacks,
};
@ -41,7 +41,8 @@ pub fn init(
) -> Result<ServoGlue, &'static str> {
info!("Entered simpleservo init function");
crate::init_tracing();
resources::set(Box::new(ResourceReaderInstance::new()));
let resource_dir = PathBuf::from(&options.resource_dir).join("servo");
resources::set(Box::new(ResourceReaderInstance::new(resource_dir)));
gl.clear_color(1.0, 1.0, 1.0, 1.0);
gl.clear(gl::COLOR_BUFFER_BIT);
@ -96,7 +97,12 @@ pub fn init(
CompositeTarget::Window,
);
let mut servo_glue = ServoGlue::new(rendering_context, servo.servo, window_callbacks);
let mut servo_glue = ServoGlue::new(
rendering_context,
servo.servo,
window_callbacks,
Some(options.resource_dir),
);
let initial_url = ServoUrl::parse(options.url.as_str())
.inspect_err(|e| error!("Invalid initial Servo URL `{}`. Error: {e:?}", options.url))

View file

@ -23,6 +23,7 @@ use servo::keyboard_types::{Key, KeyState, KeyboardEvent};
use servo::script_traits::{
MediaSessionActionType, MouseButton, TouchEventType, TouchId, TraversalDirection,
};
use servo::servo_url::ServoUrl;
use servo::style_traits::DevicePixel;
use servo::webrender_api::ScrollLocation;
use servo::webrender_traits::RenderingContext;
@ -85,6 +86,7 @@ pub struct ServoGlue {
need_present: bool,
callbacks: Rc<ServoWindowCallbacks>,
events: Vec<EmbedderEvent>,
resource_dir: Option<String>,
context_menu_sender: Option<IpcSender<ContextMenuResult>>,
/// List of top-level browsing contexts.
@ -106,6 +108,7 @@ impl ServoGlue {
rendering_context: RenderingContext,
servo: Servo<ServoWindowCallbacks>,
callbacks: Rc<ServoWindowCallbacks>,
resource_dir: Option<String>,
) -> Self {
Self {
rendering_context,
@ -114,6 +117,7 @@ impl ServoGlue {
need_present: false,
callbacks,
events: vec![],
resource_dir,
context_menu_sender: None,
webviews: HashMap::default(),
creation_order: vec![],

View file

@ -194,6 +194,10 @@ class PackageCommands(CommandBase):
print("Cleaning up from previous packaging")
delete(ohos_target_dir)
shutil.copytree(ohos_app_dir, ohos_target_dir)
resources_src_dir = path.join(self.get_top_dir(), "resources")
resources_app_dir = path.join(ohos_target_dir, "AppScope", "resources", "resfile", "servo")
os.makedirs(resources_app_dir, exist_ok=True)
shutil.copytree(resources_src_dir, resources_app_dir, dirs_exist_ok=True)
# Map non-debug profiles to 'release' buildMode HAP.
if build_type.is_custom():

View file

@ -1,3 +1,4 @@
import { common } from '@kit.AbilityKit';
import display from '@ohos.display';
import deviceInfo from '@ohos.deviceInfo';
@ -13,6 +14,7 @@ interface InitOpts {
url: string;
deviceType: string,
osFullName: string,
resourceDir: string,
displayDensity: number,
}
@ -47,6 +49,7 @@ struct Index {
libraryname: 'servoshell',
}
@State urlToLoad: string = 'https://servo.org'
private context = getContext(this) as common.UIAbilityContext;
build() {
Column() {
@ -82,11 +85,14 @@ struct Index {
.focusable(true)
.onLoad((xComponentContext) => {
this.xComponentContext = xComponentContext as ServoXComponentInterface;
let resource_dir: string = this.context.resourceDir;
console.debug("Resources are located at %s", resource_dir);
let init_options: InitOpts = {
url: this.urlToLoad,
deviceType: get_device_type(),
osFullName: deviceInfo.osFullName,
displayDensity: get_density(),
resourceDir: resource_dir,
}
this.xComponentContext.initServo(init_options)
this.xComponentContext.registerURLcallback((new_url) => {