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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 return origin_to_device_address_to_id_map_[origin][device_address]; | 60 return origin_to_device_address_to_id_map_[origin][device_address]; |
61 } | 61 } |
62 const std::string device_id = GenerateDeviceId(origin); | 62 const std::string device_id = GenerateDeviceId(origin); |
63 VLOG(1) << "Id generated for device: " << device_id; | 63 VLOG(1) << "Id generated for device: " << device_id; |
64 | 64 |
65 origin_to_device_address_to_id_map_[origin][device_address] = device_id; | 65 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; | 66 origin_to_device_id_to_address_map_[origin][device_id] = device_address; |
67 origin_to_device_id_to_services_map_[origin][device_id] = | 67 origin_to_device_id_to_services_map_[origin][device_id] = |
68 UnionOfServices(filters, optional_services); | 68 UnionOfServices(filters, optional_services); |
69 | 69 |
70 CHECK(device_id_set_.insert(device_id).second); | |
71 | |
70 return origin_to_device_address_to_id_map_[origin][device_address]; | 72 return origin_to_device_address_to_id_map_[origin][device_address]; |
71 } | 73 } |
72 | 74 |
73 void BluetoothAllowedDevicesMap::RemoveDevice( | 75 void BluetoothAllowedDevicesMap::RemoveDevice( |
74 const url::Origin& origin, | 76 const url::Origin& origin, |
75 const std::string& device_address) { | 77 const std::string& device_address) { |
76 const std::string device_id = GetDeviceId(origin, device_address); | 78 const std::string device_id = GetDeviceId(origin, device_address); |
77 DCHECK(!device_id.empty()); | 79 DCHECK(!device_id.empty()); |
78 | 80 |
79 // 1. Remove from all three maps. | 81 // 1. Remove from all three maps. |
80 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); | 82 CHECK(origin_to_device_address_to_id_map_[origin].erase(device_address)); |
81 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); | 83 CHECK(origin_to_device_id_to_address_map_[origin].erase(device_id)); |
82 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | 84 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); |
83 | 85 |
84 // 2. Remove empty map for origin. | 86 // 2. Remove empty map for origin. |
85 if (origin_to_device_address_to_id_map_[origin].empty()) { | 87 if (origin_to_device_address_to_id_map_[origin].empty()) { |
86 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | 88 CHECK(origin_to_device_address_to_id_map_.erase(origin)); |
87 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | 89 CHECK(origin_to_device_id_to_address_map_.erase(origin)); |
88 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | 90 CHECK(origin_to_device_id_to_services_map_.erase(origin)); |
89 } | 91 } |
92 | |
93 // 3. Remove from set of ids. | |
94 CHECK(device_id_set_.erase(device_id)); | |
90 } | 95 } |
91 | 96 |
92 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | 97 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( |
93 const url::Origin& origin, | 98 const url::Origin& origin, |
94 const std::string& device_address) { | 99 const std::string& device_address) { |
95 auto address_map_iter = origin_to_device_address_to_id_map_.find(origin); | 100 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()) { | 101 if (address_map_iter == origin_to_device_address_to_id_map_.end()) { |
97 return base::EmptyString(); | 102 return base::EmptyString(); |
98 } | 103 } |
99 | 104 |
(...skipping 18 matching lines...) Expand all Loading... | |
118 | 123 |
119 auto id_iter = device_id_to_address_map.find(device_id); | 124 auto id_iter = device_id_to_address_map.find(device_id); |
120 | 125 |
121 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | 126 return id_iter == device_id_to_address_map.end() ? base::EmptyString() |
122 : id_iter->second; | 127 : id_iter->second; |
123 } | 128 } |
124 | 129 |
125 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( | 130 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( |
126 const url::Origin& origin) { | 131 const url::Origin& origin) { |
127 std::string device_id = GetBase64Id(); | 132 std::string device_id = GetBase64Id(); |
128 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | 133 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); |
Jeffrey Yasskin
2016/01/14 22:13:18
I think you don't need to use the origin anymore:
ortuno
2016/01/15 01:00:08
Done.
| |
129 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | 134 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { |
130 return device_id; | 135 return device_id; |
131 } | 136 } |
132 while (ContainsKey(id_map_iter->second, device_id)) { | 137 while (ContainsKey(id_map_iter->second, device_id) || |
138 ContainsKey(device_id_set_, device_id)) { | |
133 LOG(WARNING) << "Generated repeated id."; | 139 LOG(WARNING) << "Generated repeated id."; |
134 device_id = GetBase64Id(); | 140 device_id = GetBase64Id(); |
135 } | 141 } |
136 return device_id; | 142 return device_id; |
137 } | 143 } |
138 | 144 |
139 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | 145 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( |
140 const std::vector<BluetoothScanFilter>& filters, | 146 const std::vector<BluetoothScanFilter>& filters, |
141 const std::vector<BluetoothUUID>& optional_services) { | 147 const std::vector<BluetoothUUID>& optional_services) { |
142 std::set<std::string> unionOfServices; | 148 std::set<std::string> unionOfServices; |
143 for (const auto& filter : filters) { | 149 for (const auto& filter : filters) { |
144 for (const BluetoothUUID& uuid : filter.services) { | 150 for (const BluetoothUUID& uuid : filter.services) { |
145 unionOfServices.insert(uuid.canonical_value()); | 151 unionOfServices.insert(uuid.canonical_value()); |
146 } | 152 } |
147 } | 153 } |
148 for (const BluetoothUUID& uuid : optional_services) { | 154 for (const BluetoothUUID& uuid : optional_services) { |
149 unionOfServices.insert(uuid.canonical_value()); | 155 unionOfServices.insert(uuid.canonical_value()); |
150 } | 156 } |
151 return unionOfServices; | 157 return unionOfServices; |
152 } | 158 } |
153 | 159 |
154 } // namespace content | 160 } // namespace content |
OLD | NEW |