servo/
proxies.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 std::sync::Arc;
6use std::sync::atomic::{AtomicBool, Ordering};
7
8use constellation_traits::EmbedderToConstellationMessage;
9use crossbeam_channel::{Receiver, SendError, Sender};
10use log::warn;
11use servo_config::prefs::{PrefValue, PreferencesObserver};
12
13#[derive(Clone)]
14pub(crate) struct ConstellationProxy {
15    sender: Sender<EmbedderToConstellationMessage>,
16    disconnected: Arc<AtomicBool>,
17}
18
19impl ConstellationProxy {
20    pub fn new() -> (Self, Receiver<EmbedderToConstellationMessage>) {
21        let (sender, receiver) = crossbeam_channel::unbounded();
22        (
23            Self {
24                sender,
25                disconnected: Arc::default(),
26            },
27            receiver,
28        )
29    }
30
31    pub fn disconnected(&self) -> bool {
32        self.disconnected.load(Ordering::SeqCst)
33    }
34
35    pub fn send(&self, msg: EmbedderToConstellationMessage) {
36        if self.try_send(msg).is_err() {
37            warn!("Lost connection to Constellation. Will report to embedder.")
38        }
39    }
40
41    #[allow(clippy::result_large_err)]
42    fn try_send(
43        &self,
44        msg: EmbedderToConstellationMessage,
45    ) -> Result<(), SendError<EmbedderToConstellationMessage>> {
46        if self.disconnected() {
47            return Err(SendError(msg));
48        }
49        if let Err(error) = self.sender.send(msg) {
50            self.disconnected.store(true, Ordering::SeqCst);
51            return Err(error);
52        }
53
54        Ok(())
55    }
56
57    pub fn sender(&self) -> Sender<EmbedderToConstellationMessage> {
58        self.sender.clone()
59    }
60}
61
62impl PreferencesObserver for ConstellationProxy {
63    fn prefs_changed(&self, changes: &[(&'static str, PrefValue)]) {
64        self.send(EmbedderToConstellationMessage::PreferencesUpdated(
65            changes.to_owned(),
66        ));
67    }
68}