Skip to content

Commit

Permalink
Convert ContentSecurityPolicyResponseHeaders to the new IPC serializa…
Browse files Browse the repository at this point in the history
…tion format

https://bugs.webkit.org/show_bug.cgi?id=264424

Reviewed by Alex Christensen.

* Source/WebCore/page/csp/ContentSecurityPolicyResponseHeaders.h:
(WebCore::ContentSecurityPolicyResponseHeaders::ContentSecurityPolicyResponseHeaders):
(WebCore::ContentSecurityPolicyResponseHeaders::headers const):
(WebCore::ContentSecurityPolicyResponseHeaders::setHeaders):
(WebCore::ContentSecurityPolicyResponseHeaders::httpStatusCode const):
(WebCore::ContentSecurityPolicyResponseHeaders::setHTTPStatusCode):
(WebCore::ContentSecurityPolicyResponseHeaders::encode const): Deleted.
(WebCore::ContentSecurityPolicyResponseHeaders::decode): Deleted.
* Source/WebCore/platform/WebCorePersistentCoders.cpp:
(WTF::Persistence::Coder<WebCore::ContentSecurityPolicyResponseHeaders>::encodeForPersistence):
(WTF::Persistence::Coder<WebCore::ContentSecurityPolicyResponseHeaders>::decodeForPersistence):
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in:

Canonical link: https://commits.webkit.org/270417@main
  • Loading branch information
cdumez committed Nov 9, 2023
1 parent cee3065 commit 0b83b4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
55 changes: 10 additions & 45 deletions Source/WebCore/page/csp/ContentSecurityPolicyResponseHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ enum class ContentSecurityPolicyHeaderType : bool {
class ContentSecurityPolicyResponseHeaders {
public:
ContentSecurityPolicyResponseHeaders() = default;
ContentSecurityPolicyResponseHeaders(Vector<std::pair<String, ContentSecurityPolicyHeaderType>>&& headers, int httpStatusCode)
: m_headers(WTFMove(headers))
, m_httpStatusCode(httpStatusCode)
{ }

WEBCORE_EXPORT explicit ContentSecurityPolicyResponseHeaders(const ResourceResponse&);

ContentSecurityPolicyResponseHeaders isolatedCopy() const &;
ContentSecurityPolicyResponseHeaders isolatedCopy() &&;

template <class Encoder> void encode(Encoder&) const;
template <class Decoder> static std::optional<ContentSecurityPolicyResponseHeaders> decode(Decoder&);

enum EmptyTag { Empty };
struct MarkableTraits {
static bool isEmptyValue(const ContentSecurityPolicyResponseHeaders& identifier)
Expand All @@ -64,6 +66,11 @@ class ContentSecurityPolicyResponseHeaders {

void addPolicyHeadersTo(ResourceResponse&) const;

const Vector<std::pair<String, ContentSecurityPolicyHeaderType>>& headers() const { return m_headers; }
void setHeaders(Vector<std::pair<String, ContentSecurityPolicyHeaderType>>&& headers) { m_headers = WTFMove(headers); }
int httpStatusCode() const { return m_httpStatusCode; }
void setHTTPStatusCode(int httpStatusCode) { m_httpStatusCode = httpStatusCode; }

private:
friend bool operator==(const ContentSecurityPolicyResponseHeaders&, const ContentSecurityPolicyResponseHeaders&);
friend class ContentSecurityPolicy;
Expand All @@ -81,46 +88,4 @@ inline bool operator==(const ContentSecurityPolicyResponseHeaders&a, const Conte
return a.m_headers == b.m_headers;
}

template <class Encoder>
void ContentSecurityPolicyResponseHeaders::encode(Encoder& encoder) const
{
encoder << static_cast<uint64_t>(m_headers.size());
for (auto& pair : m_headers) {
encoder << pair.first;
encoder << pair.second;
}
encoder << m_httpStatusCode;
}

template <class Decoder>
std::optional<ContentSecurityPolicyResponseHeaders> ContentSecurityPolicyResponseHeaders::decode(Decoder& decoder)
{
ContentSecurityPolicyResponseHeaders headers;

std::optional<uint64_t> headersSize;
decoder >> headersSize;
if (!headersSize)
return std::nullopt;
for (size_t i = 0; i < *headersSize; ++i) {
std::optional<String> header;
decoder >> header;
if (!header)
return std::nullopt;
std::optional<ContentSecurityPolicyHeaderType> headerType;
decoder >> headerType;
if (!headerType)
return std::nullopt;
headers.m_headers.append(std::make_pair(WTFMove(*header), WTFMove(*headerType)));
}
headers.m_headers.shrinkToFit();

std::optional<int> httpStatusCode;
decoder >> httpStatusCode;
if (!httpStatusCode)
return std::nullopt;
headers.m_httpStatusCode = *httpStatusCode;

return headers;
}

} // namespace WebCore
37 changes: 35 additions & 2 deletions Source/WebCore/platform/WebCorePersistentCoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,45 @@ std::optional<WebCore::CrossOriginEmbedderPolicy> Coder<WebCore::CrossOriginEmbe

void Coder<WebCore::ContentSecurityPolicyResponseHeaders>::encodeForPersistence(Encoder& encoder, const WebCore::ContentSecurityPolicyResponseHeaders& instance)
{
instance.encode(encoder);
encoder << static_cast<uint64_t>(instance.headers().size());
for (auto& pair : instance.headers()) {
encoder << pair.first;
encoder << pair.second;
}
encoder << instance.httpStatusCode();
}

std::optional<WebCore::ContentSecurityPolicyResponseHeaders> Coder<WebCore::ContentSecurityPolicyResponseHeaders>::decodeForPersistence(Decoder& decoder)
{
return WebCore::ContentSecurityPolicyResponseHeaders::decode(decoder);
WebCore::ContentSecurityPolicyResponseHeaders headers;

std::optional<uint64_t> headersSize;
decoder >> headersSize;
if (!headersSize)
return std::nullopt;

Vector<std::pair<String, WebCore::ContentSecurityPolicyHeaderType>> headersVector;
for (size_t i = 0; i < *headersSize; ++i) {
std::optional<String> header;
decoder >> header;
if (!header)
return std::nullopt;
std::optional<WebCore::ContentSecurityPolicyHeaderType> headerType;
decoder >> headerType;
if (!headerType)
return std::nullopt;
headersVector.append(std::make_pair(WTFMove(*header), WTFMove(*headerType)));
}
headersVector.shrinkToFit();
headers.setHeaders(WTFMove(headersVector));

std::optional<int> httpStatusCode;
decoder >> httpStatusCode;
if (!httpStatusCode)
return std::nullopt;
headers.setHTTPStatusCode(*httpStatusCode);

return headers;
}

void Coder<WebCore::ClientOrigin>::encodeForPersistence(Encoder& encoder, const WebCore::ClientOrigin& instance)
Expand Down
5 changes: 5 additions & 0 deletions Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in
Original file line number Diff line number Diff line change
Expand Up @@ -6568,6 +6568,11 @@ header: <WebCore/GraphicsContextState.h>
WebCore::AffineTransform spaceTransform;
};

class WebCore::ContentSecurityPolicyResponseHeaders {
Vector<std::pair<String, WebCore::ContentSecurityPolicyHeaderType>> headers();
int httpStatusCode();
};

#if PLATFORM(COCOA)
struct WebCore::KeypressCommand {
String commandName;
Expand Down

0 comments on commit 0b83b4f

Please sign in to comment.