File: bluetooth_low_energy_device_whitelist.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (114 lines) | stat: -rw-r--r-- 4,158 bytes parent folder | download
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h"

#include <vector>

#include "base/macros.h"
#include "base/values.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/proximity_auth/ble/pref_names.h"
#include "components/proximity_auth/logging/logging.h"

namespace proximity_auth {

BluetoothLowEnergyDeviceWhitelist::BluetoothLowEnergyDeviceWhitelist(
    PrefService* pref_service)
    : pref_service_(pref_service) {
}

BluetoothLowEnergyDeviceWhitelist::~BluetoothLowEnergyDeviceWhitelist() {
}

// static
void BluetoothLowEnergyDeviceWhitelist::RegisterPrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterDictionaryPref(prefs::kBluetoothLowEnergyDeviceWhitelist);
}

bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithAddress(
    const std::string& bluetooth_address) const {
  return pref_service_->GetDictionary(prefs::kBluetoothLowEnergyDeviceWhitelist)
      ->HasKey(bluetooth_address);
}

bool BluetoothLowEnergyDeviceWhitelist::HasDeviceWithPublicKey(
    const std::string& public_key) const {
  return !GetDeviceAddress(public_key).empty();
}

std::string BluetoothLowEnergyDeviceWhitelist::GetDevicePublicKey(
    const std::string& bluetooth_address) const {
  std::string public_key;
  GetWhitelistPrefs()->GetStringWithoutPathExpansion(bluetooth_address,
                                                     &public_key);
  return public_key;
}

std::string BluetoothLowEnergyDeviceWhitelist::GetDeviceAddress(
    const std::string& public_key) const {
  const base::DictionaryValue* device_whitelist = GetWhitelistPrefs();
  for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd();
       it.Advance()) {
    std::string value_string;
    DCHECK(it.value().IsType(base::Value::Type::STRING));
    if (it.value().GetAsString(&value_string) && value_string == public_key)
      return it.key();
  }
  return std::string();
}

std::vector<std::string> BluetoothLowEnergyDeviceWhitelist::GetPublicKeys()
    const {
  std::vector<std::string> public_keys;
  const base::DictionaryValue* device_whitelist = GetWhitelistPrefs();
  for (base::DictionaryValue::Iterator it(*device_whitelist); !it.IsAtEnd();
       it.Advance()) {
    std::string value_string;
    DCHECK(it.value().IsType(base::Value::Type::STRING));
    it.value().GetAsString(&value_string);
    public_keys.push_back(value_string);
  }
  return public_keys;
}

void BluetoothLowEnergyDeviceWhitelist::AddOrUpdateDevice(
    const std::string& bluetooth_address,
    const std::string& public_key) {
  if (HasDeviceWithPublicKey(public_key) &&
      GetDeviceAddress(public_key) != bluetooth_address) {
    PA_LOG(WARNING) << "Two devices with different bluetooth address, but the "
                       "same public key were added: " << public_key;
    RemoveDeviceWithPublicKey(public_key);
  }

  DictionaryPrefUpdate device_whitelist_update(
      pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist);
  device_whitelist_update->SetStringWithoutPathExpansion(bluetooth_address,
                                                         public_key);
}

bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithAddress(
    const std::string& bluetooth_address) {
  DictionaryPrefUpdate device_whitelist_update(
      pref_service_, prefs::kBluetoothLowEnergyDeviceWhitelist);
  return device_whitelist_update->RemoveWithoutPathExpansion(bluetooth_address,
                                                             nullptr);
}

bool BluetoothLowEnergyDeviceWhitelist::RemoveDeviceWithPublicKey(
    const std::string& public_key) {
  return RemoveDeviceWithAddress(GetDeviceAddress(public_key));
}

const base::DictionaryValue*
BluetoothLowEnergyDeviceWhitelist::GetWhitelistPrefs() const {
  return pref_service_->GetDictionary(
      prefs::kBluetoothLowEnergyDeviceWhitelist);
}

}  // namespace proximity_auth