Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / proximity_auth / ble / bluetooth_low_energy_device_whitelist.cc
blob8bf905e4aa0ecd60bf486b8c4a053ebbac68d3fb
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.
5 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h"
7 #include <vector>
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/values.h"
15 #include "components/proximity_auth/ble/pref_names.h"
16 #include "components/proximity_auth/logging/logging.h"
18 namespace proximity_auth {
20 BluetoothLowEnergyDeviceWhitelist::BluetoothLowEnergyDeviceWhitelist(
21 PrefService* pref_service)
22 : pref_service_(pref_service) {
25 BluetoothLowEnergyDeviceWhitelist::~BluetoothLowEnergyDeviceWhitelist() {
28 // static
29 void BluetoothLowEnergyDeviceWhitelist::RegisterPrefs(
30 PrefRegistrySimple* registry) {
31 registry->RegisterDictionaryPref(prefs::kBluetoothLowEnergyDeviceWhitelist);
34 bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithAddress(
35 const std::string& bluetooth_address) const {
36 return pref_service_->GetDictionary(prefs::kBluetoothLowEnergyDeviceWhitelist)
37 ->HasKey(bluetooth_address);
40 bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithPublicKey(
41 const std::string& public_key) const {
42 return !GetDeviceAddress(public_key).empty();
45 std::string BluetoothLowEnergyDeviceWhitelist::GetDevicePublicKey(
46 const std::string& bluetooth_address) const {
47 std::string public_key;
48 GetWhitelistPrefs()->GetStringWithoutPathExpansion(bluetooth_address,
49 &public_key);
50 return public_key;
53 std::string BluetoothLowEnergyDeviceWhitelist::GetDeviceAddress(
54 const std::string& public_key) const {
55 const base::DictionaryValue* device_whitelist = GetWhitelistPrefs();
56 for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd();
57 it.Advance()) {
58 std::string value_string;
59 DCHECK(it.value().IsType(base::Value::TYPE_STRING));
60 if (it.value().GetAsString(&value_string) && value_string == public_key)
61 return it.key();
63 return std::string();
66 std::vector<std::string> BluetoothLowEnergyDeviceWhitelist::GetPublicKeys()
67 const {
68 std::vector<std::string> public_keys;
69 const base::DictionaryValue* device_whitelist = GetWhitelistPrefs();
70 for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd();
71 it.Advance()) {
72 std::string value_string;
73 DCHECK(it.value().IsType(base::Value::TYPE_STRING));
74 it.value().GetAsString(&value_string);
75 public_keys.push_back(value_string);
77 return public_keys;
80 void BluetoothLowEnergyDeviceWhitelist::AddOrUpdateDevice(
81 const std::string& bluetooth_address,
82 const std::string& public_key) {
83 if (HasDeviceWithPublicKey(public_key) &&
84 GetDeviceAddress(public_key) != bluetooth_address) {
85 PA_LOG(WARNING) << "Two devices with different bluetooth address, but the "
86 "same public key were added: " << public_key;
87 RemoveDeviceWithPublicKey(public_key);
90 DictionaryPrefUpdate device_whitelist_update(
91 pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist);
92 device_whitelist_update->SetStringWithoutPathExpansion(bluetooth_address,
93 public_key);
96 bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithAddress(
97 const std::string& bluetooth_address) {
98 DictionaryPrefUpdate device_whitelist_update(
99 pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist);
100 return device_whitelist_update->RemoveWithoutPathExpansion(bluetooth_address,
101 nullptr);
104 bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithPublicKey(
105 const std::string& public_key) {
106 return RemoveDeviceWithAddress(GetDeviceAddress(public_key));
109 const base::DictionaryValue*
110 BluetoothLowEnergyDeviceWhitelist::GetWhitelistPrefs() const {
111 return pref_service_->GetDictionary(
112 prefs::kBluetoothLowEnergyDeviceWhitelist);
115 } // namespace proximity_auth