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

Side by Side 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 unified diff | Download patch
OLDNEW
(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"
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.
9 #include "device/core/device_client.h"
10 #include "device/usb/usb_device.h"
11
12 using device::UsbDevice;
13
14 namespace {
15
16 const char kDeviceNameKey[] = "name";
17 const char kVendorIdKey[] = "vendor-id";
18 const char kProductIdKey[] = "product-id";
19 const char kSerialNumberKey[] = "serial-number";
20
21 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) {
22 return !device->serial_number().empty();
23 }
24
25 const base::DictionaryValue* FindForDevice(
26 const ChooserPermissionContextBase::ObjectList& 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 (const base::DictionaryValue* device_dict : device_list) {
32 int vendor_id;
33 int product_id;
34 std::string serial_number;
35 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) &&
36 device->vendor_id() == vendor_id &&
37 device_dict->GetInteger(kProductIdKey, &product_id) &&
38 device->product_id() == product_id &&
39 device_dict->GetString(kSerialNumberKey, &serial_number) &&
40 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.
41 return device_dict;
42 }
43 return nullptr;
44 }
45
46 } // namespace
47
48 UsbChooserPermissionContext::UsbChooserPermissionContext(Profile* profile)
49 : ChooserPermissionContextBase(profile,
50 CONTENT_SETTINGS_TYPE_USB,
51 CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA),
52 observer_(this) {
53 usb_service_ = device::DeviceClient::Get()->GetUsbService();
54 if (usb_service_)
55 observer_.Add(usb_service_);
56 }
57
58 UsbChooserPermissionContext::~UsbChooserPermissionContext() {}
59
60 void UsbChooserPermissionContext::GrantDevicePermission(
61 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 UsbChooserPermissionContext::RevokeDevicePermission(
82 const GURL& requesting_origin,
83 const GURL& embedding_origin,
84 const std::string& guid) {
85 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
86 if (it != ephemeral_devices_.end()) {
87 it->second.erase(guid);
88 if (it->second.empty())
89 ephemeral_devices_.erase(it);
90 }
91
92 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
93 if (!device)
94 return;
95
96 ChooserPermissionContextBase::ObjectList device_list =
97 GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
98 const base::DictionaryValue* entry = FindForDevice(device_list, device);
99 if (entry)
100 RevokeObjectPermission(requesting_origin, embedding_origin, *entry);
101 }
102
103 bool UsbChooserPermissionContext::HasDevicePermission(
104 const GURL& requesting_origin,
105 const GURL& embedding_origin,
106 const std::string& guid) {
107 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
108 if (it != ephemeral_devices_.end())
109 return ContainsValue(it->second, guid);
110
111 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
112 if (!device)
113 return false;
114
115 ChooserPermissionContextBase::ObjectList device_list =
116 GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
117 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.
118 }
119
120 bool UsbChooserPermissionContext::IsValidObject(
121 const base::DictionaryValue& object) {
122 return object.HasKey(kDeviceNameKey) && object.HasKey(kVendorIdKey) &&
123 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.
124 }
125
126 std::string UsbChooserPermissionContext::GetStringToDisplayForObject(
127 const base::DictionaryValue& object) {
128 DCHECK(IsValidObject(object));
129 std::string name;
130 object.GetString(kDeviceNameKey, &name);
131 return name;
132 }
133
134 void UsbChooserPermissionContext::OnDeviceRemoved(
135 scoped_refptr<UsbDevice> device) {
136 for (auto& map_entry : ephemeral_devices_)
137 map_entry.second.erase(device->guid());
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698