servo-devtools 0.1.0

A component of the servo web-engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* 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](https://searchfox.org/mozilla-central/source/devtools/server/actors/webbrowser.js).
//! Connection point for remote devtools that wish to investigate a particular Browsing Context's contents.
//! Supports dynamic attaching and detaching which control notifications of navigation, etc.

use atomic_refcell::AtomicRefCell;
use devtools_traits::DevtoolScriptControlMsg::{self, GetCssDatabase, SimulateColorScheme};
use devtools_traits::{DevtoolsPageInfo, NavigationState};
use embedder_traits::Theme;
use malloc_size_of_derive::MallocSizeOf;
use rustc_hash::FxHashMap;
use serde::Serialize;
use serde_json::{Map, Value};
use servo_base::generic_channel::{self, GenericSender, SendError};
use servo_base::id::PipelineId;

use crate::actor::{Actor, ActorEncode, ActorError, ActorRegistry};
use crate::actors::inspector::InspectorActor;
use crate::actors::inspector::accessibility::AccessibilityActor;
use crate::actors::inspector::css_properties::CssPropertiesActor;
use crate::actors::reflow::ReflowActor;
use crate::actors::stylesheets::StyleSheetsActor;
use crate::actors::tab::TabDescriptorActor;
use crate::actors::thread::ThreadActor;
use crate::actors::watcher::{SessionContext, SessionContextType, WatcherActor};
use crate::id::{DevtoolsBrowserId, DevtoolsBrowsingContextId, DevtoolsOuterWindowId, IdMap};
use crate::protocol::{ClientRequest, DevtoolsConnection, JsonPacketStream};
use crate::resource::ResourceAvailable;
use crate::{EmptyReplyMsg, StreamId};

#[derive(Serialize)]
struct ListWorkersReply {
    from: String,
    workers: Vec<()>,
}

#[derive(Serialize)]
struct FrameUpdateReply {
    from: String,
    #[serde(rename = "type")]
    type_: String,
    frames: Vec<FrameUpdateMsg>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct FrameUpdateMsg {
    id: u32,
    is_top_level: bool,
    url: String,
    title: String,
}

#[derive(Serialize)]
struct TabNavigated {
    from: String,
    #[serde(rename = "type")]
    type_: String,
    url: String,
    title: Option<String>,
    #[serde(rename = "nativeConsoleAPI")]
    native_console_api: bool,
    state: String,
    is_frame_switching: bool,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct BrowsingContextTraits {
    frames: bool,
    is_browsing_context: bool,
    log_in_page: bool,
    navigation: bool,
    supports_top_level_target_flag: bool,
    watchpoints: bool,
}

#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
enum TargetType {
    Frame,
    // Other target types not implemented yet.
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct BrowsingContextActorMsg {
    actor: String,
    title: String,
    url: String,
    /// This correspond to webview_id
    #[serde(rename = "browserId")]
    browser_id: u32,
    #[serde(rename = "outerWindowID")]
    outer_window_id: u32,
    #[serde(rename = "browsingContextID")]
    browsing_context_id: u32,
    is_top_level_target: bool,
    traits: BrowsingContextTraits,
    // Implemented actors
    accessibility_actor: String,
    console_actor: String,
    css_properties_actor: String,
    inspector_actor: String,
    reflow_actor: String,
    style_sheets_actor: String,
    thread_actor: String,
    target_type: TargetType,
    // Part of the official protocol, but not yet implemented.
    // animations_actor: String,
    // changes_actor: String,
    // framerate_actor: String,
    // manifest_actor: String,
    // memory_actor: String,
    // network_content_actor: String,
    // objects_manager: String,
    // performance_actor: String,
    // resonsive_actor: String,
    // storage_actor: String,
    // tracer_actor: String,
    // web_extension_inspected_window_actor: String,
    // web_socket_actor: String,
}

/// The browsing context actor encompasses all of the other supporting actors when debugging a web
/// view. To this extent, it contains a watcher actor that helps when communicating with the host,
/// as well as resource actors that each perform one debugging function.
#[derive(MallocSizeOf)]
pub(crate) struct BrowsingContextActor {
    name: String,
    pub title: AtomicRefCell<String>,
    pub url: AtomicRefCell<String>,
    /// This corresponds to webview_id
    pub browser_id: DevtoolsBrowserId,
    // TODO: Should these ids be atomic?
    active_pipeline_id: AtomicRefCell<PipelineId>,
    active_outer_window_id: AtomicRefCell<DevtoolsOuterWindowId>,
    pub browsing_context_id: DevtoolsBrowsingContextId,
    accessibility: String,
    pub console_name: String,
    css_properties_name: String,
    pub(crate) inspector_name: String,
    reflow_name: String,
    style_sheets_name: String,
    pub thread_name: String,
    _tab: String,
    // Different pipelines may run on different script threads.
    // These should be kept around even when the active pipeline is updated,
    // in case the browsing context revisits a pipeline via history navigation.
    // TODO: Each entry is stored forever; ideally there should be a way to
    //       detect when `ScriptThread`s are destroyed and remove the associated
    //       entries.
    script_chans: AtomicRefCell<FxHashMap<PipelineId, GenericSender<DevtoolScriptControlMsg>>>,
    pub watcher_name: String,
}

impl ResourceAvailable for BrowsingContextActor {
    fn actor_name(&self) -> String {
        self.name.clone()
    }
}

impl Actor for BrowsingContextActor {
    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 {
            "listFrames" => {
                // TODO: Find out what needs to be listed here
                let msg = EmptyReplyMsg { from: self.name() };
                request.reply_final(&msg)?
            },
            "listWorkers" => {
                request.reply_final(&ListWorkersReply {
                    from: self.name(),
                    // TODO: Find out what needs to be listed here
                    workers: vec![],
                })?
            },
            _ => return Err(ActorError::UnrecognizedPacketType),
        };
        Ok(())
    }
}

impl BrowsingContextActor {
    #[expect(clippy::too_many_arguments)]
    pub(crate) fn new(
        console_name: String,
        browser_id: DevtoolsBrowserId,
        browsing_context_id: DevtoolsBrowsingContextId,
        page_info: DevtoolsPageInfo,
        pipeline_id: PipelineId,
        outer_window_id: DevtoolsOuterWindowId,
        script_sender: GenericSender<DevtoolScriptControlMsg>,
        registry: &ActorRegistry,
    ) -> BrowsingContextActor {
        let name = registry.new_name::<BrowsingContextActor>();
        let DevtoolsPageInfo {
            title,
            url,
            is_top_level_global,
            ..
        } = page_info;

        let accessibility_actor =
            AccessibilityActor::new(registry.new_name::<AccessibilityActor>());

        let properties = (|| {
            let (properties_sender, properties_receiver) = generic_channel::channel()?;
            script_sender.send(GetCssDatabase(properties_sender)).ok()?;
            properties_receiver.recv().ok()
        })()
        .unwrap_or_default();
        let css_properties_actor =
            CssPropertiesActor::new(registry.new_name::<CssPropertiesActor>(), properties);

        let inspector_name = InspectorActor::register(registry, name.clone());

        let reflow_actor = ReflowActor::new(registry.new_name::<ReflowActor>());

        let style_sheets_actor = StyleSheetsActor::new(registry.new_name::<StyleSheetsActor>());

        let tab_descriptor_actor =
            TabDescriptorActor::new(registry, name.clone(), is_top_level_global);

        let thread_actor = ThreadActor::new(
            registry.new_name::<ThreadActor>(),
            script_sender.clone(),
            Some(name.clone()),
        );

        let watcher_actor = WatcherActor::new(
            registry,
            name.clone(),
            SessionContext::new(SessionContextType::BrowserElement),
        );

        let mut script_chans = FxHashMap::default();
        script_chans.insert(pipeline_id, script_sender);

        let target = BrowsingContextActor {
            name,
            script_chans: AtomicRefCell::new(script_chans),
            title: AtomicRefCell::new(title),
            url: AtomicRefCell::new(url.into_string()),
            active_pipeline_id: AtomicRefCell::new(pipeline_id),
            active_outer_window_id: AtomicRefCell::new(outer_window_id),
            browser_id,
            browsing_context_id,
            accessibility: accessibility_actor.name(),
            console_name,
            css_properties_name: css_properties_actor.name(),
            inspector_name,
            reflow_name: reflow_actor.name(),
            style_sheets_name: style_sheets_actor.name(),
            _tab: tab_descriptor_actor.name(),
            thread_name: thread_actor.name(),
            watcher_name: watcher_actor.name(),
        };

        registry.register(accessibility_actor);
        registry.register(css_properties_actor);
        registry.register(reflow_actor);
        registry.register(style_sheets_actor);
        registry.register(tab_descriptor_actor);
        registry.register(thread_actor);
        registry.register(watcher_actor);

        target
    }

    pub(crate) fn handle_new_global(
        &self,
        pipeline: PipelineId,
        script_sender: GenericSender<DevtoolScriptControlMsg>,
    ) {
        self.script_chans
            .borrow_mut()
            .insert(pipeline, script_sender);
    }

    pub(crate) fn handle_navigate<'a>(
        &self,
        state: NavigationState,
        id_map: &mut IdMap,
        connections: impl Iterator<Item = &'a mut DevtoolsConnection>,
    ) {
        let (pipeline_id, title, url, state) = match state {
            NavigationState::Start(url) => (None, None, url, "start"),
            NavigationState::Stop(pipeline, info) => {
                (Some(pipeline), Some(info.title), info.url, "stop")
            },
        };
        if let Some(pipeline_id) = pipeline_id {
            let outer_window_id = id_map.outer_window_id(pipeline_id);
            *self.active_outer_window_id.borrow_mut() = outer_window_id;
            *self.active_pipeline_id.borrow_mut() = pipeline_id;
        }
        url.as_str().clone_into(&mut self.url.borrow_mut());
        if let Some(ref t) = title {
            self.title.borrow_mut().clone_from(t);
        }

        let msg = TabNavigated {
            from: self.name(),
            type_: "tabNavigated".to_owned(),
            url: url.as_str().to_owned(),
            title,
            native_console_api: true,
            state: state.to_owned(),
            is_frame_switching: false,
        };

        for stream in connections {
            let _ = stream.write_json_packet(&msg);
        }
    }

    pub(crate) fn title_changed(&self, pipeline_id: PipelineId, title: String) {
        if pipeline_id != self.pipeline_id() {
            return;
        }
        *self.title.borrow_mut() = title;
    }

    pub(crate) fn frame_update(&self, request: &mut ClientRequest) {
        let _ = request.write_json_packet(&FrameUpdateReply {
            from: self.name(),
            type_: "frameUpdate".into(),
            frames: vec![FrameUpdateMsg {
                id: self.browsing_context_id.value(),
                is_top_level: true,
                title: self.title.borrow().clone(),
                url: self.url.borrow().clone(),
            }],
        });
    }

    pub fn simulate_color_scheme(&self, theme: Theme) -> Result<(), ()> {
        self.script_chan()
            .send(SimulateColorScheme(self.pipeline_id(), theme))
            .map_err(|_| ())
    }

    pub(crate) fn pipeline_id(&self) -> PipelineId {
        *self.active_pipeline_id.borrow()
    }

    pub(crate) fn outer_window_id(&self) -> DevtoolsOuterWindowId {
        *self.active_outer_window_id.borrow()
    }

    /// Returns the script sender for the active pipeline.
    pub(crate) fn script_chan(&self) -> GenericSender<DevtoolScriptControlMsg> {
        self.script_chans
            .borrow()
            .get(&self.pipeline_id())
            .unwrap()
            .clone()
    }

    pub(crate) fn instruct_script_to_send_live_updates(&self, should_send_updates: bool) {
        let result = self
            .script_chan()
            .send(DevtoolScriptControlMsg::WantsLiveNotifications(
                self.pipeline_id(),
                should_send_updates,
            ));

        // Notifying the script thread may fail with a "Disconnected" error if servo
        // as a whole is being shut down.
        debug_assert!(matches!(result, Ok(_) | Err(SendError::Disconnected)));
    }
}

impl ActorEncode<BrowsingContextActorMsg> for BrowsingContextActor {
    fn encode(&self, _: &ActorRegistry) -> BrowsingContextActorMsg {
        BrowsingContextActorMsg {
            actor: self.name(),
            traits: BrowsingContextTraits {
                is_browsing_context: true,
                frames: true,
                log_in_page: false,
                navigation: true,
                supports_top_level_target_flag: true,
                watchpoints: true,
            },
            title: self.title.borrow().clone(),
            url: self.url.borrow().clone(),
            browser_id: self.browser_id.value(),
            browsing_context_id: self.browsing_context_id.value(),
            outer_window_id: self.outer_window_id().value(),
            is_top_level_target: true,
            accessibility_actor: self.accessibility.clone(),
            console_actor: self.console_name.clone(),
            css_properties_actor: self.css_properties_name.clone(),
            inspector_actor: self.inspector_name.clone(),
            reflow_actor: self.reflow_name.clone(),
            style_sheets_actor: self.style_sheets_name.clone(),
            thread_actor: self.thread_name.clone(),
            target_type: TargetType::Frame,
        }
    }
}