| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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_blacklist.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/metrics/histogram_macros.h" | |
| 9 #include "base/optional.h" | |
| 10 #include "base/strings/string_split.h" | |
| 11 #include "content/public/browser/content_browser_client.h" | |
| 12 | |
| 13 using device::BluetoothUUID; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 static base::LazyInstance<content::BluetoothBlacklist>::Leaky g_singleton = | |
| 18 LAZY_INSTANCE_INITIALIZER; | |
| 19 | |
| 20 void RecordUMAParsedNonEmptyString(bool success) { | |
| 21 UMA_HISTOGRAM_BOOLEAN("Bluetooth.Web.Blacklist.ParsedNonEmptyString", | |
| 22 success); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 BluetoothBlacklist::~BluetoothBlacklist() {} | |
| 30 | |
| 31 // static | |
| 32 BluetoothBlacklist& BluetoothBlacklist::Get() { | |
| 33 return g_singleton.Get(); | |
| 34 } | |
| 35 | |
| 36 void BluetoothBlacklist::Add(const BluetoothUUID& uuid, Value value) { | |
| 37 CHECK(uuid.IsValid()); | |
| 38 auto insert_result = blacklisted_uuids_.insert(std::make_pair(uuid, value)); | |
| 39 bool inserted = insert_result.second; | |
| 40 if (!inserted) { | |
| 41 Value& stored = insert_result.first->second; | |
| 42 if (stored != value) | |
| 43 stored = Value::EXCLUDE; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void BluetoothBlacklist::Add(base::StringPiece blacklist_string) { | |
| 48 if (blacklist_string.empty()) | |
| 49 return; | |
| 50 base::StringPairs kv_pairs; | |
| 51 bool parsed_values = false; | |
| 52 bool invalid_values = false; | |
| 53 SplitStringIntoKeyValuePairs(blacklist_string, | |
| 54 ':', // Key-value delimiter | |
| 55 ',', // Key-value pair delimiter | |
| 56 &kv_pairs); | |
| 57 for (const auto& pair : kv_pairs) { | |
| 58 BluetoothUUID uuid(pair.first); | |
| 59 if (uuid.IsValid() && pair.second.size() == 1u) { | |
| 60 switch (pair.second[0]) { | |
| 61 case 'e': | |
| 62 Add(uuid, Value::EXCLUDE); | |
| 63 parsed_values = true; | |
| 64 continue; | |
| 65 case 'r': | |
| 66 Add(uuid, Value::EXCLUDE_READS); | |
| 67 parsed_values = true; | |
| 68 continue; | |
| 69 case 'w': | |
| 70 Add(uuid, Value::EXCLUDE_WRITES); | |
| 71 parsed_values = true; | |
| 72 continue; | |
| 73 } | |
| 74 } | |
| 75 invalid_values = true; | |
| 76 } | |
| 77 RecordUMAParsedNonEmptyString(parsed_values && !invalid_values); | |
| 78 } | |
| 79 | |
| 80 bool BluetoothBlacklist::IsExcluded(const BluetoothUUID& uuid) const { | |
| 81 CHECK(uuid.IsValid()); | |
| 82 const auto& it = blacklisted_uuids_.find(uuid); | |
| 83 if (it == blacklisted_uuids_.end()) | |
| 84 return false; | |
| 85 return it->second == Value::EXCLUDE; | |
| 86 } | |
| 87 | |
| 88 bool BluetoothBlacklist::IsExcluded( | |
| 89 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | |
| 90 for (const blink::mojom::WebBluetoothScanFilterPtr& filter : filters) { | |
| 91 for (const base::Optional<BluetoothUUID>& service : filter->services) { | |
| 92 if (IsExcluded(service.value())) { | |
| 93 return true; | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 bool BluetoothBlacklist::IsExcludedFromReads(const BluetoothUUID& uuid) const { | |
| 101 CHECK(uuid.IsValid()); | |
| 102 const auto& it = blacklisted_uuids_.find(uuid); | |
| 103 if (it == blacklisted_uuids_.end()) | |
| 104 return false; | |
| 105 return it->second == Value::EXCLUDE || it->second == Value::EXCLUDE_READS; | |
| 106 } | |
| 107 | |
| 108 bool BluetoothBlacklist::IsExcludedFromWrites(const BluetoothUUID& uuid) const { | |
| 109 CHECK(uuid.IsValid()); | |
| 110 const auto& it = blacklisted_uuids_.find(uuid); | |
| 111 if (it == blacklisted_uuids_.end()) | |
| 112 return false; | |
| 113 return it->second == Value::EXCLUDE || it->second == Value::EXCLUDE_WRITES; | |
| 114 } | |
| 115 | |
| 116 void BluetoothBlacklist::RemoveExcludedUUIDs( | |
| 117 blink::mojom::WebBluetoothRequestDeviceOptions* options) { | |
| 118 mojo::Array<base::Optional<BluetoothUUID>> | |
| 119 optional_services_blacklist_filtered; | |
| 120 for (const base::Optional<BluetoothUUID>& uuid : options->optional_services) { | |
| 121 if (!IsExcluded(uuid.value())) { | |
| 122 optional_services_blacklist_filtered.push_back(uuid); | |
| 123 } | |
| 124 } | |
| 125 options->optional_services = std::move(optional_services_blacklist_filtered); | |
| 126 } | |
| 127 | |
| 128 void BluetoothBlacklist::ResetToDefaultValuesForTest() { | |
| 129 blacklisted_uuids_.clear(); | |
| 130 PopulateWithDefaultValues(); | |
| 131 PopulateWithServerProvidedValues(); | |
| 132 } | |
| 133 | |
| 134 BluetoothBlacklist::BluetoothBlacklist() { | |
| 135 PopulateWithDefaultValues(); | |
| 136 PopulateWithServerProvidedValues(); | |
| 137 } | |
| 138 | |
| 139 void BluetoothBlacklist::PopulateWithDefaultValues() { | |
| 140 blacklisted_uuids_.clear(); | |
| 141 | |
| 142 // Testing from Layout Tests Note: | |
| 143 // | |
| 144 // Random UUIDs for object & exclude permutations that do not exist in the | |
| 145 // standard blacklist are included to facilitate integration testing from | |
| 146 // Layout Tests. Unit tests can dynamically modify the blacklist, but don't | |
| 147 // offer the full integration test to the Web Bluetooth Javascript bindings. | |
| 148 // | |
| 149 // This is done for simplicity as opposed to exposing a testing API that can | |
| 150 // add to the blacklist over time, which would be over engineered. | |
| 151 // | |
| 152 // Remove testing UUIDs if the specified blacklist is updated to include UUIDs | |
| 153 // that match the specific permutations. | |
| 154 DCHECK(BluetoothUUID("00001800-0000-1000-8000-00805f9b34fb") == | |
| 155 BluetoothUUID("1800")); | |
| 156 | |
| 157 // Blacklist UUIDs updated 2016-09-01 from: | |
| 158 // https://github.com/WebBluetoothCG/registries/blob/master/gatt_blacklist.txt | |
| 159 // Short UUIDs are used for readability of this list. | |
| 160 // | |
| 161 // Services: | |
| 162 Add(BluetoothUUID("1812"), Value::EXCLUDE); | |
| 163 Add(BluetoothUUID("00001530-1212-efde-1523-785feabcd123"), Value::EXCLUDE); | |
| 164 Add(BluetoothUUID("f000ffc0-0451-4000-b000-000000000000"), Value::EXCLUDE); | |
| 165 Add(BluetoothUUID("00060000"), Value::EXCLUDE); | |
| 166 Add(BluetoothUUID("fffd"), Value::EXCLUDE); | |
| 167 // Characteristics: | |
| 168 Add(BluetoothUUID("2a02"), Value::EXCLUDE_WRITES); | |
| 169 Add(BluetoothUUID("2a03"), Value::EXCLUDE); | |
| 170 Add(BluetoothUUID("2a25"), Value::EXCLUDE); | |
| 171 // Characteristics for Layout Tests: | |
| 172 Add(BluetoothUUID("bad1c9a2-9a5b-4015-8b60-1579bbbf2135"), | |
| 173 Value::EXCLUDE_READS); | |
| 174 // Descriptors: | |
| 175 Add(BluetoothUUID("2902"), Value::EXCLUDE_WRITES); | |
| 176 Add(BluetoothUUID("2903"), Value::EXCLUDE_WRITES); | |
| 177 // Descriptors for Layout Tests: | |
| 178 Add(BluetoothUUID("bad2ddcf-60db-45cd-bef9-fd72b153cf7c"), Value::EXCLUDE); | |
| 179 Add(BluetoothUUID("bad3ec61-3cc3-4954-9702-7977df514114"), | |
| 180 Value::EXCLUDE_READS); | |
| 181 } | |
| 182 | |
| 183 void BluetoothBlacklist::PopulateWithServerProvidedValues() { | |
| 184 // DCHECK to maybe help debug https://crbug.com/604078. | |
| 185 DCHECK(GetContentClient()); | |
| 186 Add(GetContentClient()->browser()->GetWebBluetoothBlacklist()); | |
| 187 } | |
| 188 | |
| 189 } // namespace content | |
| OLD | NEW |