File: pending_write_store.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 (223 lines) | stat: -rw-r--r-- 8,039 bytes parent folder | download | duplicates (6)
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
// 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 "ash/quick_pair/repository/fast_pair/pending_write_store.h"

#include "ash/quick_pair/common/quick_pair_browser_delegate.h"
#include "base/base64.h"
#include "base/strings/string_number_conversions.h"
#include "base/types/fixed_array.h"
#include "components/cross_device/logging/logging.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"

namespace {

const char kFastPairPendingWritesPref[] = "fast_pair.pending_writes";
const char kFastPairPendingDeletesPref[] = "fast_pair.pending_deletes";

}  // namespace

namespace ash {
namespace quick_pair {

PendingWriteStore::PendingWriteStore() = default;
PendingWriteStore::~PendingWriteStore() = default;

void PendingWriteStore::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  registry->RegisterDictionaryPref(kFastPairPendingWritesPref);
  registry->RegisterDictionaryPref(kFastPairPendingDeletesPref);
}

PendingWriteStore::PendingWrite::PendingWrite(
    const std::string& mac_address,
    const nearby::fastpair::FastPairInfo fast_pair_info)
    : mac_address(mac_address), fast_pair_info(fast_pair_info) {}

PendingWriteStore::PendingWrite::PendingWrite(PendingWrite&& pending_write) =
    default;
PendingWriteStore::PendingWrite::~PendingWrite() = default;

PendingWriteStore::PendingDelete::PendingDelete(
    const std::string& mac_address,
    const std::string& hex_account_key)
    : mac_address(mac_address), hex_account_key(hex_account_key) {}

PendingWriteStore::PendingDelete::~PendingDelete() = default;

void PendingWriteStore::WritePairedDevice(
    const std::string& mac_address,
    const nearby::fastpair::FastPairInfo fast_pair_info) {
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return;
  }

  // |fast_pair_info| must be converted first to bytes and then to
  // hex-encoded string format so that no UTF-8 encoding errors are thrown.
  size_t fp_info_size = fast_pair_info.ByteSizeLong();
  base::FixedArray<uint8_t> fp_info_bytes(fp_info_size);
  if (!fast_pair_info.SerializeToArray(fp_info_bytes.data(), fp_info_size)) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": couldn't serialize fast pair info of device "
        << "with mac address " << mac_address
        << ". Not writing this device to PendingWrite list.";
    return;
  }

  ScopedDictPrefUpdate update(pref_service, kFastPairPendingWritesPref);
  update->Set(mac_address, base::HexEncode(fp_info_bytes));
}

std::vector<PendingWriteStore::PendingWrite>
PendingWriteStore::GetPendingWrites() {
  std::vector<PendingWriteStore::PendingWrite> list;
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return list;
  }

  const base::Value::Dict& result =
      pref_service->GetDict(kFastPairPendingWritesPref);

  for (const auto item : result) {
    // PendingWrite member variable |fast_pair_info| is
    // stored in |result| as a hex encoded string. This need to be parsed into
    // type nearby::fastpair::FastPairInfo before initializing a new
    // PendingWrite to store in |list|.

    // Parsing fast pair info stored as string.
    std::string fp_info_str = item.second.GetString();
    nearby::fastpair::FastPairInfo fast_pair_info;

    // To align with Android's Footprints use, removal of a device from
    // the list of devices saved to a user's account is performed by writing a
    // footprint to Footprints with empty fast pair info.
    //
    // Thus, an empty |fp_info_str| is a valid element that is
    // intended to be written to Footprints. We must explicitly catch
    // this edge-case here, because passing an empty |fp_info_str| to
    // HexStringToBytes() would result in an error.
    if (fp_info_str.empty()) {
      fast_pair_info.ParseFromString(std::string());
    } else {
      std::vector<uint8_t> fp_info_bytes;
      if (!base::HexStringToBytes(fp_info_str, &fp_info_bytes)) {
        CD_LOG(WARNING, Feature::FP)
            << __func__ << ": fast pair info of "
            << "PendingWrite with mac address " << item.first
            << " not perfectly parsed into bytes from a hex-encoded string. "
            << "PendingWrite not added to PendingWrite list.";
        continue;
      }
      // Create fast pair info from byte buffer.
      if (!fast_pair_info.ParseFromArray(fp_info_bytes.data(),
                                         fp_info_bytes.size())) {
        CD_LOG(WARNING, Feature::FP)
            << __func__ << ": failed to parse Fast Pair Info of "
            << " PendingWrite with mac address " << item.first
            << " from bytes to type nearby::fastpair::FastPairInfo. "
            << "PendingWrite not included in PendingWrite list.";
        continue;
      }
    }

    list.emplace_back(/*mac_address=*/item.first, fast_pair_info);
  }

  return list;
}

void PendingWriteStore::OnPairedDeviceSaved(const std::string& mac_address) {
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return;
  }

  ScopedDictPrefUpdate update(pref_service, kFastPairPendingWritesPref);
  update->Remove(mac_address);
}

void PendingWriteStore::DeletePairedDevice(const std::string& mac_address,
                                           const std::string& hex_account_key) {
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return;
  }
  ScopedDictPrefUpdate update(pref_service, kFastPairPendingDeletesPref);
  update->Set(mac_address, hex_account_key);
}

std::vector<PendingWriteStore::PendingDelete>
PendingWriteStore::GetPendingDeletes() {
  std::vector<PendingWriteStore::PendingDelete> list;
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return list;
  }

  const base::Value::Dict& result =
      pref_service->GetDict(kFastPairPendingDeletesPref);

  for (const auto item : result) {
    list.emplace_back(item.first, item.second.GetString());
  }

  return list;
}

void PendingWriteStore::OnPairedDeviceDeleted(const std::string& mac_address) {
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return;
  }

  ScopedDictPrefUpdate update(pref_service, kFastPairPendingDeletesPref);
  update->Remove(mac_address);
}

void PendingWriteStore::OnPairedDeviceDeleted(
    const std::vector<uint8_t>& account_key) {
  PrefService* pref_service =
      QuickPairBrowserDelegate::Get()->GetActivePrefService();
  if (!pref_service) {
    CD_LOG(WARNING, Feature::FP)
        << __func__ << ": No user pref service available.";
    return;
  }

  std::string hex_account_key = base::HexEncode(account_key);
  ScopedDictPrefUpdate update(pref_service, kFastPairPendingDeletesPref);
  const base::Value::Dict result =
      pref_service->GetDict(kFastPairPendingDeletesPref).Clone();
  for (const auto item : result) {
    if (item.second == hex_account_key) {
      CD_LOG(INFO, Feature::FP)
          << __func__ << ": Successfully removed pending delete from prefs.";
      update->Remove(item.first);
    }
  }
}

}  // namespace quick_pair
}  // namespace ash