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_bubble_delegate.h" | 5 #include "chrome/browser/usb/usb_chooser_bubble_delegate.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/metrics/histogram_macros.h" | |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/usb/usb_chooser_context.h" | 14 #include "chrome/browser/usb/usb_chooser_context.h" |
14 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 15 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
15 #include "components/bubble/bubble_controller.h" | 16 #include "components/bubble/bubble_controller.h" |
16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "device/core/device_client.h" | 19 #include "device/core/device_client.h" |
19 #include "device/devices_app/usb/type_converters.h" | 20 #include "device/devices_app/usb/type_converters.h" |
20 #include "device/usb/usb_device.h" | 21 #include "device/usb/usb_device.h" |
21 #include "device/usb/usb_device_filter.h" | 22 #include "device/usb/usb_device_filter.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
27 enum WebUsbChooserClosed { | |
28 // The user cancelled the permission prompt without selecting a device. | |
29 WEBUSB_CHOOSER_CLOSED_CANCELLED = 0, | |
30 // The user probably cancelled the permission prompt without selecting a | |
31 // device because there were no devices to select. | |
32 WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES, | |
33 // The user granted permission to access a device. | |
34 WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED, | |
35 // The user granted permission to access a device but that permission will be | |
36 // revoked when the device is disconnected. | |
37 WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, | |
38 // Maximum value for the enum. | |
39 WEBUSB_CHOOSER_CLOSED_MAX | |
40 }; | |
41 | |
26 // Check if the origin is in the description set. | 42 // Check if the origin is in the description set. |
27 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | 43 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, |
28 const GURL& origin) { | 44 const GURL& origin) { |
29 if (!set) | 45 if (!set) |
30 return false; | 46 return false; |
31 | 47 |
32 if (ContainsValue(set->origins, origin)) | 48 if (ContainsValue(set->origins, origin)) |
33 return true; | 49 return true; |
34 | 50 |
35 for (const auto& config : set->configurations) { | 51 for (const auto& config : set->configurations) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 UsbChooserContextFactory::GetForProfile(profile); | 114 UsbChooserContextFactory::GetForProfile(profile); |
99 chooser_context->GrantDevicePermission( | 115 chooser_context->GrantDevicePermission( |
100 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | 116 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, |
101 devices_[index].first->guid()); | 117 devices_[index].first->guid()); |
102 | 118 |
103 device::usb::DeviceInfoPtr device_info_ptr = | 119 device::usb::DeviceInfoPtr device_info_ptr = |
104 device::usb::DeviceInfo::From(*devices_[index].first); | 120 device::usb::DeviceInfo::From(*devices_[index].first); |
105 callback_.Run(std::move(device_info_ptr)); | 121 callback_.Run(std::move(device_info_ptr)); |
106 callback_.reset(); // Reset |callback_| so that it is only run once. | 122 callback_.reset(); // Reset |callback_| so that it is only run once. |
107 | 123 |
124 if (devices_[index].first->serial_number().empty()) { | |
125 UMA_HISTOGRAM_ENUMERATION( | |
126 "WebUsb.ChooserClosed", | |
127 WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, | |
Alexei Svitkine (slow)
2016/01/18 20:05:13
Nit: Each histogram macro expands to a lot of code
Reilly Grant (use Gerrit)
2016/01/20 00:59:37
Done.
| |
128 WEBUSB_CHOOSER_CLOSED_MAX); | |
129 } else { | |
130 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", | |
131 WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED, | |
132 WEBUSB_CHOOSER_CLOSED_MAX); | |
133 } | |
134 | |
108 if (bubble_controller_) | 135 if (bubble_controller_) |
109 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 136 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
110 } | 137 } |
111 | 138 |
112 void UsbChooserBubbleDelegate::Cancel() { | 139 void UsbChooserBubbleDelegate::Cancel() { |
140 if (devices_.size() == 0) { | |
141 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", | |
142 WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES, | |
143 WEBUSB_CHOOSER_CLOSED_MAX); | |
144 } else { | |
145 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", | |
146 WEBUSB_CHOOSER_CLOSED_CANCELLED, | |
147 WEBUSB_CHOOSER_CLOSED_MAX); | |
148 } | |
149 | |
113 if (bubble_controller_) | 150 if (bubble_controller_) |
114 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 151 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
115 } | 152 } |
116 | 153 |
117 void UsbChooserBubbleDelegate::Close() {} | 154 void UsbChooserBubbleDelegate::Close() {} |
118 | 155 |
119 void UsbChooserBubbleDelegate::OnDeviceAdded( | 156 void UsbChooserBubbleDelegate::OnDeviceAdded( |
120 scoped_refptr<device::UsbDevice> device) { | 157 scoped_refptr<device::UsbDevice> device) { |
121 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 158 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
122 FindOriginInDescriptorSet( | 159 FindOriginInDescriptorSet( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 } | 191 } |
155 } | 192 } |
156 if (observer()) | 193 if (observer()) |
157 observer()->OnOptionsInitialized(); | 194 observer()->OnOptionsInitialized(); |
158 } | 195 } |
159 | 196 |
160 void UsbChooserBubbleDelegate::set_bubble_controller( | 197 void UsbChooserBubbleDelegate::set_bubble_controller( |
161 BubbleReference bubble_controller) { | 198 BubbleReference bubble_controller) { |
162 bubble_controller_ = bubble_controller; | 199 bubble_controller_ = bubble_controller; |
163 } | 200 } |
OLD | NEW |