File: proactive_nudge_tracker.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (640 lines) | stat: -rw-r--r-- 22,512 bytes parent folder | download | duplicates (4)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright 2024 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/proactive_nudge_tracker.h"

#include <memory>

#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "chrome/browser/compose/proto/compose_optimization_guide.pb.h"
#include "components/autofill/core/common/signatures.h"
#include "components/compose/core/browser/compose_metrics.h"
#include "components/compose/core/browser/config.h"
#include "components/segmentation_platform/public/constants.h"
#include "components/segmentation_platform/public/input_context.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace compose {

namespace {
using segmentation_platform::processing::ProcessedValue;

scoped_refptr<segmentation_platform::InputContext> PopulateInputContextForField(
    const ProactiveNudgeTracker::Signals& signals,
    const compose::ComposeHintMetadata& compose_hint) {
  auto input_context =
      base::MakeRefCounted<segmentation_platform::InputContext>();

  input_context->metadata_args.emplace(
      "field_max_length",
      ProcessedValue::FromFloat(signals.field.max_length()));
  input_context->metadata_args.emplace(
      "field_width_pixels",
      ProcessedValue::FromFloat(signals.field.bounds().width()));
  input_context->metadata_args.emplace(
      "field_height_pixels",
      ProcessedValue::FromFloat(signals.field.bounds().height()));
  input_context->metadata_args.emplace(
      "field_form_control_type", ProcessedValue::FromFloat(base::to_underlying(
                                     signals.field.form_control_type())));
  input_context->metadata_args.emplace(
      "total_field_count",
      ProcessedValue::FromFloat(signals.form.fields().size()));

  int multiline_field_count = 0;
  for (const auto& f : signals.form.fields()) {
    if (f.form_control_type() == autofill::FormControlType::kTextArea ||
        f.form_control_type() == autofill::FormControlType::kContentEditable) {
      ++multiline_field_count;
    }
  }
  input_context->metadata_args.emplace(
      "multiline_field_count",
      ProcessedValue::FromFloat(multiline_field_count));
  input_context->metadata_args.emplace(
      "time_spent_on_page",
      ProcessedValue::FromFloat(
          (base::TimeTicks::Now() - signals.page_change_time).InSecondsF()));

  input_context->metadata_args.emplace(
      "field_signature",
      ProcessedValue(autofill::HashFieldSignature(
          autofill::CalculateFieldSignatureForField(signals.field))));
  input_context->metadata_args.emplace(
      "form_signature", ProcessedValue(autofill::HashFormSignature(
                            autofill::CalculateFormSignature(signals.form))));

  input_context->metadata_args.emplace("page_url",
                                       ProcessedValue(signals.page_url));
  input_context->metadata_args.emplace(
      "origin", ProcessedValue(signals.page_origin.GetURL()));

  input_context->metadata_args.emplace(
      "field_value", ProcessedValue(base::UTF16ToUTF8(signals.field.value())));
  input_context->metadata_args.emplace(
      "field_selected_text",
      ProcessedValue(base::UTF16ToUTF8(signals.field.selected_text())));
  input_context->metadata_args.emplace(
      "field_placeholder_text",
      ProcessedValue(base::UTF16ToUTF8(signals.field.placeholder())));
  input_context->metadata_args.emplace(
      "field_label", ProcessedValue(base::UTF16ToUTF8(signals.field.label())));
  input_context->metadata_args.emplace(
      "field_aria_label",
      ProcessedValue(base::UTF16ToUTF8(signals.field.aria_label())));
  input_context->metadata_args.emplace(
      "field_aria_description",
      ProcessedValue(base::UTF16ToUTF8(signals.field.aria_description())));

  for (auto& pair : compose_hint.model_params()) {
    input_context->metadata_args.emplace(pair.first,
                                         ProcessedValue(pair.second));
  }
  return input_context;
}

}  // namespace

