tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

common.rs (6330B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 use serde::ser::SerializeMap;
      6 use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
      7 use serde_json::Value;
      8 
      9 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
     10 pub struct BoolValue {
     11    value: bool,
     12 }
     13 
     14 impl BoolValue {
     15    pub fn new(val: bool) -> Self {
     16        BoolValue { value: val }
     17    }
     18 }
     19 
     20 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
     21 pub struct Cookie {
     22    pub name: String,
     23    pub value: String,
     24    #[serde(skip_serializing_if = "Option::is_none")]
     25    pub path: Option<String>,
     26    #[serde(skip_serializing_if = "Option::is_none")]
     27    pub domain: Option<String>,
     28    #[serde(default)]
     29    pub secure: bool,
     30    #[serde(default, rename = "httpOnly")]
     31    pub http_only: bool,
     32    #[serde(skip_serializing_if = "Option::is_none")]
     33    pub expiry: Option<Date>,
     34    #[serde(skip_serializing_if = "Option::is_none", rename = "sameSite")]
     35    pub same_site: Option<String>,
     36 }
     37 
     38 pub fn to_cookie<T, S>(data: T, serializer: S) -> Result<S::Ok, S::Error>
     39 where
     40    S: Serializer,
     41    T: Serialize,
     42 {
     43    #[derive(Serialize)]
     44    struct Wrapper<T> {
     45        cookie: T,
     46    }
     47 
     48    Wrapper { cookie: data }.serialize(serializer)
     49 }
     50 
     51 pub fn from_cookie<'de, D, T>(deserializer: D) -> Result<T, D::Error>
     52 where
     53    D: Deserializer<'de>,
     54    T: serde::de::DeserializeOwned,
     55    T: std::fmt::Debug,
     56 {
     57    #[derive(Debug, Deserialize)]
     58    struct Wrapper<T> {
     59        cookie: T,
     60    }
     61 
     62    let w = Wrapper::deserialize(deserializer)?;
     63    Ok(w.cookie)
     64 }
     65 
     66 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
     67 pub struct Date(pub u64);
     68 
     69 #[derive(Clone, Debug, PartialEq)]
     70 pub enum Frame {
     71    Index(u16),
     72    Element(String),
     73    Top,
     74 }
     75 
     76 impl Serialize for Frame {
     77    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
     78    where
     79        S: Serializer,
     80    {
     81        let mut map = serializer.serialize_map(Some(1))?;
     82        match self {
     83            Frame::Index(nth) => map.serialize_entry("id", nth)?,
     84            Frame::Element(el) => map.serialize_entry("element", el)?,
     85            Frame::Top => map.serialize_entry("id", &Value::Null)?,
     86        }
     87        map.end()
     88    }
     89 }
     90 
     91 impl<'de> Deserialize<'de> for Frame {
     92    fn deserialize<D>(deserializer: D) -> Result<Frame, D::Error>
     93    where
     94        D: Deserializer<'de>,
     95    {
     96        #[derive(Debug, Deserialize)]
     97        #[serde(rename_all = "lowercase")]
     98        struct JsonFrame {
     99            id: Option<u16>,
    100            element: Option<String>,
    101        }
    102 
    103        let json = JsonFrame::deserialize(deserializer)?;
    104        match (json.id, json.element) {
    105            (Some(_id), Some(_element)) => Err(de::Error::custom("conflicting frame identifiers")),
    106            (Some(id), None) => Ok(Frame::Index(id)),
    107            (None, Some(element)) => Ok(Frame::Element(element)),
    108            (None, None) => Ok(Frame::Top),
    109        }
    110    }
    111 }
    112 
    113 // TODO(nupur): Bug 1567165 - Make WebElement in Marionette a unit struct
    114 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    115 pub struct WebElement {
    116    #[serde(rename = "element-6066-11e4-a52e-4f735466cecf")]
    117    pub element: String,
    118 }
    119 
    120 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    121 pub struct Timeouts {
    122    #[serde(default, skip_serializing_if = "Option::is_none")]
    123    pub implicit: Option<u64>,
    124    #[serde(default, rename = "pageLoad", skip_serializing_if = "Option::is_none")]
    125    pub page_load: Option<u64>,
    126    #[serde(default, skip_serializing_if = "Option::is_none")]
    127    #[allow(clippy::option_option)]
    128    pub script: Option<Option<u64>>,
    129 }
    130 
    131 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    132 pub struct Window {
    133    pub handle: String,
    134 }
    135 
    136 pub fn to_name<T, S>(data: T, serializer: S) -> Result<S::Ok, S::Error>
    137 where
    138    S: Serializer,
    139    T: Serialize,
    140 {
    141    #[derive(Serialize)]
    142    struct Wrapper<T> {
    143        name: T,
    144    }
    145 
    146    Wrapper { name: data }.serialize(serializer)
    147 }
    148 
    149 pub fn from_name<'de, D, T>(deserializer: D) -> Result<T, D::Error>
    150 where
    151    D: Deserializer<'de>,
    152    T: serde::de::DeserializeOwned,
    153    T: std::fmt::Debug,
    154 {
    155    #[derive(Debug, Deserialize)]
    156    struct Wrapper<T> {
    157        name: T,
    158    }
    159 
    160    let w = Wrapper::deserialize(deserializer)?;
    161    Ok(w.name)
    162 }
    163 
    164 #[cfg(test)]
    165 mod tests {
    166    use super::*;
    167    use crate::test::{assert_de, assert_ser, assert_ser_de, ELEMENT_KEY};
    168    use serde_json::json;
    169 
    170    #[test]
    171    fn test_cookie_default_values() {
    172        let data = Cookie {
    173            name: "hello".into(),
    174            value: "world".into(),
    175            path: None,
    176            domain: None,
    177            secure: false,
    178            http_only: false,
    179            expiry: None,
    180            same_site: None,
    181        };
    182        assert_de(&data, json!({"name":"hello", "value":"world"}));
    183    }
    184 
    185    #[test]
    186    fn test_json_frame_index() {
    187        assert_ser_de(&Frame::Index(1234), json!({"id": 1234}));
    188    }
    189 
    190    #[test]
    191    fn test_json_frame_element() {
    192        assert_ser_de(&Frame::Element("elem".into()), json!({"element": "elem"}));
    193    }
    194 
    195    #[test]
    196    fn test_json_frame_parent() {
    197        assert_ser_de(&Frame::Top, json!({ "id": null }));
    198    }
    199 
    200    #[test]
    201    fn test_web_element() {
    202        let data = WebElement {
    203            element: "foo".into(),
    204        };
    205        assert_ser_de(&data, json!({ELEMENT_KEY: "foo"}));
    206    }
    207 
    208    #[test]
    209    fn test_timeouts_with_all_params() {
    210        let data = Timeouts {
    211            implicit: Some(1000),
    212            page_load: Some(200000),
    213            script: Some(Some(60000)),
    214        };
    215        assert_ser_de(
    216            &data,
    217            json!({"implicit":1000,"pageLoad":200000,"script":60000}),
    218        );
    219    }
    220 
    221    #[test]
    222    fn test_timeouts_with_missing_params() {
    223        let data = Timeouts {
    224            implicit: Some(1000),
    225            page_load: None,
    226            script: None,
    227        };
    228        assert_ser_de(&data, json!({"implicit":1000}));
    229    }
    230 
    231    #[test]
    232    fn test_timeouts_setting_script_none() {
    233        let data = Timeouts {
    234            implicit: Some(1000),
    235            page_load: None,
    236            script: Some(None),
    237        };
    238        assert_ser(&data, json!({"implicit":1000, "script":null}));
    239    }
    240 }