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 "chrome/browser/usb/usb_chooser_context.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "device/core/device_client.h" |
| 11 #include "device/usb/usb_device.h" |
| 12 |
| 13 using device::UsbDevice; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kDeviceNameKey[] = "name"; |
| 18 const char kProductIdKey[] = "product-id"; |
| 19 const char kSerialNumberKey[] = "serial-number"; |
| 20 const char kVendorIdKey[] = "vendor-id"; |
| 21 |
| 22 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) { |
| 23 return !device->serial_number().empty(); |
| 24 } |
| 25 |
| 26 const base::DictionaryValue* FindForDevice( |
| 27 const ScopedVector<base::DictionaryValue>& device_list, |
| 28 const scoped_refptr<const UsbDevice>& device) { |
| 29 const std::string utf8_serial_number = |
| 30 base::UTF16ToUTF8(device->serial_number()); |
| 31 |
| 32 for (const base::DictionaryValue* device_dict : device_list) { |
| 33 int vendor_id; |
| 34 int product_id; |
| 35 std::string serial_number; |
| 36 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) && |
| 37 device->vendor_id() == vendor_id && |
| 38 device_dict->GetInteger(kProductIdKey, &product_id) && |
| 39 device->product_id() == product_id && |
| 40 device_dict->GetString(kSerialNumberKey, &serial_number) && |
| 41 utf8_serial_number == serial_number) { |
| 42 return device_dict; |
| 43 } |
| 44 } |
| 45 return nullptr; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 UsbChooserContext::UsbChooserContext(Profile* profile) |
| 51 : ChooserContextBase(profile, |
| 52 CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA), |
| 53 observer_(this) { |
| 54 usb_service_ = device::DeviceClient::Get()->GetUsbService(); |
| 55 if (usb_service_) |
| 56 observer_.Add(usb_service_); |
| 57 } |
| 58 |
| 59 UsbChooserContext::~UsbChooserContext() {} |
| 60 |
| 61 void UsbChooserContext::GrantDevicePermission(const GURL& requesting_origin, |
| 62 const GURL& embedding_origin, |
| 63 const std::string& guid) { |
| 64 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 65 if (!device) |
| 66 return; |
| 67 |
| 68 if (CanStorePersistentEntry(device)) { |
| 69 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); |
| 70 device_dict->SetString(kDeviceNameKey, device->product_string()); |
| 71 device_dict->SetInteger(kVendorIdKey, device->vendor_id()); |
| 72 device_dict->SetInteger(kProductIdKey, device->product_id()); |
| 73 device_dict->SetString(kSerialNumberKey, device->serial_number()); |
| 74 GrantObjectPermission(requesting_origin, embedding_origin, |
| 75 device_dict.Pass()); |
| 76 } else { |
| 77 ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid); |
| 78 } |
| 79 } |
| 80 |
| 81 void UsbChooserContext::RevokeDevicePermission(const GURL& requesting_origin, |
| 82 const GURL& embedding_origin, |
| 83 const std::string& guid) { |
| 84 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| 85 if (it != ephemeral_devices_.end()) { |
| 86 it->second.erase(guid); |
| 87 if (it->second.empty()) |
| 88 ephemeral_devices_.erase(it); |
| 89 } |
| 90 |
| 91 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 92 if (!device) |
| 93 return; |
| 94 |
| 95 ScopedVector<base::DictionaryValue> device_list = |
| 96 GetGrantedObjects(requesting_origin, embedding_origin); |
| 97 const base::DictionaryValue* entry = FindForDevice(device_list, device); |
| 98 if (entry) |
| 99 RevokeObjectPermission(requesting_origin, embedding_origin, *entry); |
| 100 } |
| 101 |
| 102 bool UsbChooserContext::HasDevicePermission(const GURL& requesting_origin, |
| 103 const GURL& embedding_origin, |
| 104 const std::string& guid) { |
| 105 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| 106 if (it != ephemeral_devices_.end()) |
| 107 return ContainsValue(it->second, guid); |
| 108 |
| 109 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 110 if (!device) |
| 111 return false; |
| 112 |
| 113 ScopedVector<base::DictionaryValue> device_list = |
| 114 GetGrantedObjects(requesting_origin, embedding_origin); |
| 115 return FindForDevice(device_list, device) != nullptr; |
| 116 } |
| 117 |
| 118 bool UsbChooserContext::IsValidObject(const base::DictionaryValue& object) { |
| 119 return object.size() == 4 && object.HasKey(kDeviceNameKey) && |
| 120 object.HasKey(kVendorIdKey) && object.HasKey(kProductIdKey) && |
| 121 object.HasKey(kSerialNumberKey); |
| 122 } |
| 123 |
| 124 void UsbChooserContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { |
| 125 for (auto& map_entry : ephemeral_devices_) |
| 126 map_entry.second.erase(device->guid()); |
| 127 } |
OLD | NEW |