File: compose_text_usage_logger.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (220 lines) | stat: -rw-r--r-- 7,705 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/compose/compose_text_usage_logger.h"

#include <algorithm>
#include <bit>
#include <cstdint>

#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/signatures.h"
#include "components/compose/core/browser/compose_features.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"

namespace compose {
DOCUMENT_USER_DATA_KEY_IMPL(ComposeTextUsageLogger);

namespace {
constexpr int MAX_FIELD_METRIC_COUNT = 100;
// Note: Although OnAfterTextFieldValueChanged is only called for user
// actions like typing or pastes, we need to handle the case where the
// user edits the field after it was modified by the page. In this case,
// we'd rather be conservative when recording text changes as user typing. If
// the text length changes by more than this number, we'll ignore the change.
constexpr int MAX_CHARS_TYPED_AT_ONCE = 10;
constexpr base::TimeDelta EDITING_TIME_IDLE_TIMEOUT = base::Seconds(5);

int64_t CountWords(const std::u16string& value) {
  int64_t words = 0;
  base::String16Tokenizer tokenizer(
      value, u"", base::String16Tokenizer::WhitespacePolicy::kSkipOver);
  while (tokenizer.GetNext()) {
    ++words;
  }
  return words;
}

size_t RoundDownToPowerOfTwo(int64_t n) {
  // We use -1 as a special value indicating unknown.
  if (n < 0) {
    return -1;
  }
  return std::bit_floor<uint64_t>(n);
}

}  // namespace

ComposeTextUsageLogger::FieldMetrics::FieldMetrics() noexcept = default;
ComposeTextUsageLogger::FieldMetrics::~FieldMetrics() = default;

ComposeTextUsageLogger::ComposeTextUsageLogger(content::RenderFrameHost* rfh)
    : content::DocumentUserData<ComposeTextUsageLogger>(rfh) {
  autofill::ContentAutofillDriver* driver =
      autofill::ContentAutofillDriver::GetForRenderFrameHost(rfh);
  if (driver) {
    driver->GetAutofillManager().AddObserver(this);
  }
}

ComposeTextUsageLogger::~ComposeTextUsageLogger() {
  Reset();

  autofill::ContentAutofillDriver* driver =
      autofill::ContentAutofillDriver::GetForRenderFrameHost(
          &render_frame_host());
  if (driver) {
    driver->GetAutofillManager().RemoveObserver(this);
  }
}

void ComposeTextUsageLogger::OnAfterTextFieldValueChanged(
    autofill::AutofillManager& manager,
    autofill::FormGlobalId form,
    autofill::FieldGlobalId field,
    const std::u16string& text_value) {
  autofill::FormType form_type = autofill::FormType::kUnknownFormType;
  int64_t form_control_type = -1;
  autofill::FieldSignature field_signature;
  autofill::FormSignature form_signature;
  autofill::FormStructure* form_structure = manager.FindCachedFormById(form);
  bool is_long_field = false;
  if (form_structure) {
    form_signature = form_structure->form_signature();
    const autofill::AutofillField* field_data =
        form_structure->GetFieldById(field);
    if (field_data) {
      form_type = FieldTypeGroupToFormType(field_data->Type().group());
      form_control_type = static_cast<int64_t>(field_data->form_control_type());

      switch (field_data->form_control_type()) {
        case autofill::FormControlType::kContentEditable:
        case autofill::FormControlType::kTextArea:
          is_long_field = true;
          break;
        default:
          break;
      }
      field_signature = field_data->GetFieldSignature();
    }
  }

  // The page UKM source ID should not change while this object is alive. Keep
  // a copy of it stored so that we can log safely in the destructor.
  DCHECK(source_id_ == ukm::SourceId() ||
         source_id_ == render_frame_host().GetPageUkmSourceId())
      << "source_id shouldn't change";

  if (source_id_ == ukm::SourceId()) {
    source_id_ = render_frame_host().GetPageUkmSourceId();
  }

  if (field_metrics_.size() >= MAX_FIELD_METRIC_COUNT) {
    Reset();
  }

  FieldMetrics& metrics = field_metrics_[field];
  if (!metrics.initialized) {
    if (text_value.length() > MAX_CHARS_TYPED_AT_ONCE) {
      metrics.initial_text = text_value;
      metrics.previous_text_length = text_value.length();
    }
    metrics.initialized = true;
  } else {
    base::TimeDelta additional_editing_time =
        std::min(base::TimeTicks::Now() - metrics.last_update_time,
                 EDITING_TIME_IDLE_TIMEOUT);
    metrics.editing_time += additional_editing_time;
  }
  metrics.last_update_time = base::TimeTicks::Now();

  switch (form_type) {
    case autofill::FormType::kUnknownFormType:
      break;
    case autofill::FormType::kAddressForm:
    case autofill::FormType::kLoyaltyCardForm:
      metrics.is_autofill_field_type = true;
      break;
    case autofill::FormType::kStandaloneCvcForm:
    case autofill::FormType::kCreditCardForm:
    case autofill::FormType::kPasswordForm:
      metrics.sensitive_field = true;
      metrics.is_autofill_field_type = true;
      break;
  }

  // Note that field_data->value doesn't have the current value, so we use
  // text_value instead.
  const int64_t new_length = text_value.size();
  const int64_t delta = new_length - metrics.previous_text_length;
  if (delta > 0 && delta <= MAX_CHARS_TYPED_AT_ONCE) {
    metrics.estimate_typed_characters += delta;
  }

  metrics.form_control_type = form_control_type;

  metrics.is_long_field = is_long_field;
  metrics.field_signature = field_signature;
  metrics.form_signature = form_signature;

  metrics.previous_text_length = text_value.length();
  metrics.final_text = std::move(text_value);
}

void ComposeTextUsageLogger::Reset() {
  if (field_metrics_.empty()) {
    return;
  }

  for (const auto& entry : field_metrics_) {
    const FieldMetrics& metrics = entry.second;
    if (metrics.final_text.size() == 0) {
      continue;
    }

    int64_t typed_chars = -1;
    int64_t typed_words = -1;
    if (!metrics.sensitive_field) {
      int64_t size_change = static_cast<int64_t>(metrics.final_text.size()) -
                            static_cast<int64_t>(metrics.initial_text.size());
      typed_chars = std::min(metrics.estimate_typed_characters, size_change);

      typed_words = std::max<int64_t>(
          0, CountWords(metrics.final_text) - CountWords(metrics.initial_text));
    }

    ukm::builders::Compose_TextElementUsage builder(source_id_);
    builder.SetAutofillFormControlType(metrics.form_control_type)
        .SetTypedCharacterCount(RoundDownToPowerOfTwo(typed_chars))
        .SetTypedWordCount(RoundDownToPowerOfTwo(typed_words))
        .SetIsAutofillFieldType(metrics.is_autofill_field_type);

    if (base::FeatureList::IsEnabled(features::kEnableAdditionalTextMetrics)) {
      builder
          .SetFieldSignature(
              autofill::HashFieldSignature(metrics.field_signature))
          .SetFormSignature(autofill::HashFormSignature(metrics.form_signature))
          .SetEditingTime(ukm::GetExponentialBucketMinForUserTiming(
              metrics.editing_time.InSeconds()));
      if (metrics.is_long_field) {
        base::UmaHistogramCustomTimes(
            "Compose.TextElementUsage.LongField.EditingTime",
            metrics.editing_time, base::Seconds(2), base::Minutes(20), 50);
      }
    }

    builder.Record(ukm::UkmRecorder::Get());
  }
  field_metrics_.clear();
}

}  // namespace compose