use std::rc::Rc;
use embedder_traits::user_contents::{UserContentManagerId, UserScript, UserStyleSheet};
use servo_constellation_traits::{EmbedderToConstellationMessage, UserContentManagerAction};
use crate::Servo;
#[derive(Clone)]
pub struct UserContentManager {
pub(crate) id: UserContentManagerId,
pub(crate) servo: Servo,
}
impl UserContentManager {
pub fn new(servo: &Servo) -> Self {
Self {
id: UserContentManagerId::next(),
servo: servo.clone(),
}
}
pub(crate) fn id(&self) -> UserContentManagerId {
self.id
}
pub fn add_script(&self, user_script: Rc<UserScript>) {
self.servo.constellation_proxy().send(
EmbedderToConstellationMessage::UserContentManagerAction(
self.id,
UserContentManagerAction::AddUserScript((*user_script).clone()),
),
);
}
pub fn remove_script(&self, user_script: Rc<UserScript>) {
self.servo.constellation_proxy().send(
EmbedderToConstellationMessage::UserContentManagerAction(
self.id,
UserContentManagerAction::RemoveUserScript(user_script.id()),
),
);
}
pub fn add_stylesheet(&self, user_stylesheet: Rc<UserStyleSheet>) {
self.servo.constellation_proxy().send(
EmbedderToConstellationMessage::UserContentManagerAction(
self.id,
UserContentManagerAction::AddUserStyleSheet((*user_stylesheet).clone()),
),
);
}
pub fn remove_stylesheet(&self, user_stylesheet: Rc<UserStyleSheet>) {
self.servo.constellation_proxy().send(
EmbedderToConstellationMessage::UserContentManagerAction(
self.id,
UserContentManagerAction::RemoveUserStyleSheet(user_stylesheet.id()),
),
);
}
}
impl Drop for UserContentManager {
fn drop(&mut self) {
self.servo.constellation_proxy().send(
EmbedderToConstellationMessage::UserContentManagerAction(
self.id,
UserContentManagerAction::DestroyUserContentManager,
),
);
}
}