ProactiveNudgeTracker::Signals::Signals() = default;
ProactiveNudgeTracker::Signals::~Signals() = default;
ProactiveNudgeTracker::Signals::Signals(Signals&&) = default;
ProactiveNudgeTracker::Signals& ProactiveNudgeTracker::Signals::operator=(
    Signals&&) = default;

// Tracks user engagement as a result of the nudge.
class ProactiveNudgeTracker::EngagementTracker {
 public:
  EngagementTracker(
      autofill::FieldGlobalId field_global_id,
      segmentation_platform::TrainingRequestId training_request_id,
      ProactiveNudgeTracker* nudge_tracker)
      : field_global_id_(field_global_id),
        training_request_id_(training_request_id),
        nudge_tracker_(nudge_tracker) {}
  EngagementTracker(const EngagementTracker&) = delete;
  EngagementTracker& operator=(const EngagementTracker&) = delete;

  ~EngagementTracker() {
    ReportIfFirst(ProactiveNudgeDerivedEngagement::kIgnored);
  }

  void ComposeSessionCompleted(ComposeSessionCloseReason session_close_reason,
                               const compose::ComposeSessionEvents& events) {
    if (events.inserted_results) {
      ReportIfFirst(
          ProactiveNudgeDerivedEngagement::kAcceptedComposeSuggestion);
    } else if (events.compose_requests_count > 0) {
      ReportIfFirst(
          ProactiveNudgeDerivedEngagement::kGeneratedComposeSuggestion);
    } else {
      ReportIfFirst(ProactiveNudgeDerivedEngagement::kOpenedComposeMinimalUse);
    }
  }

  void UserDisabledNudge(bool single_site_only) {
    if (single_site_only) {
      ReportIfFirst(
          ProactiveNudgeDerivedEngagement::kNudgeDisabledOnSingleSite);
    } else {
      ReportIfFirst(ProactiveNudgeDerivedEngagement::kNudgeDisabledOnAllSites);
    }
  }

 private:
  void ReportIfFirst(ProactiveNudgeDerivedEngagement engagement) {
    if (reported_) {
      return;
    }
    nudge_tracker_->CollectTrainingData(training_request_id_, engagement);
    reported_ = true;
  }

  bool reported_ = false;
  autofill::FieldGlobalId field_global_id_;
  segmentation_platform::TrainingRequestId training_request_id_;
  raw_ptr<ProactiveNudgeTracker> nudge_tracker_;
};

ProactiveNudgeTracker::State::State() = default;
ProactiveNudgeTracker::State::~State() = default;

float ProactiveNudgeTracker::Delegate::SegmentationFallbackShowResult() {
  return base::RandFloat();
}

float ProactiveNudgeTracker::Delegate::SegmentationForceShowResult() {
  return base::RandFloat();
}

ProactiveNudgeTracker::ProactiveNudgeTracker(
    segmentation_platform::SegmentationPlatformService* segmentation_service,
    Delegate* delegate)
    : segmentation_service_(segmentation_service), delegate_(delegate) {}

void ProactiveNudgeTracker::StartObserving(content::WebContents* web_contents) {
  if (!SegmentationStateIsValid()) {
    // Unable to show proactive nudge if configuration is not consistent.
    // Todo(b/343281445): Use fallback strategy if state is invalid.
    return;
  }
  autofill_managers_observation_.Observe(
      web_contents, autofill::ScopedAutofillManagersObservation::
                        InitializationPolicy::kObservePreexistingManagers);
}

ProactiveNudgeTracker::~ProactiveNudgeTracker() {
  // Destroy all engagement trackers first to ensure CollectTrainingData() is
  // called before other parts of this class are destroyed.
  engagement_trackers_.clear();
}

