servo-devtools 0.1.0

A component of the servo web-engine.
Documentation
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Liberally derived from the [Firefox JS implementation](http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/inspector.js).

use atomic_refcell::AtomicRefCell;
use malloc_size_of_derive::MallocSizeOf;
use serde::Serialize;
use serde_json::{self, Map, Value};

use crate::actor::{Actor, ActorError, ActorRegistry};
use crate::actors::inspector::highlighter::HighlighterActor;
use crate::actors::inspector::page_style::{PageStyleActor, PageStyleMsg};
use crate::actors::inspector::walker::{WalkerActor, WalkerMsg};
use crate::protocol::ClientRequest;
use crate::{ActorMsg, StreamId};

pub mod accessibility;
pub mod css_properties;
pub mod highlighter;
pub mod layout;
pub mod node;
pub mod page_style;
pub mod style_rule;
pub mod walker;

#[derive(Serialize)]
struct GetHighlighterReply {
    from: String,
    highlighter: ActorMsg,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct GetPageStyleReply {
    from: String,
    page_style: PageStyleMsg,
}

#[derive(Serialize)]
struct GetWalkerReply {
    from: String,
    walker: WalkerMsg,
}

#[derive(Serialize)]
struct SupportsHighlightersReply {
    from: String,
    value: bool,
}

#[derive(MallocSizeOf)]
pub(crate) struct InspectorActor {
    name: String,
    highlighter_name: String,
    page_style_name: String,
    pub(crate) walker: String,
}

impl Actor for InspectorActor {
    fn name(&self) -> String {
        self.name.clone()
    }

    fn handle_message(
        &self,
        request: ClientRequest,
        registry: &ActorRegistry,
        msg_type: &str,
        _msg: &Map<String, Value>,
        _id: StreamId,
    ) -> Result<(), ActorError> {
        match msg_type {
            "getPageStyle" => {
                let msg = GetPageStyleReply {
                    from: self.name(),
                    page_style: registry.encode::<PageStyleActor, _>(&self.page_style_name),
                };
                request.reply_final(&msg)?
            },

            "getHighlighterByType" => {
                let msg = GetHighlighterReply {
                    from: self.name(),
                    highlighter: registry.encode::<HighlighterActor, _>(&self.highlighter_name),
                };
                request.reply_final(&msg)?
            },

            "getWalker" => {
                let msg = GetWalkerReply {
                    from: self.name(),
                    walker: registry.encode::<WalkerActor, _>(&self.walker),
                };
                request.reply_final(&msg)?
            },

            "supportsHighlighters" => {
                let msg = SupportsHighlightersReply {
                    from: self.name(),
                    value: true,
                };
                request.reply_final(&msg)?
            },

            _ => return Err(ActorError::UnrecognizedPacketType),
        };
        Ok(())
    }
}

impl InspectorActor {
    pub fn register(registry: &ActorRegistry, browsing_context_name: String) -> String {
        let highlighter_actor = HighlighterActor {
            name: registry.new_name::<HighlighterActor>(),
            browsing_context_name: browsing_context_name.clone(),
        };

        let page_style_actor = PageStyleActor {
            name: registry.new_name::<PageStyleActor>(),
        };

        let walker = WalkerActor {
            name: registry.new_name::<WalkerActor>(),
            mutations: AtomicRefCell::new(vec![]),
            browsing_context_name,
        };

        let inspector_actor = Self {
            name: registry.new_name::<InspectorActor>(),
            highlighter_name: highlighter_actor.name(),
            page_style_name: page_style_actor.name(),
            walker: walker.name(),
        };
        let inspector_name = inspector_actor.name();

        registry.register(highlighter_actor);
        registry.register(page_style_actor);
        registry.register(walker);
        registry.register(inspector_actor);

        inspector_name
    }
}