1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/network_isolation_key.h"
#include <cstddef>
#include <optional>
#include <string>
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/unguessable_token.h"
#include "net/base/features.h"
#include "net/base/network_isolation_partition.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "schemeful_site.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
namespace net {
namespace {
std::string GetSiteDebugString(const std::optional<SchemefulSite>& site) {
return site ? site->GetDebugString() : "null";
}
std::string GetNetworkIsolationPartitionStringForCacheKey(
NetworkIsolationPartition network_isolation_partition) {
return base::NumberToString(
base::strict_cast<int32_t>(network_isolation_partition));
}
} // namespace
NetworkIsolationKey::NetworkIsolationKey(
const SchemefulSite& top_frame_site,
const SchemefulSite& frame_site,
const std::optional<base::UnguessableToken>& nonce,
NetworkIsolationPartition network_isolation_partition)
: NetworkIsolationKey(SchemefulSite(top_frame_site),
SchemefulSite(frame_site),
std::optional<base::UnguessableToken>(nonce),
network_isolation_partition) {}
NetworkIsolationKey::NetworkIsolationKey(
SchemefulSite&& top_frame_site,
SchemefulSite&& frame_site,
std::optional<base::UnguessableToken>&& nonce,
NetworkIsolationPartition network_isolation_partition)
: top_frame_site_(std::move(top_frame_site)),
frame_site_(std::make_optional(std::move(frame_site))),
nonce_(std::move(nonce)),
network_isolation_partition_(network_isolation_partition) {
DCHECK(!nonce_ || !nonce_->is_empty());
}
NetworkIsolationKey::NetworkIsolationKey() = default;
NetworkIsolationKey::NetworkIsolationKey(
const NetworkIsolationKey& network_isolation_key) = default;
NetworkIsolationKey::NetworkIsolationKey(
NetworkIsolationKey&& network_isolation_key) = default;
NetworkIsolationKey::~NetworkIsolationKey() = default;
NetworkIsolationKey& NetworkIsolationKey::operator=(
const NetworkIsolationKey& network_isolation_key) = default;
NetworkIsolationKey& NetworkIsolationKey::operator=(
NetworkIsolationKey&& network_isolation_key) = default;
NetworkIsolationKey NetworkIsolationKey::CreateTransientForTesting() {
SchemefulSite site_with_opaque_origin;
return NetworkIsolationKey(site_with_opaque_origin, site_with_opaque_origin);
}
NetworkIsolationKey NetworkIsolationKey::CreateWithNewFrameSite(
const SchemefulSite& new_frame_site) const {
if (!top_frame_site_)
return NetworkIsolationKey();
return NetworkIsolationKey(top_frame_site_.value(), new_frame_site, nonce_,
network_isolation_partition_);
}
std::optional<std::string> NetworkIsolationKey::ToCacheKeyString() const {
if (IsTransient())
return std::nullopt;
std::string network_isolation_partition_string =
network_isolation_partition_ == NetworkIsolationPartition::kGeneral
? ""
: " " + GetNetworkIsolationPartitionStringForCacheKey(
network_isolation_partition_);
return top_frame_site_->Serialize() + " " + frame_site_->Serialize() +
network_isolation_partition_string;
}
std::string NetworkIsolationKey::ToDebugString() const {
// The space-separated serialization of |top_frame_site_| and
// |frame_site_|.
std::string return_string = GetSiteDebugString(top_frame_site_);
return_string += " " + GetSiteDebugString(frame_site_);
if (nonce_.has_value()) {
return_string += " (with nonce " + nonce_->ToString() + ")";
}
if (network_isolation_partition_ != NetworkIsolationPartition::kGeneral) {
return_string +=
" (" +
NetworkIsolationPartitionToDebugString(network_isolation_partition_) +
")";
}
return return_string;
}
bool NetworkIsolationKey::IsFullyPopulated() const {
if (!top_frame_site_.has_value()) {
return false;
}
if (!frame_site_.has_value()) {
return false;
}
return true;
}
bool NetworkIsolationKey::IsTransient() const {
if (!IsFullyPopulated())
return true;
return IsOpaque();
}
bool NetworkIsolationKey::IsEmpty() const {
return !top_frame_site_.has_value() && !frame_site_.has_value();
}
bool NetworkIsolationKey::IsOpaque() const {
if (top_frame_site_->opaque()) {
return true;
}
if (frame_site_->opaque()) {
return true;
}
if (nonce_.has_value()) {
return true;
}
return false;
}
NET_EXPORT std::ostream& operator<<(std::ostream& os,
const NetworkIsolationKey& nik) {
os << nik.ToDebugString();
return os;
}
} // namespace net
|