Chromium Code Reviews| Index: chrome/browser/usb/usb_chooser_permission_context.cc |
| diff --git a/chrome/browser/usb/usb_chooser_permission_context.cc b/chrome/browser/usb/usb_chooser_permission_context.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e87c64a786117dfda83addc222bb96148b5a556 |
| --- /dev/null |
| +++ b/chrome/browser/usb/usb_chooser_permission_context.cc |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/stl_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/usb/usb_chooser_permission_context.h" |
| +#include "content/public/browser/permission_type.h" |
| +#include "device/core/device_client.h" |
| +#include "device/usb/usb_device.h" |
| + |
| +using device::UsbDevice; |
| + |
| +namespace { |
| + |
| +const char* const kDeviceNameKey = "name"; |
| +const char* const kVendorIdKey = "vendor-id"; |
| +const char* const kProductIdKey = "product-id"; |
| +const char* const kSerialNumberKey = "serial-number"; |
| + |
| +bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) { |
| + return !device->serial_number().empty(); |
| +} |
| + |
| +const base::Value* FindForDevice(const base::ListValue& device_list, |
| + const scoped_refptr<const UsbDevice>& device) { |
| + const std::string utf8_serial_number = |
| + base::UTF16ToUTF8(device->serial_number()); |
| + |
| + for (size_t i = 0; i < device_list.GetSize(); ++i) { |
| + const base::DictionaryValue* device_dict; |
| + if (!device_list.GetDictionary(i, &device_dict)) |
| + continue; |
| + |
| + int vendor_id; |
| + int product_id; |
| + std::string serial_number; |
| + if (device_dict->GetInteger(kVendorIdKey, &vendor_id) && |
| + device->vendor_id() == vendor_id && |
| + device_dict->GetInteger(kProductIdKey, &product_id) && |
| + device->product_id() == product_id && |
| + device_dict->GetString(kSerialNumberKey, &serial_number) && |
| + utf8_serial_number == serial_number) |
| + return device_dict; |
| + } |
| + return nullptr; |
| +} |
| + |
| +scoped_ptr<base::Value> ValueFromDevice( |
| + const scoped_refptr<const UsbDevice>& device) { |
| + scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); |
| + device_dict->SetString(kDeviceNameKey, device->product_string()); |
| + device_dict->SetInteger(kVendorIdKey, device->vendor_id()); |
| + device_dict->SetInteger(kProductIdKey, device->product_id()); |
| + device_dict->SetString(kSerialNumberKey, device->serial_number()); |
| + return device_dict.Pass(); |
| +} |
| + |
| +} // namespace |
| + |
| +UsbChooserPermissionContext::UsbChooserPermissionContext(Profile* profile) |
| + : ChooserPermissionContextBase(profile, |
| + content::PermissionType::USB, |
| + CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA), |
| + observer_(this) { |
| + usb_service_ = device::DeviceClient::Get()->GetUsbService(); |
| + if (usb_service_) |
| + observer_.Add(usb_service_); |
| +} |
| + |
| +UsbChooserPermissionContext::~UsbChooserPermissionContext() {} |
| + |
| +void UsbChooserPermissionContext::GrantDevicePermission( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const std::string& guid) { |
| + scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| + if (!device) |
| + return; |
| + |
| + if (CanStorePersistentEntry(device)) { |
| + GrantObjectPermission(requesting_origin, embedding_origin, |
| + ValueFromDevice(device)); |
| + } else { |
| + ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid); |
| + } |
| +} |
| + |
| +void UsbChooserPermissionContext::RevokeDevicePermission( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const std::string& guid) { |
| + auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| + if (it != ephemeral_devices_.end()) { |
| + it->second.erase(guid); |
| + if (it->second.empty()) |
| + ephemeral_devices_.erase(it); |
| + } |
| + |
| + scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| + if (!device) |
| + return; |
| + |
| + scoped_ptr<base::ListValue> device_list = |
| + GetPreviouslyChosenObjects(requesting_origin, embedding_origin); |
| + const base::Value* entry = FindForDevice(*device_list, device); |
| + 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.
|
| + RevokeObjectPermission(requesting_origin, embedding_origin, *entry); |
| + } |
| +} |
| + |
| +bool UsbChooserPermissionContext::HasDevicePermission( |
| + const GURL& requesting_origin, |
| + const GURL& embedding_origin, |
| + const std::string& guid) { |
| + auto it = ephemeral_devices_.find(requesting_origin.GetOrigin()); |
| + if (it != ephemeral_devices_.end()) |
| + return ContainsValue(it->second, guid); |
| + |
| + scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
| + if (!device) |
| + return false; |
| + |
| + scoped_ptr<base::ListValue> device_list = |
| + GetPreviouslyChosenObjects(requesting_origin, embedding_origin); |
| + return FindForDevice(*device_list, device); |
| +} |
| + |
| +void UsbChooserPermissionContext::OnDeviceRemoved( |
| + scoped_refptr<UsbDevice> device) { |
| + for (auto& map_entry : ephemeral_devices_) |
| + map_entry.second.erase(device->guid()); |
| +} |