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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* 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/webconsole.js).
//! Mediates interaction between the remote web console and equivalent functionality (object
//! inspection, JS evaluation, autocompletion) in Servo.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};

use atomic_refcell::AtomicRefCell;
use devtools_traits::DebuggerValue::{
    self, BooleanValue, NullValue, NumberValue, ObjectValue, StringValue, VoidValue,
};
use devtools_traits::{
    ConsoleArgument, ConsoleMessage, ConsoleMessageFields, DevtoolScriptControlMsg, PageError,
    StackFrame, get_time_stamp,
};
use malloc_size_of_derive::MallocSizeOf;
use serde::Serialize;
use serde_json::{self, Map, Number, Value};
use servo_base::generic_channel::{self, GenericSender};
use servo_base::id::TEST_PIPELINE_ID;
use uuid::Uuid;

use crate::actor::{Actor, ActorError, ActorRegistry};
use crate::actors::browsing_context::BrowsingContextActor;
use crate::actors::object::{ObjectActor, ObjectPropertyDescriptor};
use crate::actors::worker::WorkerActor;
use crate::protocol::{ClientRequest, DevtoolsConnection, JsonPacketStream};
use crate::resource::{ResourceArrayType, ResourceAvailable};
use crate::{EmptyReplyMsg, StreamId, UniqueId};

#[derive(Clone, Serialize, MallocSizeOf)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DevtoolsConsoleMessage {
    #[serde(flatten)]
    fields: ConsoleMessageFields,
    #[ignore_malloc_size_of = "Currently no way to have serde_json::Value"]
    arguments: Vec<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stacktrace: Option<Vec<StackFrame>>,
    // Not implemented in Servo
    // inner_window_id
    // source_id
}

impl DevtoolsConsoleMessage {
    pub(crate) fn new(message: ConsoleMessage, registry: &ActorRegistry) -> Self {
        Self {
            fields: message.fields,
            arguments: message
                .arguments
                .into_iter()
                .map(|argument| console_argument_to_value(argument, registry))
                .collect(),
            stacktrace: message.stacktrace,
        }
    }
}

fn console_argument_to_value(argument: ConsoleArgument, registry: &ActorRegistry) -> Value {
    match argument {
        ConsoleArgument::String(value) => Value::String(value),
        ConsoleArgument::Integer(value) => Value::Number(value.into()),
        ConsoleArgument::Number(value) => {
            Number::from_f64(value).map(Value::from).unwrap_or_default()
        },
        ConsoleArgument::Boolean(value) => Value::Bool(value),
        ConsoleArgument::Object(object) => {
            // Create a new actor for the object.
            // These are currently never cleaned up, and we make no attempt at re-using the same actor
            // if the same object is logged repeatedly.
            let actor = ObjectActor::register(registry, None, object.class.clone());

            #[derive(Serialize)]
            #[serde(rename_all = "camelCase")]
            struct DevtoolsConsoleObjectArgument {
                r#type: String,
                actor: String,
                class: String,
                own_property_length: usize,
                extensible: bool,
                frozen: bool,
                sealed: bool,
                is_error: bool,
                preview: DevtoolsConsoleObjectArgumentPreview,
            }

            #[derive(Serialize)]
            #[serde(rename_all = "camelCase")]
            struct DevtoolsConsoleObjectArgumentPreview {
                kind: String,
                own_properties: HashMap<String, ObjectPropertyDescriptor>,
                own_properties_length: usize,
            }

            let own_properties: HashMap<String, ObjectPropertyDescriptor> = object
                .own_properties
                .into_iter()
                .map(|property| {
                    let property_descriptor = ObjectPropertyDescriptor {
                        configurable: property.configurable,
                        enumerable: property.enumerable,
                        writable: property.writable,
                        value: console_argument_to_value(property.value, registry),
                    };

                    (property.key, property_descriptor)
                })
                .collect();

            let argument = DevtoolsConsoleObjectArgument {
                r#type: "object".to_owned(),
                actor,
                class: object.class,
                own_property_length: own_properties.len(),
                extensible: true,
                frozen: false,
                sealed: false,
                is_error: false,
                preview: DevtoolsConsoleObjectArgumentPreview {
                    kind: "Object".to_string(),
                    own_properties_length: own_properties.len(),
                    own_properties,
                },
            };

            // to_value can fail if the implementation of Serialize fails or there are non-string map keys.
            // Neither should be possible here
            serde_json::to_value(argument).unwrap()
        },
    }
}

