OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/values.h" | |
11 #include "components/prefs/pref_registry_simple.h" | |
12 #include "components/prefs/pref_service.h" | |
13 #include "components/prefs/scoped_user_pref_update.h" | |
14 #include "components/proximity_auth/ble/pref_names.h" | |
15 #include "components/proximity_auth/logging/logging.h" | |
16 | |
17 namespace proximity_auth { | |
18 | |
19 BluetoothLowEnergyDeviceWhitelist::BluetoothLowEnergyDeviceWhitelist( | |
20 PrefService* pref_service) | |
21 : pref_service_(pref_service) { | |
22 } | |
23 | |
24 BluetoothLowEnergyDeviceWhitelist::~BluetoothLowEnergyDeviceWhitelist() { | |
25 } | |
26 | |
27 // static | |
28 void BluetoothLowEnergyDeviceWhitelist::RegisterPrefs( | |
29 PrefRegistrySimple* registry) { | |
30 registry->RegisterDictionaryPref(prefs::kBluetoothLowEnergyDeviceWhitelist); | |
31 } | |
32 | |
33 bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithAddress( | |
34 const std::string& bluetooth_address) const { | |
35 return pref_service_->GetDictionary(prefs::kBluetoothLowEnergyDeviceWhitelist) | |
36 ->HasKey(bluetooth_address); | |
37 } | |
38 | |
39 bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithPublicKey( | |
40 const std::string& public_key) const { | |
41 return !GetDeviceAddress(public_key).empty(); | |
42 } | |
43 | |
44 std::string BluetoothLowEnergyDeviceWhitelist::GetDevicePublicKey( | |
45 const std::string& bluetooth_address) const { | |
46 std::string public_key; | |
47 GetWhitelistPrefs()->GetStringWithoutPathExpansion(bluetooth_address, | |
48 &public_key); | |
49 return public_key; | |
50 } | |
51 | |
52 std::string BluetoothLowEnergyDeviceWhitelist::GetDeviceAddress( | |
53 const std::string& public_key) const { | |
54 const base::DictionaryValue* device_whitelist = GetWhitelistPrefs(); | |
55 for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd(); | |
56 it.Advance()) { | |
57 std::string value_string; | |
58 DCHECK(it.value().IsType(base::Value::Type::STRING)); | |
59 if (it.value().GetAsString(&value_string) && value_string == public_key) | |
60 return it.key(); | |
61 } | |
62 return std::string(); | |
63 } | |
64 | |
65 std::vector<std::string> BluetoothLowEnergyDeviceWhitelist::GetPublicKeys() | |
66 const { | |
67 std::vector<std::string> public_keys; | |
68 const base::DictionaryValue* device_whitelist = GetWhitelistPrefs(); | |
69 for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd(); | |
70 it.Advance()) { | |
71 std::string value_string; | |
72 DCHECK(it.value().IsType(base::Value::Type::STRING)); | |
73 it.value().GetAsString(&value_string); | |
74 public_keys.push_back(value_string); | |
75 } | |
76 return public_keys; | |
77 } | |
78 | |
79 void BluetoothLowEnergyDeviceWhitelist::AddOrUpdateDevice( | |
80 const std::string& bluetooth_address, | |
81 const std::string& public_key) { | |
82 if (HasDeviceWithPublicKey(public_key) && | |
83 GetDeviceAddress(public_key) != bluetooth_address) { | |
84 PA_LOG(WARNING) << "Two devices with different bluetooth address, but the " | |
85 "same public key were added: " << public_key; | |
86 RemoveDeviceWithPublicKey(public_key); | |
87 } | |
88 | |
89 DictionaryPrefUpdate device_whitelist_update( | |
90 pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist); | |
91 device_whitelist_update->SetStringWithoutPathExpansion(bluetooth_address, | |
92 public_key); | |
93 } | |
94 | |
95 bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithAddress( | |
96 const std::string& bluetooth_address) { | |
97 DictionaryPrefUpdate device_whitelist_update( | |
98 pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist); | |
99 return device_whitelist_update->RemoveWithoutPathExpansion(bluetooth_address, | |
100 nullptr); | |
101 } | |
102 | |
103 bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithPublicKey( | |
104 const std::string& public_key) { | |
105 return RemoveDeviceWithAddress(GetDeviceAddress(public_key)); | |
106 } | |
107 | |
108 const base::DictionaryValue* | |
109 BluetoothLowEnergyDeviceWhitelist::GetWhitelistPrefs() const { | |
110 return pref_service_->GetDictionary( | |
111 prefs::kBluetoothLowEnergyDeviceWhitelist); | |
112 } | |
113 | |
114 } // namespace proximity_auth | |
OLD | NEW |