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 "base/stl_util.h" |
| 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "base/values.h" |
| 8 #include "chrome/browser/usb/usb_chooser_permission_context.h" |
| 9 #include "content/public/browser/permission_type.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 kVendorIdKey[] = "vendor-id"; |
| 19 const char kProductIdKey[] = "product-id"; |
| 20 const char kSerialNumberKey[] = "serial-number"; |
| 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 ChooserPermissionContextBase::ObjectList& 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 return nullptr; |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 UsbChooserPermissionContext::UsbChooserPermissionContext(Profile* profile) |
| 50 : ChooserPermissionContextBase(profile, |
| 51 content::PermissionType::USB, |
| 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 UsbChooserPermissionContext::~UsbChooserPermissionContext() {} |
| 60 |
| 61 void UsbChooserPermissionContext::GrantDevicePermission( |
| 62 const GURL& requesting_origin, |
| 63 const GURL& embedding_origin, |
| 64 const std::string& guid) { |
| 65 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 66 if (!device) |
| 67 return; |
| 68 |
| 69 if (CanStorePersistentEntry(device)) { |
| 70 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); |
| 71 device_dict->SetString(kDeviceNameKey, device->product_string()); |
| 72 device_dict->SetInteger(kVendorIdKey, device->vendor_id()); |
| 73 device_dict->SetInteger(kProductIdKey, device->product_id()); |
| 74 device_dict->SetString(kSerialNumberKey, device->serial_number()); |
| 75 GrantObjectPermission(requesting_origin, embedding_origin, |
| 76 device_dict.Pass()); |
| 77 } else { |
| 78 ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid); |
| 79 } |
| 80 } |
| 81 |
| 82 void UsbChooserPermissionContext::RevokeDevicePermission( |
| 83 const GURL& requesting_origin, |
| 84 const GURL& embedding_origin, |
| 85 const std::string& guid) { |
| 86 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| 87 if (it != ephemeral_devices_.end()) { |
| 88 it->second.erase(guid); |
| 89 if (it->second.empty()) |
| 90 ephemeral_devices_.erase(it); |
| 91 } |
| 92 |
| 93 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 94 if (!device) |
| 95 return; |
| 96 |
| 97 ChooserPermissionContextBase::ObjectList device_list = |
| 98 GetPreviouslyChosenObjects(requesting_origin, embedding_origin); |
| 99 const base::DictionaryValue* entry = FindForDevice(device_list, device); |
| 100 if (entry) |
| 101 RevokeObjectPermission(requesting_origin, embedding_origin, *entry); |
| 102 } |
| 103 |
| 104 bool UsbChooserPermissionContext::HasDevicePermission( |
| 105 const GURL& requesting_origin, |
| 106 const GURL& embedding_origin, |
| 107 const std::string& guid) { |
| 108 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| 109 if (it != ephemeral_devices_.end()) |
| 110 return ContainsValue(it->second, guid); |
| 111 |
| 112 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| 113 if (!device) |
| 114 return false; |
| 115 |
| 116 ChooserPermissionContextBase::ObjectList device_list = |
| 117 GetPreviouslyChosenObjects(requesting_origin, embedding_origin); |
| 118 return FindForDevice(device_list, device); |
| 119 } |
| 120 |
| 121 bool UsbChooserPermissionContext::IsValidObject( |
| 122 const base::DictionaryValue& object) { |
| 123 return object.HasKey(kDeviceNameKey) && object.HasKey(kVendorIdKey) && |
| 124 object.HasKey(kProductIdKey) && object.HasKey(kSerialNumberKey); |
| 125 } |
| 126 |
| 127 std::string UsbChooserPermissionContext::GetStringToDisplayForObject( |
| 128 const base::DictionaryValue& object) { |
| 129 DCHECK(IsValidObject(object)); |
| 130 std::string name; |
| 131 object.GetString(kDeviceNameKey, &name); |
| 132 return name; |
| 133 } |
| 134 |
| 135 void UsbChooserPermissionContext::OnDeviceRemoved( |
| 136 scoped_refptr<UsbDevice> device) { |
| 137 for (auto& map_entry : ephemeral_devices_) |
| 138 map_entry.second.erase(device->guid()); |
| 139 } |
OLD | NEW |