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