ladybird/Libraries/LibDevTools/StorageHelpers.cpp
Sam Atkins 20f922cbdb LibWebView+LibDevTools: Populate DevTools cookies
Firefox asks the cookies actor for rows after selecting a Storage host.
Read the browser cookie jar through the DevTools delegate and serialize
matching cookies with the fields Firefox expects.

This keeps mutation support disabled, but makes existing cookies visible
in the Storage panel.
2026-06-11 15:03:47 +01:00

45 lines
1.2 KiB
C++

/*
* Copyright (c) 2026, Ladybird contributors
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <LibDevTools/StorageHelpers.h>
#include <LibURL/Parser.h>
#include <LibURL/URL.h>
namespace DevTools {
Optional<String> storage_host_for_url(String const& url_string)
{
auto url = URL::Parser::basic_parse(url_string);
if (!url.has_value())
return {};
auto const& scheme = url->scheme();
if (scheme == "http"sv || scheme == "https"sv) {
StringBuilder builder;
builder.append(scheme);
builder.append("://"sv);
builder.append(url->serialized_host());
if (auto port = url->port(); port.has_value())
builder.appendff(":{}", *port);
return builder.to_string_without_validation();
}
if (scheme == "about"sv || scheme == "file"sv || scheme == "javascript"sv || scheme == "resource"sv)
return url->serialize();
return {};
}
Optional<String> storage_host_name(String const& storage_host)
{
auto url = URL::Parser::basic_parse(storage_host);
if (!url.has_value() || !url->host().has_value())
return {};
return url->serialized_host();
}
}