Skip to main content

devtools/actors/inspector/
css_properties.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
5//! This actor holds a database of available css properties, their supported values and
6//! alternative names
7
8use std::collections::HashMap;
9use std::sync::Arc;
10
11use devtools_traits::CssDatabaseProperty;
12use malloc_size_of_derive::MallocSizeOf;
13use serde::Serialize;
14use serde_json::{Map, Value};
15
16use crate::StreamId;
17use crate::actor::{Actor, ActorError, ActorRegistry, new_actor_name};
18use crate::protocol::ClientRequest;
19
20#[derive(MallocSizeOf)]
21pub(crate) struct CssPropertiesActor {
22    name: String,
23    properties: HashMap<String, CssDatabaseProperty>,
24}
25
26#[derive(Serialize)]
27struct GetCssDatabaseReply<'a> {
28    from: String,
29    properties: &'a HashMap<String, CssDatabaseProperty>,
30}
31
32impl Actor for CssPropertiesActor {
33    fn name(&self) -> &str {
34        &self.name
35    }
36
37    /// The css properties actor can handle the following messages:
38    ///
39    /// - `getCSSDatabase`: Returns a big list of every supported css property so that the
40    ///   inspector can show the available options
41    fn handle_message(
42        &self,
43        request: ClientRequest,
44        _registry: &ActorRegistry,
45        msg_type: &str,
46        _msg: &Map<String, Value>,
47        _id: StreamId,
48    ) -> Result<(), ActorError> {
49        match msg_type {
50            "getCSSDatabase" => request.reply_final(&GetCssDatabaseReply {
51                from: self.name().into(),
52                properties: &self.properties,
53            })?,
54            _ => return Err(ActorError::UnrecognizedPacketType),
55        };
56        Ok(())
57    }
58}
59
60impl CssPropertiesActor {
61    pub fn register(
62        registry: &ActorRegistry,
63        properties: HashMap<String, CssDatabaseProperty>,
64    ) -> Arc<Self> {
65        let name = new_actor_name::<Self>();
66        let actor = Self { name, properties };
67        registry.register::<Self>(actor)
68    }
69}