File: tracker_impl.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 (715 lines) | stat: -rw-r--r-- 28,652 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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/feature_engagement/internal/tracker_impl.h"

#include <memory>
#include <optional>
#include <utility>

#include "base/check_is_test.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/no_destructor.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/clock.h"
#include "build/build_config.h"
#include "components/feature_engagement/internal/availability_model_impl.h"
#include "components/feature_engagement/internal/blocked_iph_features.h"
#include "components/feature_engagement/internal/chrome_variations_configuration.h"
#include "components/feature_engagement/internal/display_lock_controller_impl.h"
#include "components/feature_engagement/internal/editable_configuration.h"
#include "components/feature_engagement/internal/event_model.h"
#include "components/feature_engagement/internal/event_model_impl.h"
#include "components/feature_engagement/internal/feature_config_condition_validator.h"
#include "components/feature_engagement/internal/feature_config_event_storage_validator.h"
#include "components/feature_engagement/internal/in_memory_event_store.h"
#include "components/feature_engagement/internal/init_aware_event_model.h"
#include "components/feature_engagement/internal/multiple_event_model_provider.h"
#include "components/feature_engagement/internal/multiple_event_model_writer.h"
#include "components/feature_engagement/internal/never_availability_model.h"
#include "components/feature_engagement/internal/never_event_storage_validator.h"
#include "components/feature_engagement/internal/noop_display_lock_controller.h"
#include "components/feature_engagement/internal/once_condition_validator.h"
#include "components/feature_engagement/internal/persistent_event_store.h"
#include "components/feature_engagement/internal/proto/availability.pb.h"
#include "components/feature_engagement/internal/single_event_model_provider.h"
#include "components/feature_engagement/internal/stats.h"
#include "components/feature_engagement/internal/system_time_provider.h"
#include "components/feature_engagement/internal/testing_clock_time_provider.h"
#include "components/feature_engagement/public/configuration.h"
#include "components/feature_engagement/public/feature_activation.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/feature_list.h"
#include "components/feature_engagement/public/group_constants.h"
#include "components/feature_engagement/public/group_list.h"
#include "components/feature_engagement/public/pref_names.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/prefs/pref_service.h"

