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 |
| 42 void RecordChooserClosure(WebUsbChooserClosed disposition) { |
| 43 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition, |
| 44 WEBUSB_CHOOSER_CLOSED_MAX); |
| 45 } |
| 46 |
26 // Check if the origin is in the description set. | 47 // Check if the origin is in the description set. |
27 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | 48 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, |
28 const GURL& origin) { | 49 const GURL& origin) { |
29 if (!set) | 50 if (!set) |
30 return false; | 51 return false; |
31 | 52 |
32 if (ContainsValue(set->origins, origin)) | 53 if (ContainsValue(set->origins, origin)) |
33 return true; | 54 return true; |
34 | 55 |
35 for (const auto& config : set->configurations) { | 56 for (const auto& config : set->configurations) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 UsbChooserContextFactory::GetForProfile(profile); | 119 UsbChooserContextFactory::GetForProfile(profile); |
99 chooser_context->GrantDevicePermission( | 120 chooser_context->GrantDevicePermission( |
100 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | 121 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, |
101 devices_[index].first->guid()); | 122 devices_[index].first->guid()); |
102 | 123 |
103 device::usb::DeviceInfoPtr device_info_ptr = | 124 device::usb::DeviceInfoPtr device_info_ptr = |
104 device::usb::DeviceInfo::From(*devices_[index].first); | 125 device::usb::DeviceInfo::From(*devices_[index].first); |
105 callback_.Run(std::move(device_info_ptr)); | 126 callback_.Run(std::move(device_info_ptr)); |
106 callback_.reset(); // Reset |callback_| so that it is only run once. | 127 callback_.reset(); // Reset |callback_| so that it is only run once. |
107 | 128 |
| 129 RecordChooserClosure(devices_[index].first->serial_number().empty() |
| 130 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED |
| 131 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); |
| 132 |
108 if (bubble_controller_) | 133 if (bubble_controller_) |
109 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 134 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
110 } | 135 } |
111 | 136 |
112 void UsbChooserBubbleDelegate::Cancel() { | 137 void UsbChooserBubbleDelegate::Cancel() { |
| 138 RecordChooserClosure(devices_.size() == 0 |
| 139 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES |
| 140 : WEBUSB_CHOOSER_CLOSED_CANCELLED); |
| 141 |
113 if (bubble_controller_) | 142 if (bubble_controller_) |
114 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 143 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
115 } | 144 } |
116 | 145 |
117 void UsbChooserBubbleDelegate::Close() {} | 146 void UsbChooserBubbleDelegate::Close() {} |
118 | 147 |
119 void UsbChooserBubbleDelegate::OnDeviceAdded( | 148 void UsbChooserBubbleDelegate::OnDeviceAdded( |
120 scoped_refptr<device::UsbDevice> device) { | 149 scoped_refptr<device::UsbDevice> device) { |
121 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 150 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
122 FindOriginInDescriptorSet( | 151 FindOriginInDescriptorSet( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } | 183 } |
155 } | 184 } |
156 if (observer()) | 185 if (observer()) |
157 observer()->OnOptionsInitialized(); | 186 observer()->OnOptionsInitialized(); |
158 } | 187 } |
159 | 188 |
160 void UsbChooserBubbleDelegate::set_bubble_controller( | 189 void UsbChooserBubbleDelegate::set_bubble_controller( |
161 BubbleReference bubble_controller) { | 190 BubbleReference bubble_controller) { |
162 bubble_controller_ = bubble_controller; | 191 bubble_controller_ = bubble_controller; |
163 } | 192 } |
OLD | NEW |