devtools/actors/watcher/
thread_configuration.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 <https://searchfox.org/mozilla-central/source/devtools/server/actors/thread-configuration.js>
6//! This actor manages the configuration flags that the devtools host can apply to threads.
7
8use std::collections::HashMap;
9
10use malloc_size_of_derive::MallocSizeOf;
11use serde_json::{Map, Value};
12
13use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry};
14use crate::protocol::ClientRequest;
15use crate::{ActorMsg, EmptyReplyMsg, StreamId};
16
17#[derive(MallocSizeOf)]
18pub(crate) struct ThreadConfigurationActor {
19    name: String,
20    _configuration: HashMap<&'static str, bool>,
21}
22
23impl Actor for ThreadConfigurationActor {
24    fn name(&self) -> String {
25        self.name.clone()
26    }
27
28    /// The thread configuration actor can handle the following messages:
29    ///
30    /// - `updateConfiguration`: Receives new configuration flags from the devtools host.
31    fn handle_message(
32        &self,
33        request: ClientRequest,
34        _registry: &ActorRegistry,
35        msg_type: &str,
36        _msg: &Map<String, Value>,
37        _id: StreamId,
38    ) -> Result<(), ActorError> {
39        match msg_type {
40            "updateConfiguration" => {
41                // TODO: Actually update configuration
42                let msg = EmptyReplyMsg { from: self.name() };
43                request.reply_final(&msg)?
44            },
45            _ => return Err(ActorError::UnrecognizedPacketType),
46        };
47        Ok(())
48    }
49}
50
51impl ThreadConfigurationActor {
52    pub fn register(registry: &ActorRegistry) -> String {
53        let name = registry.new_name::<Self>();
54        let actor = Self {
55            name: name.clone(),
56            _configuration: HashMap::new(),
57        };
58        registry.register::<Self>(actor);
59        name
60    }
61}
62
63impl ActorEncode<ActorMsg> for ThreadConfigurationActor {
64    fn encode(&self, _: &ActorRegistry) -> ActorMsg {
65        ActorMsg { actor: self.name() }
66    }
67}