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