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 serde::Serialize;
11use serde_json::{Map, Value};
12
13use crate::actor::{Actor, ActorError, ActorRegistry};
14use crate::protocol::ClientRequest;
15use crate::{EmptyReplyMsg, StreamId};
16
17#[derive(Serialize)]
18pub struct ThreadConfigurationActorMsg {
19    actor: String,
20}
21
22pub struct ThreadConfigurationActor {
23    name: String,
24    _configuration: HashMap<&'static str, bool>,
25}
26
27impl Actor for ThreadConfigurationActor {
28    fn name(&self) -> String {
29        self.name.clone()
30    }
31
32    /// The thread configuration actor can handle the following messages:
33    ///
34    /// - `updateConfiguration`: Receives new configuration flags from the devtools host.
35    fn handle_message(
36        &self,
37        request: ClientRequest,
38        _registry: &ActorRegistry,
39        msg_type: &str,
40        _msg: &Map<String, Value>,
41        _id: StreamId,
42    ) -> Result<(), ActorError> {
43        match msg_type {
44            "updateConfiguration" => {
45                // TODO: Actually update configuration
46                let msg = EmptyReplyMsg { from: self.name() };
47                request.reply_final(&msg)?
48            },
49            _ => return Err(ActorError::UnrecognizedPacketType),
50        };
51        Ok(())
52    }
53}
54
55impl ThreadConfigurationActor {
56    pub fn new(name: String) -> Self {
57        Self {
58            name,
59            _configuration: HashMap::new(),
60        }
61    }
62
63    pub fn encodable(&self) -> ThreadConfigurationActorMsg {
64        ThreadConfigurationActorMsg { actor: self.name() }
65    }
66}