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

Side by Side Diff: chrome/browser/usb/usb_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: Convert WebUSBPermissionStore to a PermissionContext. 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_permission_context.h"
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* const kDeviceNameKey = "name";
17 const char* const kVendorIdKey = "vendor-id";
18 const char* const kProductIdKey = "product-id";
19 const char* const kSerialNumberKey = "serial-number";
20
21 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) {
22 return !device->serial_number().empty();
23 }
24
25 const base::Value* FindForDevice(const base::ListValue& device_list,
26 const scoped_refptr<const UsbDevice>& device) {
27 const std::string utf8_serial_number =
28 base::UTF16ToUTF8(device->serial_number());
29
30 for (size_t i = 0; i < device_list.GetSize(); ++i) {
31 const base::DictionaryValue* device_dict;
32 if (!device_list.GetDictionary(i, &device_dict))
33 continue;
34
35 int vendor_id;
36 int product_id;
37 std::string serial_number;
38 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) &&
39 device->vendor_id() == vendor_id &&
40 device_dict->GetInteger(kProductIdKey, &product_id) &&
41 device->product_id() == product_id &&
42 device_dict->GetString(kSerialNumberKey, &serial_number) &&
43 utf8_serial_number == serial_number)
44 return device_dict;
45 }
46 return nullptr;
47 }
48
49 scoped_ptr<base::Value> ValueFromDevice(
50 const scoped_refptr<const UsbDevice>& device) {
51 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue());
52 device_dict->SetString(kDeviceNameKey, device->product_string());
53 device_dict->SetInteger(kVendorIdKey, device->vendor_id());
54 device_dict->SetInteger(kProductIdKey, device->product_id());
55 device_dict->SetString(kSerialNumberKey, device->serial_number());
56 return device_dict.Pass();
57 }
58
59 } // namespace
60
61 UsbPermissionContext::UsbPermissionContext(Profile* profile)
62 : ChooserPermissionContextBase(profile, CONTENT_SETTINGS_TYPE_USB),
63 observer_(this) {
64 usb_service_ = device::DeviceClient::Get()->GetUsbService();
65 if (usb_service_)
66 observer_.Add(usb_service_);
67 }
68
69 UsbPermissionContext::~UsbPermissionContext() {}
70
71 void UsbPermissionContext::GrantDevicePermission(const GURL& origin,
72 const std::string& guid) {
73 DCHECK_EQ(origin, origin.GetOrigin());
74 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
75 if (!device)
76 return;
77
78 if (CanStorePersistentEntry(device)) {
79 GrantObjectPermission(origin, ValueFromDevice(device));
80 } else {
81 ephemeral_devices_[origin].insert(guid);
82 }
83 }
84
85 void UsbPermissionContext::RevokeDevicePermission(const GURL& origin,
86 const std::string& guid) {
87 DCHECK_EQ(origin, origin.GetOrigin());
88 auto it = ephemeral_devices_.find(origin);
89 if (it != ephemeral_devices_.end()) {
90 it->second.erase(guid);
91 if (it->second.empty())
92 ephemeral_devices_.erase(it);
93 }
94
95 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
96 if (!device)
97 return;
98
99 scoped_ptr<base::ListValue> device_list = GetChosenObjects(origin);
100 const base::Value* entry = FindForDevice(*device_list, device);
101 if (entry) {
102 RevokeObjectPermission(origin, *entry);
103 }
104 }
105
106 bool UsbPermissionContext::HasDevicePermission(const GURL& origin,
107 const std::string& guid) {
108 DCHECK_EQ(origin, origin.GetOrigin());
109 auto it = ephemeral_devices_.find(origin);
110 if (it != ephemeral_devices_.end())
111 return ContainsValue(it->second, guid);
112
113 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
114 if (!device)
115 return false;
116
117 scoped_ptr<base::ListValue> device_list = GetChosenObjects(origin);
118 return FindForDevice(*device_list, device);
119 }
120
121 bool UsbPermissionContext::IsRestrictedToSecureOrigins() const {
122 return true;
123 }
124
125 void UsbPermissionContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
126 for (auto& map_entry : ephemeral_devices_)
127 map_entry.second.erase(device->guid());
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698