Skip to main content

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