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 | |
| 8 | #include "base/optional.h" |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 9 | #include "base/time/time.h" |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 10 | #include "base/version.h" |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame^] | 11 | #include "components/prefs/prefs_export.h" |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 12 | |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #include <string> |
| 16 | #include <unordered_set> |
| 17 | |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame^] | 18 | class PrefRegistrySimple; |
| 19 | class PrefService; |
| 20 | |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 21 | namespace federated_learning { |
| 22 | |
| 23 | // ID used to represent a cohort of people with similar browsing habits. For |
| 24 | // more context, see the explainer at |
| 25 | // https://github.com/jkarlin/floc/blob/master/README.md |
| 26 | class FlocId { |
| 27 | public: |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 28 | static uint64_t SimHashHistory( |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 29 | const std::unordered_set<std::string>& domains); |
| 30 | |
| 31 | FlocId(); |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 32 | |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 33 | explicit FlocId(uint64_t id, |
| 34 | base::Time history_begin_time, |
| 35 | base::Time history_end_time, |
| 36 | uint32_t sorting_lsh_version); |
| 37 | |
| 38 | FlocId(const FlocId& id); |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 39 | ~FlocId(); |
| 40 | FlocId& operator=(const FlocId& id); |
| 41 | FlocId& operator=(FlocId&& id); |
| 42 | |
Yao Xiao | 95d2068 | 2020-08-18 20:30:21 | [diff] [blame] | 43 | bool operator==(const FlocId& other) const; |
| 44 | bool operator!=(const FlocId& other) const; |
| 45 | |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 46 | bool IsValid() const; |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 47 | |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 48 | // The id, followed by the chrome floc version, followed by the async floc |
| 49 | // component versions (i.e. model and sorting-lsh). This is the format to be |
| 50 | // exposed to the JS API. Precondition: |id_| must be valid. |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 51 | std::string ToStringForJsApi() const; |
| 52 | |
| 53 | base::Time history_begin_time() const { return history_begin_time_; } |
| 54 | |
| 55 | base::Time history_end_time() const { return history_end_time_; } |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 56 | |
Yao Xiao | ca909b9 | 2020-11-23 23:51:45 | [diff] [blame^] | 57 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 58 | |
| 59 | void SaveToPrefs(PrefService* local_state); |
| 60 | static FlocId ReadFromPrefs(PrefService* local_state); |
| 61 | |
| 62 | static void SaveComputeTimeToPrefs(base::Time compute_time, |
| 63 | PrefService* local_state); |
| 64 | static base::Time ReadComputeTimeFromPrefs(PrefService* local_state); |
| 65 | |
Yao Xiao | ebed8c1 | 2020-09-26 07:51:20 | [diff] [blame] | 66 | private: |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 67 | base::Optional<uint64_t> id_; |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 68 | |
Yao Xiao | a927c07 | 2020-11-12 06:49:27 | [diff] [blame] | 69 | // The time range of the actual history used to compute the floc. This should |
| 70 | // always be within the time range of each history query. |
| 71 | base::Time history_begin_time_; |
| 72 | base::Time history_end_time_; |
| 73 | |
Yao Xiao | 70c52c0a | 2020-11-05 17:23:03 | [diff] [blame] | 74 | // The main version (i.e. 1st int) of the sorting lsh component version. |
| 75 | uint32_t sorting_lsh_version_ = 0; |
Yao Xiao | dcda72b | 2020-05-18 23:05:37 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace federated_learning |
| 79 | |
| 80 | #endif // COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_ |