Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 5 | #ifndef COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_ |
| 6 | #define COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_ |
| 7 | |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 8 | #include "base/time/time.h" |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 9 | #include "base/version.h" |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame] | 10 | #include "components/prefs/prefs_export.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 11 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Yao Xiao | 3d1b19d | 2021-02-24 18:25:09 | [diff] [blame] | 12 | #include "third_party/blink/public/mojom/federated_learning/floc.mojom-forward.h" |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <string> |
| 17 | #include <unordered_set> |
| 18 | |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame] | 19 | class PrefRegistrySimple; |
| 20 | class PrefService; |
| 21 | |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 22 | namespace federated_learning { |
| 23 | |
| 24 | // ID used to represent a cohort of people with similar browsing habits. For |
| 25 | // more context, see the explainer at |
| 26 | // https://github.com/jkarlin/floc/blob/master/README.md |
| 27 | class FlocId { |
| 28 | public: |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 29 | static uint64_t SimHashHistory( |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 30 | const std::unordered_set<std::string>& domains); |
| 31 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 32 | // Create a newly computed but invalid floc (which implies permission errors, |
| 33 | // insufficient eligible history, or blocked floc). The |
| 34 | // |finch_config_version_| and the |compute_time_| will be set to the current. |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 35 | FlocId(); |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 36 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 37 | // Create a newly computed and valid floc. The |finch_config_version_| and |
| 38 | // the |compute_time_| will be set to the current. |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 39 | explicit FlocId(uint64_t id, |
| 40 | base::Time history_begin_time, |
| 41 | base::Time history_end_time, |
| 42 | uint32_t sorting_lsh_version); |
| 43 | |
| 44 | FlocId(const FlocId& id); |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 45 | ~FlocId(); |
| 46 | FlocId& operator=(const FlocId& id); |
| 47 | FlocId& operator=(FlocId&& id); |
| 48 | |
Yao Xiao | 95d2068 | 2020-08-18 20:30:21 | [diff] [blame] | 49 | bool operator==(const FlocId& other) const; |
| 50 | bool operator!=(const FlocId& other) const; |
| 51 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 52 | // True if the |id_| is successfully computed and hasn't been invalidated |
| 53 | // since the last computation. Note that an invalid FlocId still often has a |
| 54 | // legitimate compute time and finch config version, unless it's read from a |
| 55 | // fresh profile prefs. |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 56 | bool IsValid() const; |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 57 | |
Yao Xiao | 3d1b19d | 2021-02-24 18:25:09 | [diff] [blame] | 58 | // Get the blink::mojom::InterestCohort representation of this floc, with |
| 59 | // interest_cohort.id being "<id>" and interest_cohort.version being |
| 60 | // "chrome.<finch_config_version>.<sorting_lsh_version>". This is the format |
| 61 | // to be exposed to the JS API. Precondition: |id_| must be valid. |
| 62 | blink::mojom::InterestCohortPtr ToInterestCohortForJsApi() const; |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 63 | |
Yao Xiao | 74ad2a25 | 2021-02-09 00:22:44 | [diff] [blame] | 64 | // Returns the internal uint64_t number. Precondition: |id_| must be valid. |
| 65 | uint64_t ToUint64() const; |
| 66 | |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 67 | base::Time history_begin_time() const { return history_begin_time_; } |
| 68 | |
| 69 | base::Time history_end_time() const { return history_end_time_; } |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 70 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 71 | uint32_t finch_config_version() const { return finch_config_version_; } |
| 72 | |
| 73 | uint32_t sorting_lsh_version() const { return sorting_lsh_version_; } |
| 74 | |
| 75 | base::Time compute_time() const { return compute_time_; } |
| 76 | |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame] | 77 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 78 | |
Yao Xiao | c00b2bc0 | 2020-12-03 01:38:40 | [diff] [blame] | 79 | void SaveToPrefs(PrefService* prefs); |
| 80 | static FlocId ReadFromPrefs(PrefService* prefs); |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame] | 81 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 82 | // Reset |id_| and clear the prefs corresponding to the id. This assumes the |
| 83 | // current floc is already in sync with the prefs and we don't need to save |
| 84 | // other unaffected field. |
| 85 | void InvalidateIdAndSaveToPrefs(PrefService* prefs); |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame] | 86 | |
sauski | 00e1e8f | 2021-05-10 17:46:45 | [diff] [blame] | 87 | // Resets |compute_time_| to provided |compute_time| and saves it to prefs. |
| 88 | // This should at least be called if the floc compute timer is reset, to |
| 89 | // ensure that the compute cycle continues at the expected frequency. |
| 90 | void ResetComputeTimeAndSaveToPrefs(base::Time compute_time, |
| 91 | PrefService* prefs); |
| 92 | |
Yao Xiao | ebed8c1 | 2020-09-26 07:51:20 | [diff] [blame] | 93 | private: |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 94 | friend class FlocIdTester; |
| 95 | |
| 96 | // Create a floc with stated params. This will only be used to create a floc |
| 97 | // read from prefs. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 98 | explicit FlocId(absl::optional<uint64_t> id, |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 99 | base::Time history_begin_time, |
| 100 | base::Time history_end_time, |
| 101 | uint32_t finch_config_version, |
| 102 | uint32_t sorting_lsh_version, |
| 103 | base::Time compute_time); |
| 104 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 105 | absl::optional<uint64_t> id_; |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 106 | |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 107 | // The time range of the actual history used to compute the floc. This should |
| 108 | // always be within the time range of each history query. |
| 109 | base::Time history_begin_time_; |
| 110 | base::Time history_end_time_; |
| 111 | |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 112 | // The kFlocIdFinchConfigVersion feature param. When floc is loaded from |
| 113 | // prefs, this could be different from the current feature param state. |
| 114 | uint32_t finch_config_version_ = 0; |
| 115 | |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 116 | // The main version (i.e. 1st int) of the sorting lsh component version. |
| 117 | uint32_t sorting_lsh_version_ = 0; |
Yao Xiao | f6e8845 | 2020-12-21 10:59:39 | [diff] [blame] | 118 | |
| 119 | // The time when the floc was computed. compute_time_.is_null() means the |
| 120 | // floc has never been computed before, and implies |id_| is also invalid. |
| 121 | base::Time compute_time_; |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace federated_learning |
| 125 | |
| 126 | #endif // COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_ |