viewpoint-cdp 0.4.3

Low-level Chrome DevTools Protocol implementation over WebSocket
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
//! Emulation domain types.
//!
//! The Emulation domain emulates different device metrics and capabilities.

use serde::{Deserialize, Serialize};

/// Screen orientation.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScreenOrientation {
    /// Orientation type.
    #[serde(rename = "type")]
    pub orientation_type: ScreenOrientationType,
    /// Orientation angle.
    pub angle: i32,
}

/// Screen orientation type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ScreenOrientationType {
    /// Portrait orientation (primary).
    PortraitPrimary,
    /// Portrait orientation (secondary).
    PortraitSecondary,
    /// Landscape orientation (primary).
    LandscapePrimary,
    /// Landscape orientation (secondary).
    LandscapeSecondary,
}

/// Display feature.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DisplayFeature {
    /// Orientation of the display feature.
    pub orientation: DisplayFeatureOrientation,
    /// The offset from the screen origin in pixels.
    pub offset: i32,
    /// The length of the feature in pixels.
    pub mask_length: i32,
}

/// Display feature orientation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DisplayFeatureOrientation {
    /// Vertical orientation.
    Vertical,
    /// Horizontal orientation.
    Horizontal,
}

/// Device posture.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DevicePosture {
    /// Current posture type.
    #[serde(rename = "type")]
    pub posture_type: DevicePostureType,
}

/// Device posture type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DevicePostureType {
    /// Continuous posture.
    Continuous,
    /// Folded posture.
    Folded,
}

/// Parameters for Emulation.setDeviceMetricsOverride.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetDeviceMetricsOverrideParams {
    /// Overriding width value in pixels (0 disables override).
    pub width: i32,
    /// Overriding height value in pixels (0 disables override).
    pub height: i32,
    /// Overriding device scale factor value (0 disables override).
    pub device_scale_factor: f64,
    /// Whether to emulate mobile device.
    pub mobile: bool,
    /// Scale to apply to resulting view image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<f64>,
    /// Overriding screen width value in pixels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_width: Option<i32>,
    /// Overriding screen height value in pixels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_height: Option<i32>,
    /// Overriding view X position on screen in pixels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position_x: Option<i32>,
    /// Overriding view Y position on screen in pixels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position_y: Option<i32>,
    /// Do not set visible view size, rely upon explicit setVisibleSize call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dont_set_visible_size: Option<bool>,
    /// Screen orientation override.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_orientation: Option<ScreenOrientation>,
    /// The viewport dimensions and scale.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub viewport: Option<Viewport>,
    /// Display feature for foldable devices.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_feature: Option<DisplayFeature>,
    /// Device posture for foldable devices.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device_posture: Option<DevicePosture>,
}

/// Viewport for device metrics override.
#[derive(Debug, Clone, Serialize)]
pub struct Viewport {
    /// X offset in viewport.
    pub x: f64,
    /// Y offset in viewport.
    pub y: f64,
    /// Viewport width.
    pub width: f64,
    /// Viewport height.
    pub height: f64,
    /// Viewport scale.
    pub scale: f64,
}

/// Parameters for Emulation.clearDeviceMetricsOverride (empty).
#[derive(Debug, Clone, Serialize, Default)]
pub struct ClearDeviceMetricsOverrideParams {}

/// Media type for emulation.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum MediaType {
    /// Print media.
    Print,
    /// Screen media.
    Screen,
}

/// Parameters for Emulation.setEmulatedMedia.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetEmulatedMediaParams {
    /// Media type to emulate. Empty string disables the override.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<String>,
    /// Media features to emulate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub features: Option<Vec<MediaFeature>>,
}

/// Media feature for emulation.
#[derive(Debug, Clone, Serialize)]
pub struct MediaFeature {
    /// Feature name.
    pub name: String,
    /// Feature value.
    pub value: String,
}

/// Viewport size.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewportSize {
    /// Width in pixels.
    pub width: i32,
    /// Height in pixels.
    pub height: i32,
}

impl ViewportSize {
    /// Create a new viewport size.
    pub fn new(width: i32, height: i32) -> Self {
        Self { width, height }
    }
}

/// Parameters for Emulation.setTouchEmulationEnabled.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetTouchEmulationEnabledParams {
    /// Whether touch emulation is enabled.
    pub enabled: bool,
    /// Maximum touch points. Defaults to 1.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_touch_points: Option<i32>,
}

// =============================================================================
// Geolocation Emulation
// =============================================================================

/// Parameters for Emulation.setGeolocationOverride.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct SetGeolocationOverrideParams {
    /// Latitude in degrees.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latitude: Option<f64>,
    /// Longitude in degrees.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub longitude: Option<f64>,
    /// Accuracy in meters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accuracy: Option<f64>,
}

