File: event_storage_migration.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 (120 lines) | stat: -rw-r--r-- 4,483 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
// Copyright 2025 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/event_storage_migration.h"

#include "base/barrier_closure.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"

namespace feature_engagement {

namespace {
// Number of db to initialize.
const int kNumberOfDBs = 2;
// Type alias for a pair of a key and an event.
using KeyEventPair = std::pair<std::string, Event>;
// Type alias for a list of key and event pairs.
using KeyEventList = std::vector<KeyEventPair>;
}  // namespace

EventStorageMigration::EventStorageMigration(
    raw_ptr<leveldb_proto::ProtoDatabase<Event>> profile_db,
    raw_ptr<leveldb_proto::ProtoDatabase<Event>> device_db)
    : profile_db_(profile_db), device_db_(device_db) {}

EventStorageMigration::~EventStorageMigration() = default;

void EventStorageMigration::Migrate(MigrationCallback callback) {
  // If a request is already in progress, drop the new request.
  if (migration_callback_) {
    return;
  }

  RecordMigrationStatus(EventStorageMigrationStatus::kStarted);

  // Set the callback to be invoked once overall initialization is complete.
  migration_callback_ = std::move(callback);
  // Use a `BarrierClosure` to ensure all async tasks are completed before
  // executing the overall completion callback and returning the data. The
  // BarrierClosure will wait until the `OnInitializationComplete` callback
  // is itself run `kNumberOfDBs` times.
  initialization_complete_barrier_ = base::BarrierClosure(
      kNumberOfDBs,
      base::BindOnce(&EventStorageMigration::OnDBsInitializationCompleted,
                     weak_ptr_factory_.GetWeakPtr()));

  // Initialize to true. The overall success is the AND of all individual db
  // initializations. If any of them fail, this will become false.
  initialization_success_ = true;

  profile_db_->Init(
      base::BindOnce(&EventStorageMigration::OnInitializationComplete,
                     weak_ptr_factory_.GetWeakPtr()));
  device_db_->Init(
      base::BindOnce(&EventStorageMigration::OnInitializationComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void EventStorageMigration::OnInitializationComplete(
    leveldb_proto::Enums::InitStatus status) {
  bool success = status == leveldb_proto::Enums::InitStatus::kOK;
  // If any of the db fail to initialize, the overall initialization
  // will fail.
  initialization_success_ = initialization_success_ && success;
  // The `BarrierClosure` must be run regardless of the error type to ensure
  // that it is run `kNumberOfDBs` times before the
  // `OnDBsInitializationCompleted` callback can be run.
  initialization_complete_barrier_.Run();
}

void EventStorageMigration::OnDBsInitializationCompleted() {
  if (!initialization_success_) {
    RecordMigrationStatus(EventStorageMigrationStatus::kFailedToInitialize);
    std::move(migration_callback_).Run(false);
    return;
  }

  profile_db_->LoadEntries(
      base::BindOnce(&EventStorageMigration::OnLoadEntriesComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void EventStorageMigration::OnLoadEntriesComplete(
    bool success,
    std::unique_ptr<std::vector<Event>> entries) {
  if (!success) {
    RecordMigrationStatus(EventStorageMigrationStatus::kFailedToLoad);
    std::move(migration_callback_).Run(false);
    return;
  }

  // Read profile events into a list.
  std::unique_ptr<KeyEventList> event_list = std::make_unique<KeyEventList>();
  for (const auto& event : *entries) {
    event_list->push_back(KeyEventPair(event.name(), event));
  }

  // Write profile to device db.
  device_db_->UpdateEntries(
      std::move(event_list), std::make_unique<std::vector<std::string>>(),
      base::BindOnce(&EventStorageMigration::OnEventWrittenCompleted,
                     weak_ptr_factory_.GetWeakPtr()));
}

void EventStorageMigration::OnEventWrittenCompleted(bool success) {
  RecordMigrationStatus(success ? EventStorageMigrationStatus::kCompleted
                                : EventStorageMigrationStatus::kFailedToWrite);

  std::move(migration_callback_).Run(success);
}

// static
void EventStorageMigration::RecordMigrationStatus(
    EventStorageMigrationStatus status) {
  base::UmaHistogramEnumeration("InProductHelp.EventStorageMigration.Status",
                                status);
}

}  // namespace feature_engagement