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/web_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 WebBluetoothDeviceId& 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 WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); |
63 VLOG(1) << "Id generated for device: " << device_id; | 46 VLOG(1) << "Id generated for device: " << device_id; |
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 WebBluetoothDeviceId* device_id_ptr = |
79 DCHECK(!device_id.empty()); | 62 GetDeviceId(origin, device_address); |
| 63 DCHECK(device_id_ptr != nullptr); |
| 64 |
| 65 // We make a copy because we are going to remove the original value from its |
| 66 // map. |
| 67 WebBluetoothDeviceId device_id = *device_id_ptr; |
80 | 68 |
81 // 1. Remove from all three maps. | 69 // 1. Remove from all three maps. |
82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 70 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)); | 71 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)); | 72 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
85 | 73 |
86 // 2. Remove empty map for origin. | 74 // 2. Remove empty map for origin. |
87 if (origin_to_device_address_to_id_map_[origin].empty()) { | 75 if (origin_to_device_address_to_id_map_[origin].empty()) { |
88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 76 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 77 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 78 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
91 } | 79 } |
92 | 80 |
93 // 3. Remove from set of ids. | 81 // 3. Remove from set of ids. |
94 CHECK(device_id_set_.erase(device_id)); | 82 CHECK(device_id_set_.erase(device_id)); |
95 } | 83 } |
96 | 84 |
97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 85 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( |
98 const url::Origin& origin, | 86 const url::Origin& origin, |
99 const std::string& device_address) { | 87 const std::string& device_address) { |
100 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 88 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()) { | 89 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
102 return base::EmptyString(); | 90 return nullptr; |
103 } | 91 } |
104 | 92 |
105 const auto& device_address_to_id_map = address_map_iter->second; | 93 const auto& device_address_to_id_map = address_map_iter->second; |
106 | 94 |
107 auto id_iter = device_address_to_id_map.find(device_address); | 95 auto id_iter = device_address_to_id_map.find(device_address); |
108 if (id_iter == device_address_to_id_map.end()) { | 96 if (id_iter == device_address_to_id_map.end()) { |
109 return base::EmptyString(); | 97 return nullptr; |
110 } | 98 } |
111 return id_iter->second; | 99 return &(id_iter->second); |
112 } | 100 } |
113 | 101 |
114 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | 102 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( |
115 const url::Origin& origin, | 103 const url::Origin& origin, |
116 const std::string& device_id) { | 104 const WebBluetoothDeviceId& device_id) { |
117 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 105 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()) { | 106 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
119 return base::EmptyString(); | 107 return base::EmptyString(); |
120 } | 108 } |
121 | 109 |
122 const auto& device_id_to_address_map = id_map_iter->second; | 110 const auto& device_id_to_address_map = id_map_iter->second; |
123 | 111 |
124 auto id_iter = device_id_to_address_map.find(device_id); | 112 auto id_iter = device_id_to_address_map.find(device_id); |
125 | 113 |
126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 114 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
127 : id_iter->second; | 115 : id_iter->second; |
128 } | 116 } |
129 | 117 |
130 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( | 118 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService( |
131 const url::Origin& origin, | 119 const url::Origin& origin, |
132 const std::string& device_id, | 120 const WebBluetoothDeviceId& device_id, |
133 const BluetoothUUID& service_uuid) const { | 121 const BluetoothUUID& service_uuid) const { |
134 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { | 122 if (BluetoothBlacklist::Get().IsExcluded(service_uuid)) { |
135 return false; | 123 return false; |
136 } | 124 } |
137 | 125 |
138 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin); | 126 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()) { | 127 if (id_map_iter == origin_to_device_id_to_services_map_.end()) { |
140 return false; | 128 return false; |
141 } | 129 } |
142 | 130 |
143 const auto& device_id_to_services_map = id_map_iter->second; | 131 const auto& device_id_to_services_map = id_map_iter->second; |
144 | 132 |
145 auto id_iter = device_id_to_services_map.find(device_id); | 133 auto id_iter = device_id_to_services_map.find(device_id); |
146 | 134 |
147 return id_iter == device_id_to_services_map.end() | 135 return id_iter == device_id_to_services_map.end() |
148 ? false | 136 ? false |
149 : ContainsKey(id_iter->second, service_uuid); | 137 : ContainsKey(id_iter->second, service_uuid); |
150 } | 138 } |
151 | 139 |
152 std::string BluetoothAllowedDevicesMap::GenerateDeviceId() { | 140 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() { |
153 std::string device_id = GetBase64Id(); | 141 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create(); |
154 while (ContainsKey(device_id_set_, device_id)) { | 142 while (ContainsKey(device_id_set_, device_id)) { |
155 LOG(WARNING) << "Generated repeated id."; | 143 LOG(WARNING) << "Generated repeated id."; |
156 device_id = GetBase64Id(); | 144 device_id = WebBluetoothDeviceId::Create(); |
157 } | 145 } |
158 return device_id; | 146 return device_id; |
159 } | 147 } |
160 | 148 |
161 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( | 149 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo( |
162 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, | 150 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options, |
163 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* | 151 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>* |
164 unionOfServices) { | 152 unionOfServices) { |
165 for (const auto& filter : options->filters) { | 153 for (const auto& filter : options->filters) { |
166 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { | 154 for (const base::Optional<BluetoothUUID>& uuid : filter->services) { |
167 unionOfServices->insert(uuid.value()); | 155 unionOfServices->insert(uuid.value()); |
168 } | 156 } |
169 } | 157 } |
170 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | 158 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { |
171 unionOfServices->insert(uuid.value()); | 159 unionOfServices->insert(uuid.value()); |
172 } | 160 } |
173 } | 161 } |
174 | 162 |
175 } // namespace content | 163 } // namespace content |
OLD | NEW |