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

Side by Side Diff: chrome/browser/usb/usb_chooser_context.cc

Issue 1382783002: Store USB device permissions in website settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed ChooserType for now. Created 5 years, 1 month 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 "chrome/browser/usb/usb_chooser_context.h"
6
7 #include "base/stl_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.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 kDeviceNameKey[] = "name";
18 const char kGuidKey[] = "ephemeral-guid";
19 const char kProductIdKey[] = "product-id";
20 const char kSerialNumberKey[] = "serial-number";
21 const char kVendorIdKey[] = "vendor-id";
22
23 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) {
24 return !device->serial_number().empty();
25 }
26
27 const base::DictionaryValue* FindForDevice(
28 const ScopedVector<base::DictionaryValue>& device_list,
29 const scoped_refptr<const UsbDevice>& device) {
30 const std::string utf8_serial_number =
31 base::UTF16ToUTF8(device->serial_number());
32
33 for (const base::DictionaryValue* device_dict : device_list) {
34 int vendor_id;
35 int product_id;
36 std::string serial_number;
37 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) &&
38 device->vendor_id() == vendor_id &&
39 device_dict->GetInteger(kProductIdKey, &product_id) &&
40 device->product_id() == product_id &&
41 device_dict->GetString(kSerialNumberKey, &serial_number) &&
42 utf8_serial_number == serial_number) {
43 return device_dict;
44 }
45 }
46 return nullptr;
47 }
48
49 } // namespace
50
51 UsbChooserContext::UsbChooserContext(Profile* profile)
52 : ChooserContextBase(profile,
53 CONTENT_SETTINGS_TYPE_USB,
54 CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA),
55 observer_(this) {
56 usb_service_ = device::DeviceClient::Get()->GetUsbService();
57 if (usb_service_)
58 observer_.Add(usb_service_);
59 }
60
61 UsbChooserContext::~UsbChooserContext() {}
62
63 ScopedVector<base::DictionaryValue>
64 UsbChooserContext::GetPreviouslyChosenObjects(const GURL& requesting_origin,
65 const GURL& embedding_origin) {
66 ScopedVector<base::DictionaryValue> objects =
67 ChooserContextBase::GetPreviouslyChosenObjects(requesting_origin,
68 embedding_origin);
69
70 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
71 if (it != ephemeral_devices_.end()) {
72 for (const std::string& guid : it->second) {
73 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
74 DCHECK(device);
75 scoped_ptr<base::DictionaryValue> object(new base::DictionaryValue());
76 object->SetString(kDeviceNameKey, device->product_string());
77 object->SetString(kGuidKey, device->guid());
78 objects.push_back(object.Pass());
79 }
80 }
81
82 return objects.Pass();
83 }
84
85 void UsbChooserContext::RevokeObjectPermission(
86 const GURL& requesting_origin,
87 const GURL& embedding_origin,
88 const base::DictionaryValue& object) {
89 std::string guid;
90 if (object.GetString(kGuidKey, &guid)) {
91 RevokeDevicePermission(requesting_origin, embedding_origin, guid);
92 } else {
93 ChooserContextBase::RevokeObjectPermission(
94 requesting_origin, embedding_origin, object);
95 }
96 }
97
98 void UsbChooserContext::GrantDevicePermission(const GURL& requesting_origin,
99 const GURL& embedding_origin,
100 const std::string& guid) {
101 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
102 if (!device)
103 return;
104
105 if (CanStorePersistentEntry(device)) {
106 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue());
107 device_dict->SetString(kDeviceNameKey, device->product_string());
108 device_dict->SetInteger(kVendorIdKey, device->vendor_id());
109 device_dict->SetInteger(kProductIdKey, device->product_id());
110 device_dict->SetString(kSerialNumberKey, device->serial_number());
111 GrantObjectPermission(requesting_origin, embedding_origin,
112 device_dict.Pass());
113 } else {
114 ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid);
115 }
116 }
117
118 void UsbChooserContext::RevokeDevicePermission(const GURL& requesting_origin,
119 const GURL& embedding_origin,
120 const std::string& guid) {
121 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
122 if (it != ephemeral_devices_.end()) {
123 it->second.erase(guid);
124 if (it->second.empty())
125 ephemeral_devices_.erase(it);
126 }
127
128 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
129 if (!device)
130 return;
131
132 ScopedVector<base::DictionaryValue> device_list =
133 GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
134 const base::DictionaryValue* entry = FindForDevice(device_list, device);
135 if (entry)
136 RevokeObjectPermission(requesting_origin, embedding_origin, *entry);
137 }
138
139 bool UsbChooserContext::HasDevicePermission(const GURL& requesting_origin,
140 const GURL& embedding_origin,
141 const std::string& guid) {
142 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
143 if (it != ephemeral_devices_.end())
144 return ContainsValue(it->second, guid);
145
146 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
147 if (!device)
148 return false;
149
150 ScopedVector<base::DictionaryValue> device_list =
151 GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
152 return FindForDevice(device_list, device) != nullptr;
153 }
154
155 bool UsbChooserContext::IsValidObject(const base::DictionaryValue& object) {
156 return object.size() == 4 && object.HasKey(kDeviceNameKey) &&
157 object.HasKey(kVendorIdKey) && object.HasKey(kProductIdKey) &&
158 object.HasKey(kSerialNumberKey);
159 }
160
161 void UsbChooserContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
162 for (auto& map_entry : ephemeral_devices_)
163 map_entry.second.erase(device->guid());
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698