File: randomized_encoder.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 5,202 bytes parent folder | download | duplicates (5)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_CROWDSOURCING_RANDOMIZED_ENCODER_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_CROWDSOURCING_RANDOMIZED_ENCODER_H_

#include <optional>
#include <string>
#include <string_view>

#include "base/memory/raw_ptr.h"
#include "components/autofill/core/browser/proto/server.pb.h"
#include "components/autofill/core/common/signatures.h"

class PrefService;

namespace autofill {

// Encodes string values using the differential-privacy scheme as described
// in go/autofill-metadata-upload (Google internal link).
class RandomizedEncoder {
 public:
  // Form-level data-type identifiers.
  static constexpr char kFormId[] = "form-id";
  static constexpr char kFormName[] = "form-name";
  static constexpr char kFormAction[] = "form-action";
  static constexpr char kFormUrl[] = "form-url";
  static constexpr char kFormCssClass[] = "form-css-class";
  static constexpr char kFormButtonTitles[] = "button-titles";

  // Field-level data-type identifiers.
  static constexpr char kFieldId[] = "field-id";
  static constexpr char kFieldName[] = "field-name";
  static constexpr char kFieldControlType[] = "field-control-type";
  static constexpr char kFieldLabel[] = "field-label";
  static constexpr char kFieldAriaLabel[] = "field-aria-label";
  static constexpr char kFieldAriaDescription[] = "field-aria-description";
  static constexpr char kFieldCssClasses[] = "field-css-classes";
  static constexpr char kFieldPlaceholder[] = "field-placeholder";
  static constexpr char kFieldInitialValueHash[] = "field-initial-hash-value";
  static constexpr char kFieldAutocomplete[] = "field-autocomplete";
  static constexpr char kFieldPattern[] = "field-pattern";
  static constexpr char kFieldMaxLength[] = "field-max-length";
  static constexpr char kFieldSelectOptionText[] = "field-select-option-text";
  static constexpr char kFieldSelectOptionValue[] = "field-select-option-value";

  // Copy of components/unified_consent/pref_names.cc
  // We could not use the constant from components/unified_constants because of
  // a circular dependency.
  // TODO(crbug.com/40570965): resolve circular dependency and remove
  // hardcoded constant
  static constexpr char kUrlKeyedAnonymizedDataCollectionEnabled[] =
      "url_keyed_anonymized_data_collection.enabled";

  // Factory Function
  static std::optional<RandomizedEncoder> Create(PrefService* pref_service);

  RandomizedEncoder(std::string seed,
                    AutofillRandomizedValue_EncodingType encoding_type,
                    bool anonymous_url_collection_is_enabled);
  RandomizedEncoder(RandomizedEncoder&&);
  RandomizedEncoder& operator=(RandomizedEncoder&&);
  RandomizedEncoder(const RandomizedEncoder&) = delete;
  RandomizedEncoder& operator=(const RandomizedEncoder&) = delete;
  ~RandomizedEncoder();

  // Encode |data_value| using this instance's |encoding_type_|.
  // If |data_type!=FORM_URL|, the output value's length is limited by
  // |kEncodedChunkLengthInBytes|.
  std::string Encode(FormSignature form_signature,
                     FieldSignature field_signature,
                     std::string_view data_type,
                     std::string_view data_value) const;
  // Used for testing, converts |data_value| to UTF-8 and calls Encode().
  std::string EncodeForTesting(FormSignature form_signature,
                               FieldSignature field_signature,
                               std::string_view data_type,
                               std::u16string_view data_value) const;

  AutofillRandomizedValue_EncodingType encoding_type() const {
    return encoding_type_;
  }

  bool AnonymousUrlCollectionIsEnabled() const {
    return anonymous_url_collection_is_enabled_;
  }

 protected:
  // Get the pseudo-random string to use at the coin bit-field. This function
  // is internal, but exposed here to facilitate testing.
  std::string GetCoins(FormSignature form_signature,
                       FieldSignature field_signature,
                       std::string_view data_type,
                       int encoding_length) const;

  // Get the pseudo-random string to use at the noise bit-field. This function
  // is internal, but exposed here to facilitate testing.
  std::string GetNoise(FormSignature form_signature,
                       FieldSignature field_signature,
                       std::string_view data_type,
                       int encoding_length) const;

  // For |data_type==FORM_URL|, returns required chunk count to fit
  // |data_value|, but max |kMaxChunks|. Otherwise, returns 1.
  int GetChunkCount(std::string_view data_value,
                    std::string_view data_type) const;

 private:
  struct EncodingInfo {
    size_t chunk_length_in_bytes;
    size_t bit_offset;
    size_t bit_stride;
  };

  const EncodingInfo& encoding_info() const;

  std::string seed_;
  AutofillRandomizedValue_EncodingType encoding_type_;
  bool anonymous_url_collection_is_enabled_;
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_CROWDSOURCING_RANDOMIZED_ENCODER_H_