devtools/actors/
device.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::{ActorDescription, ClientRequest, Method};
11
12#[derive(Serialize)]
13struct GetDescriptionReply {
14    from: String,
15    value: SystemInfo,
16}
17
18// This is only a minimal subset of the properties exposed/expected by Firefox
19// (see https://searchfox.org/mozilla-central/source/devtools/shared/system.js#45)
20#[derive(Serialize)]
21#[serde(rename_all = "camelCase")]
22struct SystemInfo {
23    apptype: String,
24    // Display version
25    version: String,
26    // Build ID (timestamp with format YYYYMMDDhhmmss), used for compatibility checks
27    // (see https://searchfox.org/mozilla-central/source/devtools/client/shared/remote-debugging/version-checker.js#82)
28    appbuildid: String,
29    // Firefox major.minor version number, use for compatibility checks
30    platformversion: String,
31    // Display name
32    brand_name: String,
33}
34
35include!(concat!(env!("OUT_DIR"), "/build_id.rs"));
36
37pub struct DeviceActor {
38    pub name: String,
39}
40
41impl Actor for DeviceActor {
42    fn name(&self) -> String {
43        self.name.clone()
44    }
45    fn handle_message(
46        &self,
47        request: ClientRequest,
48        _registry: &ActorRegistry,
49        msg_type: &str,
50        _msg: &Map<String, Value>,
51        _id: StreamId,
52    ) -> Result<(), ActorError> {
53        match msg_type {
54            "getDescription" => {
55                let msg = GetDescriptionReply {
56                    from: self.name(),
57                    value: SystemInfo {
58                        apptype: "servo".to_string(),
59                        version: env!("CARGO_PKG_VERSION").to_string(),
60                        appbuildid: BUILD_ID.to_string(),
61                        platformversion: "133.0".to_string(),
62                        brand_name: "Servo".to_string(),
63                    },
64                };
65                request.reply_final(&msg)?
66            },
67
68            _ => return Err(ActorError::UnrecognizedPacketType),
69        };
70        Ok(())
71    }
72}
73
74impl DeviceActor {
75    pub fn new(name: String) -> DeviceActor {
76        DeviceActor { name }
77    }
78
79    pub fn description() -> ActorDescription {
80        ActorDescription {
81            category: "actor",
82            type_name: "device",
83            methods: vec![Method {
84                name: "getDescription",
85                request: Value::Null,
86                response: Value::Object(
87                    vec![(
88                        "value".to_owned(),
89                        Value::Object(
90                            vec![("_retval".to_owned(), Value::String("json".to_owned()))]
91                                .into_iter()
92                                .collect(),
93                        ),
94                    )]
95                    .into_iter()
96                    .collect(),
97                ),
98            }],
99        }
100    }
101}