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