1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/usb/usb_chooser_controller.h"
#include <stddef.h>
#include <algorithm>
#include <utility>
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/chooser_controller/title_util.h"
#include "chrome/browser/net/referrer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/usb/usb_blocklist.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/usb/web_usb_histograms.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/isolated_context_util.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "services/device/public/cpp/usb/usb_utils.h"
#include "services/device/public/mojom/usb_enumeration_options.mojom.h"
#include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom-shared.h"
#include "third_party/blink/public/common/features_generated.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#if !BUILDFLAG(IS_ANDROID)
#include "services/device/public/cpp/usb/usb_ids.h"
#endif // !BUILDFLAG(IS_ANDROID)
using content::RenderFrameHost;
using content::WebContents;
namespace {
std::u16string FormatUsbDeviceName(
const device::mojom::UsbDeviceInfo& device_info) {
std::u16string device_name;
if (device_info.product_name)
device_name = *device_info.product_name;
if (device_name.empty()) {
uint16_t vendor_id = device_info.vendor_id;
uint16_t product_id = device_info.product_id;
#if !BUILDFLAG(IS_ANDROID)
if (const char* product_name =
device::UsbIds::GetProductName(vendor_id, product_id)) {
return base::UTF8ToUTF16(product_name);
} else if (const char* vendor_name =
device::UsbIds::GetVendorName(vendor_id)) {
return l10n_util::GetStringFUTF16(
IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_NAME,
base::UTF8ToUTF16(vendor_name));
}
#endif // !BUILDFLAG(IS_ANDROID)
device_name = l10n_util::GetStringFUTF16(
IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_ID_AND_PRODUCT_ID,
base::ASCIIToUTF16(base::StringPrintf("%04x", vendor_id)),
base::ASCIIToUTF16(base::StringPrintf("%04x", product_id)));
}
return device_name;
}
void OnDeviceInfoRefreshed(
base::WeakPtr<UsbChooserContext> chooser_context,
const url::Origin& origin,
blink::mojom::WebUsbService::GetPermissionCallback callback,
device::mojom::UsbDeviceInfoPtr device_info) {
if (!chooser_context || !device_info) {
std::move(callback).Run(nullptr);
return;
}
RecordWebUsbChooserClosure(
!device_info->serial_number || device_info->serial_number->empty()
? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED
: WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED);
chooser_context->GrantDevicePermission(origin, *device_info);
std::move(callback).Run(std::move(device_info));
}
} // namespace
UsbChooserController::UsbChooserController(
RenderFrameHost* render_frame_host,
blink::mojom::WebUsbRequestDeviceOptionsPtr options,
blink::mojom::WebUsbService::GetPermissionCallback callback)
: ChooserController(
CreateChooserTitle(render_frame_host, IDS_USB_DEVICE_CHOOSER_PROMPT)),
options_(std::move(options)),
callback_(std::move(callback)),
render_frame_host_id_(render_frame_host->GetGlobalId()) {
RenderFrameHost* main_frame = render_frame_host->GetMainFrame();
origin_ = main_frame->GetLastCommittedOrigin();
Profile* profile =
Profile::FromBrowserContext(main_frame->GetBrowserContext());
chooser_context_ =
UsbChooserContextFactory::GetForProfile(profile)->AsWeakPtr();
DCHECK(chooser_context_);
chooser_context_->GetDevices(base::BindOnce(
&UsbChooserController::GotUsbDeviceList, weak_factory_.GetWeakPtr()));
}
UsbChooserController::~UsbChooserController() {
if (callback_)
std::move(callback_).Run(nullptr);
}
std::u16string UsbChooserController::GetNoOptionsText() const {
return l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT);
}
std::u16string UsbChooserController::GetOkButtonLabel() const {
return l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_CONNECT_BUTTON_TEXT);
}
std::pair<std::u16string, std::u16string>
UsbChooserController::GetThrobberLabelAndTooltip() const {
return {
l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_LOADING_LABEL),
l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_LOADING_LABEL_TOOLTIP)};
}
size_t UsbChooserController::NumOptions() const {
return devices_.size();
}
std::u16string UsbChooserController::GetOption(size_t index) const {
DCHECK_LT(index, devices_.size());
const std::u16string& device_name = devices_[index].second;
const auto& it = device_name_map_.find(device_name);
CHECK(it != device_name_map_.end());
if (it->second == 1)
return device_name;
if (!chooser_context_)
return device_name;
auto* device_info = chooser_context_->GetDeviceInfo(devices_[index].first);
if (!device_info || !device_info->serial_number)
return device_name;
return l10n_util::GetStringFUTF16(IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID,
device_name, *device_info->serial_number);
}
bool UsbChooserController::IsPaired(size_t index) const {
if (!chooser_context_)
return false;
auto* device_info = chooser_context_->GetDeviceInfo(devices_[index].first);
if (!device_info)
return false;
return chooser_context_->HasDevicePermission(origin_, *device_info);
}
void UsbChooserController::Select(const std::vector<size_t>& indices) {
DCHECK_EQ(1u, indices.size());
size_t index = indices[0];
DCHECK_LT(index, devices_.size());
const std::string& guid = devices_[index].first;
if (!chooser_context_) {
// Return nullptr for GetPermissionCallback.
std::move(callback_).Run(nullptr);
return;
}
// The prompt is about to close, destroying |this| so all the parameters
// necessary to grant permission to access the device need to be bound to
// this callback.
auto on_device_info_refreshed = base::BindOnce(
&OnDeviceInfoRefreshed, chooser_context_, origin_, std::move(callback_));
#if BUILDFLAG(IS_ANDROID)
chooser_context_->RefreshDeviceInfo(guid,
std::move(on_device_info_refreshed));
#else
auto* device_info = chooser_context_->GetDeviceInfo(guid);
DCHECK(device_info);
std::move(on_device_info_refreshed).Run(device_info->Clone());
#endif
}
void UsbChooserController::Cancel() {
RecordWebUsbChooserClosure(devices_.empty()
? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES
: WEBUSB_CHOOSER_CLOSED_CANCELLED);
}
void UsbChooserController::Close() {}
void UsbChooserController::OpenHelpCenterUrl() const {
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_frame_host_id_);
if (!render_frame_host) {
// When |render_frame_host| is not valid anymore we don't want to open help
// center url.
return;
}
WebContents::FromRenderFrameHost(render_frame_host)
->OpenURL(content::OpenURLParams(
GURL(chrome::kChooserUsbOverviewURL), content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
false /* is_renderer_initialized */),
/*navigation_handle_callback=*/{});
}
void UsbChooserController::OnDeviceAdded(
const device::mojom::UsbDeviceInfo& device_info) {
if (DisplayDevice(device_info)) {
std::u16string device_name = FormatUsbDeviceName(device_info);
devices_.push_back(std::make_pair(device_info.guid, device_name));
++device_name_map_[device_name];
if (view())
view()->OnOptionAdded(devices_.size() - 1);
}
}
void UsbChooserController::OnDeviceRemoved(
const device::mojom::UsbDeviceInfo& device_info) {
for (auto it = devices_.begin(); it != devices_.end(); ++it) {
if (it->first == device_info.guid) {
size_t index = it - devices_.begin();
DCHECK_GT(device_name_map_[it->second], 0);
if (--device_name_map_[it->second] == 0)
device_name_map_.erase(it->second);
devices_.erase(it);
if (view())
view()->OnOptionRemoved(index);
return;
}
}
}
void UsbChooserController::OnBrowserContextShutdown() {
observation_.Reset();
}
// Get a list of devices that can be shown in the chooser bubble UI for
// user to grant permsssion.
void UsbChooserController::GotUsbDeviceList(
std::vector<::device::mojom::UsbDeviceInfoPtr> devices) {
for (auto& device_info : devices) {
DCHECK(device_info);
if (DisplayDevice(*device_info)) {
std::u16string device_name = FormatUsbDeviceName(*device_info);
devices_.push_back(std::make_pair(device_info->guid, device_name));
++device_name_map_[device_name];
}
}
// Listen to UsbChooserContext for OnDeviceAdded/Removed events after the
// enumeration.
if (chooser_context_)
observation_.Observe(chooser_context_.get());
if (view())
view()->OnOptionsInitialized();
}
bool UsbChooserController::DisplayDevice(
const device::mojom::UsbDeviceInfo& device_info) const {
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_frame_host_id_);
if (!render_frame_host) {
// When |render_frame_host| is not valid anymore we don't want to display
// any device information.
return false;
}
if (!device::UsbDeviceFilterMatchesAny(options_->filters, device_info)) {
return false;
}
if (std::ranges::any_of(
options_->exclusion_filters, [&device_info](const auto& filter) {
return device::UsbDeviceFilterMatches(*filter, device_info);
})) {
return false;
}
bool is_usb_unrestricted = false;
if (base::FeatureList::IsEnabled(blink::features::kUnrestrictedUsb)) {
is_usb_unrestricted =
render_frame_host->IsFeatureEnabled(
network::mojom::PermissionsPolicyFeature::kUsbUnrestricted) &&
content::HasIsolatedContextCapability(render_frame_host);
}
// Isolated context with permission to access the policy-controlled feature
// "usb-unrestricted" can bypass the USB blocklist.
if (!is_usb_unrestricted && UsbBlocklist::Get().IsExcluded(device_info)) {
return false;
}
return true;
}
|