#include "content/browser/interest_group/interest_group_manager_impl.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/strings/to_string.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "components/cbor/values.h"
#include "components/cbor/writer.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/devtools/network_service_devtools_observer.h"
#include "content/browser/interest_group/ad_auction_page_data.h"
#include "content/browser/interest_group/for_debugging_only_report_util.h"
#include "content/browser/interest_group/interest_group_caching_storage.h"
#include "content/browser/interest_group/interest_group_real_time_report_util.h"
#include "content/browser/interest_group/interest_group_storage.h"
#include "content/browser/interest_group/interest_group_update.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/services/auction_worklet/public/cpp/real_time_reporting.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
constexpr int kMaxActiveReportRequests = …;
constexpr int kMaxReportQueueLength = …;
constexpr base::TimeDelta kMaxReportingRoundDuration = …;
constexpr base::TimeDelta kReportingInterval = …;
constexpr int kRealTimeReportDataVersion = …;
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation = …;
mojo::PendingRemote<network::mojom::DevToolsObserver> CreateDevtoolsObserver(
int frame_tree_node_id) { … }
std::unique_ptr<network::ResourceRequest> BuildUncredentialedRequest(
GURL url,
const url::Origin& frame_origin,
int frame_tree_node_id,
const network::mojom::ClientSecurityState& client_security_state,
bool is_post_method) { … }
std::vector<uint8_t> BuildRealTimeReport(
const std::vector<uint8_t>& real_time_histogram) { … }
std::unique_ptr<network::SimpleURLLoader> BuildSimpleUrlLoader(
std::unique_ptr<network::ResourceRequest> resource_request,
std::optional<std::vector<uint8_t>> real_time_histogram) { … }
std::vector<InterestGroupManager::InterestGroupDataKey>
ConvertOwnerJoinerPairsToDataKeys(
std::vector<std::pair<url::Origin, url::Origin>> owner_joiner_pairs) { … }
double GetRealTimeReportingQuota(
std::optional<std::pair<base::TimeTicks, double>> quota,
base::TimeTicks now,
double max_real_time_reports,
base::TimeDelta rate_limit_window) { … }
}
InterestGroupManagerImpl::ReportRequest::ReportRequest() = default;
InterestGroupManagerImpl::ReportRequest::~ReportRequest() = default;
InterestGroupManagerImpl::AdAuctionDataLoaderState::AdAuctionDataLoaderState()
: … { … }
InterestGroupManagerImpl::AdAuctionDataLoaderState::AdAuctionDataLoaderState(
AdAuctionDataLoaderState&& state) = default;
InterestGroupManagerImpl::AdAuctionDataLoaderState::
~AdAuctionDataLoaderState() = default;
InterestGroupManagerImpl::InterestGroupManagerImpl(
const base::FilePath& path,
bool in_memory,
ProcessMode process_mode,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
GetKAnonymityServiceDelegateCallback k_anonymity_service_callback)
: … { … }
InterestGroupManagerImpl::~InterestGroupManagerImpl() = default;
void InterestGroupManagerImpl::GetAllInterestGroupJoiningOrigins(
base::OnceCallback<void(std::vector<url::Origin>)> callback) { … }
void InterestGroupManagerImpl::GetAllInterestGroupDataKeys(
base::OnceCallback<void(std::vector<InterestGroupDataKey>)> callback) { … }
void InterestGroupManagerImpl::RemoveInterestGroupsByDataKey(
InterestGroupDataKey data_key,
base::OnceClosure callback) { … }
void InterestGroupManagerImpl::CheckPermissionsAndJoinInterestGroup(
blink::InterestGroup group,
const GURL& joining_url,
const url::Origin& frame_origin,
const net::NetworkIsolationKey& network_isolation_key,
bool report_result_only,
network::mojom::URLLoaderFactory& url_loader_factory,
AreReportingOriginsAttestedCallback attestation_callback,
blink::mojom::AdAuctionService::JoinInterestGroupCallback callback) { … }
void InterestGroupManagerImpl::CheckPermissionsAndLeaveInterestGroup(
const blink::InterestGroupKey& group_key,
const url::Origin& main_frame,
const url::Origin& frame_origin,
const net::NetworkIsolationKey& network_isolation_key,
bool report_result_only,
network::mojom::URLLoaderFactory& url_loader_factory,
blink::mojom::AdAuctionService::LeaveInterestGroupCallback callback) { … }
void InterestGroupManagerImpl::
CheckPermissionsAndClearOriginJoinedInterestGroups(
const url::Origin& owner,
const std::vector<std::string>& interest_groups_to_keep,
const url::Origin& main_frame_origin,
const url::Origin& frame_origin,
const net::NetworkIsolationKey& network_isolation_key,
bool report_result_only,
network::mojom::URLLoaderFactory& url_loader_factory,
blink::mojom::AdAuctionService::LeaveInterestGroupCallback callback) { … }
void InterestGroupManagerImpl::JoinInterestGroup(blink::InterestGroup group,
const GURL& joining_url) { … }
void InterestGroupManagerImpl::LeaveInterestGroup(
const blink::InterestGroupKey& group_key,
const ::url::Origin& main_frame) { … }
void InterestGroupManagerImpl::ClearOriginJoinedInterestGroups(
const url::Origin& owner,
std::set<std::string> interest_groups_to_keep,
url::Origin main_frame_origin) { … }
void InterestGroupManagerImpl::OnClearOriginJoinedInterestGroupsComplete(
const url::Origin& owner,
std::vector<std::string> left_interest_group_names) { … }
void InterestGroupManagerImpl::UpdateInterestGroupsOfOwner(
const url::Origin& owner,
network::mojom::ClientSecurityStatePtr client_security_state,
AreReportingOriginsAttestedCallback callback) { … }
void InterestGroupManagerImpl::UpdateInterestGroupsOfOwners(
std::vector<url::Origin> owners,
network::mojom::ClientSecurityStatePtr client_security_state,
AreReportingOriginsAttestedCallback callback) { … }
void InterestGroupManagerImpl::UpdateInterestGroupsOfOwnersWithDelay(
std::vector<url::Origin> owners,
network::mojom::ClientSecurityStatePtr client_security_state,
AreReportingOriginsAttestedCallback callback,
const base::TimeDelta& delay) { … }
void InterestGroupManagerImpl::AllowUpdateIfOlderThan(
const blink::InterestGroupKey& group_key,
base::TimeDelta update_if_older_than) { … }
void InterestGroupManagerImpl::RecordInterestGroupBids(
const blink::InterestGroupSet& group_keys) { … }
void InterestGroupManagerImpl::RecordInterestGroupWin(
const blink::InterestGroupKey& group_key,
const std::string& ad_json) { … }
void InterestGroupManagerImpl::RecordDebugReportLockout(
base::Time last_report_sent_time) { … }
void InterestGroupManagerImpl::RecordDebugReportCooldown(
const url::Origin& origin,
base::Time cooldown_start,
DebugReportCooldownType cooldown_type) { … }
void InterestGroupManagerImpl::RegisterAdKeysAsJoined(
base::flat_set<std::string> hashed_keys) { … }
void InterestGroupManagerImpl::GetInterestGroup(
const url::Origin& owner,
const std::string& name,
base::OnceCallback<void(std::optional<SingleStorageInterestGroup>)>
callback) { … }
void InterestGroupManagerImpl::GetInterestGroup(
const blink::InterestGroupKey& group_key,
base::OnceCallback<void(std::optional<SingleStorageInterestGroup>)>
callback) { … }
void InterestGroupManagerImpl::GetAllInterestGroupOwners(
base::OnceCallback<void(std::vector<url::Origin>)> callback) { … }
void InterestGroupManagerImpl::GetInterestGroupsForOwner(
const std::optional<std::string>& devtools_auction_id,
const url::Origin& owner,
base::OnceCallback<void(scoped_refptr<StorageInterestGroups>)> callback) { … }
bool InterestGroupManagerImpl::GetCachedOwnerAndSignalsOrigins(
const url::Origin& owner,
std::optional<url::Origin>& signals_origin) { … }
void InterestGroupManagerImpl::DeleteInterestGroupData(
StoragePartition::StorageKeyMatcherFunction storage_key_matcher,
base::OnceClosure completion_callback) { … }
void InterestGroupManagerImpl::DeleteAllInterestGroupData(
base::OnceClosure completion_callback) { … }
void InterestGroupManagerImpl::GetLastMaintenanceTimeForTesting(
base::RepeatingCallback<void(base::Time)> callback) const { … }
void InterestGroupManagerImpl::EnqueueReports(
ReportType report_type,
std::vector<GURL> report_urls,
int frame_tree_node_id,
const url::Origin& frame_origin,
const network::mojom::ClientSecurityState& client_security_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) { … }
void InterestGroupManagerImpl::EnqueueRealTimeReports(
std::map<url::Origin, RealTimeReportingContributions> contributions,
AdAuctionPageDataCallback ad_auction_page_data_callback,
int frame_tree_node_id,
const url::Origin& frame_origin,
const network::mojom::ClientSecurityState& client_security_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) { … }
void InterestGroupManagerImpl::SetInterestGroupPriority(
const blink::InterestGroupKey& group_key,
double priority) { … }
void InterestGroupManagerImpl::UpdateInterestGroupPriorityOverrides(
const blink::InterestGroupKey& group_key,
base::flat_map<std::string,
auction_worklet::mojom::PrioritySignalsDoublePtr>
update_priority_signals_overrides) { … }
void InterestGroupManagerImpl::SetBiddingAndAuctionServerKeys(
const url::Origin& coordinator,
const std::vector<BiddingAndAuctionServerKey>& keys,
base::Time expiration) { … }
void InterestGroupManagerImpl::GetBiddingAndAuctionServerKeys(
const url::Origin& coordinator,
base::OnceCallback<
void(std::pair<base::Time, std::vector<BiddingAndAuctionServerKey>>)>
callback) { … }
void InterestGroupManagerImpl::ClearPermissionsCache() { … }
void InterestGroupManagerImpl::QueueKAnonymityUpdateForInterestGroup(
const blink::InterestGroupKey& group_key,
const std::optional<InterestGroupKanonUpdateParameter> update_parameter) { … }
void InterestGroupManagerImpl::UpdateKAnonymity(
const blink::InterestGroupKey& interest_group_key,
const std::vector<std::string>& positive_hashed_keys,
const base::Time update_time,
bool replace_existing_values) { … }
void InterestGroupManagerImpl::GetLastKAnonymityReported(
const std::string& hashed_key,
base::OnceCallback<void(std::optional<base::Time>)> callback) { … }
void InterestGroupManagerImpl::UpdateLastKAnonymityReported(
const std::string& hashed_key) { … }
void InterestGroupManagerImpl::GetInterestGroupAdAuctionData(
url::Origin top_level_origin,
base::Uuid generation_id,
base::Time timestamp,
blink::mojom::AuctionDataConfigPtr config,
base::OnceCallback<void(BiddingAndAuctionData)> callback) { … }
void InterestGroupManagerImpl::ShuffleOwnersThenLoadInterestGroupAdAuctionData(
AdAuctionDataLoaderState state,
std::vector<url::Origin> owners) { … }
void InterestGroupManagerImpl::LoadNextInterestGroupAdAuctionData(
AdAuctionDataLoaderState state,
std::vector<url::Origin> owners) { … }
void InterestGroupManagerImpl::OnLoadedNextInterestGroupAdAuctionData(
AdAuctionDataLoaderState state,
std::vector<url::Origin> owners,
url::Origin owner,
scoped_refptr<StorageInterestGroups> groups) { … }
void InterestGroupManagerImpl::OnInterestGroupAdAuctionDataLoadComplete(
AdAuctionDataLoaderState state) { … }
void InterestGroupManagerImpl::OnAdAuctionDataLoadComplete(
AdAuctionDataLoaderState state,
std::optional<base::Time> last_report_sent_time) { … }
void InterestGroupManagerImpl::GetBiddingAndAuctionServerKey(
std::optional<url::Origin> coordinator,
base::OnceCallback<void(
base::expected<BiddingAndAuctionServerKey, std::string>)> callback) { … }
void InterestGroupManagerImpl::OnJoinInterestGroupPermissionsChecked(
blink::InterestGroup group,
const GURL& joining_url,
bool report_result_only,
AreReportingOriginsAttestedCallback attestation_callback,
blink::mojom::AdAuctionService::JoinInterestGroupCallback callback,
bool can_join) { … }
void InterestGroupManagerImpl::OnLeaveInterestGroupPermissionsChecked(
const blink::InterestGroupKey& group_key,
const url::Origin& main_frame,
bool report_result_only,
blink::mojom::AdAuctionService::LeaveInterestGroupCallback callback,
bool can_leave) { … }
void InterestGroupManagerImpl::
OnClearOriginJoinedInterestGroupsPermissionsChecked(
url::Origin owner,
std::set<std::string> interest_groups_to_keep,
url::Origin main_frame_origin,
bool report_result_only,
blink::mojom::AdAuctionService::LeaveInterestGroupCallback callback,
bool can_leave) { … }
void InterestGroupManagerImpl::GetInterestGroupsForUpdate(
const url::Origin& owner,
int groups_limit,
base::OnceCallback<void(std::vector<InterestGroupUpdateParameter>)>
callback) { … }
void InterestGroupManagerImpl::GetDebugReportLockoutAndCooldowns(
base::flat_set<url::Origin> origins,
base::OnceCallback<void(std::optional<DebugReportLockoutAndCooldowns>)>
callback) { … }
void InterestGroupManagerImpl::UpdateInterestGroup(
const blink::InterestGroupKey& group_key,
InterestGroupUpdate update,
base::OnceCallback<void(bool)> callback) { … }
void InterestGroupManagerImpl::OnUpdateComplete(
const blink::InterestGroupKey& group_key,
base::OnceCallback<void(bool)> callback,
std::optional<InterestGroupKanonUpdateParameter> kanon_update_parameter) { … }
void InterestGroupManagerImpl::ReportUpdateFailed(
const blink::InterestGroupKey& group_key,
bool parse_failure) { … }
void InterestGroupManagerImpl::OnGetInterestGroupsComplete(
base::OnceCallback<void(scoped_refptr<StorageInterestGroups>)> callback,
const std::optional<std::string>& devtools_auction_id,
scoped_refptr<StorageInterestGroups> groups) { … }
void InterestGroupManagerImpl::NotifyInterestGroupAccessed(
base::optional_ref<const std::string> devtools_auction_id,
InterestGroupObserver::AccessType type,
const url::Origin& owner_origin,
const std::string& name,
base::optional_ref<const url::Origin> component_seller_origin,
std::optional<double> bid,
base::optional_ref<const std::string> bid_currency) { … }
void InterestGroupManagerImpl::TrySendingOneReport() { … }
void InterestGroupManagerImpl::OnOneReportSent(
std::unique_ptr<network::SimpleURLLoader> simple_url_loader,
int frame_tree_node_id,
const std::string& devtools_request_id,
scoped_refptr<net::HttpResponseHeaders> response_headers) { … }
void InterestGroupManagerImpl::TimeoutReports() { … }
base::OnceClosure
InterestGroupManagerImpl::CreateNotifyInterestGroupAccessedCallback(
InterestGroupObserver::AccessType type,
const url::Origin& owner_origin,
const std::string& name) { … }
}