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