Webdriver delete cookie (#36306)

1. Implement `WebDriverCommand::DeleteCookie`
2. Remove unnecessary clone for `WebDriverCommand::GetNamedCookie`

Fixes: #36287

---------

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
This commit is contained in:
Euclid Ye 2025-04-04 13:24:47 +08:00 committed by GitHub
parent e1de46c691
commit 4bad7c0a5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 65 additions and 4 deletions

View file

@ -94,6 +94,7 @@ impl CookieStorage {
Ok(None)
}
}
pub fn clear_storage(&mut self, url: &ServoUrl) {
let domain = reg_host(url.host_str().unwrap_or(""));
let cookies = self.cookies_map.entry(domain).or_default();
@ -102,6 +103,16 @@ impl CookieStorage {
}
}
pub fn delete_cookie_with_name(&mut self, url: &ServoUrl, name: String) {
let domain = reg_host(url.host_str().unwrap_or(""));
let cookies = self.cookies_map.entry(domain).or_default();
for cookie in cookies.iter_mut() {
if cookie.cookie.name() == name {
cookie.set_expiry_time_in_past();
}
}
}
// http://tools.ietf.org/html/rfc6265#section-5.3
pub fn push(&mut self, mut cookie: ServoCookie, url: &ServoUrl, source: CookieSource) {
// https://www.ietf.org/id/draft-ietf-httpbis-cookie-alone-01.txt Step 1

View file

@ -379,6 +379,14 @@ impl ResourceChannelManager {
.clear_storage(&request);
return true;
},
CoreResourceMsg::DeleteCookie(request, name) => {
http_state
.cookie_jar
.write()
.unwrap()
.delete_cookie_with_name(&request, name);
return true;
},
CoreResourceMsg::FetchRedirect(request_builder, res_init, sender) => {
let cancellation_listener =
self.get_or_create_cancellation_listener(request_builder.id);

View file

@ -2104,6 +2104,9 @@ impl ScriptThread {
WebDriverScriptCommand::DeleteCookies(reply) => {
webdriver_handlers::handle_delete_cookies(&documents, pipeline_id, reply)
},
WebDriverScriptCommand::DeleteCookie(name, reply) => {
webdriver_handlers::handle_delete_cookie(&documents, pipeline_id, name, reply)
},
WebDriverScriptCommand::FindElementCSS(selector, reply) => {
webdriver_handlers::handle_find_element_css(
&documents,

View file

@ -23,7 +23,9 @@ use js::jsval::UndefinedValue;
use js::rust::wrappers::{JS_CallFunctionName, JS_GetProperty, JS_HasOwnProperty, JS_TypeOfValue};
use js::rust::{HandleObject, HandleValue, IdVector, ToString};
use net_traits::CookieSource::{HTTP, NonHTTP};
use net_traits::CoreResourceMsg::{DeleteCookies, GetCookiesDataForUrl, SetCookieForUrl};
use net_traits::CoreResourceMsg::{
DeleteCookie, DeleteCookies, GetCookiesDataForUrl, SetCookieForUrl,
};
use net_traits::IpcSend;
use servo_url::ServoUrl;
use webdriver::common::{WebElement, WebFrame, WebWindow};
@ -929,6 +931,7 @@ pub(crate) fn handle_add_cookie(
.unwrap();
}
// https://w3c.github.io/webdriver/#delete-all-cookies
pub(crate) fn handle_delete_cookies(
documents: &DocumentCollection,
pipeline: PipelineId,
@ -950,6 +953,29 @@ pub(crate) fn handle_delete_cookies(
reply.send(Ok(())).unwrap();
}
// https://w3c.github.io/webdriver/#delete-cookie
pub(crate) fn handle_delete_cookie(
documents: &DocumentCollection,
pipeline: PipelineId,
name: String,
reply: IpcSender<Result<(), ErrorStatus>>,
) {
let document = match documents.find_document(pipeline) {
Some(document) => document,
None => {
return reply.send(Err(ErrorStatus::UnknownError)).unwrap();
},
};
let url = document.url();
document
.window()
.as_global_scope()
.resource_threads()
.send(DeleteCookie(url, name))
.unwrap();
reply.send(Ok(())).unwrap();
}
pub(crate) fn handle_get_title(
documents: &DocumentCollection,
pipeline: PipelineId,

View file

@ -78,6 +78,7 @@ pub enum WebDriverScriptCommand {
IpcSender<Result<(), WebDriverCookieError>>,
),
DeleteCookies(IpcSender<Result<(), ErrorStatus>>),
DeleteCookie(String, IpcSender<Result<(), ErrorStatus>>),
ExecuteScript(String, IpcSender<WebDriverJSResult>),
ExecuteAsyncScript(String, IpcSender<WebDriverJSResult>),
FindElementCSS(String, IpcSender<Result<Option<String>, ErrorStatus>>),

View file

@ -493,6 +493,7 @@ pub enum CoreResourceMsg {
CookieSource,
),
DeleteCookies(ServoUrl),
DeleteCookie(ServoUrl, String),
/// Get a history state by a given history state id
GetHistoryState(HistoryStateId, IpcSender<Option<Vec<u8>>>),
/// Set a history state for a given history state id

View file

@ -1298,9 +1298,9 @@ impl Handler {
Ok(WebDriverResponse::Cookies(CookiesResponse(response)))
}
fn handle_get_cookie(&self, name: &str) -> WebDriverResult<WebDriverResponse> {
fn handle_get_cookie(&self, name: String) -> WebDriverResult<WebDriverResponse> {
let (sender, receiver) = ipc::channel().unwrap();
let cmd = WebDriverScriptCommand::GetCookie(name.to_owned(), sender);
let cmd = WebDriverScriptCommand::GetCookie(name, sender);
self.browsing_context_script_command(cmd)?;
let cookies = receiver.recv().unwrap();
let response = cookies
@ -1346,6 +1346,16 @@ impl Handler {
}
}
fn handle_delete_cookie(&self, name: String) -> WebDriverResult<WebDriverResponse> {
let (sender, receiver) = ipc::channel().unwrap();
let cmd = WebDriverScriptCommand::DeleteCookie(name, sender);
self.browsing_context_script_command(cmd)?;
match receiver.recv().unwrap() {
Ok(_) => Ok(WebDriverResponse::Void),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
fn handle_delete_cookies(&self) -> WebDriverResult<WebDriverResponse> {
let (sender, receiver) = ipc::channel().unwrap();
let cmd = WebDriverScriptCommand::DeleteCookies(sender);
@ -1832,7 +1842,7 @@ impl WebDriverHandler<ServoExtensionRoute> for Handler {
WebDriverCommand::FindElementElements(ref element, ref parameters) => {
self.handle_find_elements_from_element(element, parameters)
},
WebDriverCommand::GetNamedCookie(ref name) => self.handle_get_cookie(name),
WebDriverCommand::GetNamedCookie(name) => self.handle_get_cookie(name),
WebDriverCommand::GetCookies => self.handle_get_cookies(),
WebDriverCommand::GetActiveElement => self.handle_active_element(),
WebDriverCommand::GetElementRect(ref element) => self.handle_element_rect(element),
@ -1860,6 +1870,7 @@ impl WebDriverHandler<ServoExtensionRoute> for Handler {
WebDriverCommand::ElementClick(ref element) => self.handle_element_click(element),
WebDriverCommand::DismissAlert => self.handle_dismiss_alert(),
WebDriverCommand::DeleteCookies => self.handle_delete_cookies(),
WebDriverCommand::DeleteCookie(name) => self.handle_delete_cookie(name),
WebDriverCommand::GetTimeouts => self.handle_get_timeouts(),
WebDriverCommand::SetTimeouts(ref x) => self.handle_set_timeouts(x),
WebDriverCommand::TakeScreenshot => self.handle_take_screenshot(),