bool ProactiveNudgeTracker::ProactiveNudgeRequestedForFormField(
    Signals signals) {
  if (!MatchesCurrentField(signals.field.renderer_form_id(),
                           signals.field.global_id())) {
    ResetState();
    state_ = std::make_unique<State>();
    state_->signals = std::move(signals);
  }

  if (state_->show_state == ShowState::kShown) {
    return false;
  }

  nudge_currently_requested_ = true;
  UpdateStateForCurrentFormField();
  nudge_currently_requested_ = false;
  return state_->show_state == ShowState::kShown;
}

bool ProactiveNudgeTracker::IsTimerRunning() {
  return state_ && state_->timer.IsRunning();
}

void ProactiveNudgeTracker::FocusChangedInPage() {
  ResetState();
}

void ProactiveNudgeTracker::Clear() {
  engagement_trackers_.clear();
  seen_fields_.clear();
  ResetState();
}

void ProactiveNudgeTracker::OnAfterFocusOnFormField(
    autofill::AutofillManager& manager,
    autofill::FormGlobalId form,
    autofill::FieldGlobalId field) {
  DVLOG(2) << "ProactiveNudgeTracker: OnAfterFocusOnFormField";
  // If this focus is on the current field, we are (presumably) already focused
  // and this is a no-op.
  if (MatchesCurrentField(form, field) || state_ == nullptr) {
    return;
  }

  // Now we should transition to the kInitial state.
  ResetState();
}

void ProactiveNudgeTracker::OnAfterTextFieldValueChanged(
    autofill::AutofillManager& manager,
    autofill::FormGlobalId form,
    autofill::FieldGlobalId field,
    const std::u16string& text_value) {
  if (!MatchesCurrentField(form, field)) {
    return;
  }
  ++state_->text_change_count;
  UpdateStateForCurrentFormField();
}

void ProactiveNudgeTracker::OnAfterCaretMovedInFormField(
    autofill::AutofillManager& manager,
    const autofill::FormGlobalId& form,
    const autofill::FieldGlobalId& field,
    const std::u16string& selection,
    const gfx::Rect& caret_bounds) {
  if (!MatchesCurrentField(form, field)) {
    return;
  }

  bool selection_valid =
      GetComposeConfig().selection_nudge_enabled &&
      selection.size() >= GetComposeConfig().selection_nudge_length;

  if (IsTimerRunning() && state_->selection_nudge_requested &&
      !selection_valid) {
    // Cancel the timer if the selection is no longer valid.
    state_->timer_canceled = true;
  } else {
    state_->selection_nudge_requested = selection_valid;
    if (IsTimerRunning()) {
      // Extend the timer if it is currently running. This will restart with
      // the correct delay if the state has changed.
      StartOrRestartTimer();
    }
  }
  UpdateStateForCurrentFormField();
}

bool ProactiveNudgeTracker::SegmentationStateIsValid() {
  return !compose::GetComposeConfig().proactive_nudge_segmentation ||
         segmentation_service_ != nullptr;
}

void ProactiveNudgeTracker::ResetState() {
  DVLOG(2) << "ProactiveNudgeTracker: ResetState";
  weak_ptr_factory_.InvalidateWeakPtrs();
  state_.reset();
}

void ProactiveNudgeTracker::UpdateStateForCurrentFormField() {
  while (auto new_state = CheckForStateTransition()) {
    TransitionToState(new_state.value());
  }
}

