File: saved_device_registry.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (82 lines) | stat: -rw-r--r-- 3,349 bytes parent folder | download | duplicates (8)
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
// 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.

#ifndef ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_SAVED_DEVICE_REGISTRY_H_
#define ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_SAVED_DEVICE_REGISTRY_H_

#include <optional>
#include <string>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "base/values.h"

class PrefRegistrySimple;
class PrefService;

namespace device {
class BluetoothAdapter;
}  // namespace device

namespace ash {
namespace quick_pair {

// Saves the Fast Pair account key to disk for peripherals that have been
// paired with this device for the active user, using the bluetooth MAC address
// as a lookup key, and user prefs for storage.
class SavedDeviceRegistry {
 public:
  explicit SavedDeviceRegistry(scoped_refptr<device::BluetoothAdapter> adapter);
  SavedDeviceRegistry(const SavedDeviceRegistry&) = delete;
  SavedDeviceRegistry& operator=(const SavedDeviceRegistry&) = delete;
  ~SavedDeviceRegistry();

  // Registers preferences used by this class in the provided |registry|.
  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  // Saves the account association (|mac_address|, |account_key|) to disk.
  // Returns true on success, false on failure.
  bool SaveAccountAssociation(const std::string& mac_address,
                              const std::vector<uint8_t>& account_key);

  // Deletes the |mac_address| -> account key record from prefs based on
  // |mac address|. Returns true on success, false on failure.
  bool DeleteAccountKey(const std::string& mac_address);

  // Deletes the mac address -> |account_key| record from prefs based on
  // |account_key|. Returns true on success, false on failure.
  bool DeleteAccountKey(const std::vector<uint8_t>& account_key);

  // Retrieves an account key from disk if available, otherwise returns an
  // empty vector.
  std::optional<const std::vector<uint8_t>> GetAccountKey(
      const std::string& mac_address);

  // Checks if the account key is in the registry.
  bool IsAccountKeySavedToRegistry(const std::vector<uint8_t>& account_key);

 private:
  // Cross check the list of devices in the registry with the devices paired
  // to the BluetoothAdapter to see if any devices need to be removed from the
  // registry if they are no longer paired to the adapter. This can happen if
  // a primary user pairs and saves a device to their account, logs out, then a
  // secondary user logs in, forgets the device from the Bluetooth pairing
  // menu. When the primary user logs back in, we need to reflect the device is
  // no longer paired locally in the registry.
  void RemoveDevicesIfRemovedFromDifferentUser(PrefService* pref_service);

  // Flags cross checking the registry with the devices paired to the adapter
  // to see if any devices need to be removed from the registry (see
  // |RemoveDevicesIfRemovedFromDifferentUser|). This only needs to happen once
  // per session, which is why it is flagged. Everything else following during
  // the user session will be immediately reflected here.
  bool has_updated_saved_devices_registry_ = false;

  scoped_refptr<device::BluetoothAdapter> adapter_;
};

}  // namespace quick_pair
}  // namespace ash

#endif  // ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_SAVED_DEVICE_REGISTRY_H_