File: device_address_map.h

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 (88 lines) | stat: -rw-r--r-- 3,522 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
// Copyright 2023 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_DEVICE_ADDRESS_MAP_H_
#define ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_DEVICE_ADDRESS_MAP_H_

#include <optional>
#include <string>

#include "ash/quick_pair/common/device.h"
#include "base/containers/flat_map.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"

class PrefRegistrySimple;

namespace ash::quick_pair {

// Saves a mapping from mac address to model ID. Provides methods to persist
// or evict mac address -> model ID records from local state prefs. Also
// provides convenience methods for adding to the mapping given a device.
class DeviceAddressMap {
 public:
  // TODO(235117226): Migrate this pref to clear old entries/make sense with
  // renaming.
  static constexpr char kDeviceAddressMapPref[] = "fast_pair.device_id_map";

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

  DeviceAddressMap();
  DeviceAddressMap(const DeviceAddressMap&) = delete;
  DeviceAddressMap& operator=(const DeviceAddressMap&) = delete;
  ~DeviceAddressMap();

  // Saves mac address -> model ID records for the devices for both
  // the BLE and Classic address in memory, stored in mac_address_to_model_id.
  bool SaveModelIdForDevice(scoped_refptr<Device> device);

  // Persists the mac address -> model ID records for |device|
  // to local state prefs. Returns true if a record was persisted, false
  // otherwise.
  bool PersistRecordsForDevice(scoped_refptr<Device> device);

  // Evicts the |mac_address| -> model ID record in mac_address_to_model_id_
  // from local state prefs. Returns true if the record was evicted, false if
  // there was no |mac_address| record to evict.
  bool EvictMacAddressRecord(const std::string& mac_address);

  // Returns the model ID for |mac_address|, or std::nullopt if a matching
  // model ID isn't found.
  std::optional<const std::string> GetModelIdForMacAddress(
      const std::string& mac_address);

  // Returns true if there are mac address -> |model_id| records in
  // local state prefs, false otherwise.
  bool HasPersistedRecordsForModelId(const std::string& model_id);

 private:
  FRIEND_TEST_ALL_PREFIXES(FastPairRepositoryImplTest, PersistDeviceImages);
  FRIEND_TEST_ALL_PREFIXES(FastPairRepositoryImplTest,
                           PersistDeviceImagesNoMacAddress);
  FRIEND_TEST_ALL_PREFIXES(FastPairRepositoryImplTest, EvictDeviceImages);

  // Persists the |mac_address| -> model ID record in mac_address_to_model_id_
  // to local state prefs. Returns true if the record was persisted, false
  // if no record exists for |mac_address| or there was an error when
  // persisting.
  bool PersistMacAddressRecord(const std::string& mac_address);

  // Loads mac address -> model ID records persisted in prefs to
  // mac_address_to_model_id_.
  void LoadPersistedRecordsFromPrefs();

  // Clears the in-memory map and reloads from prefs. Used by tests.
  void RefreshCacheForTest();

  // Used to lazily load saved records from prefs.
  bool loaded_records_from_prefs_ = false;
  base::flat_map<std::string, std::string> mac_address_to_model_id_;
  base::WeakPtrFactory<DeviceAddressMap> weak_ptr_factory_{this};
};

}  // namespace ash::quick_pair

#endif  // ASH_QUICK_PAIR_REPOSITORY_FAST_PAIR_DEVICE_ADDRESS_MAP_H_