blob: c43f9de46baf4f719ac885a4ea04461e31e67dcd [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
8#include "base/optional.h"
Yao Xiaoa927c072020-11-12 06:49:279#include "base/time/time.h"
Yao Xiao70c52c0a2020-11-05 17:23:0310#include "base/version.h"
Yao Xiaoca909b92020-11-23 23:51:4511#include "components/prefs/prefs_export.h"
Yao Xiaodcda72b2020-05-18 23:05:3712
13#include <stdint.h>
14
15#include <string>
16#include <unordered_set>
17
Yao Xiaoca909b92020-11-23 23:51:4518class PrefRegistrySimple;
19class PrefService;
20
Yao Xiaodcda72b2020-05-18 23:05:3721namespace 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
26class FlocId {
27 public:
Yao Xiao70c52c0a2020-11-05 17:23:0328 static uint64_t SimHashHistory(
Yao Xiaodcda72b2020-05-18 23:05:3729 const std::unordered_set<std::string>& domains);
30
31 FlocId();
Yao Xiaodcda72b2020-05-18 23:05:3732
Yao Xiaoa927c072020-11-12 06:49:2733 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 Xiaodcda72b2020-05-18 23:05:3739 ~FlocId();
40 FlocId& operator=(const FlocId& id);
41 FlocId& operator=(FlocId&& id);
42
Yao Xiao95d20682020-08-18 20:30:2143 bool operator==(const FlocId& other) const;
44 bool operator!=(const FlocId& other) const;
45
Yao Xiaodcda72b2020-05-18 23:05:3746 bool IsValid() const;
Yao Xiaodcda72b2020-05-18 23:05:3747
Yao Xiao70c52c0a2020-11-05 17:23:0348 // 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 Xiaoa927c072020-11-12 06:49:2751 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 Xiaodcda72b2020-05-18 23:05:3756
Yao Xiaoca909b92020-11-23 23:51:4557 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 Xiaoebed8c12020-09-26 07:51:2066 private:
Yao Xiaodcda72b2020-05-18 23:05:3767 base::Optional<uint64_t> id_;
Yao Xiao70c52c0a2020-11-05 17:23:0368
Yao Xiaoa927c072020-11-12 06:49:2769 // 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 Xiao70c52c0a2020-11-05 17:23:0374 // The main version (i.e. 1st int) of the sorting lsh component version.
75 uint32_t sorting_lsh_version_ = 0;
Yao Xiaodcda72b2020-05-18 23:05:3776};
77
78} // namespace federated_learning
79
80#endif // COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_