#include "components/page_content_annotations/core/page_content_annotations_service.h"
#include <iterator>
#include <utility>
#include "base/barrier_closure.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/i18n/case_conversion.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros_local.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "components/google/core/common/google_util.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/omnibox/common/zero_suggest_cache_service_interface.h"
#include "components/optimization_guide/core/noisy_metrics_recorder.h"
#include "components/optimization_guide/core/optimization_guide_decider.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/page_content_annotations/core/page_content_annotations_enums.h"
#include "components/page_content_annotations/core/page_content_annotations_features.h"
#include "components/page_content_annotations/core/page_content_annotations_switches.h"
#include "components/page_content_annotations/core/page_content_annotations_validator.h"
#include "components/search/search.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/omnibox_proto/types.pb.h"
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
#include "components/page_content_annotations/core/page_content_annotations_model_manager.h"
#endif
namespace page_content_annotations {
namespace {
std::string PageContentAnnotationsTypeToString(
PageContentAnnotationsType annotation_type) { … }
void LogPageContentAnnotationsStorageStatus(
PageContentAnnotationsStorageStatus status,
PageContentAnnotationsType annotation_type) { … }
void LogRelatedSearchesExtracted(bool success) { … }
void LogRelatedSearchesCacheHit(bool cache_hit) { … }
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
void MaybeRecordVisibilityUKM(
const HistoryVisit& visit,
const std::optional<history::VisitContentModelAnnotations>&
content_annotations) { … }
#endif
std::string GetCanonicalSearchURL(const GURL& url,
TemplateURLService* template_url_service) { … }
}
PageContentAnnotationsService::PageContentAnnotationsService(
const std::string& application_locale,
const std::string& country_code,
optimization_guide::OptimizationGuideModelProvider*
optimization_guide_model_provider,
history::HistoryService* history_service,
TemplateURLService* template_url_service,
ZeroSuggestCacheServiceInterface* zero_suggest_cache_service,
leveldb_proto::ProtoDatabaseProvider* database_provider,
const base::FilePath& database_dir,
OptimizationGuideLogger* optimization_guide_logger,
optimization_guide::OptimizationGuideDecider* optimization_guide_decider,
scoped_refptr<base::SequencedTaskRunner> background_task_runner)
: … { … }
PageContentAnnotationsService::~PageContentAnnotationsService() = default;
void PageContentAnnotationsService::Annotate(const HistoryVisit& visit) { … }
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
bool PageContentAnnotationsService::MaybeStartAnnotateVisitBatch() { … }
void PageContentAnnotationsService::AnnotateVisitBatch() { … }
void PageContentAnnotationsService::OnAnnotationBatchComplete(
AnnotationType type,
std::vector<std::optional<history::VisitContentModelAnnotations>>*
merge_to_output,
base::OnceClosure signal_merge_complete_callback,
const std::vector<BatchAnnotationResult>& batch_result) { … }
void PageContentAnnotationsService::OnBatchVisitsAnnotated(
std::unique_ptr<
std::vector<std::optional<history::VisitContentModelAnnotations>>>
merged_annotation_outputs) { … }
#endif
void PageContentAnnotationsService::OverridePageContentAnnotatorForTesting(
PageContentAnnotator* annotator) { … }
void PageContentAnnotationsService::BatchAnnotate(
BatchAnnotationCallback callback,
const std::vector<std::string>& inputs,
AnnotationType annotation_type) { … }
std::optional<optimization_guide::ModelInfo>
PageContentAnnotationsService::GetModelInfoForType(AnnotationType type) const { … }
void PageContentAnnotationsService::RequestAndNotifyWhenModelAvailable(
AnnotationType type,
base::OnceCallback<void(bool)> callback) { … }
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
void PageContentAnnotationsService::OnPageContentAnnotated(
const HistoryVisit& visit,
const std::optional<history::VisitContentModelAnnotations>&
content_annotations) { … }
#endif
bool PageContentAnnotationsService::ShouldExtractRelatedSearchesFromZPSCache() { … }
void PageContentAnnotationsService::OnZeroSuggestResponseUpdated(
const std::string& page_url,
const ZeroSuggestCacheServiceInterface::CacheEntry& response) { … }
void PageContentAnnotationsService::OnRelatedSearchesExtracted(
const HistoryVisit& visit,
continuous_search::SearchResultExtractorClientStatus status,
continuous_search::mojom::CategoryResultsPtr results) { … }
void PageContentAnnotationsService::AddRelatedSearchesForVisit(
const HistoryVisit& visit,
const std::vector<std::string>& related_searches) { … }
void PageContentAnnotationsService::QueryURL(
const HistoryVisit& visit,
PersistAnnotationsCallback callback,
PageContentAnnotationsType annotation_type) { … }
void PageContentAnnotationsService::OnURLQueried(
const HistoryVisit& visit,
PersistAnnotationsCallback callback,
PageContentAnnotationsType annotation_type,
history::QueryURLResult url_result) { … }
void PageContentAnnotationsService::OnURLsModified(
history::HistoryService* history_service,
const history::URLRows& changed_urls) { … }
void PageContentAnnotationsService::OnURLVisitedWithNavigationId(
history::HistoryService* history_service,
const history::URLRow& url_row,
const history::VisitRow& visit_row,
std::optional<int64_t> local_navigation_id) { … }
void PageContentAnnotationsService::OnWaitForTitleDone(const GURL& url) { … }
void PageContentAnnotationsService::AddObserver(
AnnotationType annotation_type,
PageContentAnnotationsService::PageContentAnnotationsObserver* observer) { … }
void PageContentAnnotationsService::RemoveObserver(
AnnotationType annotation_type,
PageContentAnnotationsService::PageContentAnnotationsObserver* observer) { … }
void PageContentAnnotationsService::PersistRemotePageMetadata(
const HistoryVisit& visit,
const optimization_guide::proto::PageEntitiesMetadata&
page_entities_metadata) { … }
void PageContentAnnotationsService::PersistSalientImageMetadata(
const HistoryVisit& visit,
const optimization_guide::proto::SalientImageMetadata&
salient_image_metadata) { … }
void PageContentAnnotationsService::NotifyPageContentAnnotatedObservers(
AnnotationType annotation_type,
const GURL& url,
const PageContentAnnotationsResult& page_content_annotations_result) { … }
void PageContentAnnotationsService::OnOptimizationGuideResponseReceived(
const HistoryVisit& history_visit,
optimization_guide::proto::OptimizationType optimization_type,
optimization_guide::OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& metadata) { … }
HistoryVisit::HistoryVisit() = default;
HistoryVisit::HistoryVisit(base::Time nav_entry_timestamp, GURL url) { … }
HistoryVisit::HistoryVisit(history::VisitID visit_id) { … }
HistoryVisit::~HistoryVisit() = default;
HistoryVisit::HistoryVisit(const HistoryVisit&) = default;
}