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