DevTools: Implement watcher actor (#32509)

* feat: base for watcher

* feat: some more watcher tests

* feat: implement getWatcher

* refactor: clean up getWatcher

* feat: implement watchTargets

* feat: implement watchResources

* feat: very messy watchTargets fix

* refactor: clean browsing context

* feat: target configuration

* refactor: start cleanup

* refactor: more doc coments

* refactor: clean browsing context
This commit is contained in:
eri 2024-06-21 18:06:55 +02:00 committed by GitHub
parent 26c585a0c5
commit 5eb8813448
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 680 additions and 244 deletions

View file

@ -2,6 +2,11 @@
* 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]
//! (https://searchfox.org/mozilla-central/source/devtools/server/actors/descriptors/tab.js)
//! Descriptor actor that represents a web view. It can link a tab to the corresponding watcher
//! actor to enable inspection.
use std::net::TcpStream;
use serde::Serialize;
@ -10,17 +15,19 @@ use serde_json::{Map, Value};
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
use crate::actors::browsing_context::{BrowsingContextActor, BrowsingContextActorMsg};
use crate::actors::root::{DescriptorTraits, RootActor};
use crate::actors::watcher::{WatcherActor, WatcherActorMsg};
use crate::protocol::JsonPacketStream;
use crate::StreamId;
// https://searchfox.org/mozilla-central/source/devtools/server/actors/descriptors/tab.js
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TabDescriptorActorMsg {
actor: String,
browser_id: u32,
#[serde(rename = "browsingContextID")]
browsing_context_id: u32,
is_zombie_tab: bool,
#[serde(rename = "outerWindowID")]
outer_window_id: u32,
selected: bool,
title: String,
@ -46,6 +53,13 @@ struct GetFaviconReply {
favicon: String,
}
#[derive(Serialize)]
struct GetWatcherReply {
from: String,
#[serde(flatten)]
watcher: WatcherActorMsg,
}
pub struct TabDescriptorActor {
name: String,
browsing_context_actor: String,
@ -56,6 +70,14 @@ impl Actor for TabDescriptorActor {
self.name.clone()
}
/// The tab actor can handle the following messages:
///
/// - `getTarget`: Returns the surrounding `BrowsingContextActor`.
///
/// - `getFavicon`: Should return the tab favicon, but it is not yet supported.
///
/// - `getWatcher`: Returns a `WatcherActor` linked to the tab's `BrowsingContext`. It is used
/// to describe the debugging capabilities of this tab.
fn handle_message(
&self,
registry: &ActorRegistry,
@ -83,7 +105,15 @@ impl Actor for TabDescriptorActor {
});
ActorMessageStatus::Processed
},
// TODO: Unexpected message getWatcher when inspecting tab (create watcher actor)
"getWatcher" => {
let ctx_actor = registry.find::<BrowsingContextActor>(&self.browsing_context_actor);
let watcher = registry.find::<WatcherActor>(&ctx_actor.watcher);
let _ = stream.write_json_packet(&GetWatcherReply {
from: self.name(),
watcher: watcher.encodable(),
});
ActorMessageStatus::Processed
},
_ => ActorMessageStatus::Ignored,
})
}
@ -105,21 +135,22 @@ impl TabDescriptorActor {
pub fn encodable(&self, registry: &ActorRegistry, selected: bool) -> TabDescriptorActorMsg {
let ctx_actor = registry.find::<BrowsingContextActor>(&self.browsing_context_actor);
let browser_id = ctx_actor.active_pipeline.get().index.0.get();
let browsing_context_id = ctx_actor.browsing_context_id.index.0.get();
let title = ctx_actor.title.borrow().clone();
let url = ctx_actor.url.borrow().clone();
TabDescriptorActorMsg {
actor: self.name(),
browsing_context_id: ctx_actor.browsing_context_id.index.0.get(),
browser_id: ctx_actor.active_pipeline.get().index.0.get(),
browsing_context_id,
browser_id,
is_zombie_tab: false,
outer_window_id: ctx_actor.active_pipeline.get().index.0.get(),
outer_window_id: browser_id,
selected,
title,
traits: DescriptorTraits {
watcher: true,
supports_reload_descriptor: false,
supports_reload_descriptor: true,
},
url,
}