#include "components/browsing_topics/browsing_topics_service_impl.h"
#include <random>
#include <vector>
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/rand_util.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "components/browsing_topics/browsing_topics_calculator.h"
#include "components/browsing_topics/browsing_topics_page_load_data_tracker.h"
#include "components/browsing_topics/common/common_types.h"
#include "components/browsing_topics/mojom/browsing_topics_internals.mojom.h"
#include "components/browsing_topics/util.h"
#include "components/privacy_sandbox/canonical_topic.h"
#include "content/public/browser/browsing_topics_site_data_manager.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/browsing_topics/browsing_topics.mojom.h"
namespace browsing_topics {
namespace {
enum class NumberOfTopics { … };
bool ShouldClearTopicsOnStartup(
const BrowsingTopicsState& browsing_topics_state,
base::Time browsing_topics_data_accessible_since) { … }
std::vector<privacy_sandbox::CanonicalTopic> TopTopicsToClearOnStartup(
const BrowsingTopicsState& browsing_topics_state,
base::RepeatingCallback<bool(const privacy_sandbox::CanonicalTopic&)>
is_topic_allowed_by_settings) { … }
struct StartupCalculateDecision { … };
StartupCalculateDecision GetStartupCalculationDecision(
const BrowsingTopicsState& browsing_topics_state,
base::Time browsing_topics_data_accessible_since,
base::RepeatingCallback<bool(const privacy_sandbox::CanonicalTopic&)>
is_topic_allowed_by_settings) { … }
void RecordBrowsingTopicsApiResultMetrics(ApiAccessResult result,
content::RenderFrameHost* main_frame,
bool is_get_topics_request) { … }
void RecordBrowsingTopicsApiResultMetrics(
const std::vector<CandidateTopic>& valid_candidate_topics,
content::RenderFrameHost* main_frame) { … }
enum class BrowsingTopicsApiActionType { … };
void RecordBrowsingTopicsApiActionTypeMetrics(ApiCallerSource caller_source,
bool get_topics,
bool observe) { … }
std::set<HashedDomain> GetAllObservingDomains(
const BrowsingTopicsState& browsing_topics_state) { … }
}
BrowsingTopicsServiceImpl::~BrowsingTopicsServiceImpl() = default;
BrowsingTopicsServiceImpl::BrowsingTopicsServiceImpl(
const base::FilePath& profile_path,
privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
history::HistoryService* history_service,
content::BrowsingTopicsSiteDataManager* site_data_manager,
std::unique_ptr<Annotator> annotator,
TopicAccessedCallback topic_accessed_callback)
: … { … }
bool BrowsingTopicsServiceImpl::HandleTopicsWebApi(
const url::Origin& context_origin,
content::RenderFrameHost* main_frame,
ApiCallerSource caller_source,
bool get_topics,
bool observe,
std::vector<blink::mojom::EpochTopicPtr>& topics) { … }
int BrowsingTopicsServiceImpl::NumVersionsInEpochs(
const url::Origin& main_frame_origin) const { … }
void BrowsingTopicsServiceImpl::GetBrowsingTopicsStateForWebUi(
bool calculate_now,
mojom::PageHandler::GetBrowsingTopicsStateCallback callback) { … }
std::vector<privacy_sandbox::CanonicalTopic>
BrowsingTopicsServiceImpl::GetTopTopicsForDisplay() const { … }
void BrowsingTopicsServiceImpl::ValidateCalculationSchedule() { … }
Annotator* BrowsingTopicsServiceImpl::GetAnnotator() { … }
void BrowsingTopicsServiceImpl::ClearTopic(
const privacy_sandbox::CanonicalTopic& canonical_topic) { … }
void BrowsingTopicsServiceImpl::ClearTopicsDataForOrigin(
const url::Origin& origin) { … }
void BrowsingTopicsServiceImpl::ClearAllTopicsData() { … }
std::unique_ptr<BrowsingTopicsCalculator>
BrowsingTopicsServiceImpl::CreateCalculator(
privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
history::HistoryService* history_service,
content::BrowsingTopicsSiteDataManager* site_data_manager,
Annotator* annotator,
const base::circular_deque<EpochTopics>& epochs,
bool is_manually_triggered,
int previous_timeout_count,
base::Time session_start_time,
BrowsingTopicsCalculator::CalculateCompletedCallback callback) { … }
const BrowsingTopicsState& BrowsingTopicsServiceImpl::browsing_topics_state() { … }
void BrowsingTopicsServiceImpl::ScheduleBrowsingTopicsCalculation(
bool is_manually_triggered,
int previous_timeout_count,
base::TimeDelta delay,
bool persist_calculation_time) { … }
void BrowsingTopicsServiceImpl::CalculateBrowsingTopics(
bool is_manually_triggered,
int previous_timeout_count) { … }
void BrowsingTopicsServiceImpl::OnCalculateBrowsingTopicsCompleted(
EpochTopics epoch_topics) { … }
void BrowsingTopicsServiceImpl::OnBrowsingTopicsStateLoaded() { … }
void BrowsingTopicsServiceImpl::Shutdown() { … }
void BrowsingTopicsServiceImpl::OnTopicsDataAccessibleSinceUpdated() { … }
void BrowsingTopicsServiceImpl::OnHistoryDeletions(
history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) { … }
void BrowsingTopicsServiceImpl::GetBrowsingTopicsStateForWebUiHelper(
mojom::PageHandler::GetBrowsingTopicsStateCallback callback,
std::map<HashedDomain, std::string> hashed_to_unhashed_context_domains) { … }
}