1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The page style actor is responsible of informing the DevTools client of the different style
//! properties applied, including the attributes and layout of each element.

use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::iter::once;
use std::net::TcpStream;

use base::id::PipelineId;
use devtools_traits::DevtoolScriptControlMsg::{GetLayout, GetSelectors};
use devtools_traits::{ComputedNodeLayout, DevtoolScriptControlMsg};
use ipc_channel::ipc::{self, IpcSender};
use serde::Serialize;
use serde_json::{self, Map, Value};

use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
use crate::actors::inspector::node::NodeActor;
use crate::actors::inspector::style_rule::{AppliedRule, ComputedDeclaration, StyleRuleActor};
use crate::actors::inspector::walker::{find_child, WalkerActor};
use crate::protocol::JsonPacketStream;
use crate::StreamId;

#[derive(Serialize)]
struct GetAppliedReply {
    entries: Vec<AppliedEntry>,
    from: String,
}

#[derive(Serialize)]
struct GetComputedReply {
    computed: HashMap<String, ComputedDeclaration>,
    from: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AppliedEntry {
    rule: AppliedRule,
    pseudo_element: Option<()>,
    is_system: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    inherited: Option<String>,
}

#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct GetLayoutReply {
    from: String,

    display: String,
    position: String,
    z_index: String,
    box_sizing: String,

    // Would be nice to use a proper struct, blocked by
    // https://github.com/serde-rs/serde/issues/43
    auto_margins: serde_json::value::Value,
    margin_top: String,
    margin_right: String,
    margin_bottom: String,
    margin_left: String,

    border_top_width: String,
    border_right_width: String,
    border_bottom_width: String,
    border_left_width: String,

    padding_top: String,
    padding_right: String,
    padding_bottom: String,
    padding_left: String,

    width: f32,
    height: f32,
}

#[derive(Serialize)]
pub struct IsPositionEditableReply {
    pub from: String,
    pub value: bool,
}

#[derive(Serialize)]
pub struct PageStyleMsg {
    pub actor: String,
    pub traits: HashMap<String, bool>,
}

pub struct PageStyleActor {
    pub name: String,
    pub script_chan: IpcSender<DevtoolScriptControlMsg>,
    pub pipeline: PipelineId,
}

impl Actor for PageStyleActor {
    fn name(&self) -> String {
        self.name.clone()
    }

    /// The page style actor can handle the following messages:
    ///
    /// - `getApplied`: Returns the applied styles for a node, they represent the explicit css
    ///   rules set for them, both in the style attribute and in stylesheets.
    ///
    /// - `getComputed`: Returns the computed styles for a node, these include all of the supported
    ///   css properties calculated values.
    ///
    /// - `getLayout`: Returns the box layout properties for a node.
    ///
    /// - `isPositionEditable`: Informs whether you can change a style property in the inspector.
    fn handle_message(
        &self,
        registry: &ActorRegistry,
        msg_type: &str,
        msg: &Map<String, Value>,
        stream: &mut TcpStream,
        _id: StreamId,
    ) -> Result<ActorMessageStatus, ()> {
        Ok(match msg_type {
            "getApplied" => self.get_applied(msg, registry, stream)?,
            "getComputed" => self.get_computed(msg, registry, stream)?,
            "getLayout" => self.get_layout(msg, registry, stream)?,
            "isPositionEditable" => self.is_position_editable(stream),
            _ => ActorMessageStatus::Ignored,
        })
    }
}

impl PageStyleActor {
    fn get_applied(
        &self,
        msg: &Map<String, Value>,
        registry: &ActorRegistry,
        stream: &mut TcpStream,
    ) -> Result<ActorMessageStatus, ()> {
        let target = msg.get("node").ok_or(())?.as_str().ok_or(())?;
        let node = registry.find::<NodeActor>(target);
        let walker = registry.find::<WalkerActor>(&node.walker);
        let entries: Vec<_> = find_child(
            &node.script_chan,
            node.pipeline,
            target,
            registry,
            &walker.root_node.actor,
            vec![],
            |msg| msg.actor == target,
        )
        .unwrap_or_default()
        .into_iter()
        .flat_map(|node| {
            let inherited = (node.actor != target).then(|| node.actor.clone());
            let node_actor = registry.find::<NodeActor>(&node.actor);

            // Get the css selectors that match this node present in the currently active stylesheets.
            let selectors = (|| {
                let (selectors_sender, selector_receiver) = ipc::channel().ok()?;
                walker
                    .script_chan
                    .send(GetSelectors(
                        walker.pipeline,
                        registry.actor_to_script(node.actor.clone()),
                        selectors_sender,
                    ))
                    .ok()?;
                selector_receiver.recv().ok()?
            })()
            .unwrap_or_default();

            // For each selector (plus an empty one that represents the style attribute)
            // get all of the rules associated with it.
            let entries =
                once(("".into(), usize::MAX))
                    .chain(selectors)
                    .filter_map(move |selector| {
                        let rule = match node_actor.style_rules.borrow_mut().entry(selector) {
                            Entry::Vacant(e) => {
                                let name = registry.new_name("style-rule");
                                let actor = StyleRuleActor::new(
                                    name.clone(),
                                    node_actor.name(),
                                    (!e.key().0.is_empty()).then_some(e.key().clone()),
                                );
                                let rule = actor.applied(registry)?;

                                registry.register_later(Box::new(actor));
                                e.insert(name);
                                rule
                            },
                            Entry::Occupied(e) => {
                                let actor = registry.find::<StyleRuleActor>(e.get());
                                actor.applied(registry)?
                            },
                        };
                        if inherited.is_some() && rule.declarations.is_empty() {
                            return None;
                        }

                        Some(AppliedEntry {
                            rule,
                            // TODO: Handle pseudo elements
                            pseudo_element: None,
                            is_system: false,
                            inherited: inherited.clone(),
                        })
                    });
            entries
        })
        .collect();
        let msg = GetAppliedReply {
            entries,
            from: self.name(),
        };
        let _ = stream.write_json_packet(&msg);
        Ok(ActorMessageStatus::Processed)
    }

