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_controller.h" | 5 #include "chrome/browser/usb/usb_chooser_bubble_controller.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" | |
12 #include "base/stl_util.h" | |
13 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/usb/usb_chooser_context.h" | 12 #include "chrome/browser/usb/usb_chooser_context.h" |
15 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 13 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
| 14 #include "chrome/browser/usb/web_usb_histograms.h" |
16 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
17 #include "components/bubble/bubble_controller.h" | 16 #include "components/bubble/bubble_controller.h" |
18 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
19 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
20 #include "device/core/device_client.h" | 19 #include "device/core/device_client.h" |
21 #include "device/usb/mojo/type_converters.h" | 20 #include "device/usb/mojo/type_converters.h" |
22 #include "device/usb/usb_device.h" | 21 #include "device/usb/usb_device.h" |
23 #include "device/usb/usb_device_filter.h" | 22 #include "device/usb/usb_device_filter.h" |
| 23 #include "device/usb/webusb_descriptors.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace { | |
27 | |
28 // Reasons the chooser may be closed. These are used in histograms so do not | |
29 // remove/reorder entries. Only add at the end just before | |
30 // WEBUSB_CHOOSER_CLOSED_MAX. Also remember to update the enum listing in | |
31 // tools/metrics/histograms/histograms.xml. | |
32 enum WebUsbChooserClosed { | |
33 // The user cancelled the permission prompt without selecting a device. | |
34 WEBUSB_CHOOSER_CLOSED_CANCELLED = 0, | |
35 // The user probably cancelled the permission prompt without selecting a | |
36 // device because there were no devices to select. | |
37 WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES, | |
38 // The user granted permission to access a device. | |
39 WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED, | |
40 // The user granted permission to access a device but that permission will be | |
41 // revoked when the device is disconnected. | |
42 WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, | |
43 // Maximum value for the enum. | |
44 WEBUSB_CHOOSER_CLOSED_MAX | |
45 }; | |
46 | |
47 void RecordChooserClosure(WebUsbChooserClosed disposition) { | |
48 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition, | |
49 WEBUSB_CHOOSER_CLOSED_MAX); | |
50 } | |
51 | |
52 // Check if the origin is allowed. | |
53 bool FindInAllowedOrigins(const device::WebUsbAllowedOrigins* allowed_origins, | |
54 const GURL& origin) { | |
55 if (!allowed_origins) | |
56 return false; | |
57 | |
58 if (ContainsValue(allowed_origins->origins, origin)) | |
59 return true; | |
60 | |
61 for (const auto& config : allowed_origins->configurations) { | |
62 if (ContainsValue(config.origins, origin)) | |
63 return true; | |
64 | |
65 for (const auto& function : config.functions) { | |
66 if (ContainsValue(function.origins, origin)) | |
67 return true; | |
68 } | |
69 } | |
70 | |
71 return false; | |
72 } | |
73 | |
74 } // namespace | |
75 | |
76 UsbChooserBubbleController::UsbChooserBubbleController( | 26 UsbChooserBubbleController::UsbChooserBubbleController( |
77 content::RenderFrameHost* owner, | 27 content::RenderFrameHost* owner, |
78 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | 28 mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
79 content::RenderFrameHost* render_frame_host, | 29 content::RenderFrameHost* render_frame_host, |
80 const device::usb::ChooserService::GetPermissionCallback& callback) | 30 const device::usb::ChooserService::GetPermissionCallback& callback) |
81 : ChooserBubbleController(owner), | 31 : ChooserBubbleController(owner), |
82 render_frame_host_(render_frame_host), | 32 render_frame_host_(render_frame_host), |
83 callback_(callback), | 33 callback_(callback), |
84 usb_service_observer_(this), | 34 usb_service_observer_(this), |
85 weak_factory_(this) { | 35 weak_factory_(this) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 UsbChooserContextFactory::GetForProfile(profile); | 76 UsbChooserContextFactory::GetForProfile(profile); |
127 chooser_context->GrantDevicePermission( | 77 chooser_context->GrantDevicePermission( |
128 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | 78 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, |
129 devices_[index].first->guid()); | 79 devices_[index].first->guid()); |
130 | 80 |
131 device::usb::DeviceInfoPtr device_info_ptr = | 81 device::usb::DeviceInfoPtr device_info_ptr = |
132 device::usb::DeviceInfo::From(*devices_[index].first); | 82 device::usb::DeviceInfo::From(*devices_[index].first); |
133 callback_.Run(std::move(device_info_ptr)); | 83 callback_.Run(std::move(device_info_ptr)); |
134 callback_.reset(); // Reset |callback_| so that it is only run once. | 84 callback_.reset(); // Reset |callback_| so that it is only run once. |
135 | 85 |
136 RecordChooserClosure(devices_[index].first->serial_number().empty() | 86 RecordWebUsbChooserClosure( |
137 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED | 87 devices_[index].first->serial_number().empty() |
138 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); | 88 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED |
| 89 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); |
139 | 90 |
140 if (bubble_reference_) | 91 if (bubble_reference_) |
141 bubble_reference_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 92 bubble_reference_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
142 } | 93 } |
143 | 94 |
144 void UsbChooserBubbleController::Cancel() { | 95 void UsbChooserBubbleController::Cancel() { |
145 RecordChooserClosure(devices_.size() == 0 | 96 RecordWebUsbChooserClosure(devices_.size() == 0 |
146 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES | 97 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES |
147 : WEBUSB_CHOOSER_CLOSED_CANCELLED); | 98 : WEBUSB_CHOOSER_CLOSED_CANCELLED); |
148 | 99 |
149 if (bubble_reference_) | 100 if (bubble_reference_) |
150 bubble_reference_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 101 bubble_reference_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
151 } | 102 } |
152 | 103 |
153 void UsbChooserBubbleController::Close() {} | 104 void UsbChooserBubbleController::Close() {} |
154 | 105 |
155 void UsbChooserBubbleController::OnDeviceAdded( | 106 void UsbChooserBubbleController::OnDeviceAdded( |
156 scoped_refptr<device::UsbDevice> device) { | 107 scoped_refptr<device::UsbDevice> device) { |
157 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 108 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
158 FindInAllowedOrigins( | 109 FindInWebUsbAllowedOrigins( |
159 device->webusb_allowed_origins(), | 110 device->webusb_allowed_origins(), |
160 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 111 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
161 devices_.push_back(std::make_pair(device, device->product_string())); | 112 devices_.push_back(std::make_pair(device, device->product_string())); |
162 if (observer()) | 113 if (observer()) |
163 observer()->OnOptionAdded(devices_.size() - 1); | 114 observer()->OnOptionAdded(devices_.size() - 1); |
164 } | 115 } |
165 } | 116 } |
166 | 117 |
167 GURL UsbChooserBubbleController::GetHelpCenterUrl() const { | 118 GURL UsbChooserBubbleController::GetHelpCenterUrl() const { |
168 return GURL(chrome::kChooserUsbOverviewURL); | 119 return GURL(chrome::kChooserUsbOverviewURL); |
(...skipping 11 matching lines...) Expand all Loading... |
180 } | 131 } |
181 } | 132 } |
182 } | 133 } |
183 | 134 |
184 // Get a list of devices that can be shown in the chooser bubble UI for | 135 // Get a list of devices that can be shown in the chooser bubble UI for |
185 // user to grant permsssion. | 136 // user to grant permsssion. |
186 void UsbChooserBubbleController::GotUsbDeviceList( | 137 void UsbChooserBubbleController::GotUsbDeviceList( |
187 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | 138 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
188 for (const auto& device : devices) { | 139 for (const auto& device : devices) { |
189 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 140 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
190 FindInAllowedOrigins( | 141 FindInWebUsbAllowedOrigins( |
191 device->webusb_allowed_origins(), | 142 device->webusb_allowed_origins(), |
192 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 143 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
193 devices_.push_back(std::make_pair(device, device->product_string())); | 144 devices_.push_back(std::make_pair(device, device->product_string())); |
194 } | 145 } |
195 } | 146 } |
196 if (observer()) | 147 if (observer()) |
197 observer()->OnOptionsInitialized(); | 148 observer()->OnOptionsInitialized(); |
198 } | 149 } |
199 | 150 |
200 void UsbChooserBubbleController::set_bubble_reference( | 151 void UsbChooserBubbleController::set_bubble_reference( |
201 BubbleReference bubble_reference) { | 152 BubbleReference bubble_reference) { |
202 bubble_reference_ = bubble_reference; | 153 bubble_reference_ = bubble_reference; |
203 } | 154 } |
OLD | NEW |