namespace feature_engagement {

namespace {

const char kEventDBName[] = "EventDB";
const char kAvailabilityDBName[] = "AvailabilityDB";

#if !BUILDFLAG(IS_ANDROID)

// Reads event data from `config` and - if valid - places it into `result` along
// with the event count in the appropriate window.
void MaybeGetEventData(Tracker::EventList& result,
                       const EventConfig& config,
                       const EventModelReader& event_model_reader,
                       uint32_t current_day) {
  if (config.name.empty()) {
    return;
  }
  result.emplace_back(
      std::make_pair(config, event_model_reader.GetEventCount(
                                 config.name, current_day, config.window)));
}

#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace

std::unique_ptr<Tracker> CreateDemoModeTracker(
    feature_engagement::FeatureActivation feature_activation) {
  feature_engagement::FeatureActivation::State state =
      feature_activation.get_state();

  switch (state) {
    case feature_engagement::FeatureActivation::State::kAllEnabled:
      DVLOG(2) << "Enabling demo mode. All features enabled ";
      break;
    case feature_engagement::FeatureActivation::State::kAllDisabled:
      DVLOG(2) << "Enabling demo mode. All features disabled ";
      break;
    case feature_engagement::FeatureActivation::State::kSingleFeatureEnabled:
      DVLOG(2) << "Enabling demo mode. Chosen feature: "
               << feature_activation.get_unique_feature_name();
      break;
  }

  std::unique_ptr<EditableConfiguration> configuration =
      std::make_unique<EditableConfiguration>();

  // Create valid configurations for all features to ensure that the
  // OnceConditionValidator acknowledges that thet meet conditions once.
  std::vector<const base::Feature*> features = GetAllFeatures();
  for (auto* feature : features) {
    FeatureConfig feature_config;
    // If a particular feature has been chosen to use with demo mode, only
    // mark that feature with a valid configuration.
    switch (state) {
      case feature_engagement::FeatureActivation::State::kAllEnabled:
        feature_config.valid = true;
        break;
      case feature_engagement::FeatureActivation::State::kAllDisabled:
        feature_config.valid = false;
        break;
      case feature_engagement::FeatureActivation::State::kSingleFeatureEnabled:
        feature_config.valid =
            feature_activation.get_unique_feature_name() == feature->name;
    }
    feature_config.trigger.name = feature->name + std::string("_trigger");
    configuration->SetConfiguration(feature, feature_config);
  }

  auto raw_event_model = std::make_unique<EventModelImpl>(
      std::make_unique<InMemoryEventStore>(),
      std::make_unique<NeverEventStorageValidator>());

  auto event_model =
      std::make_unique<InitAwareEventModel>(std::move(raw_event_model));

  return std::make_unique<TrackerImpl>(
      std::make_unique<SingleEventModelProvider>(std::move(event_model)),
      std::make_unique<NeverAvailabilityModel>(), std::move(configuration),
      std::make_unique<NoopDisplayLockController>(),
      std::make_unique<OnceConditionValidator>(),
      std::make_unique<SystemTimeProvider>(), nullptr, nullptr, nullptr,
      nullptr);
}

// This method is declared in
// //components/feature_engagement/public/tracker.h
// and should be linked in to any binary using Tracker::Create.
// static
std::unique_ptr<Tracker> Tracker::Create(
    const base::FilePath& storage_dir,
    const base::FilePath& device_storage_dir,
    PrefService* pref_service,
    const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
    leveldb_proto::ProtoDatabaseProvider* db_provider,
    std::unique_ptr<TrackerEventExporter> event_exporter,
    const ConfigurationProviderList& configuration_providers,
    std::unique_ptr<SessionController> session_controller) {
  DVLOG(2) << "Creating Tracker";
  if (base::FeatureList::IsEnabled(kIPHDemoMode)) {
    // GetFieldTrialParamValueByFeature returns an empty string if the param is
    // not set. In this case, we want to enable all features.
    std::string chosen_feature_name = base::GetFieldTrialParamValueByFeature(
        kIPHDemoMode, kIPHDemoModeFeatureChoiceParam);
    feature_engagement::FeatureActivation feature_action =
        chosen_feature_name.empty()
            ? feature_engagement::FeatureActivation::AllEnabled()
            : feature_engagement::FeatureActivation(chosen_feature_name);
    return CreateDemoModeTracker(feature_action);
  }

  base::FilePath event_storage_dir =
      storage_dir.AppendASCII(std::string(kEventDBName));
  auto event_db = db_provider->GetDB<Event>(
      leveldb_proto::ProtoDbType::FEATURE_ENGAGEMENT_EVENT, event_storage_dir,
      background_task_runner);

  auto profile_event_db = event_db.get();

  auto event_store =
      std::make_unique<PersistentEventStore>(std::move(event_db));

  auto configuration = std::make_unique<ChromeVariationsConfiguration>();
  configuration->LoadConfigs(configuration_providers, GetAllFeatures(),
                             GetAllGroups());

  auto event_storage_validator =
      std::make_unique<FeatureConfigEventStorageValidator>();
  event_storage_validator->InitializeFeatures(GetAllFeatures(), GetAllGroups(),
                                              *configuration);

  auto raw_event_model = std::make_unique<EventModelImpl>(
      std::move(event_store), std::move(event_storage_validator));

  auto event_model =
      std::make_unique<InitAwareEventModel>(std::move(raw_event_model));

  auto condition_validator =
      std::make_unique<FeatureConfigConditionValidator>();
  auto time_provider = std::make_unique<SystemTimeProvider>();

  base::FilePath availability_storage_dir =
      storage_dir.AppendASCII(std::string(kAvailabilityDBName));
  auto availability_db = db_provider->GetDB<Availability>(
      leveldb_proto::ProtoDbType::FEATURE_ENGAGEMENT_AVAILABILITY,
      availability_storage_dir, background_task_runner);
  auto availability_store_loader = base::BindOnce(
      &PersistentAvailabilityStore::LoadAndUpdateStore,
      availability_storage_dir, std::move(availability_db), GetAllFeatures());

  auto availability_model = std::make_unique<AvailabilityModelImpl>(
      std::move(availability_store_loader));

  std::unique_ptr<EventModelProvider> event_model_provider;
  std::unique_ptr<EventStorageMigration> event_storage_migration;
  if (IsOnDeviceStorageEnabled()) {
    base::FilePath device_event_storage_dir =
        device_storage_dir.AppendASCII(std::string(kEventDBName));
    auto device_event_db = db_provider->GetUniqueDB<Event>(
        leveldb_proto::ProtoDbType::FEATURE_ENGAGEMENT_EVENT,
        device_event_storage_dir, background_task_runner);

    // If the migration is completed, we don't need to migrate the data.
    // TODO(crbug.com/426624087): Remove this and all the code related to it
    // once the migration is completed.
    event_storage_migration = std::make_unique<EventStorageMigration>(
        profile_event_db, device_event_db.get());

    auto device_event_store =
        std::make_unique<PersistentEventStore>(std::move(device_event_db));

    auto device_event_storage_validator =
        std::make_unique<FeatureConfigEventStorageValidator>();
    device_event_storage_validator->InitializeFeatures(
        GetAllFeatures(), GetAllGroups(), *configuration);

    auto device_raw_event_model = std::make_unique<EventModelImpl>(
        std::move(device_event_store),
        std::move(device_event_storage_validator));

    auto device_event_model = std::make_unique<InitAwareEventModel>(
        std::move(device_raw_event_model));
    event_model_provider = std::make_unique<MultipleEventModelProvider>(
        std::move(event_model), std::move(device_event_model));
  } else {
    event_model_provider =
        std::make_unique<SingleEventModelProvider>(std::move(event_model));
  }

  return std::make_unique<TrackerImpl>(
      std::move(event_model_provider), std::move(availability_model),
      std::move(configuration), std::make_unique<DisplayLockControllerImpl>(),
      std::move(condition_validator), std::move(time_provider),
      std::move(event_exporter), std::move(session_controller),
      std::move(event_storage_migration), pref_service);
}

TrackerImpl::TrackerImpl(
    std::unique_ptr<EventModelProvider> event_model_provider,
    std::unique_ptr<AvailabilityModel> availability_model,
    std::unique_ptr<Configuration> configuration,
    std::unique_ptr<DisplayLockController> display_lock_controller,
    std::unique_ptr<ConditionValidator> condition_validator,
    std::unique_ptr<TimeProvider> time_provider,
    std::unique_ptr<TrackerEventExporter> event_exporter,
    std::unique_ptr<SessionController> session_controller,
    std::unique_ptr<EventStorageMigration> event_storage_migration,
    PrefService* pref_service)
    : event_model_provider_(std::move(event_model_provider)),
      availability_model_(std::move(availability_model)),
      configuration_(std::move(configuration)),
      display_lock_controller_(std::move(display_lock_controller)),
      condition_validator_(std::move(condition_validator)),
      time_provider_(std::move(time_provider)),
      event_exporter_(std::move(event_exporter)),
      session_controller_(std::move(session_controller)),
      event_storage_migration_(std::move(event_storage_migration)),
      pref_service_(pref_service) {
  availability_model_->Initialize(
      base::BindOnce(&TrackerImpl::OnAvailabilityModelInitializationFinished,
                     weak_ptr_factory_.GetWeakPtr()),
      time_provider_->GetCurrentDay());

  // If the migration is not completed, we need to migrate the data from the
  // profile db to the device db.
  if (event_storage_migration_ &&
      !pref_service_->GetBoolean(
          kFeatureEngagementProfileToDeviceMigrationCompleted)) {
    event_storage_migration_->Migrate(
        base::BindOnce(&TrackerImpl::OnEventStorageMigrationFinished,
                       weak_ptr_factory_.GetWeakPtr()));
    return;
  }

  if (IsOnDeviceStorageEnabled()) {
    // If the migration was completed, log the status.
    EventStorageMigration::RecordMigrationStatus(
        EventStorageMigration::EventStorageMigrationStatus::kNotRequired);
  }

  event_model_provider_->Initialize(
      base::BindOnce(&TrackerImpl::OnEventModelInitializationFinished,
                     weak_ptr_factory_.GetWeakPtr()),
      time_provider_->GetCurrentDay());
}

TrackerImpl::~TrackerImpl() = default;

void TrackerImpl::NotifyEvent(const std::string& event) {
  GetEventModelWriter()->IncrementEvent(event, time_provider_->GetCurrentDay());
  stats::RecordNotifyEvent(event, configuration_.get(),
                           event_model_provider_->IsReady());
}

#if !BUILDFLAG(IS_ANDROID)
void TrackerImpl::NotifyUsedEvent(const base::Feature& feature) {
  const auto& feature_config = configuration_->GetFeatureConfig(feature);
  if (!feature_config.used.name.empty()) {
    NotifyEvent(feature_config.used.name);
  }
}

void TrackerImpl::ClearEventData(const base::Feature& feature) {
  const auto& feature_config = configuration_->GetFeatureConfig(feature);
  if (!feature_config.trigger.name.empty()) {
    GetEventModelWriter()->ClearEvent(feature_config.trigger.name);
  }
  if (!feature_config.used.name.empty()) {
    GetEventModelWriter()->ClearEvent(feature_config.used.name);
  }
  for (const auto& event_config : feature_config.event_configs) {
    GetEventModelWriter()->ClearEvent(event_config.name);
  }
}

Tracker::EventList TrackerImpl::ListEvents(const base::Feature& feature) const {
  EventList result;
  if (!IsInitialized()) {
    return result;
  }
  const auto& feature_config = configuration_->GetFeatureConfig(feature);
  const auto current_day = time_provider_->GetCurrentDay();
  auto* event_model_reader = GetEventModelReaderForFeature(feature_config);
  MaybeGetEventData(result, feature_config.trigger, *event_model_reader,
                    current_day);
  MaybeGetEventData(result, feature_config.used, *event_model_reader,
                    current_day);
  for (const auto& event_config : feature_config.event_configs) {
    MaybeGetEventData(result, event_config, *event_model_reader, current_day);
  }
  return result;
}

#endif  // !BUILDFLAG(IS_ANDROID)

bool TrackerImpl::ShouldTriggerHelpUI(const base::Feature& feature) {
  return ShouldTriggerHelpUIWithSnooze(feature).ShouldShowIph();
}

TrackerImpl::TriggerDetails TrackerImpl::ShouldTriggerHelpUIWithSnooze(
    const base::Feature& feature) {
  if (session_controller_ && session_controller_->ShouldResetSession()) {
    condition_validator_->ResetSession();
  }
  if (IsFeatureBlockedByTest(feature)) {
    return TriggerDetails(false, false);
  }

  FeatureConfig feature_config = configuration_->GetFeatureConfig(feature);
  std::vector<GroupConfig> group_configs;
  for (auto group : feature_config.groups) {
    group_configs.push_back(configuration_->GetGroupConfigByName(group));
  }
  auto* event_model_reader = GetEventModelReaderForFeature(feature_config);
  ConditionValidator::Result result = condition_validator_->MeetsConditions(
      feature, feature_config, group_configs, *event_model_reader,
      *availability_model_, *display_lock_controller_, configuration_.get(),
      *time_provider_);
  if (result.NoErrors()) {
    condition_validator_->NotifyIsShowing(
        feature, feature_config, configuration_->GetRegisteredFeatures());

    DCHECK_NE("", feature_config.trigger.name);
    GetEventModelWriter()->IncrementEvent(feature_config.trigger.name,
                                          time_provider_->GetCurrentDay());

    for (auto group : feature_config.groups) {
      GroupConfig group_config = configuration_->GetGroupConfigByName(group);
      GetEventModelWriter()->IncrementEvent(group_config.trigger.name,
                                            time_provider_->GetCurrentDay());
    }
  }

  stats::RecordShouldTriggerHelpUI(feature, feature_config, result);
  DVLOG(2) << "Trigger result for " << feature.name
           << ": trigger=" << result.NoErrors()
           << " tracking_only=" << feature_config.tracking_only << " "
           << result;

  bool should_show_iph = false;

  if (feature_config.tracking_only) {
    if (result.NoErrors()) {
      // Because tracking only features always return false to the client,
      // clients do not have the context to correctly dismiss. The dismiss is
      // needed because our condition validator thinks the feature is currently
      // showing. See https://crbug.com/1188679 for more details.
      Dismissed(feature);
    }
    should_show_iph = false;
  } else {
    should_show_iph = result.NoErrors();
  }

  if (should_show_iph) {
    DCHECK(start_times_.find(feature.name) == start_times_.end());
    start_times_[feature.name] = time_provider_->Now();
  }

  return TriggerDetails(should_show_iph, result.should_show_snooze);
}

bool TrackerImpl::WouldTriggerHelpUI(const base::Feature& feature) const {
  if (session_controller_ && session_controller_->ShouldResetSession()) {
    condition_validator_->ResetSession();
  }

  if (IsFeatureBlockedByTest(feature)) {
    return false;
  }

  FeatureConfig feature_config = configuration_->GetFeatureConfig(feature);
  std::vector<GroupConfig> group_configs;
  for (auto group : feature_config.groups) {
    group_configs.push_back(configuration_->GetGroupConfigByName(group));
  }
  auto* event_model_reader = GetEventModelReaderForFeature(feature_config);
  ConditionValidator::Result result = condition_validator_->MeetsConditions(
      feature, feature_config, group_configs, *event_model_reader,
      *availability_model_, *display_lock_controller_, configuration_.get(),
      *time_provider_);
  DVLOG(2) << "Would trigger result for " << feature.name
           << ": trigger=" << result.NoErrors()
           << " tracking_only=" << feature_config.tracking_only << " "
           << result;
  return result.NoErrors() && !feature_config.tracking_only;
}

bool TrackerImpl::HasEverTriggered(const base::Feature& feature,
                                   bool from_window) const {
  const FeatureConfig feature_config =
      configuration_->GetFeatureConfig(feature);
  const EventConfig trigger_config = feature_config.trigger;

  uint32_t window_size =
      from_window ? trigger_config.window : trigger_config.storage;

  uint32_t event_count =
      GetEventModelReaderForFeature(feature_config)
          ->GetEventCount(trigger_config.name, time_provider_->GetCurrentDay(),
                          window_size);

  return event_count > 0;
}

Tracker::TriggerState TrackerImpl::GetTriggerState(
    const base::Feature& feature) const {
  if (!IsInitialized()) {
    DVLOG(2) << "TriggerState for " << feature.name << ": "
             << static_cast<int>(Tracker::TriggerState::NOT_READY);
    return Tracker::TriggerState::NOT_READY;
  }

  FeatureConfig feature_config = configuration_->GetFeatureConfig(feature);
  std::vector<GroupConfig> group_configs;
  for (auto group : feature_config.groups) {
    group_configs.push_back(configuration_->GetGroupConfigByName(group));
  }
  auto* event_model_reader = GetEventModelReaderForFeature(feature_config);
  ConditionValidator::Result result = condition_validator_->MeetsConditions(
      feature, configuration_->GetFeatureConfig(feature), group_configs,
      *event_model_reader, *availability_model_, *display_lock_controller_,
      configuration_.get(), *time_provider_);

  if (result.trigger_ok) {
    DVLOG(2) << "TriggerState for " << feature.name << ": "
             << static_cast<int>(Tracker::TriggerState::HAS_NOT_BEEN_DISPLAYED);
    return Tracker::TriggerState::HAS_NOT_BEEN_DISPLAYED;
  }

  DVLOG(2) << "TriggerState for " << feature.name << ": "
           << static_cast<int>(Tracker::TriggerState::HAS_BEEN_DISPLAYED);
  return Tracker::TriggerState::HAS_BEEN_DISPLAYED;
}

void TrackerImpl::Dismissed(const base::Feature& feature) {
  DVLOG(2) << "Dismissing " << feature.name;
  DCHECK(!IsFeatureBlockedByTest(feature));

  condition_validator_->NotifyDismissed(feature);
  stats::RecordUserDismiss();
  RecordShownTime(feature);
}

void TrackerImpl::DismissedWithSnooze(
    const base::Feature& feature,
    std::optional<SnoozeAction> snooze_action) {
  DCHECK(!IsFeatureBlockedByTest(feature));

  FeatureConfig feature_config = configuration_->GetFeatureConfig(feature);
  if (snooze_action == SnoozeAction::SNOOZED) {
    GetEventModelWriter()->IncrementSnooze(feature_config.trigger.name,
                                           time_provider_->GetCurrentDay(),
                                           time_provider_->Now());
  } else if (snooze_action == SnoozeAction::DISMISSED) {
    GetEventModelWriter()->DismissSnooze(feature_config.trigger.name);
  }
  Dismissed(feature);
  if (snooze_action.has_value())
    stats::RecordUserSnoozeAction(snooze_action.value());
}

std::unique_ptr<DisplayLockHandle> TrackerImpl::AcquireDisplayLock() {
  return display_lock_controller_->AcquireDisplayLock();
}

void TrackerImpl::SetPriorityNotification(const base::Feature& feature) {
  // If the handler hasn't been registered.
  auto iter = priority_notification_handlers_.find(feature.name);
  if (iter == priority_notification_handlers_.end()) {
    condition_validator_->SetPriorityNotification(feature.name);
    return;
  }

  // We already have a handler. Serve the request and remove the handler.
  condition_validator_->SetPriorityNotification(std::nullopt);
  std::move(iter->second).Run();
  priority_notification_handlers_.erase(feature.name);
}

std::optional<std::string> TrackerImpl::GetPendingPriorityNotification() {
  return condition_validator_->GetPendingPriorityNotification();
}

void TrackerImpl::RegisterPriorityNotificationHandler(
    const base::Feature& feature,
    base::OnceClosure callback) {
  // If we already have a pending notification, handle it right away.
  auto pending_priority_notification =
      condition_validator_->GetPendingPriorityNotification();
  if (pending_priority_notification.has_value() &&
      pending_priority_notification.value() == feature.name) {
    std::move(callback).Run();
    condition_validator_->SetPriorityNotification(std::nullopt);
    return;
  }

  // We don't have the notification yet. Cache the handler.
  priority_notification_handlers_.emplace(feature.name, std::move(callback));
}

void TrackerImpl::UnregisterPriorityNotificationHandler(
    const base::Feature& feature) {
  priority_notification_handlers_.erase(feature.name);
}

#if BUILDFLAG(IS_CHROMEOS)
void TrackerImpl::UpdateConfig(const base::Feature& feature,
                               const ConfigurationProvider* provider) {
  CHECK(IsInitialized());
  configuration_->UpdateConfig(feature, provider);
}
#endif

const Configuration* TrackerImpl::GetConfigurationForTesting() const {
  CHECK_IS_TEST();
  return configuration_.get();
}

void TrackerImpl::SetClockForTesting(const base::Clock& clock,
                                     base::Time initial_now) {
  CHECK_IS_TEST();
  time_provider_ =
      std::make_unique<TestingClockTimeProvider>(clock, initial_now);
}

bool TrackerImpl::IsInFeatureTestMode() const {
  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  return blocked->AreAnyFeaturesBlocked();
}

bool TrackerImpl::IsInitialized() const {
  return event_model_provider_->IsReady() && availability_model_->IsReady();
}

void TrackerImpl::AddOnInitializedCallback(OnInitializedCallback callback) {
  if (IsInitializationFinished()) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), IsInitialized()));
    return;
  }

  on_initialized_callbacks_.push_back(std::move(callback));
}

