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