devtools/actors/
process.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].
6//!
7//! [Firefox JS implementation]: https://searchfox.org/mozilla-central/source/devtools/server/actors/descriptors/process.js
8
9use serde::Serialize;
10use serde_json::{Map, Value};
11
12use crate::StreamId;
13use crate::actor::{Actor, ActorError, ActorRegistry};
14use crate::actors::root::DescriptorTraits;
15use crate::protocol::ClientRequest;
16
17#[derive(Serialize)]
18struct ListWorkersReply {
19    from: String,
20    workers: Vec<u32>, // TODO: use proper JSON structure.
21}
22
23#[derive(Serialize)]
24#[serde(rename_all = "camelCase")]
25pub struct ProcessActorMsg {
26    actor: String,
27    id: u32,
28    is_parent: bool,
29    is_windowless_parent: bool,
30    traits: DescriptorTraits,
31}
32
33pub struct ProcessActor {
34    name: String,
35}
36
37impl Actor for ProcessActor {
38    fn name(&self) -> String {
39        self.name.clone()
40    }
41
42    /// The process actor can handle the following messages:
43    ///
44    /// - `listWorkers`: Returns a list of web workers, not supported yet.
45    fn handle_message(
46        &self,
47        request: ClientRequest,
48        _registry: &ActorRegistry,
49        msg_type: &str,
50        _msg: &Map<String, Value>,
51        _id: StreamId,
52    ) -> Result<(), ActorError> {
53        match msg_type {
54            "listWorkers" => {
55                let reply = ListWorkersReply {
56                    from: self.name(),
57                    workers: vec![],
58                };
59                request.reply_final(&reply)?
60            },
61
62            _ => return Err(ActorError::UnrecognizedPacketType),
63        };
64        Ok(())
65    }
66}
67
68impl ProcessActor {
69    pub fn new(name: String) -> Self {
70        Self { name }
71    }
72
73    pub fn encodable(&self) -> ProcessActorMsg {
74        ProcessActorMsg {
75            actor: self.name(),
76            id: 0,
77            is_parent: true,
78            is_windowless_parent: false,
79            traits: Default::default(),
80        }
81    }
82}