summaryrefslogtreecommitdiff
path: root/chromium/components/federated_learning/floc_id.cc
blob: 38caf081581a1d6a93dca095e6e2511d1b15abeb (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
// 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.

#include "components/federated_learning/floc_id.h"

#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "components/federated_learning/sim_hash.h"

namespace federated_learning {

namespace {

constexpr char kFlocVersion[] = "1.0.0";

// This is only for experimentation and won't be served to websites.
constexpr size_t kNumberOfBitsInFloc = 50;
static_assert(kNumberOfBitsInFloc > 0 &&
                  kNumberOfBitsInFloc <= std::numeric_limits<uint64_t>::digits,
              "Number of bits in the floc id must be greater than 0 and no "
              "greater than 64.");

}  // namespace

// static
FlocId FlocId::CreateFromHistory(
    const std::unordered_set<std::string>& domains) {
  return FlocId(SimHashStrings(domains, kNumberOfBitsInFloc));
}

FlocId::FlocId() = default;

FlocId::FlocId(uint64_t id) : id_(id) {}

FlocId::FlocId(const FlocId& id) = default;

FlocId::~FlocId() = default;

FlocId& FlocId::operator=(const FlocId& id) = default;

FlocId& FlocId::operator=(FlocId&& id) = default;

bool FlocId::IsValid() const {
  return id_.has_value();
}

uint64_t FlocId::ToUint64() const {
  DCHECK(id_.has_value());
  return id_.value();
}

std::string FlocId::ToDebugHeaderValue() const {
  if (!id_.has_value())
    return "null";
  return ToHeaderValue();
}

std::string FlocId::ToHeaderValue() const {
  DCHECK(id_.has_value());
  return base::StrCat({base::NumberToString(id_.value()), ".", kFlocVersion});
}

}  // namespace federated_learning