OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/usb/usb_chooser_context.h" | 5 #include "chrome/browser/usb/usb_chooser_context.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/metrics/histogram_macros.h" | |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "device/core/device_client.h" | 15 #include "device/core/device_client.h" |
15 #include "device/usb/usb_device.h" | 16 #include "device/usb/usb_device.h" |
16 | 17 |
17 using device::UsbDevice; | 18 using device::UsbDevice; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const char kDeviceNameKey[] = "name"; | 22 const char kDeviceNameKey[] = "name"; |
22 const char kGuidKey[] = "ephemeral-guid"; | 23 const char kGuidKey[] = "ephemeral-guid"; |
23 const char kProductIdKey[] = "product-id"; | 24 const char kProductIdKey[] = "product-id"; |
24 const char kSerialNumberKey[] = "serial-number"; | 25 const char kSerialNumberKey[] = "serial-number"; |
25 const char kVendorIdKey[] = "vendor-id"; | 26 const char kVendorIdKey[] = "vendor-id"; |
26 | 27 |
28 enum WebUsbPermissionRevoked { | |
29 // Permission to access a USB device was revoked by the user. | |
30 WEBUSB_PERMISSION_REVOKED = 0, | |
31 // Permission to access an ephemeral USB device was revoked by the user. | |
32 WEBUSB_PERMISSION_REVOKED_EPHEMERAL, | |
33 // Maximum value for the enum. | |
34 WEBUSB_PERMISSION_REVOKED_MAX | |
35 }; | |
36 | |
27 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) { | 37 bool CanStorePersistentEntry(const scoped_refptr<const UsbDevice>& device) { |
28 return !device->serial_number().empty(); | 38 return !device->serial_number().empty(); |
29 } | 39 } |
30 | 40 |
31 const base::DictionaryValue* FindForDevice( | 41 const base::DictionaryValue* FindForDevice( |
32 const std::vector<scoped_ptr<base::DictionaryValue>>& device_list, | 42 const std::vector<scoped_ptr<base::DictionaryValue>>& device_list, |
33 const scoped_refptr<const UsbDevice>& device) { | 43 const scoped_refptr<const UsbDevice>& device) { |
34 const std::string utf8_serial_number = | 44 const std::string utf8_serial_number = |
35 base::UTF16ToUTF8(device->serial_number()); | 45 base::UTF16ToUTF8(device->serial_number()); |
36 | 46 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 return objects; | 119 return objects; |
110 } | 120 } |
111 | 121 |
112 void UsbChooserContext::RevokeObjectPermission( | 122 void UsbChooserContext::RevokeObjectPermission( |
113 const GURL& requesting_origin, | 123 const GURL& requesting_origin, |
114 const GURL& embedding_origin, | 124 const GURL& embedding_origin, |
115 const base::DictionaryValue& object) { | 125 const base::DictionaryValue& object) { |
116 std::string guid; | 126 std::string guid; |
117 if (object.GetString(kGuidKey, &guid)) { | 127 if (object.GetString(kGuidKey, &guid)) { |
118 RevokeDevicePermission(requesting_origin, embedding_origin, guid); | 128 RevokeDevicePermission(requesting_origin, embedding_origin, guid); |
129 UMA_HISTOGRAM_ENUMERATION("WebUsb.PermissionRevoked", | |
130 WEBUSB_PERMISSION_REVOKED_EPHEMERAL, | |
Alexei Svitkine (slow)
2016/01/18 20:05:13
Ditto.
Reilly Grant (use Gerrit)
2016/01/20 00:59:37
Done.
| |
131 WEBUSB_PERMISSION_REVOKED_MAX); | |
119 } else { | 132 } else { |
120 ChooserContextBase::RevokeObjectPermission(requesting_origin, | 133 ChooserContextBase::RevokeObjectPermission(requesting_origin, |
121 embedding_origin, object); | 134 embedding_origin, object); |
135 UMA_HISTOGRAM_ENUMERATION("WebUsb.PermissionRevoked", | |
136 WEBUSB_PERMISSION_REVOKED, | |
137 WEBUSB_PERMISSION_REVOKED_MAX); | |
122 } | 138 } |
123 } | 139 } |
124 | 140 |
125 void UsbChooserContext::GrantDevicePermission(const GURL& requesting_origin, | 141 void UsbChooserContext::GrantDevicePermission(const GURL& requesting_origin, |
126 const GURL& embedding_origin, | 142 const GURL& embedding_origin, |
127 const std::string& guid) { | 143 const std::string& guid) { |
128 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); | 144 scoped_refptr<UsbDevice> device = usb_service_->GetDevice(guid); |
129 if (!device) | 145 if (!device) |
130 return; | 146 return; |
131 | 147 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 bool UsbChooserContext::IsValidObject(const base::DictionaryValue& object) { | 201 bool UsbChooserContext::IsValidObject(const base::DictionaryValue& object) { |
186 return object.size() == 4 && object.HasKey(kDeviceNameKey) && | 202 return object.size() == 4 && object.HasKey(kDeviceNameKey) && |
187 object.HasKey(kVendorIdKey) && object.HasKey(kProductIdKey) && | 203 object.HasKey(kVendorIdKey) && object.HasKey(kProductIdKey) && |
188 object.HasKey(kSerialNumberKey); | 204 object.HasKey(kSerialNumberKey); |
189 } | 205 } |
190 | 206 |
191 void UsbChooserContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { | 207 void UsbChooserContext::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { |
192 for (auto& map_entry : ephemeral_devices_) | 208 for (auto& map_entry : ephemeral_devices_) |
193 map_entry.second.erase(device->guid()); | 209 map_entry.second.erase(device->guid()); |
194 } | 210 } |
OLD | NEW |