Chromium Code Reviews| 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* const kDeviceNameKey = "name"; | |
| 18 const char* const kVendorIdKey = "vendor-id"; | |
| 19 const char* const kProductIdKey = "product-id"; | |
| 20 const char* const kSerialNumberKey = "serial-number"; | |
| 21 | |
| 22 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) { | |
| 23 return !device->serial_number().empty(); | |
| 24 } | |
| 25 | |
| 26 const base::Value* FindForDevice(const base::ListValue& device_list, | |
| 27 const scoped_refptr<const UsbDevice>& device) { | |
| 28 const std::string utf8_serial_number = | |
| 29 base::UTF16ToUTF8(device->serial_number()); | |
| 30 | |
| 31 for (size_t i = 0; i < device_list.GetSize(); ++i) { | |
| 32 const base::DictionaryValue* device_dict; | |
| 33 if (!device_list.GetDictionary(i, &device_dict)) | |
| 34 continue; | |
| 35 | |
| 36 int vendor_id; | |
| 37 int product_id; | |
| 38 std::string serial_number; | |
| 39 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) && | |
| 40 device->vendor_id() == vendor_id && | |
| 41 device_dict->GetInteger(kProductIdKey, &product_id) && | |
| 42 device->product_id() == product_id && | |
| 43 device_dict->GetString(kSerialNumberKey, &serial_number) && | |
| 44 utf8_serial_number == serial_number) | |
| 45 return device_dict; | |
| 46 } | |
| 47 return nullptr; | |
| 48 } | |
| 49 | |
| 50 scoped_ptr<base::Value> ValueFromDevice( | |
| 51 const scoped_refptr<const UsbDevice>& device) { | |
| 52 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); | |
| 53 device_dict->SetString(kDeviceNameKey, device->product_string()); | |
| 54 device_dict->SetInteger(kVendorIdKey, device->vendor_id()); | |
| 55 device_dict->SetInteger(kProductIdKey, device->product_id()); | |
| 56 device_dict->SetString(kSerialNumberKey, device->serial_number()); | |
| 57 return device_dict.Pass(); | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 UsbChooserPermissionContext::UsbChooserPermissionContext(Profile* profile) | |
| 63 : ChooserPermissionContextBase(profile, | |
| 64 content::PermissionType::USB, | |
| 65 CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA), | |
| 66 observer_(this) { | |
| 67 usb_service_ = device::DeviceClient::Get()->GetUsbService(); | |
| 68 if (usb_service_) | |
| 69 observer_.Add(usb_service_); | |
| 70 } | |
| 71 | |
| 72 UsbChooserPermissionContext::~UsbChooserPermissionContext() {} | |
| 73 | |
| 74 void UsbChooserPermissionContext::GrantDevicePermission( | |
| 75 const GURL& requesting_origin, | |
| 76 const GURL& embedding_origin, | |
| 77 const std::string& guid) { | |
| 78 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); | |
| 79 if (!device) | |
| 80 return; | |
| 81 | |
| 82 if (CanStorePersistentEntry(device)) { | |
| 83 GrantObjectPermission(requesting_origin, embedding_origin, | |
| 84 ValueFromDevice(device)); | |
| 85 } else { | |
| 86 ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void UsbChooserPermissionContext::RevokeDevicePermission( | |
| 91 const GURL& requesting_origin, | |
| 92 const GURL& embedding_origin, | |
| 93 const std::string& guid) { | |
| 94 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); | |
| 95 if (it != ephemeral_devices_.end()) { | |
| 96 it->second.erase(guid); | |
| 97 if (it->second.empty()) | |
| 98 ephemeral_devices_.erase(it); | |
| 99 } | |
| 100 | |
| 101 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); | |
| 102 if (!device) | |
| 103 return; | |
| 104 | |
| 105 scoped_ptr<base::ListValue> device_list = | |
| 106 GetPreviouslyChosenObjects(requesting_origin, embedding_origin); | |
| 107 const base::Value* entry = FindForDevice(*device_list, device); | |
| 108 if (entry) { | |
|
Bernhard Bauer
2015/10/12 11:03:48
Braces are optional for one-line bodies, and in ot
Reilly Grant (use Gerrit)
2015/10/13 01:32:24
Done.
| |
| 109 RevokeObjectPermission(requesting_origin, embedding_origin, *entry); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 bool UsbChooserPermissionContext::HasDevicePermission( | |
| 114 const GURL& requesting_origin, | |
| 115 const GURL& embedding_origin, | |
| 116 const std::string& guid) { | |
| 117 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); | |
| 118 if (it != ephemeral_devices_.end()) | |
| 119 return ContainsValue(it->second, guid); | |
| 120 | |
| 121 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); | |
| 122 if (!device) | |
| 123 return false; | |
| 124 | |
| 125 scoped_ptr<base::ListValue> device_list = | |
| 126 GetPreviouslyChosenObjects(requesting_origin, embedding_origin); | |
| 127 return FindForDevice(*device_list, device); | |
| 128 } | |
| 129 | |
| 130 void UsbChooserPermissionContext::OnDeviceRemoved( | |
| 131 scoped_refptr<UsbDevice> device) { | |
| 132 for (auto& map_entry : ephemeral_devices_) | |
| 133 map_entry.second.erase(device->guid()); | |
| 134 } | |
| OLD | NEW |