void TrackerImpl::OnEventModelInitializationFinished(bool success) {
  DCHECK_EQ(success, event_model_provider_->IsReady());
  event_model_provider_initialization_finished_ = true;

  DVLOG(2) << "Event model initialization result = " << success;

  if (event_exporter_) {
    event_exporter_->ExportEvents(base::BindOnce(
        &TrackerImpl::OnReceiveExportedEvents, weak_ptr_factory_.GetWeakPtr()));
  } else {
    MaybePostInitializedCallbacks();
  }
}

void TrackerImpl::OnEventStorageMigrationFinished(bool success) {
  DVLOG(2) << "Event storage migration result = " << success;

  if (success) {
    pref_service_->SetBoolean(
        kFeatureEngagementProfileToDeviceMigrationCompleted, true);
  }

  // Initialize the event model provider.
  event_model_provider_->Initialize(
      base::BindOnce(&TrackerImpl::OnEventModelInitializationFinished,
                     weak_ptr_factory_.GetWeakPtr()),
      time_provider_->GetCurrentDay());

  event_storage_migration_ = nullptr;
}

void TrackerImpl::OnAvailabilityModelInitializationFinished(bool success) {
  DCHECK_EQ(success, availability_model_->IsReady());
  availability_model_initialization_finished_ = true;

  DVLOG(2) << "Availability model initialization result = " << success;

  MaybePostInitializedCallbacks();
}

