Skip to main content

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