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

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: More chooser, less permission. 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 "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 kVendorIdKey[] = "vendor-id";
19 const char kProductIdKey[] = "product-id";
20 const char kSerialNumberKey[] = "serial-number";
21
22 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) {
23 return !device->serial_number().empty();
24 }
25
26 const base::DictionaryValue* FindForDevice(
27 const ScopedVector<base::DictionaryValue>& device_list,
28 const scoped_refptr<const UsbDevice>& device) {
29 const std::string utf8_serial_number =
30 base::UTF16ToUTF8(device->serial_number());
31
32 for (const base::DictionaryValue* device_dict : device_list) {
33 int vendor_id;
34 int product_id;
35 std::string serial_number;
36 if (device_dict->GetInteger(kVendorIdKey, &vendor_id) &&
37 device->vendor_id() == vendor_id &&
38 device_dict->GetInteger(kProductIdKey, &product_id) &&
39 device->product_id() == product_id &&
40 device_dict->GetString(kSerialNumberKey, &serial_number) &&
41 utf8_serial_number == serial_number) {
42 return device_dict;
43 }
44 }
45 return nullptr;
46 }
47
48 } // namespace
49
50 UsbChooserContext::UsbChooserContext(Profile* profile)
51 : ChooserContextBase(profile,
52 CONTENT_SETTINGS_TYPE_USB,
53 CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA),
54 observer_(this) {
55 usb_service_ = device::DeviceClient::Get()->GetUsbService();
56 if (usb_service_)
57 observer_.Add(usb_service_);
58 }
59
60 UsbChooserContext::~UsbChooserContext() {}
61
62 void UsbChooserContext::GrantDevicePermission(const GURL& requesting_origin,
63 const GURL& embedding_origin,
64 const std::string& guid) {
65 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
66 if (!device)
67 return;
68
69 if (CanStorePersistentEntry(device)) {
70 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue());
71 device_dict->SetString(kDeviceNameKey, device->product_string());
72 device_dict->SetInteger(kVendorIdKey, device->vendor_id());
73 device_dict->SetInteger(kProductIdKey, device->product_id());
74 device_dict->SetString(kSerialNumberKey, device->serial_number());
75 GrantObjectPermission(requesting_origin, embedding_origin,
76 device_dict.Pass());
77 } else {
78 ephemeral_devices_[requesting_origin.GetOrigin()].insert(guid);
79 }
80 }
81
82 void UsbChooserContext::RevokeDevicePermission(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 ScopedVector<base::DictionaryValue> 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 UsbChooserContext::HasDevicePermission(const GURL& requesting_origin,
104 const GURL& embedding_origin,
105 const std::string& guid) {
106 auto it = ephemeral_devices_.find(requesting_origin.GetOrigin());
107 if (it != ephemeral_devices_.end())
108 return ContainsValue(it->second, guid);
109
110 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid);
111 if (!device)
112 return false;
113
114 ScopedVector<base::DictionaryValue> device_list =
115 GetPreviouslyChosenObjects(requesting_origin, embedding_origin);
116 return FindForDevice(device_list, device) != nullptr;
117 }
118
119 bool UsbChooserContext::IsValidObject(const base::DictionaryValue& object) {
120 return object.size() == 4 && object.HasKey(kDeviceNameKey) &&
121 object.HasKey(kVendorIdKey) && object.HasKey(kProductIdKey) &&
122 object.HasKey(kSerialNumberKey);
123 }
124
125 std::string UsbChooserContext::GetStringToDisplayForObject(
126 const base::DictionaryValue& object) {
127 DCHECK(IsValidObject(object));
128 std::string name;
129 object.GetString(kDeviceNameKey, &name);
130 return name;
131 }
132
133 void UsbChooserContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
134 for (auto& map_entry : ephemeral_devices_)
135 map_entry.second.erase(device->guid());
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698