servo/
user_content_manager.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use constellation_traits::{EmbedderToConstellationMessage, UserContentManagerAction};
6use embedder_traits::user_contents::{UserContentManagerId, UserScript};
7
8use crate::Servo;
9
10/// The [`UserContentManager`] allows embedders to inject content (scripts, styles) during
11/// into the pages loaded within the `WebView`. The same `UserContentManager` can be
12/// shared among multiple `WebView`s. Any updates to the `UserContentManager` will
13/// take effect only after the page is reloaded.
14#[derive(Clone)]
15pub struct UserContentManager {
16    pub(crate) id: UserContentManagerId,
17    pub(crate) servo: Servo,
18}
19
20impl UserContentManager {
21    pub fn new(servo: &Servo) -> Self {
22        Self {
23            id: UserContentManagerId::next(),
24            servo: servo.clone(),
25        }
26    }
27
28    pub(crate) fn id(&self) -> UserContentManagerId {
29        self.id
30    }
31
32    pub fn add_script(&self, user_script: UserScript) {
33        self.servo.constellation_proxy().send(
34            EmbedderToConstellationMessage::UserContentManagerAction(
35                self.id,
36                UserContentManagerAction::AddUserScript(user_script),
37            ),
38        );
39    }
40}
41
42impl Drop for UserContentManager {
43    fn drop(&mut self) {
44        self.servo.constellation_proxy().send(
45            EmbedderToConstellationMessage::UserContentManagerAction(
46                self.id,
47                UserContentManagerAction::DestroyUserContentManager,
48            ),
49        );
50    }
51}