std::optional<ProactiveNudgeTracker::ShowState>
ProactiveNudgeTracker::CheckForStateTransition() {
  switch (state_->show_state) {
    case ShowState::kInitial:
      // Block if the cached result should not show the nudge.
      if (!CachedSegmentationResult().value_or(true)) {
        return ShowState::kBlockedBySegmentation;
      }
      if (CanStartFocusTimer() || CanStartTextSettledTimer() ||
          CanStartSelectionTimer()) {
        return ShowState::kWaitingForTimerToStop;
      }
      // Remain in initial state until any delay timer can be started.
      return std::nullopt;
    case ShowState::kWaitingForTimerToStop:
      if (state_->timer_canceled) {
        return ShowState::kTimerCanceled;
      }
      if (!IsTimerRunning()) {
        return SegmentationStateIsValid() ? ShowState::kWaitingForSegmentation
                                          : ShowState::kBlockedBySegmentation;
      }
      // Continue to wait if the timer is running or not canceled.
      return std::nullopt;
    case ShowState::kWaitingForSegmentation:
      // Use cached segmentation result if possible.
      if (auto result = CachedSegmentationResult(); result.has_value()) {
        return result.value() ? ShowState::kWaitingForProactiveNudgeRequest
                              : ShowState::kBlockedBySegmentation;
      }
      if (state_->segmentation_result.has_value()) {
        bool segmentation_succeeded =
            !state_->segmentation_result->ordered_labels.empty() &&
            state_->segmentation_result->ordered_labels[0] ==
                segmentation_platform::kComposePrmotionLabelShow;
        seen_fields_.emplace(state_->signals.field.global_id(),
                             segmentation_succeeded);
        return segmentation_succeeded
                   ? ShowState::kWaitingForProactiveNudgeRequest
                   : ShowState::kBlockedBySegmentation;
      }
      return std::nullopt;
    case ShowState::kWaitingForProactiveNudgeRequest:
      if (nudge_currently_requested_) {
        return ShowState::kShown;
      }
      return std::nullopt;
    case ShowState::kTimerCanceled:
      if (state_->timer_canceled) {
        return std::nullopt;
      }
      return ShowState::kShown;
    case ShowState::kShown:
      if (CanStartSelectionTimer()) {
        // Start waiting for the selection delay timer.
        return ShowState::kWaitingForTimerToStop;
      }
      return std::nullopt;
    case ShowState::kBlockedBySegmentation:
      return std::nullopt;
  }
}

void ProactiveNudgeTracker::TransitionToState(ShowState new_show_state) {
  switch (new_show_state) {
    case ShowState::kInitial:
      NOTREACHED();
    case ShowState::kWaitingForTimerToStop:
      BeginWaitingForTimerToStop();
      break;
    case ShowState::kWaitingForSegmentation:
      BeginSegmentation();
      break;
    case ShowState::kWaitingForProactiveNudgeRequest:
      BeginWaitingForProactiveNudgeRequest();
      break;
    case ShowState::kBlockedBySegmentation:
      BeginBlockedBySegmentation();
      break;
    case ShowState::kShown:
      BeginShown();
      break;
    case ShowState::kTimerCanceled:
      BeginTimerCanceled();
      break;
  }
  state_->show_state = new_show_state;
}

void ProactiveNudgeTracker::BeginWaitingForTimerToStop() {
  StartOrRestartTimer();
}

bool ProactiveNudgeTracker::CanStartFocusTimer() {
  if (!GetComposeConfig().proactive_nudge_enabled) {
    return false;
  }
  if (GetComposeConfig().proactive_nudge_field_per_navigation &&
      CachedSegmentationResult().has_value()) {
    return false;
  }
  return GetComposeConfig().proactive_nudge_focus_delay > base::Seconds(0);
}

bool ProactiveNudgeTracker::CanStartTextSettledTimer() {
  if (!GetComposeConfig().proactive_nudge_enabled) {
    return false;
  }
  if (GetComposeConfig().proactive_nudge_field_per_navigation &&
      CachedSegmentationResult().has_value()) {
    return false;
  }
  if (state_->text_change_count <
      GetComposeConfig().proactive_nudge_text_change_count) {
    return false;
  }
  return GetComposeConfig().proactive_nudge_text_settled_delay >
         base::Seconds(0);
}

bool ProactiveNudgeTracker::CanStartSelectionTimer() {
  if (!state_ || !CachedSegmentationResult().value_or(true)) {
    return false;
  }
  if (GetComposeConfig().selection_nudge_once_per_focus &&
      state_->selection_nudge_shown) {
    return false;
  }
  return GetComposeConfig().selection_nudge_delay > base::Seconds(0) &&
         state_->selection_nudge_requested;
}

