Skip to main content

devtools/actors/watcher/
network_parent.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
5use std::sync::Arc;
6
7use malloc_size_of_derive::MallocSizeOf;
8use serde_json::{Map, Value};
9
10use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry, new_actor_name};
11use crate::protocol::ClientRequest;
12use crate::{ActorMsg, EmptyReplyMsg, StreamId};
13
14#[derive(MallocSizeOf)]
15pub(crate) struct NetworkParentActor {
16    name: String,
17}
18
19impl Actor for NetworkParentActor {
20    fn name(&self) -> &str {
21        &self.name
22    }
23
24    /// The network parent actor can handle the following messages:
25    ///
26    /// - `setSaveRequestAndResponseBodies`: Doesn't do anything yet
27    fn handle_message(
28        &self,
29        request: ClientRequest,
30        _registry: &ActorRegistry,
31        msg_type: &str,
32        _msg: &Map<String, Value>,
33        _id: StreamId,
34    ) -> Result<(), ActorError> {
35        match msg_type {
36            "setSaveRequestAndResponseBodies" => {
37                let msg = EmptyReplyMsg {
38                    from: self.name().into(),
39                };
40                request.reply_final(&msg)?
41            },
42            "setPersist" => {
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 NetworkParentActor {
55    pub fn register(registry: &ActorRegistry) -> Arc<Self> {
56        let name = new_actor_name::<Self>();
57        let actor = Self { name };
58        registry.register::<Self>(actor)
59    }
60}
61
62impl ActorEncode<ActorMsg> for NetworkParentActor {
63    fn encode(&self, _: &ActorRegistry) -> ActorMsg {
64        ActorMsg {
65            actor: self.name().into(),
66        }
67    }
68}