File: proximity_auth_pref_manager.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 (112 lines) | stat: -rw-r--r-- 4,102 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
// 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/proximity_auth_pref_manager.h"

#include <memory>
#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/logging/logging.h"
#include "components/proximity_auth/proximity_auth_pref_names.h"

namespace proximity_auth {

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

ProximityAuthPrefManager::~ProximityAuthPrefManager() {}

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

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

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

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

std::string ProximityAuthPrefManager::GetDeviceAddress(
    const std::string& public_key) const {
  const base::DictionaryValue* remote_ble_devices = GetRemoteBleDevices();
  for (base::DictionaryValue::Iterator it(*remote_ble_devices); !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> ProximityAuthPrefManager::GetPublicKeys() const {
  std::vector<std::string> public_keys;
  const base::DictionaryValue* remote_ble_devices = GetRemoteBleDevices();
  for (base::DictionaryValue::Iterator it(*remote_ble_devices); !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 ProximityAuthPrefManager::AddOrUpdateDevice(
    const std::string& bluetooth_address,
    const std::string& public_key) {
  PA_LOG(INFO) << "Adding " << public_key << " , " << bluetooth_address
               << " pair.";
  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 remote_ble_devices_update(
      pref_service_, prefs::kProximityAuthRemoteBleDevices);
  remote_ble_devices_update->SetStringWithoutPathExpansion(bluetooth_address,
                                                           public_key);
}

bool ProximityAuthPrefManager::RemoveDeviceWithAddress(
    const std::string& bluetooth_address) {
  DictionaryPrefUpdate remote_ble_devices_update(
      pref_service_, prefs::kProximityAuthRemoteBleDevices);
  return remote_ble_devices_update->RemoveWithoutPathExpansion(
      bluetooth_address, nullptr);
}

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

const base::DictionaryValue* ProximityAuthPrefManager::GetRemoteBleDevices()
    const {
  return pref_service_->GetDictionary(prefs::kProximityAuthRemoteBleDevices);
}

}  // namespace proximity_auth