summaryrefslogtreecommitdiff
path: root/chromium/components/federated_learning/floc_id.h
blob: 7e3154e372a6649e6f4d586191ba5e43e7195494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_
#define COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_

#include "base/optional.h"
#include "base/time/time.h"
#include "base/version.h"

#include <stdint.h>

#include <string>
#include <unordered_set>

namespace federated_learning {

// ID used to represent a cohort of people with similar browsing habits. For
// more context, see the explainer at
// https://github.com/jkarlin/floc/blob/master/README.md
class FlocId {
 public:
  static uint64_t SimHashHistory(
      const std::unordered_set<std::string>& domains);

  FlocId();

  explicit FlocId(uint64_t id,
                  base::Time history_begin_time,
                  base::Time history_end_time,
                  uint32_t sorting_lsh_version);

  FlocId(const FlocId& id);
  ~FlocId();
  FlocId& operator=(const FlocId& id);
  FlocId& operator=(FlocId&& id);

  bool operator==(const FlocId& other) const;
  bool operator!=(const FlocId& other) const;

  bool IsValid() const;

  // The id, followed by the chrome floc version, followed by the async floc
  // component versions (i.e. model and sorting-lsh). This is the format to be
  // exposed to the JS API. Precondition: |id_| must be valid.
  std::string ToStringForJsApi() const;

  base::Time history_begin_time() const { return history_begin_time_; }

  base::Time history_end_time() const { return history_end_time_; }

 private:
  base::Optional<uint64_t> id_;

  // The time range of the actual history used to compute the floc. This should
  // always be within the time range of each history query.
  base::Time history_begin_time_;
  base::Time history_end_time_;

  // The main version (i.e. 1st int) of the sorting lsh component version.
  uint32_t sorting_lsh_version_ = 0;
};

}  // namespace federated_learning

#endif  // COMPONENTS_FEDERATED_LEARNING_FLOC_ID_H_