mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01: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
|
@ -10,6 +10,7 @@
|
|||
|
||||
pub mod input_events;
|
||||
pub mod resources;
|
||||
pub mod user_content_manager;
|
||||
mod webdriver;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue