Skip to content

minimal implementation of cookies #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
87 changes: 87 additions & 0 deletions src/cookies.rs
Original file line number Diff line number Diff line change
@@ -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<Cookie> {
serde_wasm_bindgen::from_value(self.get_internal(details).await).unwrap()
}

pub async fn set(self: &Self, details: SetCookieDetails) -> Option<Cookie> {
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<String>,
pub expiration_date: Option<u64>,
pub http_only: Option<bool>,
pub name: Option<String>,
pub partition_key: Option<CookiePartitionKey>,
pub path: Option<String>,
pub same_site: Option<SameSiteStatus>,
pub secure: Option<bool>,
pub store_id: Option<String>,
pub url: String,
pub value: Option<String>
}

#[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<CookiePartitionKey>,
pub store_id: Option<String>,
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<bool>,
pub top_level_site: Option<bool>,
}

#[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<u64>,
pub host_only: bool,
pub http_only: bool,
pub name: String,
pub partition_key: Option<CookiePartitionKey>,
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,
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod action;
mod bookmarks;
#[cfg(feature = "firefox")]
mod browser_action;
mod cookies;
mod commands;
#[cfg(feature = "firefox")]
mod contextual_identities;
Expand All @@ -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::*;
Expand Down Expand Up @@ -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;
Expand Down