    fn get_computed(
        &self,
        msg: &Map<String, Value>,
        registry: &ActorRegistry,
        stream: &mut TcpStream,
    ) -> Result<ActorMessageStatus, ()> {
        let target = msg.get("node").ok_or(())?.as_str().ok_or(())?;
        let node_actor = registry.find::<NodeActor>(target);
        let computed = (|| match node_actor
            .style_rules
            .borrow_mut()
            .entry(("".into(), usize::MAX))
        {
            Entry::Vacant(e) => {
                let name = registry.new_name("style-rule");
                let actor = StyleRuleActor::new(name.clone(), target.into(), None);
                let computed = actor.computed(registry)?;
                registry.register_later(Box::new(actor));
                e.insert(name);
                Some(computed)
            },
            Entry::Occupied(e) => {
                let actor = registry.find::<StyleRuleActor>(e.get());
                Some(actor.computed(registry)?)
            },
        })()
        .unwrap_or_default();
        let msg = GetComputedReply {
            computed,
            from: self.name(),
        };
        let _ = stream.write_json_packet(&msg);
        Ok(ActorMessageStatus::Processed)
    }

    fn get_layout(
        &self,
        msg: &Map<String, Value>,
        registry: &ActorRegistry,
        stream: &mut TcpStream,
    ) -> Result<ActorMessageStatus, ()> {
        let target = msg.get("node").ok_or(())?.as_str().ok_or(())?;
        let (computed_node_sender, computed_node_receiver) = ipc::channel().map_err(|_| ())?;
        self.script_chan
            .send(GetLayout(
                self.pipeline,
                registry.actor_to_script(target.to_owned()),
                computed_node_sender,
            ))
            .unwrap();
        let ComputedNodeLayout {
            display,
            position,
            z_index,
            box_sizing,
            auto_margins,
            margin_top,
            margin_right,
            margin_bottom,
            margin_left,
            border_top_width,
            border_right_width,
            border_bottom_width,
            border_left_width,
            padding_top,
            padding_right,
            padding_bottom,
            padding_left,
            width,
            height,
        } = computed_node_receiver.recv().map_err(|_| ())?.ok_or(())?;
        let msg_auto_margins = msg
            .get("autoMargins")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        let msg = GetLayoutReply {
            from: self.name(),
            display,
            position,
            z_index,
            box_sizing,
            auto_margins: if msg_auto_margins {
                let mut m = Map::new();
                let auto = serde_json::value::Value::String("auto".to_owned());
                if auto_margins.top {
                    m.insert("top".to_owned(), auto.clone());
                }
                if auto_margins.right {
                    m.insert("right".to_owned(), auto.clone());
                }
                if auto_margins.bottom {
                    m.insert("bottom".to_owned(), auto.clone());
                }
                if auto_margins.left {
                    m.insert("left".to_owned(), auto);
                }
                serde_json::value::Value::Object(m)
            } else {
                serde_json::value::Value::Null
            },
            margin_top,
            margin_right,
            margin_bottom,
            margin_left,
            border_top_width,
            border_right_width,
            border_bottom_width,
            border_left_width,
            padding_top,
            padding_right,
            padding_bottom,
            padding_left,
            width,
            height,
        };
        let msg = serde_json::to_string(&msg).map_err(|_| ())?;
        let msg = serde_json::from_str::<Value>(&msg).map_err(|_| ())?;
        let _ = stream.write_json_packet(&msg);
        Ok(ActorMessageStatus::Processed)
    }

    fn is_position_editable(&self, stream: &mut TcpStream) -> ActorMessageStatus {
        let msg = IsPositionEditableReply {
            from: self.name(),
            value: false,
        };
        let _ = stream.write_json_packet(&msg);
        ActorMessageStatus::Processed
    }
}