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
use crate::protocol::cdp::{
    types::{Event, JsUInt},
    Browser,
    Network::{CookieParam, DeleteCookies},
    Page,
    Page::PrintToPDF,
    DOM::Node,
};

use serde::{Deserialize, Serialize};

use serde_json::Value;

pub type CallId = JsUInt;

use thiserror::Error;

use anyhow::Result;

type JsInt = i32;

#[derive(Deserialize, Debug, PartialEq, Clone, Error)]
#[error("Method call error {}: {}", code, message)]
pub struct RemoteError {
    pub code: JsInt,
    pub message: String,
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Response {
    #[serde(rename(deserialize = "id"))]
    pub call_id: CallId,
    pub result: Option<Value>,
    pub error: Option<RemoteError>,
}

pub fn parse_response<T>(response: Response) -> Result<T>
where
    T: serde::de::DeserializeOwned + std::fmt::Debug,
{
    if let Some(error) = response.error {
        return Err(error.into());
    }

    let result: T = serde_json::from_value(response.result.unwrap())?;

    Ok(result)
}

#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum Message {
    Event(Event),
    Response(Response),
    ConnectionShutdown,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct TransferMode {
    mode: String,
}

impl From<TransferMode> for Option<Page::PrintToPDFTransfer_modeOption> {
    fn from(val: TransferMode) -> Self {
        if val.mode == "base64" {
            Some(Page::PrintToPDFTransfer_modeOption::ReturnAsBase64)
        } else if val.mode == "stream" {
            Some(Page::PrintToPDFTransfer_modeOption::ReturnAsStream)
        } else {
            None
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct PrintToPdfOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub landscape: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_header_footer: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub print_background: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub paper_width: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub paper_height: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin_top: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin_bottom: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin_left: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin_right: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_ranges: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ignore_invalid_page_ranges: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub header_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub footer_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefer_css_page_size: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer_mode: Option<TransferMode>,
}

pub fn parse_raw_message(raw_message: &str) -> Result<Message> {
    Ok(serde_json::from_str::<Message>(raw_message)?)
}

#[derive(Clone, Debug)]
pub enum Bounds {
    Minimized,
    Maximized,
    Fullscreen,
    Normal {
        /// The offset from the left edge of the screen to the window in pixels.
        left: Option<JsUInt>,
        /// The offset from the top edge of the screen to the window in pixels.
        top: Option<JsUInt>,
        /// The window width in pixels.
        width: Option<f64>,
        /// THe window height in pixels.
        height: Option<f64>,
    },
}

impl Bounds {
    /// Set normal window state without setting any coordinates
    pub fn normal() -> Self {
        Self::Normal {
            left: None,
            top: None,
            width: None,
            height: None,
        }
    }
}

impl From<CookieParam> for DeleteCookies {
    fn from(v: CookieParam) -> Self {
        Self {
            name: v.name,
            url: v.url,
            domain: v.domain,
            path: v.path,
        }
    }
}

impl From<Bounds> for Browser::Bounds {
    fn from(val: Bounds) -> Self {
        match val {
            Bounds::Minimized => Browser::Bounds {
                left: None,
                top: None,
                width: None,
                height: None,
                window_state: Some(Browser::WindowState::Minimized),
            },
            Bounds::Maximized => Browser::Bounds {
                left: None,
                top: None,
                width: None,
                height: None,
                window_state: Some(Browser::WindowState::Maximized),
            },
            Bounds::Fullscreen => Browser::Bounds {
                left: None,
                top: None,
                width: None,
                height: None,
                window_state: Some(Browser::WindowState::Fullscreen),
            },
            Bounds::Normal {
                left,
                top,
                width,
                height,
            } => Browser::Bounds {
                left,
                top,
                width: width.map(|f| f as u32),
                height: height.map(|f| f as u32),
                window_state: Some(Browser::WindowState::Normal),
            },
        }
    }
}

#[derive(Clone, Debug)]
pub struct CurrentBounds {
    pub left: JsUInt,
    pub top: JsUInt,
    pub width: f64,
    pub height: f64,
    pub state: Browser::WindowState,
}

impl From<Browser::Bounds> for CurrentBounds {
    fn from(bounds: Browser::Bounds) -> Self {
        Self {
            left: bounds.left.unwrap(),
            top: bounds.top.unwrap(),
            width: f64::from(bounds.width.unwrap()),
            height: f64::from(bounds.height.unwrap()),
            state: bounds.window_state.unwrap(),
        }
    }
}

impl Default for PrintToPDF {
    fn default() -> Self {
        PrintToPDF {
            display_header_footer: None,
            footer_template: None,
            header_template: None,
            ignore_invalid_page_ranges: None,
            landscape: None,
            margin_bottom: None,
            margin_left: None,
            margin_right: None,
            margin_top: None,
            page_ranges: None,
            paper_height: None,
            paper_width: None,
            prefer_css_page_size: None,
            print_background: None,
            scale: None,
            transfer_mode: None,
        }
    }
}

struct SearchVisitor<'a, F> {
    predicate: F,
    item: Option<&'a Node>,
}

impl<'a, F: FnMut(&Node) -> bool> SearchVisitor<'a, F> {
    fn new(predicate: F) -> Self {
        SearchVisitor {
            predicate,
            item: None,
        }
    }

