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 "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "content/common/bluetooth/bluetooth_scan_filter.h" | |
14 #include "crypto/random.h" | |
15 #include "device/bluetooth/bluetooth_uuid.h" | |
16 | |
17 using device::BluetoothUUID; | |
18 | |
19 namespace content { | |
20 | |
21 namespace { | |
22 const std::string GetBase64Id() { | |
23 std::string bytes(16 /* 128bits */, '\0'); | |
24 | |
25 crypto::RandBytes(base::WriteInto(&bytes /* str */, | |
26 bytes.size() + 1 /* lenght_with_null */), | |
Jeffrey Yasskin
2016/01/06 00:47:57
s/lenght/length/
ortuno
2016/01/13 01:41:43
Done.
| |
27 bytes.size()); | |
28 | |
29 base::Base64Encode(bytes, &bytes); | |
30 | |
31 return bytes; | |
32 } | |
33 } // namespace | |
34 | |
35 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | |
36 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | |
37 | |
38 const std::string& BluetoothAllowedDevicesMap::AddDevice( | |
39 const std::string& origin, | |
40 const std::string& device_address, | |
41 const std::vector<BluetoothScanFilter>& filters, | |
42 const std::vector<BluetoothUUID>& optional_services) { | |
43 VLOG(1) << "Adding a device to Map of Allowed Devices."; | |
44 | |
45 if (ContainsKey(origin_to_device_address_to_id_map_[origin], | |
46 device_address)) { | |
47 VLOG(1) << "Device already in map of allowed devices."; | |
48 return origin_to_device_address_to_id_map_[origin][device_address]; | |
49 } | |
50 const std::string device_id = GenerateDeviceId(origin); | |
51 VLOG(1) << "Id generated for device: " << device_id; | |
52 | |
53 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | |
54 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | |
55 origin_to_device_id_to_services_map_[origin][device_id] = | |
56 UnionOfServices(filters, optional_services); | |
57 | |
58 return origin_to_device_address_to_id_map_[origin][device_address]; | |
59 } | |
60 | |
61 void BluetoothAllowedDevicesMap::RemoveDevice( | |
62 const std::string& origin, | |
63 const std::string& device_address) { | |
64 if (!HasDevicePermissionFromDeviceAddress(origin, device_address)) { | |
65 return; | |
66 } | |
67 // 1. Remove from all three maps. | |
68 std::string device_id = | |
69 origin_to_device_address_to_id_map_[origin][device_address]; | |
70 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | |
71 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | |
72 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | |
73 | |
74 // 2. Remove empty map for origin. | |
75 if (origin_to_device_address_to_id_map_[origin].empty()) { | |
76 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | |
77 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | |
78 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | |
79 } | |
80 } | |
81 | |
82 bool BluetoothAllowedDevicesMap::HasDevicePermissionFromDeviceId( | |
83 const std::string& origin, | |
84 const std::string& device_id) { | |
85 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
86 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
87 return false; | |
88 } | |
89 return ContainsKey(id_map_iter->second, device_id); | |
90 } | |
91 | |
92 bool BluetoothAllowedDevicesMap::HasDevicePermissionFromDeviceAddress( | |
93 const std::string& origin, | |
94 const std::string& device_address) { | |
95 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | |
96 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | |
97 return false; | |
98 } | |
99 return ContainsKey(address_map_iter->second, device_address); | |
100 } | |
101 | |
102 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | |
103 const std::string& origin, | |
104 const std::string& device_address) { | |
105 CHECK(HasDevicePermissionFromDeviceAddress(origin, device_address)); | |
106 return origin_to_device_address_to_id_map_[origin][device_address]; | |
107 } | |
108 | |
109 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | |
110 const std::string& origin, | |
111 const std::string& device_id) { | |
112 CHECK(HasDevicePermissionFromDeviceId(origin, device_id)); | |
113 return origin_to_device_id_to_address_map_[origin][device_id]; | |
114 } | |
115 | |
116 const std::string BluetoothAllowedDevicesMap::GenerateDeviceId( | |
Jeffrey Yasskin
2016/01/06 00:47:56
Don't make by-value return types const: it prevent
ortuno
2016/01/13 01:41:43
Done. You mean it prevents the optimization in whi
Jeffrey Yasskin
2016/01/13 02:31:36
Yep.
| |
117 const std::string& origin) { | |
118 std::string device_id = GetBase64Id(); | |
119 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
120 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
121 return device_id; | |
122 } | |
123 while (ContainsKey(id_map_iter->second, device_id)) { | |
124 LOG(WARNING) << "Generated repeated id."; | |
125 device_id = GetBase64Id(); | |
126 } | |
127 return GetBase64Id(); | |
Jeffrey Yasskin
2016/01/06 00:47:56
Return device_id, right?
ortuno
2016/01/13 01:41:43
Done.
| |
128 } | |
129 | |
130 const std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | |
Jeffrey Yasskin
2016/01/06 00:47:56
Un-const this too.
ortuno
2016/01/13 01:41:43
Done.
| |
131 const std::vector<BluetoothScanFilter>& filters, | |
132 const std::vector<BluetoothUUID>& optional_services) { | |
133 std::set<std::string> unionOfServices; | |
134 for (const BluetoothScanFilter& filter : filters) { | |
135 for (const BluetoothUUID& uuid : filter.services) { | |
136 unionOfServices.insert(uuid.canonical_value()); | |
137 } | |
138 } | |
139 for (const BluetoothUUID& uuid : optional_services) { | |
140 unionOfServices.insert(uuid.canonical_value()); | |
141 } | |
142 return unionOfServices; | |
143 } | |
144 | |
145 } // namespace content | |
OLD | NEW |