Skip to main content

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