devtools/actors/
object.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 serde::Serialize;
6use serde_json::{Map, Value};
7
8use crate::StreamId;
9use crate::actor::{Actor, ActorError, ActorRegistry};
10use crate::protocol::ClientRequest;
11
12#[derive(Serialize)]
13pub struct ObjectPreview {
14    kind: String,
15    url: String,
16}
17
18#[derive(Serialize)]
19#[serde(rename_all = "camelCase")]
20pub struct ObjectActorMsg {
21    actor: String,
22    #[serde(rename = "type")]
23    type_: String,
24    class: String,
25    own_property_length: i32,
26    extensible: bool,
27    frozen: bool,
28    sealed: bool,
29    is_error: bool,
30    preview: ObjectPreview,
31}
32
33pub struct ObjectActor {
34    pub name: String,
35    pub _uuid: String,
36}
37
38impl Actor for ObjectActor {
39    fn name(&self) -> String {
40        self.name.clone()
41    }
42    fn handle_message(
43        &self,
44        _request: ClientRequest,
45        _: &ActorRegistry,
46        _: &str,
47        _: &Map<String, Value>,
48        _: StreamId,
49    ) -> Result<(), ActorError> {
50        // TODO: Handle enumSymbols for console object inspection
51        Err(ActorError::UnrecognizedPacketType)
52    }
53}
54
55impl ObjectActor {
56    pub fn register(registry: &ActorRegistry, uuid: String) -> String {
57        if !registry.script_actor_registered(uuid.clone()) {
58            let name = registry.new_name("object");
59            let actor = ObjectActor {
60                name: name.clone(),
61                _uuid: uuid.clone(),
62            };
63
64            registry.register_script_actor(uuid, name.clone());
65            registry.register_later(Box::new(actor));
66
67            name
68        } else {
69            registry.script_to_actor(uuid)
70        }
71    }
72}
73
74// TODO: Remove once the message is used.
75#[allow(dead_code)]
76pub trait ObjectToProtocol {
77    fn encode(self) -> ObjectActorMsg;
78}
79
80impl ObjectToProtocol for ObjectActor {
81    fn encode(self) -> ObjectActorMsg {
82        // TODO: Review hardcoded values here
83        ObjectActorMsg {
84            actor: self.name,
85            type_: "object".into(),
86            class: "Window".into(),
87            own_property_length: 0,
88            extensible: true,
89            frozen: false,
90            sealed: false,
91            is_error: false,
92            preview: ObjectPreview {
93                kind: "ObjectWithURL".into(),
94                url: "".into(),
95            },
96        }
97    }
98}