OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" |
6 | 6 |
7 #include <string> | |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/base64.h" | |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/optional.h" | 11 #include "base/optional.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 14 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
15 #include "crypto/random.h" | 15 #include "content/common/bluetooth/bluetooth_device_id.h" |
16 | 16 |
17 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | |
22 const size_t kIdLength = 16 /* 128bits */; | |
23 | |
24 std::string GetBase64Id() { | |
25 std::string bytes( | |
26 kIdLength + 1 /* to avoid bytes being reallocated by WriteInto */, '\0'); | |
27 | |
28 crypto::RandBytes( | |
29 base::WriteInto(&bytes /* str */, kIdLength + 1 /* length_with_null */), | |
30 kIdLength); | |
31 | |
32 base::Base64Encode(bytes, &bytes); | |
33 | |
34 return bytes; | |
35 } | |
36 } // namespace | |
37 | |
38 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} | 21 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
40 | 23 |
41 const std::string& BluetoothAllowedDevicesMap::AddDevice( | 24 const BluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
42 const url::Origin& origin, | 25 const url::Origin& origin, |
43 const std::string& device_address, | 26 const std::string& device_address, |
44 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
45 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 28 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
46 | 29 |
47 // "Unique" Origins generate the same key in maps, therefore are not | 30 // "Unique" Origins generate the same key in maps, therefore are not |
48 // supported. | 31 // supported. |
49 CHECK(!origin.unique()); | 32 CHECK(!origin.unique()); |
50 | 33 |
51 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | 34 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; |
52 auto id_iter = device_address_to_id_map.find(device_address); | 35 auto id_iter = device_address_to_id_map.find(device_address); |
53 if (id_iter != device_address_to_id_map.end()) { | 36 if (id_iter != device_address_to_id_map.end()) { |
54 VLOG(1) << "Device already in map of allowed devices."; | 37 VLOG(1) << "Device already in map of allowed devices."; |
55 const auto& device_id = id_iter->second; | 38 const auto& device_id = id_iter->second; |
56 | 39 |
57 AddUnionOfServicesTo( | 40 AddUnionOfServicesTo( |
58 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 41 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
59 | 42 |
60 return origin_to_device_address_to_id_map_[origin][device_address]; | 43 return origin_to_device_address_to_id_map_[origin][device_address]; |
61 } | 44 } |
62 const std::string device_id = GenerateDeviceId(); | 45 const BluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
63 VLOG(1) << "Id generated for device: " << device_id; | 46 VLOG(1) << "Id generated for device: " << device_id.str(); |
64 | 47 |
65 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 48 origin_to_device_address_to_id_map_[origin][device_address] = device_id; |
66 origin_to_device_id_to_address_map_[origin][device_id] = device_address; | 49 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
67 AddUnionOfServicesTo( | 50 AddUnionOfServicesTo( |
68 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 51 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
69 | 52 |
70 CHECK(device_id_set_.insert(device_id).second); | 53 CHECK(device_id_set_.insert(device_id).second); |
71 | 54 |
72 return origin_to_device_address_to_id_map_[origin][device_address]; | 55 return origin_to_device_address_to_id_map_[origin][device_address]; |
73 } | 56 } |
74 | 57 |
75 void BluetoothAllowedDevicesMap::RemoveDevice( | 58 void BluetoothAllowedDevicesMap::RemoveDevice( |
76 const url::Origin& origin, | 59 const url::Origin& origin, |
77 const std::string& device_address) { | 60 const std::string& device_address) { |
78 const std::string device_id = GetDeviceId(origin, device_address); | 61 const base::Optional<BluetoothDeviceId> device_id = |
79 DCHECK(!device_id.empty()); | 62 GetDeviceId(origin, device_address); |
63 DCHECK(device_id); | |
80 | 64 |
81 // 1. Remove from all three maps. | 65 // 1. Remove from all three maps. |
82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 66 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
83 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 67 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id.value())); |
84 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 68 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id.value())); |
85 | 69 |
86 // 2. Remove empty map for origin. | 70 // 2. Remove empty map for origin. |
87 if (origin_to_device_address_to_id_map_[origin].empty()) { | 71 if (origin_to_device_address_to_id_map_[origin].empty()) { |
88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 72 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 73 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 74 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
91 } | 75 } |
92 | 76 |
93 // 3. Remove from set of ids. | 77 // 3. Remove from set of ids. |
94 CHECK(device_id_set_.erase(device_id)); | 78 CHECK(device_id_set_.erase(device_id.value())); |
95 } | 79 } |
96 | 80 |
97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 81 base::Optional<BluetoothDeviceId> BluetoothAllowedDevicesMap::GetDeviceId( |
98 const url::Origin& origin, | 82 const url::Origin& origin, |
99 const std::string& device_address) { | 83 const std::string& device_address) { |
100 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 84 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); |
101 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { | 85 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
102 return base::EmptyString(); | 86 return base::nullopt; |
103 } | 87 } |
104 | 88 |
105 const auto& device_address_to_id_map = address_map_iter->second; | 89 const auto& device_address_to_id_map = address_map_iter->second; |
106 | 90 |
107 auto id_iter = device_address_to_id_map.find(device_address); | 91 auto id_iter = device_address_to_id_map.find(device_address); |
108 if (id_iter == device_address_to_id_map.end()) { | 92 if (id_iter == device_address_to_id_map.end()) { |
109 return base::EmptyString(); | 93 return base::nullopt; |
110 } | 94 } |
111 return id_iter->second; | 95 return base::make_optional(id_iter->second); |
Jeffrey Yasskin
2016/06/03 17:22:04
It'd be nice to avoid needing a copy here. Have th
ortuno
2016/06/06 22:22:59
Done.
| |
112 } | 96 } |
113 | 97 |
114 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 98 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
115 const url::Origin& origin, | 99 const url::Origin& origin, |
116 const std::string& device_id) { | 100 const BluetoothDeviceId& device_id) { |
117 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 101 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
118 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | 102 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
119 return base::EmptyString(); | 103 return base::EmptyString(); |
120 } | 104 } |
121 | 105 |
122 const auto& device_id_to_address_map = id_map_iter->second; | 106 const auto& device_id_to_address_map = id_map_iter->second; |
123 | 107 |
124 auto id_iter = device_id_to_address_map.find(device_id); | 108 auto id_iter = device_id_to_address_map.find(device_id); |
125 | 109 |
126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 110 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
127 : id_iter->second; | 111 : id_iter->second; |
128 } | 112 } |
129 | 113 |
130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 114 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
131 const url::Origin& origin, | 115 const url::Origin& origin, |
132 const std::string& device_id, | 116 const BluetoothDeviceId& device_id, |
133 const BluetoothUUID& service_uuid) const { | 117 const BluetoothUUID& service_uuid) const { |
134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 118 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
135 return false; | 119 return false; |
136 } | 120 } |
137 | 121 |
138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 122 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); |
139 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { | 123 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
140 return false; | 124 return false; |
141 } | 125 } |
142 | 126 |
143 const auto& device_id_to_services_map = id_map_iter->second; | 127 const auto& device_id_to_services_map = id_map_iter->second; |
144 | 128 |
145 auto id_iter = device_id_to_services_map.find(device_id); | 129 auto id_iter = device_id_to_services_map.find(device_id); |
146 | 130 |
147 return id_iter == device_id_to_services_map.end() | 131 return id_iter == device_id_to_services_map.end() |
148 ? false | 132 ? false |
149 : ContainsKey(id_iter->second, service_uuid); | 133 : ContainsKey(id_iter->second, service_uuid); |
150 } | 134 } |
151 | 135 |
152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 136 BluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
153 std::string device_id = GetBase64Id(); | 137 BluetoothDeviceId device_id = BluetoothDeviceId::Create(); |
154 while (ContainsKey(device_id_set_, device_id)) { | 138 while (ContainsKey(device_id_set_, device_id)) { |
155 LOG(WARNING) << "Generated repeated id."; | 139 LOG(WARNING) << "Generated repeated id."; |
156 device_id = GetBase64Id(); | 140 device_id = BluetoothDeviceId::Create(); |
157 } | 141 } |
158 return device_id; | 142 return device_id; |
159 } | 143 } |
160 | 144 |
161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 145 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 146 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
163 std::unordered_set<BluetoothUUID>* unionOfServices) { | 147 std::unordered_set<BluetoothUUID>* unionOfServices) { |
164 for (const auto& filter : options->filters) { | 148 for (const auto& filter : options->filters) { |
165 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 149 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
166 unionOfServices->insert(uuid.value()); | 150 unionOfServices->insert(uuid.value()); |
167 } | 151 } |
168 } | 152 } |
169 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 153 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
170 unionOfServices->insert(uuid.value()); | 154 unionOfServices->insert(uuid.value()); |
171 } | 155 } |
172 } | 156 } |
173 | 157 |
174 } // namespace content | 158 } // namespace content |
OLD | NEW |