devtools/actors/
stylesheets.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 malloc_size_of_derive::MallocSizeOf;
6use serde::Serialize;
7use serde_json::{Map, Value};
8
9use crate::StreamId;
10use crate::actor::{Actor, ActorError, ActorRegistry};
11use crate::protocol::ClientRequest;
12
13/// <https://searchfox.org/mozilla-central/source/devtools/server/actors/resources/stylesheets.js>
14#[derive(Serialize)]
15#[serde(rename_all = "camelCase")]
16pub(crate) struct StyleSheetData {
17    /// Unique identifier for this stylesheet.
18    resource_id: String,
19    /// The URL of the stylesheet. Optional for inline stylesheets.
20    href: Option<String>,
21    /// The URL of the document that owns this stylesheet.
22    node_href: String,
23    /// Whether the stylesheet is disabled.
24    disabled: bool,
25    /// The title of the stylesheet.
26    title: String,
27    /// Whether this is a browser stylesheet.
28    system: bool,
29    /// Whether this stylesheet was created by DevTools.
30    is_new: bool,
31    /// Optional source map URL.
32    source_map_url: Option<String>,
33    /// The index of this stylesheet in the document's stylesheet list.
34    style_sheet_index: i32,
35    // TODO: the following fields will be implemented later once we fetch the stylesheets
36    // constructed: bool,
37    // file_name: Option<String>,
38    // at_rules: Vec<Rule>,
39    // rule_count: u32,
40    // source_map_base_url: String,
41}
42
43#[derive(Serialize)]
44#[serde(rename_all = "camelCase")]
45struct GetStyleSheetsReply {
46    from: String,
47    style_sheets: Vec<StyleSheetData>,
48}
49
50#[derive(MallocSizeOf)]
51pub(crate) struct StyleSheetsActor {
52    name: String,
53}
54
55impl Actor for StyleSheetsActor {
56    fn name(&self) -> String {
57        self.name.clone()
58    }
59    fn handle_message(
60        &self,
61        request: ClientRequest,
62        _registry: &ActorRegistry,
63        msg_type: &str,
64        _msg: &Map<String, Value>,
65        _id: StreamId,
66    ) -> Result<(), ActorError> {
67        match msg_type {
68            "getStyleSheets" => {
69                let msg = GetStyleSheetsReply {
70                    from: self.name(),
71                    // TODO: Fetch actual stylesheets from the script thread.
72                    style_sheets: vec![],
73                };
74                request.reply_final(&msg)?
75            },
76
77            _ => return Err(ActorError::UnrecognizedPacketType),
78        };
79        Ok(())
80    }
81}
82
83impl StyleSheetsActor {
84    pub fn register(registry: &ActorRegistry) -> String {
85        let name = registry.new_name::<Self>();
86        let actor = StyleSheetsActor { name: name.clone() };
87        registry.register::<Self>(actor);
88        name
89    }
90}