| OLD | NEW |
| (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_bubble_delegate.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 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" | |
| 14 #include "chrome/browser/usb/usb_chooser_context.h" | |
| 15 #include "chrome/browser/usb/usb_chooser_context_factory.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "components/bubble/bubble_controller.h" | |
| 18 #include "content/public/browser/render_frame_host.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 #include "device/core/device_client.h" | |
| 21 #include "device/usb/mojo/type_converters.h" | |
| 22 #include "device/usb/usb_device.h" | |
| 23 #include "device/usb/usb_device_filter.h" | |
| 24 #include "url/gurl.h" | |
| 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 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate( | |
| 77 content::RenderFrameHost* owner, | |
| 78 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
| 79 content::RenderFrameHost* render_frame_host, | |
| 80 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) | |
| 81 : ChooserBubbleDelegate(owner), | |
| 82 render_frame_host_(render_frame_host), | |
| 83 callback_(callback), | |
| 84 usb_service_observer_(this), | |
| 85 weak_factory_(this) { | |
| 86 device::UsbService* usb_service = | |
| 87 device::DeviceClient::Get()->GetUsbService(); | |
| 88 if (!usb_service) | |
| 89 return; | |
| 90 | |
| 91 if (!usb_service_observer_.IsObserving(usb_service)) | |
| 92 usb_service_observer_.Add(usb_service); | |
| 93 | |
| 94 if (!device_filters.is_null()) | |
| 95 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); | |
| 96 | |
| 97 usb_service->GetDevices(base::Bind( | |
| 98 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); | |
| 99 } | |
| 100 | |
| 101 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { | |
| 102 if (!callback_.is_null()) | |
| 103 callback_.Run(nullptr); | |
| 104 } | |
| 105 | |
| 106 size_t UsbChooserBubbleDelegate::NumOptions() const { | |
| 107 return devices_.size(); | |
| 108 } | |
| 109 | |
| 110 const base::string16& UsbChooserBubbleDelegate::GetOption(size_t index) const { | |
| 111 DCHECK_LT(index, devices_.size()); | |
| 112 return devices_[index].second; | |
| 113 } | |
| 114 | |
| 115 void UsbChooserBubbleDelegate::Select(size_t index) { | |
| 116 DCHECK_LT(index, devices_.size()); | |
| 117 content::WebContents* web_contents = | |
| 118 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
| 119 GURL embedding_origin = | |
| 120 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
| 121 Profile* profile = | |
| 122 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 123 UsbChooserContext* chooser_context = | |
| 124 UsbChooserContextFactory::GetForProfile(profile); | |
| 125 chooser_context->GrantDevicePermission( | |
| 126 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
| 127 devices_[index].first->guid()); | |
| 128 | |
| 129 device::usb::DeviceInfoPtr device_info_ptr = | |
| 130 device::usb::DeviceInfo::From(*devices_[index].first); | |
| 131 callback_.Run(std::move(device_info_ptr)); | |
| 132 callback_.reset(); // Reset |callback_| so that it is only run once. | |
| 133 | |
| 134 RecordChooserClosure(devices_[index].first->serial_number().empty() | |
| 135 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED | |
| 136 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); | |
| 137 | |
| 138 if (bubble_controller_) | |
| 139 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | |
| 140 } | |
| 141 | |
| 142 void UsbChooserBubbleDelegate::Cancel() { | |
| 143 RecordChooserClosure(devices_.size() == 0 | |
| 144 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES | |
| 145 : WEBUSB_CHOOSER_CLOSED_CANCELLED); | |
| 146 | |
| 147 if (bubble_controller_) | |
| 148 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | |
| 149 } | |
| 150 | |
| 151 void UsbChooserBubbleDelegate::Close() {} | |
| 152 | |
| 153 void UsbChooserBubbleDelegate::OnDeviceAdded( | |
| 154 scoped_refptr<device::UsbDevice> device) { | |
| 155 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 156 FindInAllowedOrigins( | |
| 157 device->webusb_allowed_origins(), | |
| 158 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
| 159 devices_.push_back(std::make_pair(device, device->product_string())); | |
| 160 if (observer()) | |
| 161 observer()->OnOptionAdded(devices_.size() - 1); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 GURL UsbChooserBubbleDelegate::GetHelpCenterUrl() const { | |
| 166 return GURL(chrome::kChooserUsbOverviewURL); | |
| 167 } | |
| 168 | |
| 169 void UsbChooserBubbleDelegate::OnDeviceRemoved( | |
| 170 scoped_refptr<device::UsbDevice> device) { | |
| 171 for (auto it = devices_.begin(); it != devices_.end(); ++it) { | |
| 172 if (it->first == device) { | |
| 173 size_t index = it - devices_.begin(); | |
| 174 devices_.erase(it); | |
| 175 if (observer()) | |
| 176 observer()->OnOptionRemoved(index); | |
| 177 return; | |
| 178 } | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 // Get a list of devices that can be shown in the chooser bubble UI for | |
| 183 // user to grant permsssion. | |
| 184 void UsbChooserBubbleDelegate::GotUsbDeviceList( | |
| 185 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
| 186 for (const auto& device : devices) { | |
| 187 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 188 FindInAllowedOrigins( | |
| 189 device->webusb_allowed_origins(), | |
| 190 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
| 191 devices_.push_back(std::make_pair(device, device->product_string())); | |
| 192 } | |
| 193 } | |
| 194 if (observer()) | |
| 195 observer()->OnOptionsInitialized(); | |
| 196 } | |
| 197 | |
| 198 void UsbChooserBubbleDelegate::set_bubble_controller( | |
| 199 BubbleReference bubble_controller) { | |
| 200 bubble_controller_ = bubble_controller; | |
| 201 } | |
| OLD | NEW |