blob: f119fed6b8a630ffb7b9f09208eb07e38e8c450d [file] [log] [blame]
Yao Xiaodcda72b2020-05-18 23:05:371// 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 Xiaoa927c072020-11-12 06:49:278#include "base/time/time.h"
Yao Xiao70c52c0a2020-11-05 17:23:039#include "base/version.h"
Yao Xiaoca909b92020-11-23 23:51:4510#include "components/prefs/prefs_export.h"
Anton Bikineev1156b5f2021-05-15 22:35:3611#include "third_party/abseil-cpp/absl/types/optional.h"
Yao Xiao3d1b19d2021-02-24 18:25:0912#include "third_party/blink/public/mojom/federated_learning/floc.mojom-forward.h"
Yao Xiaodcda72b2020-05-18 23:05:3713
14#include <stdint.h>
15
16#include <string>
17#include <unordered_set>
18
Yao Xiaoca909b92020-11-23 23:51:4519class PrefRegistrySimple;
20class PrefService;
21
Yao Xiaodcda72b2020-05-18 23:05:3722namespace 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
27class FlocId {
28 public:
Yao Xiao70c52c0a2020-11-05 17:23:0329 static uint64_t SimHashHistory(
Yao Xiaodcda72b2020-05-18 23:05:3730 const std::unordered_set<std::string>& domains);
31
Yao Xiaof6e88452020-12-21 10:59:3932 // 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 Xiaodcda72b2020-05-18 23:05:3735 FlocId();
Yao Xiaodcda72b2020-05-18 23:05:3736
Yao Xiaof6e88452020-12-21 10:59:3937 // Create a newly computed and valid floc. The |finch_config_version_| and
38 // the |compute_time_| will be set to the current.
Yao Xiaoa927c072020-11-12 06:49:2739 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 Xiaodcda72b2020-05-18 23:05:3745 ~FlocId();
46 FlocId& operator=(const FlocId& id);
47 FlocId& operator=(FlocId&& id);
48
Yao Xiao95d20682020-08-18 20:30:2149 bool operator==(const FlocId& other) const;
50 bool operator!=(const FlocId& other) const;
51
Yao Xiaof6e88452020-12-21 10:59:3952 // 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 Xiaodcda72b2020-05-18 23:05:3756 bool IsValid() const;
Yao Xiaodcda72b2020-05-18 23:05:3757
Yao Xiao3d1b19d2021-02-24 18:25:0958 // 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 Xiaoa927c072020-11-12 06:49:2763
Yao Xiao74ad2a252021-02-09 00:22:4464 // Returns the internal uint64_t number. Precondition: |id_| must be valid.
65 uint64_t ToUint64() const;
66
Yao Xiaoa927c072020-11-12 06:49:2767 base::Time history_begin_time() const { return history_begin_time_; }
68
69 base::Time history_end_time() const { return history_end_time_; }
Yao Xiaodcda72b2020-05-18 23:05:3770
Yao Xiaof6e88452020-12-21 10:59:3971 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 Xiaoca909b92020-11-23 23:51:4577 static void RegisterPrefs(PrefRegistrySimple* registry);
78
Yao Xiaoc00b2bc02020-12-03 01:38:4079 void SaveToPrefs(PrefService* prefs);
80 static FlocId ReadFromPrefs(PrefService* prefs);
Yao Xiaoca909b92020-11-23 23:51:4581
Yao Xiaof6e88452020-12-21 10:59:3982 // 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 Xiaoca909b92020-11-23 23:51:4586
sauski00e1e8f2021-05-10 17:46:4587 // 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 Xiaoebed8c12020-09-26 07:51:2093 private:
Yao Xiaof6e88452020-12-21 10:59:3994 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 Bikineev1156b5f2021-05-15 22:35:3698 explicit FlocId(absl::optional<uint64_t> id,
Yao Xiaof6e88452020-12-21 10:59:3999 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 Bikineev1156b5f2021-05-15 22:35:36105 absl::optional<uint64_t> id_;
Yao Xiao70c52c0a2020-11-05 17:23:03106
Yao Xiaoa927c072020-11-12 06:49:27107 // 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 Xiaof6e88452020-12-21 10:59:39112 // 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 Xiao70c52c0a2020-11-05 17:23:03116 // The main version (i.e. 1st int) of the sorting lsh component version.
117 uint32_t sorting_lsh_version_ = 0;
Yao Xiaof6e88452020-12-21 10:59:39118
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 Xiaodcda72b2020-05-18 23:05:37122};
123
124} // namespace federated_learning
125
126#endif // COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_