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 "components/web_bluetooth/web_bluetooth_device_id.h" |
14 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 15 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
15 #include "crypto/random.h" | |
16 | 16 |
17 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
| 18 using web_bluetooth::WebBluetoothDeviceId; |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
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() {} | 22 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} |
39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | 23 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} |
40 | 24 |
41 const std::string& BluetoothAllowedDevicesMap::AddDevice( | 25 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( |
42 const url::Origin& origin, | 26 const url::Origin& origin, |
43 const std::string& device_address, | 27 const std::string& device_address, |
44 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 28 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
45 VLOG(1) << "Adding a device to Map of Allowed Devices."; | 29 VLOG(1) << "Adding a device to Map of Allowed Devices."; |
46 | 30 |
47 // "Unique" Origins generate the same key in maps, therefore are not | 31 // "Unique" Origins generate the same key in maps, therefore are not |
48 // supported. | 32 // supported. |
49 CHECK(!origin.unique()); | 33 CHECK(!origin.unique()); |
50 | 34 |
51 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; | 35 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); | 36 auto id_iter = device_address_to_id_map.find(device_address); |
53 if (id_iter != device_address_to_id_map.end()) { | 37 if (id_iter != device_address_to_id_map.end()) { |
54 VLOG(1) << "Device already in map of allowed devices."; | 38 VLOG(1) << "Device already in map of allowed devices."; |
55 const auto& device_id = id_iter->second; | 39 const auto& device_id = id_iter->second; |
56 | 40 |
57 AddUnionOfServicesTo( | 41 AddUnionOfServicesTo( |
58 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 42 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
59 | 43 |
60 return origin_to_device_address_to_id_map_[origin][device_address]; | 44 return origin_to_device_address_to_id_map_[origin][device_address]; |
61 } | 45 } |
62 const std::string device_id = GenerateDeviceId(); | 46 const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
63 VLOG(1) << "Id generated for device: " << device_id; | 47 VLOG(1) << "Id generated for device: " << device_id; |
64 | 48 |
65 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 49 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; | 50 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
67 AddUnionOfServicesTo( | 51 AddUnionOfServicesTo( |
68 options, &origin_to_device_id_to_services_map_[origin][device_id]); | 52 options, &origin_to_device_id_to_services_map_[origin][device_id]); |
69 | 53 |
70 CHECK(device_id_set_.insert(device_id).second); | 54 CHECK(device_id_set_.insert(device_id).second); |
71 | 55 |
72 return origin_to_device_address_to_id_map_[origin][device_address]; | 56 return origin_to_device_address_to_id_map_[origin][device_address]; |
73 } | 57 } |
74 | 58 |
75 void BluetoothAllowedDevicesMap::RemoveDevice( | 59 void BluetoothAllowedDevicesMap::RemoveDevice( |
76 const url::Origin& origin, | 60 const url::Origin& origin, |
77 const std::string& device_address) { | 61 const std::string& device_address) { |
78 const std::string device_id = GetDeviceId(origin, device_address); | 62 const WebBluetoothDeviceId* device_id_ptr = |
79 DCHECK(!device_id.empty()); | 63 GetDeviceId(origin, device_address); |
| 64 DCHECK(device_id_ptr != nullptr); |
| 65 |
| 66 // We make a copy because we are going to remove the original value from its |
| 67 // map. |
| 68 WebBluetoothDeviceId device_id = *device_id_ptr; |
80 | 69 |
81 // 1. Remove from all three maps. | 70 // 1. Remove from all three maps. |
82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 71 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)); | 72 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
84 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 73 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
85 | 74 |
86 // 2. Remove empty map for origin. | 75 // 2. Remove empty map for origin. |
87 if (origin_to_device_address_to_id_map_[origin].empty()) { | 76 if (origin_to_device_address_to_id_map_[origin].empty()) { |
88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 77 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 78 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 79 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
91 } | 80 } |
92 | 81 |
93 // 3. Remove from set of ids. | 82 // 3. Remove from set of ids. |
94 CHECK(device_id_set_.erase(device_id)); | 83 CHECK(device_id_set_.erase(device_id)); |
95 } | 84 } |
96 | 85 |
97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 86 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( |
98 const url::Origin& origin, | 87 const url::Origin& origin, |
99 const std::string& device_address) { | 88 const std::string& device_address) { |
100 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 89 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()) { | 90 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
102 return base::EmptyString(); | 91 return nullptr; |
103 } | 92 } |
104 | 93 |
105 const auto& device_address_to_id_map = address_map_iter->second; | 94 const auto& device_address_to_id_map = address_map_iter->second; |
106 | 95 |
107 auto id_iter = device_address_to_id_map.find(device_address); | 96 auto id_iter = device_address_to_id_map.find(device_address); |
108 if (id_iter == device_address_to_id_map.end()) { | 97 if (id_iter == device_address_to_id_map.end()) { |
109 return base::EmptyString(); | 98 return nullptr; |
110 } | 99 } |
111 return id_iter->second; | 100 return &(id_iter->second); |
112 } | 101 } |
113 | 102 |
114 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 103 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
115 const url::Origin& origin, | 104 const url::Origin& origin, |
116 const std::string& device_id) { | 105 const WebBluetoothDeviceId& device_id) { |
117 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 106 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()) { | 107 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
119 return base::EmptyString(); | 108 return base::EmptyString(); |
120 } | 109 } |
121 | 110 |
122 const auto& device_id_to_address_map = id_map_iter->second; | 111 const auto& device_id_to_address_map = id_map_iter->second; |
123 | 112 |
124 auto id_iter = device_id_to_address_map.find(device_id); | 113 auto id_iter = device_id_to_address_map.find(device_id); |
125 | 114 |
126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 115 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
127 : id_iter->second; | 116 : id_iter->second; |
128 } | 117 } |
129 | 118 |
130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 119 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
131 const url::Origin& origin, | 120 const url::Origin& origin, |
132 const std::string& device_id, | 121 const WebBluetoothDeviceId& device_id, |
133 const BluetoothUUID& service_uuid) const { | 122 const BluetoothUUID& service_uuid) const { |
134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 123 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
135 return false; | 124 return false; |
136 } | 125 } |
137 | 126 |
138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 127 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()) { | 128 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
140 return false; | 129 return false; |
141 } | 130 } |
142 | 131 |
143 const auto& device_id_to_services_map = id_map_iter->second; | 132 const auto& device_id_to_services_map = id_map_iter->second; |
144 | 133 |
145 auto id_iter = device_id_to_services_map.find(device_id); | 134 auto id_iter = device_id_to_services_map.find(device_id); |
146 | 135 |
147 return id_iter == device_id_to_services_map.end() | 136 return id_iter == device_id_to_services_map.end() |
148 ? false | 137 ? false |
149 : ContainsKey(id_iter->second, service_uuid); | 138 : ContainsKey(id_iter->second, service_uuid); |
150 } | 139 } |
151 | 140 |
152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 141 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
153 std::string device_id = GetBase64Id(); | 142 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); |
154 while (ContainsKey(device_id_set_, device_id)) { | 143 while (ContainsKey(device_id_set_, device_id)) { |
155 LOG(WARNING) << "Generated repeated id."; | 144 LOG(WARNING) << "Generated repeated id."; |
156 device_id = GetBase64Id(); | 145 device_id = WebBluetoothDeviceId::Create(); |
157 } | 146 } |
158 return device_id; | 147 return device_id; |
159 } | 148 } |
160 | 149 |
161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 150 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 151 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
163 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* | 152 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
164 unionOfServices) { | 153 unionOfServices) { |
165 for (const auto& filter : options->filters) { | 154 for (const auto& filter : options->filters) { |
166 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 155 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
167 unionOfServices->insert(uuid.value()); | 156 unionOfServices->insert(uuid.value()); |
168 } | 157 } |
169 } | 158 } |
170 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 159 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
171 unionOfServices->insert(uuid.value()); | 160 unionOfServices->insert(uuid.value()); |
172 } | 161 } |
173 } | 162 } |
174 | 163 |
175 } // namespace content | 164 } // namespace content |
OLD | NEW |