diff --git a/Cargo.toml b/Cargo.toml index 29215ab..8de27a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ crate-type = ["cdylib", "rlib"] js-sys = "0.3.61" wasm-bindgen = "0.2.84" wasm-bindgen-futures = "0.4.34" +serde = { version = "1.0", features = ["derive"] } +tsify-next = { version = "0.5.4", features = ["js"] } [features] default = [] diff --git a/src/cookies.rs b/src/cookies.rs new file mode 100644 index 0000000..babb30d --- /dev/null +++ b/src/cookies.rs @@ -0,0 +1,87 @@ +use serde::{Deserialize, Serialize}; +use tsify_next::{serde_wasm_bindgen, Tsify}; +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; + +#[wasm_bindgen] +extern "C" { + pub type Cookies; + + #[wasm_bindgen(method, js_name = "get")] + async fn get_internal(this: &Cookies, details: CookieDetails) -> JsValue; + + #[wasm_bindgen(method, js_name = "set")] + async fn set_internal(this: &Cookies, details: SetCookieDetails) -> JsValue; +} + +impl Cookies { + pub async fn get(self: &Self, details: CookieDetails) -> Option { + serde_wasm_bindgen::from_value(self.get_internal(details).await).unwrap() + } + + pub async fn set(self: &Self, details: SetCookieDetails) -> Option { + serde_wasm_bindgen::from_value(self.set_internal(details).await).unwrap() + } +} + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +#[serde(rename_all = "camelCase")] +pub struct SetCookieDetails { + pub domain: Option, + pub expiration_date: Option, + pub http_only: Option, + pub name: Option, + pub partition_key: Option, + pub path: Option, + pub same_site: Option, + pub secure: Option, + pub store_id: Option, + pub url: String, + pub value: Option +} + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +#[serde(rename_all = "camelCase")] +pub struct CookieDetails { + pub name: String, + pub partition_key: Option, + pub store_id: Option, + pub url: String, +} + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +#[serde(rename_all = "camelCase")] +pub struct CookiePartitionKey { + pub has_cross_site_ancestor: Option, + pub top_level_site: Option, +} + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +#[serde(rename_all = "camelCase")] +pub struct Cookie { + pub domain: String, + pub expiration_date: Option, + pub host_only: bool, + pub http_only: bool, + pub name: String, + pub partition_key: Option, + pub path: String, + pub same_site: SameSiteStatus, + pub secure: bool, + pub session: bool, + pub store_id: String, + pub value: String +} + + +#[derive(Tsify, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum SameSiteStatus { + NoRestriction, + Lax, + Strict, + Unspecified, +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 8347e22..38d8b83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ mod action; mod bookmarks; #[cfg(feature = "firefox")] mod browser_action; +mod cookies; mod commands; #[cfg(feature = "firefox")] mod contextual_identities; @@ -30,6 +31,7 @@ pub use action::*; pub use bookmarks::*; #[cfg(feature = "firefox")] pub use browser_action::*; +pub use cookies::*; pub use commands::*; #[cfg(feature = "firefox")] pub use contextual_identities::*; @@ -84,6 +86,9 @@ extern "C" { #[wasm_bindgen(method, getter, js_name = browserAction)] pub fn browser_action(this: &Browser) -> BrowserAction; + #[wasm_bindgen(method, getter)] + pub fn cookies(this: &Browser) -> Cookies; + #[cfg(feature = "firefox")] #[wasm_bindgen(method, getter, js_name = contextualIdentities)] pub fn contextual_identities(this: &Browser) -> ContextualIdentities;