Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: content/browser/bluetooth/bluetooth_allowed_devices_map.cc

Issue 2658473002: Refactor BluetoothAllowedDevicesMap (Closed)
Patch Set: cleaned up layout test code Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 <utility>
8 #include <vector>
9 8
10 #include "base/logging.h" 9 #include "content/browser/bluetooth/bluetooth_allowed_devices.h"
11 #include "base/optional.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_util.h"
14 #include "content/browser/bluetooth/bluetooth_blocklist.h"
15 #include "content/common/bluetooth/web_bluetooth_device_id.h"
16
17 using device::BluetoothUUID;
18 10
19 namespace content { 11 namespace content {
20 12
21 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {} 13 BluetoothAllowedDevicesMap::BluetoothAllowedDevicesMap() {}
14
22 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} 15 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {}
23 16
24 const WebBluetoothDeviceId& BluetoothAllowedDevicesMap::AddDevice( 17 content::BluetoothAllowedDevices&
25 const url::Origin& origin, 18 BluetoothAllowedDevicesMap::GetOrCreateAllowedDevices(
26 const std::string& device_address, 19 const url::Origin& origin) {
27 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) {
28 DVLOG(1) << "Adding a device to Map of Allowed Devices.";
29
30 // "Unique" Origins generate the same key in maps, therefore are not 20 // "Unique" Origins generate the same key in maps, therefore are not
31 // supported. 21 // supported.
32 CHECK(!origin.unique()); 22 CHECK(!origin.unique());
33 23 auto iter = origin_to_allowed_devices_map_.find(origin);
34 auto device_address_to_id_map = origin_to_device_address_to_id_map_[origin]; 24 if (iter == origin_to_allowed_devices_map_.end()) {
35 auto id_iter = device_address_to_id_map.find(device_address); 25 iter = origin_to_allowed_devices_map_.insert(
36 if (id_iter != device_address_to_id_map.end()) { 26 iter, std::make_pair(origin, content::BluetoothAllowedDevices()));
37 DVLOG(1) << "Device already in map of allowed devices.";
38 const auto& device_id = id_iter->second;
39
40 AddUnionOfServicesTo(
41 options, &origin_to_device_id_to_services_map_[origin][device_id]);
42
43 return origin_to_device_address_to_id_map_[origin][device_address];
44 } 27 }
45 const WebBluetoothDeviceId device_id = GenerateUniqueDeviceId(); 28 return iter->second;
46 DVLOG(1) << "Id generated for device: " << device_id;
47
48 origin_to_device_address_to_id_map_[origin][device_address] = device_id;
49 origin_to_device_id_to_address_map_[origin][device_id] = device_address;
50 AddUnionOfServicesTo(
51 options, &origin_to_device_id_to_services_map_[origin][device_id]);
52
53 CHECK(device_id_set_.insert(device_id).second);
54
55 return origin_to_device_address_to_id_map_[origin][device_address];
56 } 29 }
57 30
58 void BluetoothAllowedDevicesMap::RemoveDevice( 31 void BluetoothAllowedDevicesMap::Clear() {
59 const url::Origin& origin, 32 origin_to_allowed_devices_map_.clear();
60 const std::string& device_address) {
61 const WebBluetoothDeviceId* device_id_ptr =
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;
68
69 // 1. Remove from all three maps.
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 // 3. Remove from set of ids.
82 CHECK(device_id_set_.erase(device_id));
83 } 33 }
84 34
85 const WebBluetoothDeviceId* BluetoothAllowedDevicesMap::GetDeviceId( 35 } // namespace content
86 const url::Origin& origin,
87 const std::string& device_address) {
88 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin);
89 if (address_map_iter == origin_to_device_address_to_id_map_.end()) {
90 return nullptr;
91 }
92
93 const auto& device_address_to_id_map = address_map_iter->second;
94
95 auto id_iter = device_address_to_id_map.find(device_address);
96 if (id_iter == device_address_to_id_map.end()) {
97 return nullptr;
98 }
99 return &(id_iter->second);
100 }
101
102 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress(
103 const url::Origin& origin,
104 const WebBluetoothDeviceId& device_id) {
105 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin);
106 if (id_map_iter == origin_to_device_id_to_address_map_.end()) {
107 return base::EmptyString();
108 }
109
110 const auto& device_id_to_address_map = id_map_iter->second;
111
112 auto id_iter = device_id_to_address_map.find(device_id);
113
114 return id_iter == device_id_to_address_map.end() ? base::EmptyString()
115 : id_iter->second;
116 }
117
118 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessAtLeastOneService(
119 const url::Origin& origin,
120 const WebBluetoothDeviceId& device_id) const {
121 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin);
122 if (id_map_iter == origin_to_device_id_to_services_map_.end()) {
123 return false;
124 }
125
126 const auto& device_id_to_services_map = id_map_iter->second;
127
128 auto id_iter = device_id_to_services_map.find(device_id);
129
130 return id_iter == device_id_to_services_map.end() ? false
131 : !id_iter->second.empty();
132 }
133
134 bool BluetoothAllowedDevicesMap::IsOriginAllowedToAccessService(
135 const url::Origin& origin,
136 const WebBluetoothDeviceId& device_id,
137 const BluetoothUUID& service_uuid) const {
138 if (BluetoothBlocklist::Get().IsExcluded(service_uuid)) {
139 return false;
140 }
141
142 auto id_map_iter = origin_to_device_id_to_services_map_.find(origin);
143 if (id_map_iter == origin_to_device_id_to_services_map_.end()) {
144 return false;
145 }
146
147 const auto& device_id_to_services_map = id_map_iter->second;
148
149 auto id_iter = device_id_to_services_map.find(device_id);
150
151 return id_iter == device_id_to_services_map.end()
152 ? false
153 : base::ContainsKey(id_iter->second, service_uuid);
154 }
155
156 WebBluetoothDeviceId BluetoothAllowedDevicesMap::GenerateUniqueDeviceId() {
157 WebBluetoothDeviceId device_id = WebBluetoothDeviceId::Create();
158 while (base::ContainsKey(device_id_set_, device_id)) {
159 LOG(WARNING) << "Generated repeated id.";
160 device_id = WebBluetoothDeviceId::Create();
161 }
162 return device_id;
163 }
164
165 void BluetoothAllowedDevicesMap::AddUnionOfServicesTo(
166 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options,
167 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash>*
168 unionOfServices) {
169 if (options->filters) {
170 for (const auto& filter : options->filters.value()) {
171 if (!filter->services) {
172 continue;
173 }
174
175 for (const BluetoothUUID& uuid : filter->services.value()) {
176 unionOfServices->insert(uuid);
177 }
178 }
179 }
180
181 for (const BluetoothUUID& uuid : options->optional_services) {
182 unionOfServices->insert(uuid);
183 }
184 }
185
186 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698