mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Allow setting userscripts directly without the need of files (#35388)
* Allow settings userscripts through preferences Signed-off-by: Tony <legendmastertony@gmail.com> * mach fmt instead of cargo fmt Signed-off-by: Tony <legendmastertony@gmail.com> * Fix pref loading not working for array values Signed-off-by: Tony <legendmastertony@gmail.com> * Use pref! in userscripts instead Signed-off-by: Tony <legendmastertony@gmail.com> * Implement the model jdm suggested - Remove userscripts from all places and move it to servoshell - Add in `UserContentManager` struct and passing it through `Servo::new` all the way down to script thread Signed-off-by: Tony <legendmastertony@gmail.com> * Apply suggestions from code review and format Signed-off-by: Tony <legendmastertony@gmail.com> * Revert unrelated change Signed-off-by: Tony <legendmastertony@gmail.com> --------- Signed-off-by: Tony <legendmastertony@gmail.com> Signed-off-by: Tony <68118705+Legend-Master@users.noreply.github.com>
This commit is contained in:
parent
53a2e61fec
commit
5a76906d64
16 changed files with 143 additions and 51 deletions
|
@ -35,11 +35,6 @@ pub struct Opts {
|
||||||
/// True to turn off incremental layout.
|
/// True to turn off incremental layout.
|
||||||
pub nonincremental_layout: bool,
|
pub nonincremental_layout: bool,
|
||||||
|
|
||||||
/// Where to load userscripts from, if any. An empty string will load from
|
|
||||||
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
|
|
||||||
/// won't be loaded
|
|
||||||
pub userscripts: Option<String>,
|
|
||||||
|
|
||||||
pub user_stylesheets: Vec<(Vec<u8>, ServoUrl)>,
|
pub user_stylesheets: Vec<(Vec<u8>, ServoUrl)>,
|
||||||
|
|
||||||
/// True to exit on thread failure instead of displaying about:failure.
|
/// True to exit on thread failure instead of displaying about:failure.
|
||||||
|
@ -191,7 +186,6 @@ impl Default for Opts {
|
||||||
time_profiling: None,
|
time_profiling: None,
|
||||||
time_profiler_trace_path: None,
|
time_profiler_trace_path: None,
|
||||||
nonincremental_layout: false,
|
nonincremental_layout: false,
|
||||||
userscripts: None,
|
|
||||||
user_stylesheets: Vec::new(),
|
user_stylesheets: Vec::new(),
|
||||||
hard_fail: true,
|
hard_fail: true,
|
||||||
webdriver_port: None,
|
webdriver_port: None,
|
||||||
|
|
|
@ -43,13 +43,11 @@ impl TryFrom<&Value> for PrefValue {
|
||||||
.unwrap_or(Err("Could not parse number from JSON".into())),
|
.unwrap_or(Err("Could not parse number from JSON".into())),
|
||||||
Value::String(value) => Ok(value.clone().into()),
|
Value::String(value) => Ok(value.clone().into()),
|
||||||
Value::Array(array) => {
|
Value::Array(array) => {
|
||||||
let array = array.iter().map(TryInto::try_into);
|
let array = array
|
||||||
if !array.clone().all(|v| v.is_ok()) {
|
.iter()
|
||||||
return Err(format!(
|
.map(TryInto::<PrefValue>::try_into)
|
||||||
"Cannot turn all array avlues into preference: {array:?}"
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
));
|
Ok(PrefValue::Array(array))
|
||||||
}
|
|
||||||
Ok(PrefValue::Array(array.map(Result::unwrap).collect()))
|
|
||||||
},
|
},
|
||||||
Value::Object(_) => Err("Cannot turn object into preference".into()),
|
Value::Object(_) => Err("Cannot turn object into preference".into()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,6 +119,7 @@ use devtools_traits::{
|
||||||
ScriptToDevtoolsControlMsg,
|
ScriptToDevtoolsControlMsg,
|
||||||
};
|
};
|
||||||
use embedder_traits::resources::{self, Resource};
|
use embedder_traits::resources::{self, Resource};
|
||||||
|
use embedder_traits::user_content_manager::UserContentManager;
|
||||||
use embedder_traits::{
|
use embedder_traits::{
|
||||||
Cursor, EmbedderMsg, EmbedderProxy, ImeEvent, InputEvent, MediaSessionActionType,
|
Cursor, EmbedderMsg, EmbedderProxy, ImeEvent, InputEvent, MediaSessionActionType,
|
||||||
MediaSessionEvent, MediaSessionPlaybackState, MouseButton, MouseButtonAction, MouseButtonEvent,
|
MediaSessionEvent, MediaSessionPlaybackState, MouseButton, MouseButtonAction, MouseButtonEvent,
|
||||||
|
@ -471,6 +472,9 @@ pub struct Constellation<STF, SWF> {
|
||||||
/// Read during startup and provided to image caches that are created
|
/// Read during startup and provided to image caches that are created
|
||||||
/// on an as-needed basis, rather than retrieving it every time.
|
/// on an as-needed basis, rather than retrieving it every time.
|
||||||
rippy_data: Vec<u8>,
|
rippy_data: Vec<u8>,
|
||||||
|
|
||||||
|
/// User content manager
|
||||||
|
user_content_manager: UserContentManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// State needed to construct a constellation.
|
/// State needed to construct a constellation.
|
||||||
|
@ -523,6 +527,9 @@ pub struct InitialConstellationState {
|
||||||
|
|
||||||
#[cfg(feature = "webgpu")]
|
#[cfg(feature = "webgpu")]
|
||||||
pub wgpu_image_map: WGPUImageMap,
|
pub wgpu_image_map: WGPUImageMap,
|
||||||
|
|
||||||
|
/// User content manager
|
||||||
|
pub user_content_manager: UserContentManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Data needed for webdriver
|
/// Data needed for webdriver
|
||||||
|
@ -739,6 +746,7 @@ where
|
||||||
active_media_session: None,
|
active_media_session: None,
|
||||||
user_agent: state.user_agent,
|
user_agent: state.user_agent,
|
||||||
rippy_data,
|
rippy_data,
|
||||||
|
user_content_manager: state.user_content_manager,
|
||||||
};
|
};
|
||||||
|
|
||||||
constellation.run();
|
constellation.run();
|
||||||
|
@ -982,6 +990,7 @@ where
|
||||||
player_context: WindowGLContext::get(),
|
player_context: WindowGLContext::get(),
|
||||||
user_agent: self.user_agent.clone(),
|
user_agent: self.user_agent.clone(),
|
||||||
rippy_data: self.rippy_data.clone(),
|
rippy_data: self.rippy_data.clone(),
|
||||||
|
user_content_manager: self.user_content_manager.clone(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let pipeline = match result {
|
let pipeline = match result {
|
||||||
|
|
|
@ -23,6 +23,7 @@ use compositing_traits::{CompositionPipeline, CompositorMsg, CompositorProxy};
|
||||||
use constellation_traits::WindowSizeData;
|
use constellation_traits::WindowSizeData;
|
||||||
use crossbeam_channel::{Sender, unbounded};
|
use crossbeam_channel::{Sender, unbounded};
|
||||||
use devtools_traits::{DevtoolsControlMsg, ScriptToDevtoolsControlMsg};
|
use devtools_traits::{DevtoolsControlMsg, ScriptToDevtoolsControlMsg};
|
||||||
|
use embedder_traits::user_content_manager::UserContentManager;
|
||||||
use fonts::{SystemFontServiceProxy, SystemFontServiceProxySender};
|
use fonts::{SystemFontServiceProxy, SystemFontServiceProxySender};
|
||||||
use ipc_channel::Error;
|
use ipc_channel::Error;
|
||||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
||||||
|
@ -196,6 +197,9 @@ pub struct InitialPipelineState {
|
||||||
|
|
||||||
/// The image bytes associated with the RippyPNG embedder resource.
|
/// The image bytes associated with the RippyPNG embedder resource.
|
||||||
pub rippy_data: Vec<u8>,
|
pub rippy_data: Vec<u8>,
|
||||||
|
|
||||||
|
/// User content manager
|
||||||
|
pub user_content_manager: UserContentManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NewPipeline {
|
pub struct NewPipeline {
|
||||||
|
@ -292,6 +296,7 @@ impl Pipeline {
|
||||||
player_context: state.player_context,
|
player_context: state.player_context,
|
||||||
user_agent: state.user_agent,
|
user_agent: state.user_agent,
|
||||||
rippy_data: state.rippy_data,
|
rippy_data: state.rippy_data,
|
||||||
|
user_content_manager: state.user_content_manager,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Spawn the child process.
|
// Spawn the child process.
|
||||||
|
@ -498,6 +503,7 @@ pub struct UnprivilegedPipelineContent {
|
||||||
player_context: WindowGLContext,
|
player_context: WindowGLContext,
|
||||||
user_agent: Cow<'static, str>,
|
user_agent: Cow<'static, str>,
|
||||||
rippy_data: Vec<u8>,
|
rippy_data: Vec<u8>,
|
||||||
|
user_content_manager: UserContentManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnprivilegedPipelineContent {
|
impl UnprivilegedPipelineContent {
|
||||||
|
@ -543,6 +549,7 @@ impl UnprivilegedPipelineContent {
|
||||||
compositor_api: self.cross_process_compositor_api.clone(),
|
compositor_api: self.cross_process_compositor_api.clone(),
|
||||||
player_context: self.player_context.clone(),
|
player_context: self.player_context.clone(),
|
||||||
inherited_secure_context: self.load_data.inherited_secure_context,
|
inherited_secure_context: self.load_data.inherited_secure_context,
|
||||||
|
user_content_manager: self.user_content_manager,
|
||||||
},
|
},
|
||||||
layout_factory,
|
layout_factory,
|
||||||
Arc::new(self.system_font_service.to_proxy()),
|
Arc::new(self.system_font_service.to_proxy()),
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use std::fs::{File, read_dir};
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
|
@ -19,37 +16,24 @@ use crate::script_runtime::CanGc;
|
||||||
|
|
||||||
pub(crate) fn load_script(head: &HTMLHeadElement) {
|
pub(crate) fn load_script(head: &HTMLHeadElement) {
|
||||||
let doc = head.owner_document();
|
let doc = head.owner_document();
|
||||||
let path_str = match doc.window().get_userscripts_path() {
|
let userscripts = doc.window().userscripts().to_owned();
|
||||||
Some(p) => p,
|
if userscripts.is_empty() {
|
||||||
None => return,
|
return;
|
||||||
};
|
}
|
||||||
let window = Trusted::new(doc.window());
|
let window = Trusted::new(doc.window());
|
||||||
doc.add_delayed_task(task!(UserScriptExecute: move || {
|
doc.add_delayed_task(task!(UserScriptExecute: move || {
|
||||||
let win = window.root();
|
let win = window.root();
|
||||||
let cx = win.get_cx();
|
let cx = win.get_cx();
|
||||||
rooted!(in(*cx) let mut rval = UndefinedValue());
|
rooted!(in(*cx) let mut rval = UndefinedValue());
|
||||||
|
|
||||||
let path = PathBuf::from(&path_str);
|
for user_script in userscripts {
|
||||||
let mut files = read_dir(path)
|
|
||||||
.expect("Bad path passed to --userscripts")
|
|
||||||
.filter_map(|e| e.ok())
|
|
||||||
.map(|e| e.path())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
files.sort();
|
|
||||||
|
|
||||||
for file in files {
|
|
||||||
let mut f = File::open(&file).unwrap();
|
|
||||||
let mut contents = vec![];
|
|
||||||
f.read_to_end(&mut contents).unwrap();
|
|
||||||
let script_text = SourceCode::Text(
|
let script_text = SourceCode::Text(
|
||||||
Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
|
Rc::new(DOMString::from_string(user_script.script))
|
||||||
);
|
);
|
||||||
|
|
||||||
let global_scope = win.as_global_scope();
|
let global_scope = win.as_global_scope();
|
||||||
global_scope.evaluate_script_on_global_with_result(
|
global_scope.evaluate_script_on_global_with_result(
|
||||||
&script_text,
|
&script_text,
|
||||||
&file.to_string_lossy(),
|
&user_script.source_file.map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
|
||||||
rval.handle_mut(),
|
rval.handle_mut(),
|
||||||
1,
|
1,
|
||||||
ScriptFetchOptions::default_classic_script(global_scope),
|
ScriptFetchOptions::default_classic_script(global_scope),
|
||||||
|
|
|
@ -26,6 +26,7 @@ use crossbeam_channel::{Sender, unbounded};
|
||||||
use cssparser::{Parser, ParserInput, SourceLocation};
|
use cssparser::{Parser, ParserInput, SourceLocation};
|
||||||
use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
|
use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use embedder_traits::user_content_manager::{UserContentManager, UserScript};
|
||||||
use embedder_traits::{
|
use embedder_traits::{
|
||||||
AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, SimpleDialog, Theme,
|
AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, SimpleDialog, Theme,
|
||||||
WebDriverJSError, WebDriverJSResult,
|
WebDriverJSError, WebDriverJSResult,
|
||||||
|
@ -373,10 +374,9 @@ pub(crate) struct Window {
|
||||||
/// Unminify Css.
|
/// Unminify Css.
|
||||||
unminify_css: bool,
|
unminify_css: bool,
|
||||||
|
|
||||||
/// Where to load userscripts from, if any. An empty string will load from
|
/// User content manager
|
||||||
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
|
#[no_trace]
|
||||||
/// won't be loaded.
|
user_content_manager: UserContentManager,
|
||||||
userscripts_path: Option<String>,
|
|
||||||
|
|
||||||
/// Window's GL context from application
|
/// Window's GL context from application
|
||||||
#[ignore_malloc_size_of = "defined in script_thread"]
|
#[ignore_malloc_size_of = "defined in script_thread"]
|
||||||
|
@ -624,8 +624,8 @@ impl Window {
|
||||||
&self.compositor_api
|
&self.compositor_api
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_userscripts_path(&self) -> Option<String> {
|
pub(crate) fn userscripts(&self) -> &[UserScript] {
|
||||||
self.userscripts_path.clone()
|
self.user_content_manager.scripts()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_player_context(&self) -> WindowGLContext {
|
pub(crate) fn get_player_context(&self) -> WindowGLContext {
|
||||||
|
@ -2797,7 +2797,7 @@ impl Window {
|
||||||
unminify_js: bool,
|
unminify_js: bool,
|
||||||
unminify_css: bool,
|
unminify_css: bool,
|
||||||
local_script_source: Option<String>,
|
local_script_source: Option<String>,
|
||||||
userscripts_path: Option<String>,
|
user_content_manager: UserContentManager,
|
||||||
user_agent: Cow<'static, str>,
|
user_agent: Cow<'static, str>,
|
||||||
player_context: WindowGLContext,
|
player_context: WindowGLContext,
|
||||||
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
|
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
|
||||||
|
@ -2884,7 +2884,7 @@ impl Window {
|
||||||
has_sent_idle_message: Cell::new(false),
|
has_sent_idle_message: Cell::new(false),
|
||||||
relayout_event,
|
relayout_event,
|
||||||
unminify_css,
|
unminify_css,
|
||||||
userscripts_path,
|
user_content_manager,
|
||||||
player_context,
|
player_context,
|
||||||
throttled: Cell::new(false),
|
throttled: Cell::new(false),
|
||||||
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
|
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
|
||||||
|
|
|
@ -43,6 +43,7 @@ use devtools_traits::{
|
||||||
CSSError, DevtoolScriptControlMsg, DevtoolsPageInfo, NavigationState,
|
CSSError, DevtoolScriptControlMsg, DevtoolsPageInfo, NavigationState,
|
||||||
ScriptToDevtoolsControlMsg, WorkerId,
|
ScriptToDevtoolsControlMsg, WorkerId,
|
||||||
};
|
};
|
||||||
|
use embedder_traits::user_content_manager::UserContentManager;
|
||||||
use embedder_traits::{
|
use embedder_traits::{
|
||||||
EmbedderMsg, InputEvent, MediaSessionActionType, Theme, WebDriverScriptCommand,
|
EmbedderMsg, InputEvent, MediaSessionActionType, Theme, WebDriverScriptCommand,
|
||||||
};
|
};
|
||||||
|
@ -303,10 +304,9 @@ pub struct ScriptThread {
|
||||||
/// Unminify Css.
|
/// Unminify Css.
|
||||||
unminify_css: bool,
|
unminify_css: bool,
|
||||||
|
|
||||||
/// Where to load userscripts from, if any. An empty string will load from
|
/// User content manager
|
||||||
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
|
#[no_trace]
|
||||||
/// won't be loaded
|
user_content_manager: UserContentManager,
|
||||||
userscripts_path: Option<String>,
|
|
||||||
|
|
||||||
/// An optional string allowing the user agent to be set for testing.
|
/// An optional string allowing the user agent to be set for testing.
|
||||||
user_agent: Cow<'static, str>,
|
user_agent: Cow<'static, str>,
|
||||||
|
@ -938,8 +938,8 @@ impl ScriptThread {
|
||||||
unminify_js: opts.unminify_js,
|
unminify_js: opts.unminify_js,
|
||||||
local_script_source: opts.local_script_source.clone(),
|
local_script_source: opts.local_script_source.clone(),
|
||||||
unminify_css: opts.unminify_css,
|
unminify_css: opts.unminify_css,
|
||||||
userscripts_path: opts.userscripts.clone(),
|
|
||||||
user_agent,
|
user_agent,
|
||||||
|
user_content_manager: state.user_content_manager,
|
||||||
player_context: state.player_context,
|
player_context: state.player_context,
|
||||||
node_ids: Default::default(),
|
node_ids: Default::default(),
|
||||||
is_user_interacting: Cell::new(false),
|
is_user_interacting: Cell::new(false),
|
||||||
|
@ -3096,7 +3096,7 @@ impl ScriptThread {
|
||||||
self.unminify_js,
|
self.unminify_js,
|
||||||
self.unminify_css,
|
self.unminify_css,
|
||||||
self.local_script_source.clone(),
|
self.local_script_source.clone(),
|
||||||
self.userscripts_path.clone(),
|
self.user_content_manager.clone(),
|
||||||
self.user_agent.clone(),
|
self.user_agent.clone(),
|
||||||
self.player_context.clone(),
|
self.player_context.clone(),
|
||||||
#[cfg(feature = "webgpu")]
|
#[cfg(feature = "webgpu")]
|
||||||
|
|
|
@ -102,6 +102,7 @@ impl ApplicationHandler<WakerEvent> for App {
|
||||||
}),
|
}),
|
||||||
window_delegate.clone(),
|
window_delegate.clone(),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
|
Default::default(),
|
||||||
);
|
);
|
||||||
servo.setup_logging();
|
servo.setup_logging();
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,7 @@ use constellation::{
|
||||||
};
|
};
|
||||||
use constellation_traits::{ConstellationMsg, WindowSizeData};
|
use constellation_traits::{ConstellationMsg, WindowSizeData};
|
||||||
use crossbeam_channel::{Receiver, Sender, unbounded};
|
use crossbeam_channel::{Receiver, Sender, unbounded};
|
||||||
|
use embedder_traits::user_content_manager::UserContentManager;
|
||||||
pub use embedder_traits::*;
|
pub use embedder_traits::*;
|
||||||
use env_logger::Builder as EnvLoggerBuilder;
|
use env_logger::Builder as EnvLoggerBuilder;
|
||||||
use euclid::Scale;
|
use euclid::Scale;
|
||||||
|
@ -264,6 +265,7 @@ impl Servo {
|
||||||
mut embedder: Box<dyn EmbedderMethods>,
|
mut embedder: Box<dyn EmbedderMethods>,
|
||||||
window: Rc<dyn WindowMethods>,
|
window: Rc<dyn WindowMethods>,
|
||||||
user_agent: Option<String>,
|
user_agent: Option<String>,
|
||||||
|
user_content_manager: UserContentManager,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// Global configuration options, parsed from the command line.
|
// Global configuration options, parsed from the command line.
|
||||||
opts::set_options(opts);
|
opts::set_options(opts);
|
||||||
|
@ -501,6 +503,7 @@ impl Servo {
|
||||||
#[cfg(feature = "webgpu")]
|
#[cfg(feature = "webgpu")]
|
||||||
wgpu_image_map,
|
wgpu_image_map,
|
||||||
protocols,
|
protocols,
|
||||||
|
user_content_manager,
|
||||||
);
|
);
|
||||||
|
|
||||||
if cfg!(feature = "webdriver") {
|
if cfg!(feature = "webdriver") {
|
||||||
|
@ -1053,6 +1056,7 @@ fn create_constellation(
|
||||||
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
|
external_images: Arc<Mutex<WebrenderExternalImageRegistry>>,
|
||||||
#[cfg(feature = "webgpu")] wgpu_image_map: WGPUImageMap,
|
#[cfg(feature = "webgpu")] wgpu_image_map: WGPUImageMap,
|
||||||
protocols: ProtocolRegistry,
|
protocols: ProtocolRegistry,
|
||||||
|
user_content_manager: UserContentManager,
|
||||||
) -> Sender<ConstellationMsg> {
|
) -> Sender<ConstellationMsg> {
|
||||||
// Global configuration options, parsed from the command line.
|
// Global configuration options, parsed from the command line.
|
||||||
let opts = opts::get();
|
let opts = opts::get();
|
||||||
|
@ -1105,6 +1109,7 @@ fn create_constellation(
|
||||||
webrender_external_images: external_images,
|
webrender_external_images: external_images,
|
||||||
#[cfg(feature = "webgpu")]
|
#[cfg(feature = "webgpu")]
|
||||||
wgpu_image_map,
|
wgpu_image_map,
|
||||||
|
user_content_manager,
|
||||||
};
|
};
|
||||||
|
|
||||||
let layout_factory = Arc::new(layout_thread_2020::LayoutFactoryImpl());
|
let layout_factory = Arc::new(layout_thread_2020::LayoutFactoryImpl());
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
pub mod input_events;
|
pub mod input_events;
|
||||||
pub mod resources;
|
pub mod resources;
|
||||||
|
pub mod user_content_manager;
|
||||||
mod webdriver;
|
mod webdriver;
|
||||||
|
|
||||||
use std::fmt::{Debug, Error, Formatter};
|
use std::fmt::{Debug, Error, Formatter};
|
||||||
|
|
55
components/shared/embedder/user_content_manager.rs
Normal file
55
components/shared/embedder/user_content_manager.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/* 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::path::PathBuf;
|
||||||
|
|
||||||
|
use malloc_size_of::MallocSizeOfOps;
|
||||||
|
use malloc_size_of_derive::MallocSizeOf;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
|
||||||
|
pub struct UserContentManager {
|
||||||
|
user_scripts: Vec<UserScript>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserContentManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
UserContentManager::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_script(&mut self, script: impl Into<UserScript>) {
|
||||||
|
self.user_scripts.push(script.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scripts(&self) -> &[UserScript] {
|
||||||
|
&self.user_scripts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
|
pub struct UserScript {
|
||||||
|
pub script: String,
|
||||||
|
pub source_file: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maybe we should implement `MallocSizeOf` for `PathBuf` in `malloc_size_of` crate?
|
||||||
|
impl malloc_size_of::MallocSizeOf for UserScript {
|
||||||
|
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
|
||||||
|
let mut sum = 0;
|
||||||
|
sum += self.script.size_of(ops);
|
||||||
|
if let Some(path) = &self.source_file {
|
||||||
|
sum += unsafe { ops.malloc_size_of(path.as_path()) };
|
||||||
|
}
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Into<String>> From<T> for UserScript {
|
||||||
|
fn from(script: T) -> Self {
|
||||||
|
UserScript {
|
||||||
|
script: script.into(),
|
||||||
|
source_file: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,6 +33,7 @@ use constellation_traits::{
|
||||||
use crossbeam_channel::{RecvTimeoutError, Sender};
|
use crossbeam_channel::{RecvTimeoutError, Sender};
|
||||||
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, WorkerId};
|
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, WorkerId};
|
||||||
use embedder_traits::input_events::InputEvent;
|
use embedder_traits::input_events::InputEvent;
|
||||||
|
use embedder_traits::user_content_manager::UserContentManager;
|
||||||
use embedder_traits::{MediaSessionActionType, Theme, WebDriverScriptCommand};
|
use embedder_traits::{MediaSessionActionType, Theme, WebDriverScriptCommand};
|
||||||
use euclid::{Rect, Scale, Size2D, UnknownUnit};
|
use euclid::{Rect, Scale, Size2D, UnknownUnit};
|
||||||
use http::{HeaderMap, Method};
|
use http::{HeaderMap, Method};
|
||||||
|
@ -463,6 +464,8 @@ pub struct InitialScriptState {
|
||||||
pub compositor_api: CrossProcessCompositorApi,
|
pub compositor_api: CrossProcessCompositorApi,
|
||||||
/// Application window's GL Context for Media player
|
/// Application window's GL Context for Media player
|
||||||
pub player_context: WindowGLContext,
|
pub player_context: WindowGLContext,
|
||||||
|
/// User content manager
|
||||||
|
pub user_content_manager: UserContentManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This trait allows creating a `ServiceWorkerManager` without depending on the `script`
|
/// This trait allows creating a `ServiceWorkerManager` without depending on the `script`
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
@ -16,6 +17,7 @@ use servo::config::opts::Opts;
|
||||||
use servo::config::prefs::Preferences;
|
use servo::config::prefs::Preferences;
|
||||||
use servo::servo_config::pref;
|
use servo::servo_config::pref;
|
||||||
use servo::servo_url::ServoUrl;
|
use servo::servo_url::ServoUrl;
|
||||||
|
use servo::user_content_manager::{UserContentManager, UserScript};
|
||||||
use servo::webxr::glwindow::GlWindowDiscovery;
|
use servo::webxr::glwindow::GlWindowDiscovery;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
use servo::webxr::openxr::{AppInfo, OpenXrDiscovery};
|
use servo::webxr::openxr::{AppInfo, OpenXrDiscovery};
|
||||||
|
@ -146,6 +148,13 @@ impl App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut user_content_manager = UserContentManager::new();
|
||||||
|
for script in load_userscripts(self.servoshell_preferences.userscripts_directory.as_deref())
|
||||||
|
.expect("Loading userscripts failed")
|
||||||
|
{
|
||||||
|
user_content_manager.add_script(script);
|
||||||
|
}
|
||||||
|
|
||||||
let servo = Servo::new(
|
let servo = Servo::new(
|
||||||
self.opts.clone(),
|
self.opts.clone(),
|
||||||
self.preferences.clone(),
|
self.preferences.clone(),
|
||||||
|
@ -153,6 +162,7 @@ impl App {
|
||||||
embedder,
|
embedder,
|
||||||
Rc::new(UpcastedWindow(window.clone())),
|
Rc::new(UpcastedWindow(window.clone())),
|
||||||
self.servoshell_preferences.user_agent.clone(),
|
self.servoshell_preferences.user_agent.clone(),
|
||||||
|
user_content_manager,
|
||||||
);
|
);
|
||||||
servo.setup_logging();
|
servo.setup_logging();
|
||||||
|
|
||||||
|
@ -442,3 +452,20 @@ impl ApplicationHandler<WakerEvent> for App {
|
||||||
self.suspended.set(true);
|
self.suspended.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn load_userscripts(userscripts_directory: Option<&Path>) -> std::io::Result<Vec<UserScript>> {
|
||||||
|
let mut userscripts = Vec::new();
|
||||||
|
if let Some(userscripts_directory) = &userscripts_directory {
|
||||||
|
let mut files = std::fs::read_dir(userscripts_directory)?
|
||||||
|
.map(|e| e.map(|entry| entry.path()))
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
files.sort();
|
||||||
|
for file in files {
|
||||||
|
userscripts.push(UserScript {
|
||||||
|
script: std::fs::read_to_string(&file)?,
|
||||||
|
source_file: Some(file),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(userscripts)
|
||||||
|
}
|
||||||
|
|
|
@ -99,6 +99,7 @@ pub fn init(
|
||||||
embedder_callbacks,
|
embedder_callbacks,
|
||||||
window_callbacks.clone(),
|
window_callbacks.clone(),
|
||||||
None,
|
None,
|
||||||
|
Default::default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
APP.with(|app| {
|
APP.with(|app| {
|
||||||
|
|
|
@ -131,6 +131,7 @@ pub fn init(
|
||||||
embedder_callbacks,
|
embedder_callbacks,
|
||||||
window_callbacks.clone(),
|
window_callbacks.clone(),
|
||||||
None, /* user_agent */
|
None, /* user_agent */
|
||||||
|
Default::default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let app_state = RunningAppState::new(
|
let app_state = RunningAppState::new(
|
||||||
|
|
|
@ -56,6 +56,9 @@ pub(crate) struct ServoShellPreferences {
|
||||||
pub output_image_path: Option<String>,
|
pub output_image_path: Option<String>,
|
||||||
/// Whether or not to exit after Servo detects a stable output image in all WebViews.
|
/// Whether or not to exit after Servo detects a stable output image in all WebViews.
|
||||||
pub exit_after_stable_image: bool,
|
pub exit_after_stable_image: bool,
|
||||||
|
/// Where to load userscripts from, if any.
|
||||||
|
/// and if the option isn't passed userscripts won't be loaded.
|
||||||
|
pub userscripts_directory: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ServoShellPreferences {
|
impl Default for ServoShellPreferences {
|
||||||
|
@ -74,6 +77,7 @@ impl Default for ServoShellPreferences {
|
||||||
user_agent: None,
|
user_agent: None,
|
||||||
output_image_path: None,
|
output_image_path: None,
|
||||||
exit_after_stable_image: false,
|
exit_after_stable_image: false,
|
||||||
|
userscripts_directory: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -622,6 +626,9 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
|
||||||
screen_size_override,
|
screen_size_override,
|
||||||
output_image_path,
|
output_image_path,
|
||||||
exit_after_stable_image: exit_after_load,
|
exit_after_stable_image: exit_after_load,
|
||||||
|
userscripts_directory: opt_match
|
||||||
|
.opt_default("userscripts", "resources/user-agent-js")
|
||||||
|
.map(PathBuf::from),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -636,7 +643,6 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
|
||||||
time_profiling,
|
time_profiling,
|
||||||
time_profiler_trace_path: opt_match.opt_str("profiler-trace-path"),
|
time_profiler_trace_path: opt_match.opt_str("profiler-trace-path"),
|
||||||
nonincremental_layout,
|
nonincremental_layout,
|
||||||
userscripts: opt_match.opt_default("userscripts", "resources/user-agent-js"),
|
|
||||||
user_stylesheets,
|
user_stylesheets,
|
||||||
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
|
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
|
||||||
webdriver_port,
|
webdriver_port,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue