0

Allow url::Origin and net::SchemefulSite to deserialize a string view

Instead of requiring the caller to provide a std::string reference,
which can entail otherwise unnecessary allocation.

Change-Id: I663623042f169d1e59efbf162b5acfa5a1104606
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5854624
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Maks Orlovich <morlovich@chromium.org>
Commit-Queue: Andrew Paseltiner <apaseltiner@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1354511}
This commit is contained in:
Andrew Paseltiner
2024-09-12 13:59:18 +00:00
committed by Chromium LUCI CQ
parent e40415fbac
commit a0a164acbe
4 changed files with 11 additions and 8 deletions

@ -4,6 +4,8 @@
#include "net/base/schemeful_site.h"
#include <string_view>
#include "base/check.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/memory_usage_estimator.h"
@ -113,7 +115,7 @@ void SchemefulSite::ConvertWebSocketToHttp() {
}
// static
SchemefulSite SchemefulSite::Deserialize(const std::string& value) {
SchemefulSite SchemefulSite::Deserialize(std::string_view value) {
return SchemefulSite(GURL(value));
}
@ -159,13 +161,13 @@ bool SchemefulSite::operator<(const SchemefulSite& other) const {
// static
std::optional<SchemefulSite> SchemefulSite::DeserializeWithNonce(
base::PassKey<NetworkAnonymizationKey>,
const std::string& value) {
std::string_view value) {
return DeserializeWithNonce(value);
}
// static
std::optional<SchemefulSite> SchemefulSite::DeserializeWithNonce(
const std::string& value) {
std::string_view value) {
std::optional<url::Origin> result = url::Origin::Deserialize(value);
if (!result)
return std::nullopt;

@ -8,6 +8,7 @@
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include "base/gtest_prod_util.h"
#include "base/types/pass_key.h"
@ -96,7 +97,7 @@ class NET_EXPORT SchemefulSite {
// Deserializes a string obtained from `Serialize()` to a `SchemefulSite`.
// Returns an opaque `SchemefulSite` if the value was invalid in any way.
static SchemefulSite Deserialize(const std::string& value);
static SchemefulSite Deserialize(std::string_view value);
// Returns a serialized version of `site_as_origin_`. If the underlying origin
// is invalid, returns an empty string. If serialization of opaque origins
@ -119,7 +120,7 @@ class NET_EXPORT SchemefulSite {
// `SchemefulSite`. Returns nullopt if the value was invalid in any way.
static std::optional<SchemefulSite> DeserializeWithNonce(
base::PassKey<NetworkAnonymizationKey>,
const std::string& value);
std::string_view value);
// Returns a serialized version of `site_as_origin_`. For an opaque
// `site_as_origin_`, this serializes with the nonce. See
@ -191,7 +192,7 @@ class NET_EXPORT SchemefulSite {
// Deserializes a string obtained from `SerializeWithNonce()` to a
// `SchemefulSite`. Returns nullopt if the value was invalid in any way.
static std::optional<SchemefulSite> DeserializeWithNonce(
const std::string& value);
std::string_view value);
// Returns a serialized version of `site_as_origin_`. For an opaque
// `site_as_origin_`, this serializes with the nonce. See

@ -355,7 +355,7 @@ std::optional<std::string> Origin::SerializeWithNonceImpl() const {
}
// static
std::optional<Origin> Origin::Deserialize(const std::string& value) {
std::optional<Origin> Origin::Deserialize(std::string_view value) {
std::string data;
if (!base::Base64Decode(value, &data))
return std::nullopt;

@ -460,7 +460,7 @@ class COMPONENT_EXPORT(URL) Origin {
// Deserializes an origin from |ToValueWithNonce|. Returns nullopt if the
// value was invalid in any way.
static std::optional<Origin> Deserialize(const std::string& value);
static std::optional<Origin> Deserialize(std::string_view value);
// The tuple is used for both tuple origins (e.g. https://example.com:80), as
// well as for opaque origins, where it tracks the tuple origin from which