impl SetGeolocationOverrideParams {
    /// Create geolocation override with coordinates.
    pub fn new(latitude: f64, longitude: f64) -> Self {
        Self {
            latitude: Some(latitude),
            longitude: Some(longitude),
            accuracy: Some(0.0), // Default to exact accuracy
        }
    }

    /// Create geolocation override with coordinates and accuracy.
    pub fn with_accuracy(latitude: f64, longitude: f64, accuracy: f64) -> Self {
        Self {
            latitude: Some(latitude),
            longitude: Some(longitude),
            accuracy: Some(accuracy),
        }
    }

    /// Create params that indicate position unavailable.
    pub fn unavailable() -> Self {
        Self::default()
    }
}

/// Parameters for Emulation.clearGeolocationOverride (empty).
#[derive(Debug, Clone, Serialize, Default)]
pub struct ClearGeolocationOverrideParams {}

// =============================================================================
// Timezone Emulation
// =============================================================================

/// Parameters for Emulation.setTimezoneOverride.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetTimezoneOverrideParams {
    /// The timezone identifier. If empty, disables the override.
    pub timezone_id: String,
}

impl SetTimezoneOverrideParams {
    /// Create timezone override.
    pub fn new(timezone_id: impl Into<String>) -> Self {
        Self {
            timezone_id: timezone_id.into(),
        }
    }
}

// =============================================================================
// Locale Emulation
// =============================================================================

/// Parameters for Emulation.setLocaleOverride.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetLocaleOverrideParams {
    /// ICU locale. Empty string disables the override.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locale: Option<String>,
}

impl SetLocaleOverrideParams {
    /// Create locale override.
    pub fn new(locale: impl Into<String>) -> Self {
        Self {
            locale: Some(locale.into()),
        }
    }

    /// Clear locale override.
    pub fn clear() -> Self {
        Self { locale: None }
    }
}

// =============================================================================
// User Agent Emulation
// =============================================================================

/// Parameters for Emulation.setUserAgentOverride.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetUserAgentOverrideParams {
    /// User agent to use.
    pub user_agent: String,
    /// Browser language to emulate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept_language: Option<String>,
    /// The platform navigator.platform should return.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platform: Option<String>,
    /// User agent client hints.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_agent_metadata: Option<UserAgentMetadata>,
}

impl SetUserAgentOverrideParams {
    /// Create user agent override.
    pub fn new(user_agent: impl Into<String>) -> Self {
        Self {
            user_agent: user_agent.into(),
            accept_language: None,
            platform: None,
            user_agent_metadata: None,
        }
    }
}

/// User agent metadata.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UserAgentMetadata {
    /// Brands.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub brands: Option<Vec<UserAgentBrandVersion>>,
    /// Full version list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_version_list: Option<Vec<UserAgentBrandVersion>>,
    /// Full version.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_version: Option<String>,
    /// Platform.
    pub platform: String,
    /// Platform version.
    pub platform_version: String,
    /// Architecture.
    pub architecture: String,
    /// Model.
    pub model: String,
    /// Mobile.
    pub mobile: bool,
    /// Bitness.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bitness: Option<String>,
    /// Whether on a `WoW64` machine.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wow64: Option<bool>,
}

/// User agent brand version.
#[derive(Debug, Clone, Serialize)]
pub struct UserAgentBrandVersion {
    /// Brand.
    pub brand: String,
    /// Version.
    pub version: String,
}

// =============================================================================
// Vision Deficiency Emulation
// =============================================================================

/// Vision deficiency type for emulation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[derive(Default)]
pub enum VisionDeficiency {
    /// No vision deficiency emulation.
    #[default]
    None,
    /// Achromatopsia (no color vision).
    Achromatopsia,
    /// Blue blindness.
    BlurredVision,
    /// Deuteranopia (green-blind).
    Deuteranopia,
    /// Protanopia (red-blind).
    Protanopia,
    /// Tritanopia (blue-blind).
    Tritanopia,
}

/// Parameters for Emulation.setEmulatedVisionDeficiency.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetEmulatedVisionDeficiencyParams {
    /// Vision deficiency type to emulate.
    #[serde(rename = "type")]
    pub deficiency_type: VisionDeficiency,
}

impl SetEmulatedVisionDeficiencyParams {
    /// Create vision deficiency emulation params.
    pub fn new(deficiency_type: VisionDeficiency) -> Self {
        Self { deficiency_type }
    }

    /// Clear vision deficiency emulation.
    pub fn clear() -> Self {
        Self {
            deficiency_type: VisionDeficiency::None,
        }
    }
}