void ProactiveNudgeTracker::StartOrRestartTimer() {
  if (!state_) {
    return;
  }
  if (state_->timer.IsRunning()) {
    state_->timer.Stop();
  }

  base::TimeDelta delay = GetComposeConfig().proactive_nudge_focus_delay;
  if (CanStartSelectionTimer()) {
    delay = GetComposeConfig().selection_nudge_delay;
  } else if (CanStartTextSettledTimer()) {
    delay = GetComposeConfig().proactive_nudge_text_settled_delay;
  }

  if (delay.is_zero()) {
    return;
  }
  state_->timer.Start(FROM_HERE, delay, this,
                      &ProactiveNudgeTracker::ShowTimerElapsed);
}

void ProactiveNudgeTracker::BeginTimerCanceled() {
  if (!state_) {
    return;
  }
  state_->timer.Stop();
  state_->selection_nudge_requested = false;
  state_->timer_canceled = false;
}

void ProactiveNudgeTracker::BeginSegmentation() {
  if (!compose::GetComposeConfig().proactive_nudge_segmentation) {
    autofill::FieldGlobalId field_global_id = state_->signals.field.global_id();
    seen_fields_.emplace(field_global_id, true);
    return;
  }
  segmentation_platform::PredictionOptions options;
  options.on_demand_execution = true;

  segmentation_service_->GetClassificationResult(
      segmentation_platform::kComposePromotionKey, options,
      PopulateInputContextForField(state_->signals,
                                   delegate_->GetComposeHintMetadata()),
      base::BindOnce(&ProactiveNudgeTracker::GotClassificationResult,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ProactiveNudgeTracker::BeginWaitingForProactiveNudgeRequest() {
  if (state_->segmentation_result &&
      (state_->segmentation_result_ignored_for_training ||
       compose::GetComposeConfig()
           .proactive_nudge_always_collect_training_data)) {
    engagement_trackers_[state_->signals.field.global_id()] =
        std::make_unique<EngagementTracker>(
            state_->signals.field.global_id(),
            state_->segmentation_result->request_id, this);
  }
  if (nudge_currently_requested_) {
    // If no async calls were made the first request is still active and there
    // is no need to request the nudge again.
    return;
  }
  compose::ComposeEntryPoint entry_point =
      state_->selection_nudge_requested
          ? compose::ComposeEntryPoint::kSelectionNudge
          : compose::ComposeEntryPoint::kProactiveNudge;
  delegate_->ShowProactiveNudge(state_->signals.field.renderer_form_id(),
                                state_->signals.field.global_id(), entry_point);
}

void ProactiveNudgeTracker::BeginBlockedBySegmentation() {
  if (state_->show_state != ShowState::kWaitingForSegmentation) {
    return;
  }

  if (state_->selection_nudge_requested) {
    state_->selection_nudge_requested = false;
    return;
  }

  delegate_->GetPageUkmTracker()->ComposeProactiveNudgeShouldShow();
  compose::LogComposeProactiveNudgeShowStatus(
      compose::ComposeShowStatus::kProactiveNudgeBlockedBySegmentationPlatform);
}

void ProactiveNudgeTracker::BeginShown() {
  if (!state_ ||
      state_->show_state != ShowState::kWaitingForProactiveNudgeRequest) {
    return;
  }

  if (state_->selection_nudge_requested) {
    state_->selection_nudge_requested = false;
    state_->selection_nudge_shown = true;
    compose::LogComposeSelectionNudgeCtr(
        compose::ComposeNudgeCtrEvent::kNudgeDisplayed);
    return;
  }

  compose::LogComposeProactiveNudgeCtr(
      compose::ComposeNudgeCtrEvent::kNudgeDisplayed);
  compose::LogComposeProactiveNudgeShowStatus(
      compose::ComposeShowStatus::kShouldShow);
  delegate_->GetPageUkmTracker()->ProactiveNudgeShown();
  delegate_->GetPageUkmTracker()->ComposeProactiveNudgeShouldShow();
}

void ProactiveNudgeTracker::ShowTimerElapsed() {
  DVLOG(2) << "ProactiveNudgeTracker: ShowTimerElapsed";
  // If we are not waiting for the timer, the elapsed timer is stale and should
  // be ignored.
  if (!state_ || state_->show_state != ShowState::kWaitingForTimerToStop) {
    return;
  }
  UpdateStateForCurrentFormField();
}


void ProactiveNudgeTracker::GotClassificationResult(
    const segmentation_platform::ClassificationResult& result) {
  if (!state_ || state_->show_state != ShowState::kWaitingForSegmentation) {
    return;
  }

  state_->segmentation_result = result;

  switch (state_->segmentation_result->status) {
    case segmentation_platform::PredictionStatus::kFailed:
    case segmentation_platform::PredictionStatus::kNotReady:
      if (delegate_->SegmentationFallbackShowResult() <
          compose::GetComposeConfig().proactive_nudge_show_probability) {
        // Override default DontShow decision.
        state_->segmentation_result->ordered_labels.emplace_back(
            segmentation_platform::kComposePrmotionLabelShow);
      }
      break;
    case segmentation_platform::PredictionStatus::kSucceeded:
      if (delegate_->SegmentationForceShowResult() <
          compose::GetComposeConfig().proactive_nudge_force_show_probability) {
        state_->segmentation_result->ordered_labels = {
            segmentation_platform::kComposePrmotionLabelShow};
        state_->segmentation_result_ignored_for_training = true;
      }
      break;
  }

  UpdateStateForCurrentFormField();
}

void ProactiveNudgeTracker::CollectTrainingData(
    const segmentation_platform::TrainingRequestId training_request_id,
    ProactiveNudgeDerivedEngagement engagement) {
  segmentation_platform::TrainingLabels training_labels;
  base::UmaHistogramEnumeration("Compose.ProactiveNudge.DerivedEngagement",
                                engagement);
  training_labels.output_metric =
      std::make_pair("Compose.ProactiveNudge.DerivedEngagement",
                     static_cast<base::HistogramBase::Sample32>(engagement));
  ukm::SourceId source =
      state_ ? state_->signals.ukm_source_id : ukm::kInvalidSourceId;
  segmentation_service_->CollectTrainingData(
      segmentation_platform::proto::SegmentId::
          OPTIMIZATION_TARGET_SEGMENTATION_COMPOSE_PROMOTION,
      training_request_id, source, training_labels, base::DoNothing());
}

bool ProactiveNudgeTracker::MatchesCurrentField(autofill::FormGlobalId form,
                                                autofill::FieldGlobalId field) {
  return state_ && state_->signals.field.renderer_form_id() == form &&
         state_->signals.field.global_id() == field;
}

void ProactiveNudgeTracker::ComposeSessionCompleted(
    autofill::FieldGlobalId field_global_id,
    ComposeSessionCloseReason session_close_reason,
    const compose::ComposeSessionEvents& events) {
  auto iter = engagement_trackers_.find(field_global_id);
  if (iter != engagement_trackers_.end()) {
    iter->second->ComposeSessionCompleted(session_close_reason, events);
    engagement_trackers_.erase(iter);
  }
  ResetState();
}

void ProactiveNudgeTracker::OnUserDisabledNudge(bool single_site_only) {
  for (auto& iter : engagement_trackers_) {
    iter.second->UserDisabledNudge(single_site_only);
  }
  engagement_trackers_.clear();
}

std::optional<bool> ProactiveNudgeTracker::CachedSegmentationResult() {
  if (!state_) {
    return std::nullopt;
  }
  if (auto iter = seen_fields_.find(state_->signals.field.global_id());
      iter != seen_fields_.end()) {
    return iter->second;
  }
  return std::nullopt;
}

}  // namespace compose