Skip to main content

ohos_web_binding/protocol/
inner_protocol.rs

1use std::{
2    collections::HashMap,
3    ffi::CString,
4    sync::{LazyLock, Mutex},
5};
6
7use bitflags::bitflags;
8use ohos_web_sys::{
9    ArkWeb_CustomSchemeOption, ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CODE_CACHE_ENABLED,
10    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CORS_ENABLED,
11    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CSP_BYPASSING,
12    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_DISPLAY_ISOLATED,
13    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_FETCH_ENABLED,
14    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_LOCAL,
15    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_SECURE,
16    ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_STANDARD,
17    ArkWeb_CustomSchemeOption_OH_ARKWEB_SCHEME_OPTION_NONE, OH_ArkWeb_RegisterCustomSchemes,
18};
19
20/// custom protocol list
21static CUSTOM_PROTOCOL_LIST: LazyLock<Mutex<HashMap<String, CustomProtocolOption>>> =
22    LazyLock::new(|| Mutex::new(HashMap::new()));
23
24bitflags! {
25    #[derive(PartialEq, Clone, Copy)]
26    pub struct CustomProtocolOption: u32 {
27        const None = 0;
28        const Standard = 1;
29        const Local = 2;
30        const DisplayIsolated = 4;
31        const Secure = 8;
32        const CorsEnabled = 16;
33        const CspBypassing = 32;
34        const FetchEnabled = 64;
35        const CodeCacheEnabled = 128;
36    }
37}
38
39impl From<CustomProtocolOption> for ArkWeb_CustomSchemeOption {
40    fn from(value: CustomProtocolOption) -> Self {
41        let mut option = 0;
42        if value.contains(CustomProtocolOption::Standard) {
43            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_STANDARD;
44        }
45        if value.contains(CustomProtocolOption::Local) {
46            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_LOCAL;
47        }
48        if value.contains(CustomProtocolOption::DisplayIsolated) {
49            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_DISPLAY_ISOLATED;
50        }
51        if value.contains(CustomProtocolOption::Secure) {
52            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_SECURE;
53        }
54        if value.contains(CustomProtocolOption::CorsEnabled) {
55            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CORS_ENABLED;
56        }
57        if value.contains(CustomProtocolOption::CspBypassing) {
58            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CSP_BYPASSING;
59        }
60        if value.contains(CustomProtocolOption::FetchEnabled) {
61            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_FETCH_ENABLED;
62        }
63        if value.contains(CustomProtocolOption::CodeCacheEnabled) {
64            option |= ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CODE_CACHE_ENABLED;
65        }
66        option
67    }
68}
69
70impl From<ArkWeb_CustomSchemeOption> for CustomProtocolOption {
71    fn from(value: ArkWeb_CustomSchemeOption) -> Self {
72        match value {
73            #![allow(non_upper_case_globals)]
74            ArkWeb_CustomSchemeOption_OH_ARKWEB_SCHEME_OPTION_NONE => CustomProtocolOption::None,
75            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_STANDARD => {
76                CustomProtocolOption::Standard
77            }
78            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_LOCAL => CustomProtocolOption::Local,
79            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_DISPLAY_ISOLATED => {
80                CustomProtocolOption::DisplayIsolated
81            }
82            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_SECURE => CustomProtocolOption::Secure,
83            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CORS_ENABLED => {
84                CustomProtocolOption::CorsEnabled
85            }
86            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CSP_BYPASSING => {
87                CustomProtocolOption::CspBypassing
88            }
89            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_FETCH_ENABLED => {
90                CustomProtocolOption::FetchEnabled
91            }
92            ArkWeb_CustomSchemeOption_ARKWEB_SCHEME_OPTION_CODE_CACHE_ENABLED => {
93                CustomProtocolOption::CodeCacheEnabled
94            }
95            _ => CustomProtocolOption::None,
96        }
97    }
98}
99
100/// Register custom protocol for ArkWeb
101/// You can register multi custom protocol at once
102/// You need to call `CustomProtocol::register` to register to web before web init.
103/// ```ignore
104/// CustomProtocol::register("custom", CustomProtocolOption::Standard);
105/// CustomProtocol::register("custom2", CustomProtocolOption::Standard);
106/// CustomProtocol::register_to_web();
107/// ```
108pub struct CustomProtocol;
109
110impl CustomProtocol {
111    /// Add a custom protocol to the list.
112    ///
113    /// # Arguments
114    ///
115    /// * `protocol` - The protocol to add.
116    ///
117    pub fn add_protocol(protocol: &str) {
118        CustomProtocol::add_protocol_with_option(protocol, CustomProtocolOption::Standard);
119    }
120
121    /// Add a custom protocol to the list.
122    ///
123    /// # Arguments
124    ///
125    /// * `protocol` - The protocol to add.
126    /// * `option` - The option for the protocol.
127    ///
128    pub fn add_protocol_with_option(protocol: &str, option: CustomProtocolOption) {
129        let mut list = CUSTOM_PROTOCOL_LIST.lock().unwrap();
130        list.insert(protocol.to_string(), option);
131    }
132
133    /// Check if a protocol is registered.
134    ///
135    /// # Arguments
136    ///
137    /// * `protocol` - The protocol to check.
138    ///
139    /// # Return
140    pub fn is_protocol_registered(protocol: &str) -> bool {
141        let list = CUSTOM_PROTOCOL_LIST.lock().unwrap();
142        list.contains_key(&protocol.to_string())
143    }
144
145    /// Register all custom protocols to the web view.
146    ///
147    /// Note: You need to call this function after all custom protocols are added and before web init.
148    pub fn register() {
149        let mut list = CUSTOM_PROTOCOL_LIST.lock().unwrap();
150        list.iter_mut().for_each(|p| unsafe {
151            let protocol_cstr = CString::new(p.0.clone()).unwrap();
152            let option: ArkWeb_CustomSchemeOption = (*p.1).into();
153            OH_ArkWeb_RegisterCustomSchemes(protocol_cstr.as_ptr(), option as i32);
154        });
155    }
156}