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 "chrome/browser/usb/usb_blocklist.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "components/variations/variations_associated_data.h" |
| 11 #include "device/usb/usb_device.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 static base::LazyInstance<UsbBlocklist>::Leaky g_singleton = |
| 16 LAZY_INSTANCE_INITIALIZER; |
| 17 |
| 18 } // namespace |
| 19 |
| 20 // The == and < operators are necessary to use Entries in std::set. |
| 21 bool operator==(const UsbBlocklist::Entry& a, const UsbBlocklist::Entry& b) { |
| 22 return a.vendor_id == b.vendor_id && a.product_id == b.product_id && |
| 23 a.version == b.version; |
| 24 } |
| 25 |
| 26 bool operator<(const UsbBlocklist::Entry& a, const UsbBlocklist::Entry& b) { |
| 27 if (a.vendor_id == b.vendor_id) { |
| 28 if (a.product_id == b.product_id) |
| 29 return a.version < b.version; |
| 30 return a.product_id < b.product_id; |
| 31 } |
| 32 return a.vendor_id < b.vendor_id; |
| 33 } |
| 34 |
| 35 UsbBlocklist::Entry::Entry(uint16_t vendor_id, |
| 36 uint16_t product_id, |
| 37 uint16_t version) |
| 38 : vendor_id(vendor_id), product_id(product_id), version(version) {} |
| 39 |
| 40 UsbBlocklist::~UsbBlocklist() {} |
| 41 |
| 42 // static |
| 43 UsbBlocklist& UsbBlocklist::Get() { |
| 44 return g_singleton.Get(); |
| 45 } |
| 46 |
| 47 void UsbBlocklist::Exclude(const Entry& entry) { |
| 48 blocklisted_devices_.insert(entry); |
| 49 } |
| 50 |
| 51 void UsbBlocklist::Exclude(base::StringPiece blocklist_string) { |
| 52 for (const auto& entry : |
| 53 base::SplitStringPiece(blocklist_string, ",", base::TRIM_WHITESPACE, |
| 54 base::SPLIT_WANT_NONEMPTY)) { |
| 55 std::vector<base::StringPiece> components = base::SplitStringPiece( |
| 56 entry, ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 57 if (components.size() != 3 || components[0].size() != 4 || |
| 58 components[1].size() != 4 || components[2].size() != 4) { |
| 59 continue; |
| 60 } |
| 61 |
| 62 uint32_t vendor_id; |
| 63 uint32_t product_id; |
| 64 uint32_t version; |
| 65 if (!base::HexStringToUInt(components[0], &vendor_id) || |
| 66 !base::HexStringToUInt(components[1], &product_id) || |
| 67 !base::HexStringToUInt(components[2], &version)) { |
| 68 continue; |
| 69 } |
| 70 |
| 71 Exclude(Entry(vendor_id, product_id, version)); |
| 72 } |
| 73 } |
| 74 |
| 75 bool UsbBlocklist::IsExcluded(const Entry& entry) { |
| 76 return base::ContainsValue(blocklisted_devices_, entry); |
| 77 } |
| 78 |
| 79 bool UsbBlocklist::IsExcluded(scoped_refptr<const device::UsbDevice> device) { |
| 80 return IsExcluded(Entry(device->vendor_id(), device->product_id(), |
| 81 device->device_version())); |
| 82 } |
| 83 |
| 84 void UsbBlocklist::ResetToDefaultValuesForTest() { |
| 85 blocklisted_devices_.clear(); |
| 86 PopulateWithDefaultValues(); |
| 87 PopulateWithServerProvidedValues(); |
| 88 } |
| 89 |
| 90 UsbBlocklist::UsbBlocklist() { |
| 91 PopulateWithDefaultValues(); |
| 92 PopulateWithServerProvidedValues(); |
| 93 } |
| 94 |
| 95 void UsbBlocklist::PopulateWithDefaultValues() { |
| 96 // To add a device to the blocklist add an entry here as well as configuring |
| 97 // a Finch trial so that the blocklist update is pushed out to existing users |
| 98 // as quickly as possible, e.g.: |
| 99 // |
| 100 // Exclude({ 0x18D0, 0x58F0, 0x1BAD }); |
| 101 } |
| 102 |
| 103 void UsbBlocklist::PopulateWithServerProvidedValues() { |
| 104 std::string blocklist_string = variations::GetVariationParamValue( |
| 105 "WebUSBBlocklist", "blocklist_additions"); |
| 106 Exclude(blocklist_string); |
| 107 } |
OLD | NEW |