Skip to main content

devtools/actors/
inspector.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//! Liberally derived from the [Firefox JS implementation](http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/inspector.js).
6
7use std::sync::Arc;
8
9use malloc_size_of_derive::MallocSizeOf;
10use serde::Serialize;
11use serde_json::{self, Map, Value};
12
13use crate::actor::{Actor, ActorError, ActorRegistry, new_actor_name};
14use crate::actors::inspector::highlighter::HighlighterActor;
15use crate::actors::inspector::page_style::{PageStyleActor, PageStyleMsg};
16use crate::actors::inspector::walker::{WalkerActor, WalkerMsg};
17use crate::protocol::ClientRequest;
18use crate::{ActorMsg, StreamId};
19
20pub mod accessibility;
21pub mod accessible_walker;
22pub mod css_properties;
23pub mod highlighter;
24pub mod layout;
25pub mod node;
26pub mod page_style;
27pub mod simulator;
28pub mod style_rule;
29pub mod walker;
30
31#[derive(Serialize)]
32struct GetHighlighterReply {
33    from: String,
34    highlighter: ActorMsg,
35}
36
37#[derive(Serialize)]
38#[serde(rename_all = "camelCase")]
39struct GetPageStyleReply {
40    from: String,
41    page_style: PageStyleMsg,
42}
43
44#[derive(Serialize)]
45struct GetWalkerReply {
46    from: String,
47    walker: WalkerMsg,
48}
49
50#[derive(Serialize)]
51struct SupportsHighlightersReply {
52    from: String,
53    value: bool,
54}
55
56#[derive(MallocSizeOf)]
57pub(crate) struct InspectorActor {
58    name: String,
59    highlighter_name: String,
60    page_style_name: String,
61    pub(crate) walker_name: String,
62}
63
64impl Actor for InspectorActor {
65    fn name(&self) -> &str {
66        &self.name
67    }
68
69    fn handle_message(
70        &self,
71        request: ClientRequest,
72        registry: &ActorRegistry,
73        msg_type: &str,
74        _msg: &Map<String, Value>,
75        _id: StreamId,
76    ) -> Result<(), ActorError> {
77        match msg_type {
78            "getPageStyle" => {
79                let msg = GetPageStyleReply {
80                    from: self.name().into(),
81                    page_style: registry.encode::<PageStyleActor, _>(&self.page_style_name),
82                };
83                request.reply_final(&msg)?
84            },
85
86            "getHighlighterByType" => {
87                let msg = GetHighlighterReply {
88                    from: self.name().into(),
89                    highlighter: registry.encode::<HighlighterActor, _>(&self.highlighter_name),
90                };
91                request.reply_final(&msg)?
92            },
93
94            "getWalker" => {
95                let msg = GetWalkerReply {
96                    from: self.name().into(),
97                    walker: registry.encode::<WalkerActor, _>(&self.walker_name),
98                };
99                request.reply_final(&msg)?
100            },
101
102            "supportsHighlighters" => {
103                let msg = SupportsHighlightersReply {
104                    from: self.name().into(),
105                    value: true,
106                };
107                request.reply_final(&msg)?
108            },
109
110            _ => return Err(ActorError::UnrecognizedPacketType),
111        };
112        Ok(())
113    }
114}
115
116impl InspectorActor {
117    pub fn register(registry: &ActorRegistry, browsing_context_name: String) -> Arc<Self> {
118        let highlighter_actor = HighlighterActor::register(registry, browsing_context_name.clone());
119        let page_style_actor = PageStyleActor::register(registry);
120        let walker_actor = WalkerActor::register(registry, browsing_context_name);
121
122        let actor = Self {
123            name: new_actor_name::<InspectorActor>(),
124            highlighter_name: highlighter_actor.name().into(),
125            page_style_name: page_style_actor.name().into(),
126            walker_name: walker_actor.name().into(),
127        };
128
129        registry.register::<Self>(actor)
130    }
131}