File: sync_transport_data_prefs.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 (215 lines) | stat: -rw-r--r-- 8,156 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
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
// Copyright 2021 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/glue/sync_transport_data_prefs.h"

#include <memory>
#include <utility>

#include "base/base64.h"
#include "base/feature_list.h"
#include "base/json/values_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "components/sync/base/account_pref_utils.h"
#include "components/sync/base/features.h"
#include "components/sync/base/pref_names.h"
#include "google_apis/gaia/gaia_id.h"

namespace syncer {

namespace {

const char kSyncGaiaId[] = "sync.gaia_id";

// Keys for the `kSyncTransportDataPerAccount` dictionary pref:
const char kSyncCacheGuid[] = "sync.cache_guid";
const char kSyncBirthday[] = "sync.birthday";
const char kSyncBagOfChips[] = "sync.bag_of_chips";
// 64-bit integer serialization of the base::Time when the last sync occurred.
const char kSyncLastSyncedTime[] = "sync.last_synced_time";
// 64-bit integer serialization of the base::Time of the last sync poll.
const char kSyncLastPollTime[] = "sync.last_poll_time";
// 64-bit integer serialization of base::TimeDelta storing poll intervals
// received by the server. For historic reasons, this is called
// "short_poll_interval", but it's not worth the hassle to rename it.
const char kSyncPollInterval[] = "sync.short_poll_interval";

}  // namespace

SyncTransportDataPrefs::SyncTransportDataPrefs(
    PrefService* pref_service,
    const signin::GaiaIdHash& gaia_id_hash)
    : pref_service_(pref_service), gaia_id_hash_(gaia_id_hash) {
}

SyncTransportDataPrefs::~SyncTransportDataPrefs() = default;

// static
void SyncTransportDataPrefs::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterStringPref(kSyncGaiaId, std::string());

  registry->RegisterDictionaryPref(
      prefs::internal::kSyncTransportDataPerAccount);
}

void SyncTransportDataPrefs::ClearForCurrentAccount() {
  ClearAccountKeyedPrefValue(pref_service_,
                             prefs::internal::kSyncTransportDataPerAccount,
                             gaia_id_hash_);
}

// static
void SyncTransportDataPrefs::KeepAccountSettingsPrefsOnlyForUsers(
    PrefService* pref_service,
    const std::vector<signin::GaiaIdHash>& available_gaia_ids) {
  KeepAccountKeyedPrefValuesOnlyForUsers(
      pref_service, prefs::internal::kSyncTransportDataPerAccount,
      available_gaia_ids);
}

base::Time SyncTransportDataPrefs::GetLastSyncedTime() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncLastSyncedTime);
  return base::ValueToTime(value).value_or(base::Time());
}

void SyncTransportDataPrefs::SetLastSyncedTime(base::Time time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncLastSyncedTime, base::TimeToValue(time));
}

base::Time SyncTransportDataPrefs::GetLastPollTime() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncLastPollTime);
  return base::ValueToTime(value).value_or(base::Time());
}

void SyncTransportDataPrefs::SetLastPollTime(base::Time time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncLastPollTime, base::TimeToValue(time));
}

base::TimeDelta SyncTransportDataPrefs::GetPollInterval() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncPollInterval);
  base::TimeDelta poll_interval =
      base::ValueToTimeDelta(value).value_or(base::TimeDelta());

  // If the poll interval is unreasonably short, reset it. This will cause
  // callers to use a reasonable default value instead.
  // This fixes a past bug where stored pref values were accidentally
  // re-interpreted from "seconds" to "microseconds"; see crbug.com/1246850.
  if (poll_interval < base::Minutes(1)) {
    return base::TimeDelta();
  }
  return poll_interval;
}

void SyncTransportDataPrefs::SetPollInterval(base::TimeDelta interval) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncPollInterval, base::TimeDeltaToValue(interval));
}

void SyncTransportDataPrefs::SetCurrentSyncingGaiaId(const GaiaId& gaia_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  pref_service_->SetString(kSyncGaiaId, gaia_id.ToString());
}

GaiaId SyncTransportDataPrefs::GetCurrentSyncingGaiaId() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return GaiaId(pref_service_->GetString(kSyncGaiaId));
}

void SyncTransportDataPrefs::ClearCurrentSyncingGaiaId() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  pref_service_->ClearPref(kSyncGaiaId);
}

// static
bool SyncTransportDataPrefs::HasCurrentSyncingGaiaId(
    const PrefService* pref_service) {
  return !pref_service->GetString(kSyncGaiaId).empty();
}

// static
void SyncTransportDataPrefs::ClearCurrentSyncingGaiaId(
    PrefService* pref_service) {
  pref_service->ClearPref(kSyncGaiaId);
}

void SyncTransportDataPrefs::SetCacheGuid(const std::string& cache_guid) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncCacheGuid, base::Value(cache_guid));
}

std::string SyncTransportDataPrefs::GetCacheGuid() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncCacheGuid);
  return value && value->is_string() ? value->GetString() : std::string();
}

void SyncTransportDataPrefs::SetBirthday(const std::string& birthday) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncBirthday, base::Value(birthday));
}

std::string SyncTransportDataPrefs::GetBirthday() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncBirthday);
  return value && value->is_string() ? value->GetString() : std::string();
}

void SyncTransportDataPrefs::SetBagOfChips(const std::string& bag_of_chips) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  // `bag_of_chips` contains a serialized proto which is not utf-8, hence
  // we use base64 encoding in prefs.
  std::string encoded = base::Base64Encode(bag_of_chips);
  SetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncBagOfChips, base::Value(encoded));
}

std::string SyncTransportDataPrefs::GetBagOfChips() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  std::string encoded;
  const base::Value* value = GetAccountKeyedPrefDictEntry(
      pref_service_, prefs::internal::kSyncTransportDataPerAccount,
      gaia_id_hash_, kSyncBagOfChips);
  if (value && value->is_string()) {
    encoded = value->GetString();
  }

  // `kSyncBagOfChips` gets stored in base64 because it represents a serialized
  // proto which is not utf-8 encoding.
  std::string decoded;
  base::Base64Decode(encoded, &decoded);
  return decoded;
}

}  // namespace syncer