#[derive(Clone, Serialize, MallocSizeOf)]
#[serde(rename_all = "camelCase")]
struct DevtoolsPageError {
    #[serde(flatten)]
    page_error: PageError,
    category: String,
    error: bool,
    warning: bool,
    info: bool,
    private: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    stacktrace: Option<Vec<StackFrame>>,
    // Not implemented in Servo
    // inner_window_id
    // source_id
    // has_exception
    // exception
}

impl From<PageError> for DevtoolsPageError {
    fn from(page_error: PageError) -> Self {
        Self {
            page_error,
            category: "script".to_string(),
            error: true,
            warning: false,
            info: false,
            private: false,
            stacktrace: None,
        }
    }
}
#[derive(Clone, Serialize, MallocSizeOf)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PageErrorWrapper {
    page_error: DevtoolsPageError,
}

impl From<PageError> for PageErrorWrapper {
    fn from(page_error: PageError) -> Self {
        Self {
            page_error: page_error.into(),
        }
    }
}

#[derive(Clone, Serialize, MallocSizeOf)]
#[serde(untagged)]
pub(crate) enum ConsoleResource {
    ConsoleMessage(DevtoolsConsoleMessage),
    PageError(PageErrorWrapper),
}

impl ConsoleResource {
    pub fn resource_type(&self) -> String {
        match self {
            ConsoleResource::ConsoleMessage(_) => "console-message".into(),
            ConsoleResource::PageError(_) => "error-message".into(),
        }
    }
}