bool TrackerImpl::IsInitializationFinished() const {
  bool event_migration_finished =
      event_exporter_ == nullptr || event_migration_finished_;
  return event_model_provider_initialization_finished_ &&
         availability_model_initialization_finished_ &&
         event_migration_finished;
}

void TrackerImpl::MaybePostInitializedCallbacks() {
  if (!IsInitializationFinished())
    return;

  DVLOG(2) << "Initialization finished.";

  for (auto& callback : on_initialized_callbacks_) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), IsInitialized()));
  }

  on_initialized_callbacks_.clear();
}

void TrackerImpl::RecordShownTime(const base::Feature& feature) {
  std::string feature_name = feature.name;
  auto iter = start_times_.find(feature_name);
  if (iter == start_times_.end())
    return;

  UmaHistogramTimes("InProductHelp.ShownTime." + feature_name,
                    time_provider_->Now() - iter->second);
  UmaHistogramMediumTimes("InProductHelp.ShownTime2." + feature_name,
                          time_provider_->Now() - iter->second);
  start_times_.erase(feature_name);
}

void TrackerImpl::OnReceiveExportedEvents(
    std::vector<TrackerEventExporter::EventData> events) {
  for (auto& event : events) {
    GetEventModelWriter()->IncrementEvent(event.event_name, event.day);
  }

  event_migration_finished_ = true;
  MaybePostInitializedCallbacks();
}

EventModelWriter* TrackerImpl::GetEventModelWriter() {
  return event_model_provider_->GetEventModelWriter();
}

const EventModelReader* TrackerImpl::GetEventModelReaderForFeature(
    const FeatureConfig& feature_config) const {
  return event_model_provider_->GetEventModelReaderForFeature(feature_config);
}

// static
void Tracker::PropagateTestStateToChildProcess(
    base::CommandLine& command_line) {
  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  blocked->MaybeWriteToCommandLine(command_line);
}

// static
bool TrackerImpl::IsFeatureBlockedByTest(const base::Feature& feature) {
  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  return blocked->IsFeatureBlocked(feature.name);
}

}  // namespace feature_engagement