    fn visit(&mut self, n: &'a Node) {
        if (self.predicate)(n) {
            self.item = Some(n);
        } else if self.item.is_none() {
            if let Some(c) = &n.children {
                c.iter().for_each(|n| self.visit(n));
            }
        }
    }
}

impl Node {
    /// Returns the first node for which the given closure returns true.
    ///
    /// Nodes are inspected breadth-first down their children.
    pub fn find<F: FnMut(&Self) -> bool>(&self, predicate: F) -> Option<&Self> {
        let mut s = SearchVisitor::new(predicate);
        s.visit(self);
        s.item
    }
}

#[cfg(test)]
mod tests {
    use log::trace;
    use serde_json::json;

    use super::*;

    #[test]
    fn pass_through_channel() {
        env_logger::try_init().unwrap_or(());

        let attached_to_target_json = json!({
            "method": "Target.attachedToTarget",
            "params": {
                "sessionId": "8BEF122ABAB0C43B5729585A537F424A",
                "targetInfo": {
                    "targetId": "26DEBCB2A45BEFC67A84012AC32C8B2A",
                    "type": "page",
                    "title": "",
                    "url": "about:blank",
                    "attached": true,
                    "browserContextId": "946423F3D201EFA1A5FCF3462E340C15"
                },
                "waitingForDebugger": false
            }
        });

        let _event: Message = serde_json::from_value(attached_to_target_json).unwrap();
    }

    #[test]
    fn parse_event_fully() {
        env_logger::try_init().unwrap_or(());

        let attached_to_target_json = json!({
            "method": "Target.attachedToTarget",
            "params": {
                "sessionId": "8BEF122ABAB0C43B5729585A537F424A",
                "targetInfo": {
                    "targetId": "26DEBCB2A45BEFC67A84012AC32C8B2A",
                    "type": "page",
                    "title": "",
                    "url": "about:blank",
                    "attached": true,
                    "browserContextId": "946423F3D201EFA1A5FCF3462E340C15"
                },
                "waitingForDebugger": false
            }
        });

        if let Ok(Event::AttachedToTarget(_)) = serde_json::from_value(attached_to_target_json) {
        } else {
            panic!("Failed to parse event properly");
        }

        let received_target_msg_event = json!({
            "method": "Target.receivedMessageFromTarget",
            "params": {
                "sessionId": "8BEF122ABAB0C43B5729585A537F424A",
                "message": "{\"id\":43473,\"result\":{\"data\":\"kDEgAABII=\"}}",
                "targetId": "26DEBCB2A45BEFC67A84012AC32C8B2A"
            }
        });
        let event: Event = serde_json::from_value(received_target_msg_event).unwrap();
        match event {
            Event::ReceivedMessageFromTarget(ev) => {
                trace!("{:?}", ev);
            }
            _ => panic!("bad news"),
        }
    }

    #[test]
    fn easy_parse_messages() {
        env_logger::try_init().unwrap_or(());

        let example_message_strings = [
            // browser method response:
            "{\"id\":1,\"result\":{\"browserContextIds\":[\"C2652EACAAA12B41038F1F2137C57A6E\"]}}",
            "{\"id\":2,\"result\":{\"targetInfos\":[{\"targetId\":\"225A1B90036320AB4DB2E28F04AA6EE0\",\"type\":\"page\",\"title\":\"\",\"url\":\"about:blank\",\"attached\":false,\"browserContextId\":\"04FB807A65CFCA420C03E1134EB9214E\"}]}}",
            "{\"id\":3,\"result\":{}}",
            // browser event:
            "{\"method\":\"Target.attachedToTarget\",\"params\":{\"sessionId\":\"8BEF122ABAB0C43B5729585A537F424A\",\"targetInfo\":{\"targetId\":\"26DEBCB2A45BEFC67A84012AC32C8B2A\",\"type\":\"page\",\"title\":\"\",\"url\":\"about:blank\",\"attached\":true,\"browserContextId\":\"946423F3D201EFA1A5FCF3462E340C15\"},\"waitingForDebugger\":false}}",
            // browser event which indicates target method response:
            "{\"method\":\"Target.receivedMessageFromTarget\",\"params\":{\"sessionId\":\"8BEF122ABAB0C43B5729585A537F424A\",\"message\":\"{\\\"id\\\":43473,\\\"result\\\":{\\\"data\\\":\\\"iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAYAAACadoJwAAAMa0lEQVR4nO3XMQEAIAzAMMC/5+GiHCQK+nbPzCwAAIDAeR0AAAD8w4AAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABAxoAAAAAZAwIAAGQMCAAAkDEgAABII=\\\"}}\",\"targetId\":\"26DEBCB2A45BEFC67A84012AC32C8B2A\"}}"
        ];

        for msg_string in &example_message_strings {
            let _message: super::Message = parse_raw_message(msg_string).unwrap();
        }
    }
}