#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "components/browsing_topics/annotator_impl.h"
#include <vector>
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/dcheck_is_on.h"
#include "base/files/file_util.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "components/optimization_guide/core/optimization_guide_model_provider.h"
#include "components/optimization_guide/proto/models.pb.h"
#include "components/optimization_guide/proto/page_topics_model_metadata.pb.h"
#include "components/optimization_guide/proto/page_topics_override_list.pb.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/zlib/google/compression_utils.h"
namespace browsing_topics {
namespace {
const int32_t kNoneCategoryId = …;
const int32_t kMeaninglessPrefixV2MinVersion = …;
const base::FilePath::CharType kOverrideListBasePath[] = …);
enum class OverrideListFileLoadResult { … };
void RecordOverrideListFileLoadResult(OverrideListFileLoadResult result) { … }
std::optional<std::unordered_map<std::string, std::vector<int32_t>>>
LoadOverrideListFromFile(const base::FilePath& path) { … }
int MeaninglessPrefixLength(const std::string& host) { … }
bool IsModelTaxonomyVersionSupported(int model_taxonomy_version) { … }
}
AnnotatorImpl::AnnotatorImpl(
optimization_guide::OptimizationGuideModelProvider* model_provider,
scoped_refptr<base::SequencedTaskRunner> background_task_runner,
const std::optional<optimization_guide::proto::Any>& model_metadata)
: … { … }
AnnotatorImpl::~AnnotatorImpl() = default;
void AnnotatorImpl::NotifyWhenModelAvailable(base::OnceClosure callback) { … }
std::optional<optimization_guide::ModelInfo>
AnnotatorImpl::GetBrowsingTopicsModelInfo() const { … }
void AnnotatorImpl::BatchAnnotate(BatchAnnotationCallback callback,
const std::vector<std::string>& inputs) { … }
void AnnotatorImpl::OnOverrideListLoadAttemptDone(
BatchAnnotationCallback callback,
const std::vector<std::string>& inputs,
std::optional<std::unordered_map<std::string, std::vector<int32_t>>>
override_list) { … }
void AnnotatorImpl::StartBatchAnnotate(BatchAnnotationCallback callback,
const std::vector<std::string>& inputs) { … }
void AnnotatorImpl::OnBatchComplete(
BatchAnnotationCallback callback,
std::unique_ptr<std::vector<Annotation>> annotations_ptr) { … }
std::string AnnotatorImpl::PreprocessHost(const std::string& host) const { … }
void AnnotatorImpl::AnnotateSingleInput(
base::OnceClosure single_input_done_signal,
Annotation* annotation) { … }
void AnnotatorImpl::PostprocessCategoriesToBatchAnnotationResult(
base::OnceClosure single_input_done_signal,
Annotation* annotation,
const std::optional<std::vector<tflite::task::core::Category>>& output) { … }
std::optional<std::vector<int32_t>>
AnnotatorImpl::ExtractCategoriesFromModelOutput(
const std::vector<tflite::task::core::Category>& model_output) const { … }
void AnnotatorImpl::UnloadModel() { … }
void AnnotatorImpl::OnModelUpdated(
optimization_guide::proto::OptimizationTarget optimization_target,
base::optional_ref<const optimization_guide::ModelInfo> model_info) { … }
}