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