devtools/actors/inspector/
accessibility.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 Accessibility actor is responsible for the Accessibility tab in the DevTools page. Right
6//! now it is a placeholder for future functionality.
7
8use malloc_size_of_derive::MallocSizeOf;
9use serde::Serialize;
10use serde_json::{Map, Value};
11
12use crate::StreamId;
13use crate::actor::{Actor, ActorError, ActorRegistry};
14use crate::actors::inspector::accessible_walker::AccessibleWalkerActor;
15use crate::actors::inspector::simulator::SimulatorActor;
16use crate::protocol::ClientRequest;
17
18#[derive(Serialize)]
19struct BootstrapState {
20    enabled: bool,
21}
22
23#[derive(Serialize)]
24struct BootstrapReply {
25    from: String,
26    state: BootstrapState,
27}
28
29#[derive(Serialize)]
30struct GetSimulatorReply {
31    from: String,
32    simulator: ActorMsg,
33}
34
35#[derive(Serialize)]
36#[serde(rename_all = "camelCase")]
37struct AccessibilityTraits {
38    tabbing_order: bool,
39}
40
41#[derive(Serialize)]
42struct GetTraitsReply {
43    from: String,
44    traits: AccessibilityTraits,
45}
46
47#[derive(Serialize)]
48struct ActorMsg {
49    actor: String,
50}
51
52#[derive(Serialize)]
53struct GetWalkerReply {
54    from: String,
55    walker: ActorMsg,
56}
57
58#[derive(MallocSizeOf)]
59pub(crate) struct AccessibilityActor {
60    name: String,
61}
62
63impl Actor for AccessibilityActor {
64    fn name(&self) -> String {
65        self.name.clone()
66    }
67
68    /// The accesibility actor can handle the following messages:
69    ///
70    /// - `bootstrap`: It is required but it doesn't do anything yet
71    ///
72    /// - `getSimulator`: Returns a new Simulator actor
73    ///
74    /// - `getTraits`: Informs the DevTools client about the configuration of the accessibility actor
75    ///
76    /// - `getWalker`: Returns a new AccessibleWalker actor (not to be confused with the general
77    ///   inspector Walker actor)
78    fn handle_message(
79        &self,
80        request: ClientRequest,
81        registry: &ActorRegistry,
82        msg_type: &str,
83        _msg: &Map<String, Value>,
84        _id: StreamId,
85    ) -> Result<(), ActorError> {
86        match msg_type {
87            "bootstrap" => {
88                let msg = BootstrapReply {
89                    from: self.name(),
90                    state: BootstrapState { enabled: false },
91                };
92                request.reply_final(&msg)?
93            },
94            "getSimulator" => {
95                let msg = GetSimulatorReply {
96                    from: self.name(),
97                    simulator: ActorMsg {
98                        actor: SimulatorActor::register(registry),
99                    },
100                };
101                request.reply_final(&msg)?
102            },
103            "getTraits" => {
104                let msg = GetTraitsReply {
105                    from: self.name(),
106                    traits: AccessibilityTraits {
107                        tabbing_order: true,
108                    },
109                };
110                request.reply_final(&msg)?
111            },
112            "getWalker" => {
113                let msg = GetWalkerReply {
114                    from: self.name(),
115                    walker: ActorMsg {
116                        actor: AccessibleWalkerActor::register(registry),
117                    },
118                };
119                request.reply_final(&msg)?
120            },
121            _ => return Err(ActorError::UnrecognizedPacketType),
122        };
123        Ok(())
124    }
125}
126
127impl AccessibilityActor {
128    pub fn register(registry: &ActorRegistry) -> String {
129        let name = registry.new_name::<Self>();
130        let actor = Self { name: name.clone() };
131        registry.register::<Self>(actor);
132        name
133    }
134}