Skip to main content

cbf_chrome/data/
custom_scheme.rs

1//! Chrome-specific custom scheme request/response models.
2
3/// Immutable startup registration for a Chrome-backed custom scheme handler.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ChromeCustomSchemeRegistration {
6    pub scheme: String,
7    pub host: String,
8}
9
10/// Chromium-observed request method for a custom scheme load.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum ChromeCustomSchemeRequestMethod {
13    Get,
14    Other(String),
15}
16
17impl ChromeCustomSchemeRequestMethod {
18    pub fn as_str(&self) -> &str {
19        match self {
20            Self::Get => "GET",
21            Self::Other(value) => value.as_str(),
22        }
23    }
24}
25
26/// A host-routable custom scheme request emitted by the Chrome backend.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct ChromeCustomSchemeRequest {
29    pub request_id: u64,
30    pub profile_id: String,
31    pub url: String,
32    pub scheme: String,
33    pub host: String,
34    pub path: String,
35    pub query: Option<String>,
36    pub method: ChromeCustomSchemeRequestMethod,
37}
38
39/// Host-visible completion result for a custom scheme request.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum ChromeCustomSchemeResponseResult {
42    Ok,
43    NotFound,
44    Aborted,
45}
46
47/// Response body and headers for a custom scheme request.
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct ChromeCustomSchemeResponse {
50    pub request_id: u64,
51    pub result: ChromeCustomSchemeResponseResult,
52    pub body: Vec<u8>,
53    pub mime_type: String,
54    pub content_security_policy: Option<String>,
55    pub access_control_allow_origin: Option<String>,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::{
61        ChromeCustomSchemeRegistration, ChromeCustomSchemeRequest, ChromeCustomSchemeRequestMethod,
62        ChromeCustomSchemeResponse, ChromeCustomSchemeResponseResult,
63    };
64
65    #[test]
66    fn request_method_as_str_returns_expected_value() {
67        assert_eq!(ChromeCustomSchemeRequestMethod::Get.as_str(), "GET");
68        assert_eq!(
69            ChromeCustomSchemeRequestMethod::Other("HEAD".to_string()).as_str(),
70            "HEAD"
71        );
72    }
73
74    #[test]
75    fn custom_scheme_models_are_cloneable() {
76        let registration = ChromeCustomSchemeRegistration {
77            scheme: "app".to_string(),
78            host: "simpleapp".to_string(),
79        };
80        let request = ChromeCustomSchemeRequest {
81            request_id: 1,
82            profile_id: "profile".to_string(),
83            url: "app://simpleapp/ui.html".to_string(),
84            scheme: "app".to_string(),
85            host: "simpleapp".to_string(),
86            path: "/ui.html".to_string(),
87            query: None,
88            method: ChromeCustomSchemeRequestMethod::Get,
89        };
90        let response = ChromeCustomSchemeResponse {
91            request_id: 1,
92            result: ChromeCustomSchemeResponseResult::Ok,
93            body: b"ok".to_vec(),
94            mime_type: "text/plain".to_string(),
95            content_security_policy: Some("default-src 'self'".to_string()),
96            access_control_allow_origin: Some("app://simpleapp".to_string()),
97        };
98
99        assert_eq!(registration.clone(), registration);
100        assert_eq!(request.clone(), request);
101        assert_eq!(response.clone(), response);
102    }
103}