Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1080)

Unified Diff: chrome/browser/usb/usb_chooser_permission_context.cc

Issue 1382783002: Store USB device permissions in website settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove UsbPermissionContext and add ChooserPermissionContext helper functions. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a4931ff3d40ac2aaf365f12ade687a75398d4d42
--- /dev/null
+++ b/chrome/browser/usb/usb_chooser_permission_context.cc
@@ -0,0 +1,138 @@
+// 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"
raymes 2015/10/14 02:07:04 nit: this should go first with a space separating
Reilly Grant (use Gerrit) 2015/10/22 00:57:03 Done.
+#include "device/core/device_client.h"
+#include "device/usb/usb_device.h"
+
+using device::UsbDevice;
+
+namespace {
+
+const char kDeviceNameKey[] = "name";
+const char kVendorIdKey[] = "vendor-id";
+const char kProductIdKey[] = "product-id";
+const char kSerialNumberKey[] = "serial-number";
+
+bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) {
+ return !device->serial_number().empty();
+}
+
+const base::DictionaryValue* FindForDevice(
+ const ChooserPermissionContextBase::ObjectList& device_list,
+ const scoped_refptr<const UsbDevice>& device) {
+ const std::string utf8_serial_number =
+ base::UTF16ToUTF8(device->serial_number());
+
+ for (const base::DictionaryValue* device_dict : device_list) {
+ 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)
raymes 2015/10/14 02:07:04 nit: {} for multiline if
Reilly Grant (use Gerrit) 2015/10/22 00:57:02 Done.
+ return device_dict;
+ }
+ return nullptr;
+}
+
+} // namespace
+
+UsbChooserPermissionContext::UsbChooserPermissionContext(Profile* profile)
+ : ChooserPermissionContextBase(profile,
+ CONTENT_SETTINGS_TYPE_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)) {
+ 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());
+ GrantObjectPermission(requesting_origin, embedding_origin,
+ device_dict.Pass());
+ } 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;
+
+ ChooserPermissionContextBase::ObjectList device_list =
+ GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
+ const base::DictionaryValue* entry = FindForDevice(device_list, device);
+ if (entry)
+ 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;
+
+ ChooserPermissionContextBase::ObjectList device_list =
+ GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
+ return FindForDevice(device_list, device);
raymes 2015/10/14 02:07:04 optional: != nullptr?
Reilly Grant (use Gerrit) 2015/10/22 00:57:02 Done.
+}
+
+bool UsbChooserPermissionContext::IsValidObject(
+ const base::DictionaryValue& object) {
+ return object.HasKey(kDeviceNameKey) && object.HasKey(kVendorIdKey) &&
+ object.HasKey(kProductIdKey) && object.HasKey(kSerialNumberKey);
raymes 2015/10/14 02:07:04 I think we should probably also check that the num
Reilly Grant (use Gerrit) 2015/10/22 00:57:02 Done.
+}
+
+std::string UsbChooserPermissionContext::GetStringToDisplayForObject(
+ const base::DictionaryValue& object) {
+ DCHECK(IsValidObject(object));
+ std::string name;
+ object.GetString(kDeviceNameKey, &name);
+ return name;
+}
+
+void UsbChooserPermissionContext::OnDeviceRemoved(
+ scoped_refptr<UsbDevice> device) {
+ for (auto& map_entry : ephemeral_devices_)
+ map_entry.second.erase(device->guid());
+}

Powered by Google App Engine
This is Rietveld 408576698