File: local_data_migration_item_queue.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (172 lines) | stat: -rw-r--r-- 6,328 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
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
// 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 "components/sync/service/local_data_migration_item_queue.h"

#include "base/time/time.h"
#include "components/sync/base/data_type.h"
#include "components/sync/service/data_type_manager.h"
#include "components/sync/service/sync_service.h"

namespace syncer {

namespace {

// The time limit for activating the data type is set to 50 minutes, as this is
// in line with the `SigninPromoTabHelper` waiting for a sign in event.
constexpr base::TimeDelta kTimeLimitForActivatingDataType = base::Minutes(50);

// Removes items which had been added to the queue before the time limit
// has been exceeded, or have a data type that is non-preferred.
void RemoveExpiredItemsAndItemsOfNonPreferredDataTypes(
    std::map<LocalDataItemModel::DataId, std::tuple<DataType, base::Time>>&
        items,
    const DataTypeSet& preferred_data_types,
    const base::Time& current_time_stamp) {
  // Use this to keep the items for which the data type is not active yet.
  std::map<LocalDataItemModel::DataId, std::tuple<DataType, base::Time>>
      items_to_keep;

  for (auto& item : items) {
    DataType data_type = std::get<0>(item.second);
    base::Time initialized_time_stamp = std::get<1>(item.second);
    LocalDataItemModel::DataId data_id = item.first;

    // Do not include the item if the time limit since the initialization
    // of its move has been exceeded. This can happen for example if a user
    // with custom passphrase enabled signs in after a sign in promo which
    // initializes this queue, but the user still has to enter their
    // passphrase. As they may forget that entering their passphrase would
    // move the data, we do nothing instead. Also, remove items which have a
    // data type which is non-preferred.
    if (current_time_stamp - initialized_time_stamp <
            kTimeLimitForActivatingDataType &&
        preferred_data_types.Has(data_type)) {
      items_to_keep[data_id] = std::move(item.second);
    }
  }

  items = std::move(items_to_keep);
}

// Returns those items which have a data type that is active, so that their
// migration to account storage can be triggered. The items for which the data
// type is not active are kept in `items` in case it will be activated later,
// e.g. if the user enters their custom passphrase.
std::map<DataType, std::vector<LocalDataItemModel::DataId>>
MoveItemsOfActiveDataTypesToVector(
    std::map<LocalDataItemModel::DataId, std::tuple<DataType, base::Time>>&
        items,
    const DataTypeSet& active_data_types) {
  // Use this to keep the items for which the data type is not active yet.
  std::map<LocalDataItemModel::DataId, std::tuple<DataType, base::Time>>
      items_to_keep;
  // Items that are added here will be migrated to account storage.
  std::map<DataType, std::vector<LocalDataItemModel::DataId>> result;

  for (auto& item : items) {
    DataType data_type = std::get<0>(item.second);
    LocalDataItemModel::DataId data_id = item.first;

    // Only include those items in the vector for which the data type is already
    // active.
    if (active_data_types.Has(data_type)) {
      result[data_type].push_back(data_id);
    } else {
      items_to_keep[data_id] = std::move(item.second);
    }
  }

  items = std::move(items_to_keep);
  return result;
}

}  // namespace

LocalDataMigrationItemQueue::LocalDataMigrationItemQueue(
    SyncService* sync_service,
    DataTypeManager* data_type_manager)
    : sync_service_(sync_service),
      data_type_manager_(data_type_manager),
      clock_(base::DefaultClock::GetInstance()) {
  CHECK(sync_service);
  CHECK(data_type_manager);

  sync_service_observer_.Observe(sync_service_);
}

LocalDataMigrationItemQueue::~LocalDataMigrationItemQueue() = default;

void LocalDataMigrationItemQueue::
    TriggerLocalDataMigrationForItemsWhenTypeBecomesActive(
        DataType data_type,
        std::vector<LocalDataItemModel::DataId> items) {
  base::Time current_timestamp = clock_->Now();
  for (const auto& item : items) {
    items_[item] = std::tuple(data_type, current_timestamp);
  }

  // Fire once in case the sync service is already active.
  OnStateChanged(sync_service_);
}

void LocalDataMigrationItemQueue::OnStateChanged(SyncService* sync_service) {
  CHECK_EQ(sync_service, sync_service_);

  // Do not move the data if the user has consented to sync in the meantime. In
  // this case, simply leave the data in local storage.
  if (sync_service->HasSyncConsent()) {
    items_.clear();
    return;
  }

  switch (sync_service->GetTransportState()) {
    case SyncService::TransportState::DISABLED:
    case SyncService::TransportState::PAUSED: {
      items_.clear();
      return;
    }
    case SyncService::TransportState::START_DEFERRED:
    case SyncService::TransportState::INITIALIZING:
    case SyncService::TransportState::PENDING_DESIRED_CONFIGURATION:
    case SyncService::TransportState::CONFIGURING:
      // Keep waiting if the sync service is not active yet.
      return;
    case SyncService::TransportState::ACTIVE:
      break;
  }

  CHECK_EQ(DataTypeManager::CONFIGURED, data_type_manager_->state());
  RemoveExpiredItemsAndItemsOfNonPreferredDataTypes(
      items_, sync_service->GetPreferredDataTypes(), clock_->Now());

  std::map<DataType, std::vector<LocalDataItemModel::DataId>> items_to_migrate =
      MoveItemsOfActiveDataTypesToVector(items_,
                                         sync_service->GetActiveDataTypes());

  if (items_to_migrate.empty()) {
    return;
  }

  // Use the data type manager directly rather than
  // `SyncService::TriggerLocalDataMigration()`, since this object is used for a
  // single data item move operation from the sign in promo. This is different
  // from batch upload, so we shouldn't record the batch upload histogram in
  // that case.
  data_type_manager_->TriggerLocalDataMigrationForItems(items_to_migrate);
}

void LocalDataMigrationItemQueue::OnSyncShutdown(SyncService* sync_service) {
  items_.clear();
}

void LocalDataMigrationItemQueue::SetClockForTesting(base::Clock* clock) {
  clock_ = clock;
}

size_t LocalDataMigrationItemQueue::GetItemsCountForTesting() const {
  return items_.size();
}

}  // namespace syncer