Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/bluetooth/bluetooth_allowed_devices_map.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "content/common/bluetooth/bluetooth_scan_filter.h" | |
| 14 #include "crypto/random.h" | |
| 15 #include "device/bluetooth/bluetooth_uuid.h" | |
| 16 | |
| 17 using device::BluetoothUUID; | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace { | |
| 22 const size_t kIdLength = 16 /* 128bits */; | |
|
palmer
2016/01/15 01:13:59
Nit: I don't know if these comments are strictly n
| |
| 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() {} | |
| 39 BluetoothAllowedDevicesMap::~BluetoothAllowedDevicesMap() {} | |
| 40 | |
| 41 const std::string& BluetoothAllowedDevicesMap::AddDevice( | |
| 42 const url::Origin& origin, | |
| 43 const std::string& device_address, | |
| 44 const std::vector<BluetoothScanFilter>& filters, | |
| 45 const std::vector<BluetoothUUID>& optional_services) { | |
| 46 VLOG(1) << "Adding a device to Map of Allowed Devices."; | |
| 47 | |
| 48 // "Unique" Origins generate the same key in maps. The set of "unique" | |
| 49 // Origins that generate the same key does not intersect the set of | |
| 50 // trustworthy origins; since Bluetooth is only available for trustworthy | |
|
palmer
2016/01/15 01:13:59
Nit: The correct terminology might be "potentially
ortuno
2016/01/15 02:52:21
Done.
| |
| 51 // origins we should never receive a request from a "unique" Origin. | |
| 52 // See url::Origin for what constitutes a "unique" Origin and the | |
| 53 // Secure Contexts spec for what constitutes a Trusworthy Origin: | |
| 54 // https://w3c.github.io/webappsec-secure-contexts/ | |
| 55 CHECK(!origin.unique()); | |
| 56 | |
| 57 if (ContainsKey(origin_to_device_address_to_id_map_[origin], | |
| 58 device_address)) { | |
| 59 VLOG(1) << "Device already in map of allowed devices."; | |
| 60 return origin_to_device_address_to_id_map_[origin][device_address]; | |
| 61 } | |
| 62 const std::string device_id = GenerateDeviceId(origin); | |
| 63 VLOG(1) << "Id generated for device: " << device_id; | |
| 64 | |
| 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; | |
| 67 origin_to_device_id_to_services_map_[origin][device_id] = | |
| 68 UnionOfServices(filters, optional_services); | |
| 69 | |
| 70 return origin_to_device_address_to_id_map_[origin][device_address]; | |
| 71 } | |
| 72 | |
| 73 void BluetoothAllowedDevicesMap::RemoveDevice( | |
| 74 const url::Origin& origin, | |
| 75 const std::string& device_address) { | |
| 76 const std::string device_id = GetDeviceId(origin, device_address); | |
| 77 DCHECK(!device_id.empty()); | |
| 78 | |
| 79 // 1. Remove from all three maps. | |
| 80 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)); | |
| 82 CHECK(origin_to_device_id_to_services_map_[origin].erase(device_id)); | |
| 83 | |
| 84 // 2. Remove empty map for origin. | |
| 85 if (origin_to_device_address_to_id_map_[origin].empty()) { | |
| 86 CHECK(origin_to_device_address_to_id_map_.erase(origin)); | |
| 87 CHECK(origin_to_device_id_to_address_map_.erase(origin)); | |
| 88 CHECK(origin_to_device_id_to_services_map_.erase(origin)); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 const std::string& BluetoothAllowedDevicesMap::GetDeviceId( | |
| 93 const url::Origin& origin, | |
| 94 const std::string& device_address) { | |
| 95 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()) { | |
| 97 return base::EmptyString(); | |
| 98 } | |
| 99 | |
| 100 const auto& device_address_to_id_map = address_map_iter->second; | |
| 101 | |
| 102 auto id_iter = device_address_to_id_map.find(device_address); | |
| 103 if (id_iter == device_address_to_id_map.end()) { | |
| 104 return base::EmptyString(); | |
| 105 } | |
| 106 return id_iter->second; | |
| 107 } | |
| 108 | |
| 109 const std::string& BluetoothAllowedDevicesMap::GetDeviceAddress( | |
| 110 const url::Origin& origin, | |
| 111 const std::string& device_id) { | |
| 112 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
| 113 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 114 return base::EmptyString(); | |
| 115 } | |
| 116 | |
| 117 const auto& device_id_to_address_map = id_map_iter->second; | |
| 118 | |
| 119 auto id_iter = device_id_to_address_map.find(device_id); | |
| 120 | |
| 121 return id_iter == device_id_to_address_map.end() ? base::EmptyString() | |
| 122 : id_iter->second; | |
| 123 } | |
| 124 | |
| 125 std::string BluetoothAllowedDevicesMap::GenerateDeviceId( | |
| 126 const url::Origin& origin) { | |
| 127 std::string device_id = GetBase64Id(); | |
| 128 auto id_map_iter = origin_to_device_id_to_address_map_.find(origin); | |
| 129 if (id_map_iter == origin_to_device_id_to_address_map_.end()) { | |
| 130 return device_id; | |
| 131 } | |
| 132 while (ContainsKey(id_map_iter->second, device_id)) { | |
| 133 LOG(WARNING) << "Generated repeated id."; | |
| 134 device_id = GetBase64Id(); | |
| 135 } | |
| 136 return device_id; | |
| 137 } | |
| 138 | |
| 139 std::set<std::string> BluetoothAllowedDevicesMap::UnionOfServices( | |
| 140 const std::vector<BluetoothScanFilter>& filters, | |
| 141 const std::vector<BluetoothUUID>& optional_services) { | |
| 142 std::set<std::string> unionOfServices; | |
| 143 for (const auto& filter : filters) { | |
| 144 for (const BluetoothUUID& uuid : filter.services) { | |
| 145 unionOfServices.insert(uuid.canonical_value()); | |
| 146 } | |
| 147 } | |
| 148 for (const BluetoothUUID& uuid : optional_services) { | |
| 149 unionOfServices.insert(uuid.canonical_value()); | |
| 150 } | |
| 151 return unionOfServices; | |
| 152 } | |
| 153 | |
| 154 } // namespace content | |
| OLD | NEW |