Skip to main content

devtools/actors/inspector/
layout.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//! The layout actor informs the DevTools client of the layout properties of the document, such as
6//! grids or flexboxes. It acts as a placeholder for now.
7
8use std::sync::Arc;
9
10use malloc_size_of_derive::MallocSizeOf;
11use serde::Serialize;
12use serde_json::{Map, Value};
13
14use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry, new_actor_name};
15use crate::protocol::ClientRequest;
16use crate::{ActorMsg, StreamId};
17
18#[derive(MallocSizeOf)]
19pub(crate) struct LayoutInspectorActor {
20    name: String,
21}
22
23#[derive(Serialize)]
24pub(crate) struct GetGridsReply {
25    from: String,
26    grids: Vec<String>,
27}
28
29#[derive(Serialize)]
30pub(crate) struct GetCurrentFlexboxReply {
31    from: String,
32    flexbox: Option<()>,
33}
34
35impl Actor for LayoutInspectorActor {
36    fn name(&self) -> &str {
37        &self.name
38    }
39
40    /// The layout inspector actor can handle the following messages:
41    ///
42    /// - `getGrids`: Returns a list of CSS grids, non functional at the moment
43    ///
44    /// - `getCurrentFlexbox`: Returns the active flexbox, non functional at the moment
45    fn handle_message(
46        &self,
47        request: ClientRequest,
48        _registry: &ActorRegistry,
49        msg_type: &str,
50        _msg: &Map<String, Value>,
51        _id: StreamId,
52    ) -> Result<(), ActorError> {
53        match msg_type {
54            "getGrids" => {
55                let msg = GetGridsReply {
56                    from: self.name().into(),
57                    // TODO: Actually create a list of grids
58                    grids: vec![],
59                };
60                request.reply_final(&msg)?
61            },
62            "getCurrentFlexbox" => {
63                let msg = GetCurrentFlexboxReply {
64                    from: self.name().into(),
65                    // TODO: Create and return the current flexbox object
66                    flexbox: None,
67                };
68                request.reply_final(&msg)?
69            },
70            _ => return Err(ActorError::UnrecognizedPacketType),
71        };
72        Ok(())
73    }
74}
75
76impl LayoutInspectorActor {
77    pub fn register(registry: &ActorRegistry) -> Arc<Self> {
78        let name = new_actor_name::<Self>();
79        let actor = Self { name };
80        registry.register::<Self>(actor)
81    }
82}
83
84impl ActorEncode<ActorMsg> for LayoutInspectorActor {
85    fn encode(&self, _: &ActorRegistry) -> ActorMsg {
86        ActorMsg {
87            actor: self.name().into(),
88        }
89    }
90}