epoch_topics.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/browsing_topics/epoch_topics.h"
  5. #include "base/hash/legacy_hash.h"
  6. #include "base/json/values_util.h"
  7. #include "base/logging.h"
  8. #include "base/numerics/checked_math.h"
  9. #include "components/browsing_topics/util.h"
  10. #include "third_party/blink/public/common/features.h"
  11. #include "url/gurl.h"
  12. namespace browsing_topics {
  13. namespace {
  14. const char kTopTopicsAndObservingDomainsNameKey[] =
  15. "top_topics_and_observing_domains";
  16. const char kPaddedTopTopicsStartIndexNameKey[] =
  17. "padded_top_topics_start_index";
  18. const char kTaxonomySizeNameKey[] = "taxonomy_size";
  19. const char kTaxonomyVersionNameKey[] = "taxonomy_version";
  20. const char kModelVersionNameKey[] = "model_version";
  21. const char kCalculationTimeNameKey[] = "calculation_time";
  22. bool ShouldUseRandomTopic(uint64_t random_or_top_topic_decision_hash) {
  23. return base::checked_cast<int>(random_or_top_topic_decision_hash % 100) <
  24. blink::features::kBrowsingTopicsUseRandomTopicProbabilityPercent.Get();
  25. }
  26. } // namespace
  27. EpochTopics::EpochTopics(base::Time calculation_time)
  28. : calculation_time_(calculation_time) {}
  29. EpochTopics::EpochTopics(
  30. std::vector<TopicAndDomains> top_topics_and_observing_domains,
  31. size_t padded_top_topics_start_index,
  32. size_t taxonomy_size,
  33. int taxonomy_version,
  34. int64_t model_version,
  35. base::Time calculation_time)
  36. : top_topics_and_observing_domains_(
  37. std::move(top_topics_and_observing_domains)),
  38. padded_top_topics_start_index_(padded_top_topics_start_index),
  39. taxonomy_size_(taxonomy_size),
  40. taxonomy_version_(taxonomy_version),
  41. model_version_(model_version),
  42. calculation_time_(calculation_time) {
  43. DCHECK_EQ(base::checked_cast<int>(top_topics_and_observing_domains_.size()),
  44. blink::features::kBrowsingTopicsNumberOfTopTopicsPerEpoch.Get());
  45. DCHECK_LE(padded_top_topics_start_index,
  46. top_topics_and_observing_domains_.size());
  47. DCHECK_GT(taxonomy_version_, 0);
  48. DCHECK_GT(model_version_, 0);
  49. }
  50. EpochTopics::EpochTopics(EpochTopics&&) = default;
  51. EpochTopics& EpochTopics::operator=(EpochTopics&&) = default;
  52. EpochTopics::~EpochTopics() = default;
  53. // static
  54. EpochTopics EpochTopics::FromDictValue(const base::Value::Dict& dict_value) {
  55. const base::Value* calculation_time_value =
  56. dict_value.Find(kCalculationTimeNameKey);
  57. if (!calculation_time_value)
  58. return EpochTopics(base::Time());
  59. base::Time calculation_time =
  60. base::ValueToTime(calculation_time_value).value();
  61. std::vector<TopicAndDomains> top_topics_and_observing_domains;
  62. const base::Value::List* top_topics_and_observing_domains_value =
  63. dict_value.FindList(kTopTopicsAndObservingDomainsNameKey);
  64. if (!top_topics_and_observing_domains_value)
  65. return EpochTopics(calculation_time);
  66. for (const base::Value& topic_and_observing_domains_value :
  67. *top_topics_and_observing_domains_value) {
  68. const base::Value::Dict* topic_and_observing_domains_dict_value =
  69. topic_and_observing_domains_value.GetIfDict();
  70. if (!topic_and_observing_domains_dict_value)
  71. return EpochTopics(calculation_time);
  72. top_topics_and_observing_domains.push_back(TopicAndDomains::FromDictValue(
  73. *topic_and_observing_domains_dict_value));
  74. }
  75. if (top_topics_and_observing_domains.empty())
  76. return EpochTopics(calculation_time);
  77. absl::optional<int> padded_top_topics_start_index_value =
  78. dict_value.FindInt(kPaddedTopTopicsStartIndexNameKey);
  79. if (!padded_top_topics_start_index_value)
  80. return EpochTopics(calculation_time);
  81. size_t padded_top_topics_start_index =
  82. static_cast<size_t>(*padded_top_topics_start_index_value);
  83. absl::optional<int> taxonomy_size_value =
  84. dict_value.FindInt(kTaxonomySizeNameKey);
  85. if (!taxonomy_size_value)
  86. return EpochTopics(calculation_time);
  87. size_t taxonomy_size = static_cast<size_t>(*taxonomy_size_value);
  88. absl::optional<int> taxonomy_version_value =
  89. dict_value.FindInt(kTaxonomyVersionNameKey);
  90. if (!taxonomy_version_value)
  91. return EpochTopics(calculation_time);
  92. int taxonomy_version = *taxonomy_version_value;
  93. const base::Value* model_version_value =
  94. dict_value.Find(kModelVersionNameKey);
  95. if (!model_version_value)
  96. return EpochTopics(calculation_time);
  97. absl::optional<int64_t> model_version_int64_value =
  98. base::ValueToInt64(model_version_value);
  99. if (!model_version_int64_value)
  100. return EpochTopics(calculation_time);
  101. int64_t model_version = *model_version_int64_value;
  102. return EpochTopics(std::move(top_topics_and_observing_domains),
  103. padded_top_topics_start_index, taxonomy_size,
  104. taxonomy_version, model_version, calculation_time);
  105. }
  106. base::Value::Dict EpochTopics::ToDictValue() const {
  107. base::Value::List top_topics_and_observing_domains_list;
  108. for (const TopicAndDomains& topic_and_domains :
  109. top_topics_and_observing_domains_) {
  110. top_topics_and_observing_domains_list.Append(
  111. topic_and_domains.ToDictValue());
  112. }
  113. base::Value::Dict result_dict;
  114. result_dict.Set(kTopTopicsAndObservingDomainsNameKey,
  115. std::move(top_topics_and_observing_domains_list));
  116. result_dict.Set(kPaddedTopTopicsStartIndexNameKey,
  117. base::checked_cast<int>(padded_top_topics_start_index_));
  118. result_dict.Set(kTaxonomySizeNameKey,
  119. base::checked_cast<int>(taxonomy_size_));
  120. result_dict.Set(kTaxonomyVersionNameKey, taxonomy_version_);
  121. result_dict.Set(kModelVersionNameKey, base::Int64ToValue(model_version_));
  122. result_dict.Set(kCalculationTimeNameKey,
  123. base::TimeToValue(calculation_time_));
  124. return result_dict;
  125. }
  126. absl::optional<Topic> EpochTopics::TopicForSite(
  127. const std::string& top_domain,
  128. const HashedDomain& hashed_context_domain,
  129. ReadOnlyHmacKey hmac_key,
  130. bool& output_is_true_topic,
  131. bool& candidate_topic_filtered) const {
  132. return TopicForSiteHelper(top_domain, /*need_filtering=*/true,
  133. /*allow_random_or_padded_topic=*/true,
  134. hashed_context_domain, hmac_key,
  135. output_is_true_topic, candidate_topic_filtered);
  136. }
  137. absl::optional<Topic> EpochTopics::TopicForSiteForDisplay(
  138. const std::string& top_domain,
  139. ReadOnlyHmacKey hmac_key) const {
  140. bool output_is_true_topic = false;
  141. bool candidate_topic_filtered = false;
  142. return TopicForSiteHelper(top_domain, /*need_filtering=*/false,
  143. /*allow_random_or_padded_topic=*/false,
  144. /*hashed_context_domain=*/{}, hmac_key,
  145. output_is_true_topic, candidate_topic_filtered);
  146. }
  147. void EpochTopics::ClearTopics() {
  148. top_topics_and_observing_domains_.clear();
  149. padded_top_topics_start_index_ = 0;
  150. }
  151. void EpochTopics::ClearTopic(Topic topic) {
  152. for (TopicAndDomains& topic_and_domains : top_topics_and_observing_domains_) {
  153. if (topic_and_domains.topic() != topic)
  154. continue;
  155. // Invalidate `topic_and_domains`. We cannot delete the entry from
  156. // `top_topics_and_observing_domains_` because it would modify the list of
  157. // topics, and would break the ability to return the same topic for the same
  158. // site for the epoch .
  159. topic_and_domains = TopicAndDomains();
  160. }
  161. }
  162. void EpochTopics::ClearContextDomain(
  163. const HashedDomain& hashed_context_domain) {
  164. for (TopicAndDomains& topic_and_domains : top_topics_and_observing_domains_) {
  165. topic_and_domains.ClearDomain(hashed_context_domain);
  166. }
  167. }
  168. absl::optional<Topic> EpochTopics::TopicForSiteHelper(
  169. const std::string& top_domain,
  170. bool need_filtering,
  171. bool allow_random_or_padded_topic,
  172. const HashedDomain& hashed_context_domain,
  173. ReadOnlyHmacKey hmac_key,
  174. bool& output_is_true_topic,
  175. bool& candidate_topic_filtered) const {
  176. DCHECK(!output_is_true_topic);
  177. DCHECK(!candidate_topic_filtered);
  178. // The topics calculation failed, or the topics has been cleared.
  179. if (empty())
  180. return absl::nullopt;
  181. uint64_t random_or_top_topic_decision_hash =
  182. HashTopDomainForRandomOrTopTopicDecision(hmac_key, calculation_time_,
  183. top_domain);
  184. if (ShouldUseRandomTopic(random_or_top_topic_decision_hash)) {
  185. if (!allow_random_or_padded_topic)
  186. return absl::nullopt;
  187. uint64_t random_topic_index_decision =
  188. HashTopDomainForRandomTopicIndexDecision(hmac_key, calculation_time_,
  189. top_domain);
  190. size_t random_topic_index = random_topic_index_decision % taxonomy_size_;
  191. return Topic(base::checked_cast<int>(random_topic_index + 1));
  192. }
  193. uint64_t top_topic_index_decision_hash =
  194. HashTopDomainForTopTopicIndexDecision(hmac_key, calculation_time_,
  195. top_domain);
  196. size_t top_topic_index =
  197. top_topic_index_decision_hash % top_topics_and_observing_domains_.size();
  198. if (!allow_random_or_padded_topic &&
  199. padded_top_topics_start_index_ <= top_topic_index)
  200. return absl::nullopt;
  201. const TopicAndDomains& topic_and_observing_domains =
  202. top_topics_and_observing_domains_[top_topic_index];
  203. if (!topic_and_observing_domains.IsValid())
  204. return absl::nullopt;
  205. // Only add the topic if the context has observed it before.
  206. if (need_filtering && !topic_and_observing_domains.hashed_domains().count(
  207. hashed_context_domain)) {
  208. candidate_topic_filtered = true;
  209. return absl::nullopt;
  210. }
  211. if (top_topic_index < padded_top_topics_start_index_)
  212. output_is_true_topic = true;
  213. return topic_and_observing_domains.topic();
  214. }
  215. } // namespace browsing_topics