#[derive(Serialize)]
pub struct ConsoleClearMessage {
    pub level: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AutocompleteReply {
    from: String,
    matches: Vec<String>,
    match_prop: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EvaluateJSReply {
    from: String,
    input: String,
    result: Value,
    timestamp: u64,
    exception: Value,
    exception_message: Value,
    has_exception: bool,
    helper_result: Value,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EvaluateJSEvent {
    from: String,
    #[serde(rename = "type")]
    type_: String,
    input: String,
    result: Value,
    timestamp: u64,
    #[serde(rename = "resultID")]
    result_id: String,
    exception: Value,
    exception_message: Value,
    has_exception: bool,
    helper_result: Value,
}

#[derive(Serialize)]
struct EvaluateJSAsyncReply {
    from: String,
    #[serde(rename = "resultID")]
    result_id: String,
}

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

#[derive(MallocSizeOf)]
pub(crate) enum Root {
    BrowsingContext(String),
    DedicatedWorker(String),
}

#[derive(MallocSizeOf)]
pub(crate) struct ConsoleActor {
    name: String,
    root: Root,
    cached_events: AtomicRefCell<HashMap<UniqueId, Vec<ConsoleResource>>>,
    /// Used to control whether to send resource array messages from
    /// `handle_console_resource`. It starts being false, and it only gets
    /// activated after the client requests `console-message` or `error-message`
    /// resources for the first time. Otherwise we would be sending messages
    /// before the client is ready to receive them.
    client_ready_to_receive_messages: AtomicBool,
}

impl ConsoleActor {
    pub fn new(name: String, root: Root) -> Self {
        Self {
            name,
            root,
            cached_events: Default::default(),
            client_ready_to_receive_messages: false.into(),
        }
    }

    fn script_chan(&self, registry: &ActorRegistry) -> GenericSender<DevtoolScriptControlMsg> {
        match &self.root {
            Root::BrowsingContext(browsing_context_name) => registry
                .find::<BrowsingContextActor>(browsing_context_name)
                .script_chan(),
            Root::DedicatedWorker(worker_name) => registry
                .find::<WorkerActor>(worker_name)
                .script_chan
                .clone(),
        }
    }

    fn current_unique_id(&self, registry: &ActorRegistry) -> UniqueId {
        match &self.root {
            Root::BrowsingContext(browsing_context_name) => UniqueId::Pipeline(
                registry
                    .find::<BrowsingContextActor>(browsing_context_name)
                    .pipeline_id(),
            ),
            Root::DedicatedWorker(worker_name) => {
                UniqueId::Worker(registry.find::<WorkerActor>(worker_name).worker_id)
            },
        }
    }

    // TODO: This should be handled with struct serialization instead of manually adding values to a map
    fn value_to_json(value: DebuggerValue, registry: &ActorRegistry) -> Value {
        let mut m = Map::new();
        match value {
            VoidValue => {
                m.insert("type".to_owned(), Value::String("undefined".to_owned()));
                Value::Object(m)
            },
            NullValue => {
                m.insert("type".to_owned(), Value::String("null".to_owned()));
                Value::Object(m)
            },
            BooleanValue(val) => Value::Bool(val),
            NumberValue(val) => {
                if val.is_nan() {
                    m.insert("type".to_owned(), Value::String("NaN".to_owned()));
                    Value::Object(m)
                } else if val.is_infinite() {
                    if val < 0. {
                        m.insert("type".to_owned(), Value::String("-Infinity".to_owned()));
                    } else {
                        m.insert("type".to_owned(), Value::String("Infinity".to_owned()));
                    }
                    Value::Object(m)
                } else if val == 0. && val.is_sign_negative() {
                    m.insert("type".to_owned(), Value::String("-0".to_owned()));
                    Value::Object(m)
                } else {
                    Value::Number(Number::from_f64(val).unwrap())
                }
            },
            StringValue(s) => Value::String(s),
            ObjectValue {
                uuid,
                class,
                preview,
            } => {
                let properties = preview
                    .clone()
                    .and_then(|preview| preview.own_properties)
                    .unwrap_or_default();
                let actor = ObjectActor::register_with_properties(
                    registry,
                    Some(uuid),
                    class.clone(),
                    properties,
                );

                m.insert("type".to_owned(), Value::String("object".to_owned()));
                m.insert("class".to_owned(), Value::String(class));
                m.insert("actor".to_owned(), Value::String(actor));
                m.insert("extensible".to_owned(), Value::Bool(true));
                m.insert("frozen".to_owned(), Value::Bool(false));
                m.insert("sealed".to_owned(), Value::Bool(false));

                // Build preview
                // <https://searchfox.org/firefox-main/source/devtools/server/actors/object/previewers.js#849>
                let Some(preview) = preview else {
                    return Value::Object(m);
                };
                let mut preview_map = Map::new();

                if preview.kind == "ArrayLike" {
                    if let Some(length) = preview.array_length {
                        preview_map.insert("length".to_owned(), Value::Number(length.into()));
                    }
                } else {
                    if let Some(ref props) = preview.own_properties {
                        let mut own_props_map = Map::new();
                        for prop in props {
                            let descriptor =
                                serde_json::to_value(ObjectPropertyDescriptor::from(prop)).unwrap();
                            own_props_map.insert(prop.name.clone(), descriptor);
                        }
                        preview_map
                            .insert("ownProperties".to_owned(), Value::Object(own_props_map));
                    }

                    if let Some(length) = preview.own_properties_length {
                        preview_map.insert(
                            "ownPropertiesLength".to_owned(),
                            Value::Number(length.into()),
                        );
                        m.insert("ownPropertyLength".to_owned(), Value::Number(length.into()));
                    }
                }
                preview_map.insert("kind".to_owned(), Value::String(preview.kind));

                // Function-specific metadata
                if let Some(function) = preview.function {
                    if let Some(name) = function.name {
                        m.insert("name".to_owned(), Value::String(name));
                    }
                    if let Some(display_name) = function.display_name {
                        m.insert("displayName".to_owned(), Value::String(display_name));
                    }
                    m.insert(
                        "parameterNames".to_owned(),
                        Value::Array(
                            function
                                .parameter_names
                                .into_iter()
                                .map(Value::String)
                                .collect(),
                        ),
                    );
                    m.insert("isAsync".to_owned(), Value::Bool(function.is_async));
                    m.insert("isGenerator".to_owned(), Value::Bool(function.is_generator));
                }

                m.insert("preview".to_owned(), Value::Object(preview_map));

                Value::Object(m)
            },
        }
    }

    fn evaluate_js(
        &self,
        registry: &ActorRegistry,
        msg: &Map<String, Value>,
    ) -> Result<EvaluateJSReply, ()> {
        let input = msg.get("text").unwrap().as_str().unwrap().to_owned();
        let frame_actor_id = msg
            .get("frameActor")
            .and_then(|v| v.as_str())
            .map(String::from);
        let (chan, port) = generic_channel::channel().unwrap();
        // FIXME: Redesign messages so we don't have to fake pipeline ids when communicating with workers.
        let pipeline = match self.current_unique_id(registry) {
            UniqueId::Pipeline(p) => p,
            UniqueId::Worker(_) => TEST_PIPELINE_ID,
        };
        self.script_chan(registry)
            .send(DevtoolScriptControlMsg::Eval(
                input.clone(),
                pipeline,
                frame_actor_id,
                chan,
            ))
            .unwrap();

        let eval_result = port.recv().map_err(|_| ())?;
        let has_exception = eval_result.has_exception;

        let reply = EvaluateJSReply {
            from: self.name(),
            input,
            result: Self::value_to_json(eval_result.value, registry),
            timestamp: get_time_stamp(),
            exception: Value::Null,
            exception_message: Value::Null,
            has_exception,
            helper_result: Value::Null,
        };
        Ok(reply)
    }

    pub(crate) fn handle_console_resource(
        &self,
        resource: ConsoleResource,
        id: UniqueId,
        registry: &ActorRegistry,
        stream: &mut DevtoolsConnection,
    ) {
        self.cached_events
            .borrow_mut()
            .entry(id.clone())
            .or_default()
            .push(resource.clone());
        if !self
            .client_ready_to_receive_messages
            .load(Ordering::Relaxed)
        {
            return;
        }
        let resource_type = resource.resource_type();
        if id == self.current_unique_id(registry) {
            if let Root::BrowsingContext(browsing_context_name) = &self.root {
                registry
                    .find::<BrowsingContextActor>(browsing_context_name)
                    .resource_array(
                        resource,
                        resource_type,
                        ResourceArrayType::Available,
                        stream,
                    )
            };
        }
    }

    pub(crate) fn send_clear_message(
        &self,
        id: UniqueId,
        registry: &ActorRegistry,
        stream: &mut DevtoolsConnection,
    ) {
        if id == self.current_unique_id(registry) {
            if let Root::BrowsingContext(browsing_context_name) = &self.root {
                registry
                    .find::<BrowsingContextActor>(browsing_context_name)
                    .resource_array(
                        ConsoleClearMessage {
                            level: "clear".to_owned(),
                        },
                        "console-message".into(),
                        ResourceArrayType::Available,
                        stream,
                    )
            };
        }
    }

    pub(crate) fn get_cached_messages(
        &self,
        registry: &ActorRegistry,
        resource: &str,
    ) -> Vec<ConsoleResource> {
        let id = self.current_unique_id(registry);
        let cached_events = self.cached_events.borrow();
        let Some(events) = cached_events.get(&id) else {
            return vec![];
        };
        events
            .iter()
            .filter(|event| event.resource_type() == resource)
            .cloned()
            .collect()
    }

    pub(crate) fn received_first_message_from_client(&self) {
        self.client_ready_to_receive_messages
            .store(true, Ordering::Relaxed);
    }
}

impl Actor for ConsoleActor {
    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 {
            "clearMessagesCacheAsync" => {
                self.cached_events
                    .borrow_mut()
                    .remove(&self.current_unique_id(registry));
                let msg = EmptyReplyMsg { from: self.name() };
                request.reply_final(&msg)?
            },

            // TODO: implement autocompletion like onAutocomplete in
            //      http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/webconsole.js
            "autocomplete" => {
                let msg = AutocompleteReply {
                    from: self.name(),
                    matches: vec![],
                    match_prop: "".to_owned(),
                };
                request.reply_final(&msg)?
            },

            "evaluateJS" => {
                let msg = self.evaluate_js(registry, msg);
                request.reply_final(&msg)?
            },

            "evaluateJSAsync" => {
                let result_id = Uuid::new_v4().to_string();
                let early_reply = EvaluateJSAsyncReply {
                    from: self.name(),
                    result_id: result_id.clone(),
                };
                // Emit an eager reply so that the client starts listening
                // for an async event with the resultID
                let mut stream = request.reply(&early_reply)?;

                if msg.get("eager").and_then(|v| v.as_bool()).unwrap_or(false) {
                    // We don't support the side-effect free evaluation that eager evaluation
                    // really needs.
                    return Ok(());
                }

                let reply = self.evaluate_js(registry, msg).unwrap();
                let msg = EvaluateJSEvent {
                    from: self.name(),
                    type_: "evaluationResult".to_owned(),
                    input: reply.input,
                    result: reply.result,
                    timestamp: reply.timestamp,
                    result_id,
                    exception: reply.exception,
                    exception_message: reply.exception_message,
                    has_exception: reply.has_exception,
                    helper_result: reply.helper_result,
                };
                // Send the data from evaluateJS along with a resultID
                stream.write_json_packet(&msg)?
            },

            "setPreferences" => {
                let msg = SetPreferencesReply {
                    from: self.name(),
                    updated: vec![],
                };
                request.reply_final(&msg)?
            },

            // NOTE: Do not handle `startListeners`, it is a legacy API.
            // Instead, enable the resource in `WatcherActor::supported_resources`
            // and handle the messages there.
            _ => return Err(ActorError::UnrecognizedPacketType),
        };
        Ok(())
    }
}