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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
// Copyright 2019 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/hid/hid_chooser_context.h"
#include <set>
#include <string_view>
#include <utility>
#include "base/containers/contains.h"
#include "base/containers/map_util.h"
#include "base/observer_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/hid/hid_policy_allowed_devices.h"
#include "chrome/browser/hid/hid_policy_allowed_devices_factory.h"
#include "chrome/browser/hid/web_view_chooser_context.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/browser/device_service.h"
#include "extensions/buildflags/buildflags.h"
#include "services/device/public/cpp/device_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "components/user_manager/user.h"
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/containers/fixed_flat_set.h"
#include "extensions/common/constants.h"
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
namespace {
constexpr char kHidDeviceNameKey[] = "name";
constexpr char kHidGuidKey[] = "guid";
constexpr char kHidVendorIdKey[] = "vendor-id";
constexpr char kHidProductIdKey[] = "product-id";
constexpr char kHidSerialNumberKey[] = "serial-number";
using content_settings::SettingSource;
bool IsPolicyGrantedObject(const base::Value::Dict& object) {
return object.size() == 1 && object.FindString(kHidDeviceNameKey);
}
base::Value::Dict VendorAndProductIdsToValue(uint16_t vendor_id,
uint16_t product_id) {
base::Value::Dict object;
object.Set(kHidDeviceNameKey,
l10n_util::GetStringFUTF16(
IDS_HID_POLICY_DESCRIPTION_FOR_VENDOR_ID_AND_PRODUCT_ID,
base::ASCIIToUTF16(base::StringPrintf("%04X", vendor_id)),
base::ASCIIToUTF16(base::StringPrintf("%04X", product_id))));
DCHECK(IsPolicyGrantedObject(object));
return object;
}
base::Value::Dict VendorIdToValue(uint16_t vendor_id) {
base::Value::Dict object;
object.Set(kHidDeviceNameKey,
l10n_util::GetStringFUTF16(
IDS_HID_POLICY_DESCRIPTION_FOR_VENDOR_ID,
base::ASCIIToUTF16(base::StringPrintf("%04X", vendor_id))));
DCHECK(IsPolicyGrantedObject(object));
return object;
}
base::Value::Dict UsagePageAndUsageToValue(uint16_t usage_page,
uint16_t usage) {
base::Value::Dict object;
object.Set(kHidDeviceNameKey,
l10n_util::GetStringFUTF16(
IDS_HID_POLICY_DESCRIPTION_FOR_USAGE_AND_USAGE_PAGE,
base::ASCIIToUTF16(base::StringPrintf("%04X", usage)),
base::ASCIIToUTF16(base::StringPrintf("%04X", usage_page))));
DCHECK(IsPolicyGrantedObject(object));
return object;
}
base::Value::Dict UsagePageToValue(uint16_t usage_page) {
base::Value::Dict object;
object.Set(kHidDeviceNameKey,
l10n_util::GetStringFUTF16(
IDS_HID_POLICY_DESCRIPTION_FOR_USAGE_PAGE,
base::ASCIIToUTF16(base::StringPrintf("%04X", usage_page))));
DCHECK(IsPolicyGrantedObject(object));
return object;
}
} // namespace
void HidChooserContext::DeviceObserver::OnDeviceAdded(
const device::mojom::HidDeviceInfo& device) {}
void HidChooserContext::DeviceObserver::OnDeviceRemoved(
const device::mojom::HidDeviceInfo& device) {}
void HidChooserContext::DeviceObserver::OnDeviceChanged(
const device::mojom::HidDeviceInfo& device) {}
void HidChooserContext::DeviceObserver::OnHidManagerConnectionError() {}
HidChooserContext::HidChooserContext(Profile* profile)
: ObjectPermissionContextBase(
ContentSettingsType::HID_GUARD,
ContentSettingsType::HID_CHOOSER_DATA,
HostContentSettingsMapFactory::GetForProfile(profile)),
profile_(profile) {
DCHECK(profile_);
}
HidChooserContext::~HidChooserContext() {
// Notify observers that the chooser context is about to be destroyed.
// Observers must remove themselves from the observer lists.
for (auto& observer : device_observer_list_) {
observer.OnHidChooserContextShutdown();
DCHECK(!device_observer_list_.HasObserver(&observer));
}
web_view_chooser_context_.OnHidChooserContextShutdown();
DCHECK(permission_observer_list_.empty());
}
// static
base::Value::Dict HidChooserContext::DeviceInfoToValue(
const device::mojom::HidDeviceInfo& device) {
base::Value::Dict value;
value.Set(
kHidDeviceNameKey,
base::UTF16ToUTF8(HidChooserContext::DisplayNameFromDeviceInfo(device)));
value.Set(kHidVendorIdKey, device.vendor_id);
value.Set(kHidProductIdKey, device.product_id);
if (HidChooserContext::CanStorePersistentEntry(device)) {
// Use the USB serial number as a persistent identifier. If it is
// unavailable, only ephemeral permissions may be granted.
value.Set(kHidSerialNumberKey, device.serial_number);
} else {
// The GUID is a temporary ID created on connection that remains valid until
// the device is disconnected. Ephemeral permissions are keyed by this ID
// and must be granted again each time the device is connected.
value.Set(kHidGuidKey, device.guid);
}
DCHECK(!IsPolicyGrantedObject(value));
return value;
}
// static
std::u16string HidChooserContext::DisplayNameFromDeviceInfo(
const device::mojom::HidDeviceInfo& device) {
if (device.product_name.empty()) {
auto device_id_string = base::ASCIIToUTF16(
base::StringPrintf("%04X:%04X", device.vendor_id, device.product_id));
return l10n_util::GetStringFUTF16(IDS_HID_CHOOSER_ITEM_WITHOUT_NAME,
device_id_string);
}
return base::UTF8ToUTF16(device.product_name);
}
// static
bool HidChooserContext::CanStorePersistentEntry(
const device::mojom::HidDeviceInfo& device) {
return !device.serial_number.empty() && !device.product_name.empty();
}
std::u16string HidChooserContext::GetObjectDisplayName(
const base::Value::Dict& object) {
const std::string* name = object.FindString(kHidDeviceNameKey);
DCHECK(name);
return base::UTF8ToUTF16(*name);
}
std::string HidChooserContext::GetKeyForObject(
const base::Value::Dict& object) {
if (!IsValidObject(object))
return std::string();
if (IsPolicyGrantedObject(object)) {
return *object.FindString(kHidDeviceNameKey);
}
return base::JoinString(
{base::NumberToString(*(object.FindInt(kHidVendorIdKey))),
base::NumberToString(*(object.FindInt(kHidProductIdKey))),
*(object.FindString(kHidSerialNumberKey))},
"|");
}
bool HidChooserContext::IsValidObject(const base::Value::Dict& object) {
if (IsPolicyGrantedObject(object))
return true;
// Other objects must have name, vendor, product, and either a GUID or a
// serial number.
if (object.size() != 4 || !object.FindString(kHidDeviceNameKey) ||
!object.FindInt(kHidProductIdKey) || !object.FindInt(kHidVendorIdKey)) {
return false;
}
const std::string* guid = object.FindString(kHidGuidKey);
if (guid && !guid->empty())
return true;
const std::string* serial_number = object.FindString(kHidSerialNumberKey);
return serial_number && !serial_number->empty();
}
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
HidChooserContext::GetGrantedObjects(const url::Origin& origin) {
std::vector<std::unique_ptr<Object>> objects =
ObjectPermissionContextBase::GetGrantedObjects(origin);
if (CanRequestObjectPermission(origin)) {
auto it = ephemeral_devices_.find(origin);
if (it != ephemeral_devices_.end()) {
for (const std::string& guid : it->second) {
// |devices_| should be initialized when |ephemeral_devices_| is filled.
// Because |ephemeral_devices_| is filled by GrantDevicePermission()
// which is called in HidChooserController::Select(), this method will
// always be called after device initialization in HidChooserController
// which always returns after the device list initialization in this
// class.
DCHECK(base::Contains(devices_, guid));
objects.push_back(std::make_unique<Object>(
origin, DeviceInfoToValue(*devices_[guid]),
content_settings::SettingSource::kUser, IsOffTheRecord()));
}
}
}
if (CanApplyPolicy()) {
auto* policy = HidPolicyAllowedDevicesFactory::GetForProfile(profile_);
for (const auto& entry : policy->device_policy()) {
if (!base::Contains(entry.second, origin))
continue;
auto object =
VendorAndProductIdsToValue(entry.first.first, entry.first.second);
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, std::move(object), SettingSource::kPolicy, IsOffTheRecord()));
}
for (const auto& entry : policy->vendor_policy()) {
if (!base::Contains(entry.second, origin))
continue;
auto object = VendorIdToValue(entry.first);
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, std::move(object), SettingSource::kPolicy, IsOffTheRecord()));
}
for (const auto& entry : policy->usage_policy()) {
if (!base::Contains(entry.second, origin))
continue;
auto object =
UsagePageAndUsageToValue(entry.first.first, entry.first.second);
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, std::move(object), SettingSource::kPolicy, IsOffTheRecord()));
}
for (const auto& entry : policy->usage_page_policy()) {
if (!base::Contains(entry.second, origin))
continue;
auto object = UsagePageToValue(entry.first);
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, std::move(object), SettingSource::kPolicy, IsOffTheRecord()));
}
if (base::Contains(policy->all_devices_policy(), origin)) {
base::Value::Dict object;
object.Set(
kHidDeviceNameKey,
l10n_util::GetStringUTF16(IDS_HID_POLICY_DESCRIPTION_FOR_ANY_DEVICE));
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, std::move(object), SettingSource::kPolicy, IsOffTheRecord()));
}
}
return objects;
}
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
HidChooserContext::GetAllGrantedObjects() {
std::vector<std::unique_ptr<Object>> objects =
ObjectPermissionContextBase::GetAllGrantedObjects();
for (const auto& map_entry : ephemeral_devices_) {
const url::Origin& origin = map_entry.first;
if (!CanRequestObjectPermission(origin))
continue;
for (const auto& guid : map_entry.second) {
DCHECK(base::Contains(devices_, guid));
objects.push_back(
std::make_unique<Object>(origin, DeviceInfoToValue(*devices_[guid]),
SettingSource::kUser, IsOffTheRecord()));
}
}
if (CanApplyPolicy()) {
auto* policy = HidPolicyAllowedDevicesFactory::GetForProfile(profile_);
for (const auto& entry : policy->device_policy()) {
auto object =
VendorAndProductIdsToValue(entry.first.first, entry.first.second);
for (const auto& origin : entry.second) {
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, object.Clone(), SettingSource::kPolicy, IsOffTheRecord()));
}
}
for (const auto& entry : policy->vendor_policy()) {
auto object = VendorIdToValue(entry.first);
for (const auto& origin : entry.second) {
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, object.Clone(), SettingSource::kPolicy, IsOffTheRecord()));
}
}
for (const auto& entry : policy->usage_policy()) {
auto object =
UsagePageAndUsageToValue(entry.first.first, entry.first.second);
for (const auto& origin : entry.second) {
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, object.Clone(), SettingSource::kPolicy, IsOffTheRecord()));
}
}
for (const auto& entry : policy->usage_page_policy()) {
auto object = UsagePageToValue(entry.first);
for (const auto& origin : entry.second) {
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, object.Clone(), SettingSource::kPolicy, IsOffTheRecord()));
}
}
base::Value::Dict object;
object.Set(
kHidDeviceNameKey,
l10n_util::GetStringUTF16(IDS_HID_POLICY_DESCRIPTION_FOR_ANY_DEVICE));
for (const auto& origin : policy->all_devices_policy()) {
objects.push_back(std::make_unique<ObjectPermissionContextBase::Object>(
origin, object.Clone(), SettingSource::kPolicy, IsOffTheRecord()));
}
}
return objects;
}
void HidChooserContext::RevokeObjectPermission(
const url::Origin& origin,
const base::Value::Dict& object) {
const std::string* guid = object.FindString(kHidGuidKey);
if (!guid) {
ObjectPermissionContextBase::RevokeObjectPermission(origin, object);
// TODO(crbug.com/40627829): Record UMA (WEBHID_PERMISSION_REVOKED).
return;
}
auto it = ephemeral_devices_.find(origin);
if (it != ephemeral_devices_.end()) {
std::set<std::string>& devices = it->second;
DCHECK(IsValidObject(object));
devices.erase(*guid);
if (devices.empty())
ephemeral_devices_.erase(it);
NotifyPermissionRevoked(origin);
}
// TODO(crbug.com/40627829): Record UMA (WEBHID_PERMISSION_REVOKED_EPHEMERAL).
}
void HidChooserContext::GrantDevicePermission(
const url::Origin& origin,
const device::mojom::HidDeviceInfo& device,
const std::optional<url::Origin>& embedding_origin_of_web_view) {
if (embedding_origin_of_web_view) {
web_view_chooser_context_.GrantDevicePermission(
origin, *embedding_origin_of_web_view, device);
return;
}
if (CanStorePersistentEntry(device)) {
GrantObjectPermission(origin, DeviceInfoToValue(device));
} else {
ephemeral_devices_[origin].insert(device.guid);
NotifyPermissionChanged();
}
}
void HidChooserContext::RevokeDevicePermission(
const url::Origin& origin,
const device::mojom::HidDeviceInfo& device,
const std::optional<url::Origin>& embedding_origin_of_web_view) {
if (embedding_origin_of_web_view) {
web_view_chooser_context_.RevokeDevicePermission(
origin, *embedding_origin_of_web_view, device);
return;
}
if (CanStorePersistentEntry(device)) {
RevokePersistentDevicePermission(origin, device);
} else {
RevokeEphemeralDevicePermission(origin, device);
}
}
void HidChooserContext::RevokePersistentDevicePermission(
const url::Origin& origin,
const device::mojom::HidDeviceInfo& device) {
std::vector<std::unique_ptr<Object>> object_list = GetGrantedObjects(origin);
for (const auto& object : object_list) {
const base::Value::Dict& device_value = object->value;
DCHECK(IsValidObject(device_value));
const auto* serial_number = device_value.FindString(kHidSerialNumberKey);
if (device.vendor_id == *device_value.FindInt(kHidVendorIdKey) &&
device.product_id == *device_value.FindInt(kHidProductIdKey) &&
serial_number && device.serial_number == *serial_number) {
RevokeObjectPermission(origin, device_value);
}
}
}
void HidChooserContext::RevokeEphemeralDevicePermission(
const url::Origin& origin,
const device::mojom::HidDeviceInfo& device) {
auto it = ephemeral_devices_.find(origin);
if (it == ephemeral_devices_.end()) {
return;
}
std::set<std::string>& device_guids = it->second;
bool revoked_permission =
std::erase_if(device_guids, [&](const auto& guid) {
auto* device_ptr = base::FindPtrOrNull(devices_, guid);
return device_ptr &&
device_ptr->physical_device_id == device.physical_device_id;
}) > 0;
if (device_guids.empty()) {
ephemeral_devices_.erase(it);
}
if (revoked_permission) {
NotifyPermissionRevoked(origin);
}
}
bool HidChooserContext::HasDevicePermission(
const url::Origin& origin,
const device::mojom::HidDeviceInfo& device,
const std::optional<url::Origin>& embedding_origin_of_web_view) {
if (device.is_excluded_by_blocklist) {
bool is_device_protected_due_to_fido =
base::Contains(device.collections, device::mojom::kPageFido,
[](const auto& c) { return c->usage->usage_page; });
if (base::FeatureList::IsEnabled(
features::kSecurityKeyHidInterfacesAreFido) &&
IsKnownSecurityKey(device)) {
is_device_protected_due_to_fido = true;
}
if (!is_device_protected_due_to_fido || !IsFidoAllowedForOrigin(origin)) {
return false;
}
}
if (CanApplyPolicy() &&
HidPolicyAllowedDevicesFactory::GetForProfile(profile_)
->HasDevicePermission(origin, device)) {
return true;
}
if (!CanRequestObjectPermission(origin))
return false;
if (embedding_origin_of_web_view) {
return web_view_chooser_context_.HasDevicePermission(
origin, *embedding_origin_of_web_view, device);
}
auto it = ephemeral_devices_.find(origin);
if (it != ephemeral_devices_.end() &&
base::Contains(it->second, device.guid)) {
return true;
}
for (const auto& object :
ObjectPermissionContextBase::GetGrantedObjects(origin)) {
const base::Value::Dict& device_value = object->value;
// Objects provided by the parent class can be assumed valid.
DCHECK(IsValidObject(device_value));
if (device.vendor_id != *device_value.FindInt(kHidVendorIdKey) ||
device.product_id != *device_value.FindInt(kHidProductIdKey)) {
continue;
}
const auto* serial_number = device_value.FindString(kHidSerialNumberKey);
if (serial_number && device.serial_number == *serial_number)
return true;
}
return false;
}
bool HidChooserContext::IsFidoAllowedForOrigin(const url::Origin& origin) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
static constexpr auto kPrivilegedExtensionIds =
base::MakeFixedFlatSet<std::string_view>({
"ckcendljdlmgnhghiaomidhiiclmapok", // gnubbyd-v3 dev
"lfboplenmmjcmpbkeemecobbadnmpfhi", // gnubbyd-v3 prod
"gdmilihokhggmmlomocddffphkaikkke", // gnubbyd-v3 flywheel
});
if (origin.scheme() == extensions::kExtensionScheme &&
base::Contains(kPrivilegedExtensionIds, origin.host())) {
return true;
}
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
return false;
}
bool HidChooserContext::IsKnownSecurityKey(
const device::mojom::HidDeviceInfo& device) {
static constexpr uint16_t kVendorGoogle = 0x18d1;
static constexpr uint16_t kProductTitan = 0x5026;
return device.vendor_id == kVendorGoogle &&
device.product_id == kProductTitan;
}
void HidChooserContext::AddDeviceObserver(DeviceObserver* observer) {
EnsureHidManagerConnection();
device_observer_list_.AddObserver(observer);
}
void HidChooserContext::RemoveDeviceObserver(DeviceObserver* observer) {
device_observer_list_.RemoveObserver(observer);
}
void HidChooserContext::GetDevices(
device::mojom::HidManager::GetDevicesCallback callback) {
if (!is_initialized_) {
EnsureHidManagerConnection();
pending_get_devices_requests_.push(std::move(callback));
return;
}
std::vector<device::mojom::HidDeviceInfoPtr> device_list;
device_list.reserve(devices_.size());
for (const auto& pair : devices_)
device_list.push_back(pair.second->Clone());
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), std::move(device_list)));
}
const device::mojom::HidDeviceInfo* HidChooserContext::GetDeviceInfo(
const std::string& guid) {
DCHECK(is_initialized_);
auto it = devices_.find(guid);
return it == devices_.end() ? nullptr : it->second.get();
}
device::mojom::HidManager* HidChooserContext::GetHidManager() {
EnsureHidManagerConnection();
return hid_manager_.get();
}
void HidChooserContext::SetHidManagerForTesting(
mojo::PendingRemote<device::mojom::HidManager> manager,
device::mojom::HidManager::GetDevicesCallback callback) {
hid_manager_.Bind(std::move(manager));
hid_manager_.set_disconnect_handler(base::BindOnce(
&HidChooserContext::OnHidManagerConnectionError, base::Unretained(this)));
hid_manager_->GetDevicesAndSetClient(
client_receiver_.BindNewEndpointAndPassRemote(),
base::BindOnce(&HidChooserContext::OnHidManagerInitializedForTesting,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void HidChooserContext::OnHidManagerInitializedForTesting(
device::mojom::HidManager::GetDevicesCallback callback,
std::vector<device::mojom::HidDeviceInfoPtr> devices) {
DCHECK(devices.empty());
DCHECK(pending_get_devices_requests_.empty());
is_initialized_ = true;
std::move(callback).Run({});
}
void HidChooserContext::PermissionForWebViewChanged() {
NotifyPermissionChanged();
}
void HidChooserContext::PermissionForWebViewRevoked(
const url::Origin& web_view_origin) {
NotifyPermissionRevoked(web_view_origin);
}
base::WeakPtr<HidChooserContext> HidChooserContext::AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void HidChooserContext::Shutdown() {
FlushScheduledSaveSettingsCalls();
permissions::ObjectPermissionContextBase::Shutdown();
}
void HidChooserContext::DeviceAdded(device::mojom::HidDeviceInfoPtr device) {
DCHECK(device);
// Update the device list.
if (!base::Contains(devices_, device->guid))
devices_.insert({device->guid, device->Clone()});
// Notify all observers.
for (auto& observer : device_observer_list_)
observer.OnDeviceAdded(*device);
}
void HidChooserContext::DeviceRemoved(device::mojom::HidDeviceInfoPtr device) {
DCHECK(device);
DCHECK(base::Contains(devices_, device->guid));
// Update the device list.
devices_.erase(device->guid);
// Notify all device observers.
for (auto& observer : device_observer_list_)
observer.OnDeviceRemoved(*device);
// Next we'll notify observers for revoked permissions. If the device does not
// support persistent permissions then device permissions are revoked on
// disconnect.
if (CanStorePersistentEntry(*device))
return;
std::vector<url::Origin> revoked_origins;
for (auto& map_entry : ephemeral_devices_) {
if (map_entry.second.erase(device->guid) > 0)
revoked_origins.push_back(map_entry.first);
}
if (revoked_origins.empty())
return;
for (auto& observer : permission_observer_list_) {
observer.OnObjectPermissionChanged(guard_content_settings_type_,
data_content_settings_type_);
for (auto& origin : revoked_origins) {
observer.OnPermissionRevoked(origin);
}
}
}
void HidChooserContext::DeviceChanged(device::mojom::HidDeviceInfoPtr device) {
DCHECK(device);
DCHECK(base::Contains(devices_, device->guid));
// Update the device list.
devices_[device->guid] = device->Clone();
// Notify all observers.
for (auto& observer : device_observer_list_)
observer.OnDeviceChanged(*device);
}
void HidChooserContext::EnsureHidManagerConnection() {
if (hid_manager_)
return;
mojo::PendingRemote<device::mojom::HidManager> manager;
content::GetDeviceService().BindHidManager(
manager.InitWithNewPipeAndPassReceiver());
SetUpHidManagerConnection(std::move(manager));
}
void HidChooserContext::SetUpHidManagerConnection(
mojo::PendingRemote<device::mojom::HidManager> manager) {
hid_manager_.Bind(std::move(manager));
hid_manager_.set_disconnect_handler(base::BindOnce(
&HidChooserContext::OnHidManagerConnectionError, base::Unretained(this)));
hid_manager_->GetDevicesAndSetClient(
client_receiver_.BindNewEndpointAndPassRemote(),
base::BindOnce(&HidChooserContext::InitDeviceList,
weak_factory_.GetWeakPtr()));
}
void HidChooserContext::InitDeviceList(
std::vector<device::mojom::HidDeviceInfoPtr> devices) {
for (auto& device : devices)
devices_.insert({device->guid, std::move(device)});
is_initialized_ = true;
while (!pending_get_devices_requests_.empty()) {
std::vector<device::mojom::HidDeviceInfoPtr> device_list;
device_list.reserve(devices.size());
for (const auto& entry : devices_)
device_list.push_back(entry.second->Clone());
std::move(pending_get_devices_requests_.front())
.Run(std::move(device_list));
pending_get_devices_requests_.pop();
}
}
void HidChooserContext::OnHidManagerConnectionError() {
hid_manager_.reset();
client_receiver_.reset();
devices_.clear();
std::vector<url::Origin> revoked_origins;
revoked_origins.reserve(ephemeral_devices_.size());
for (const auto& map_entry : ephemeral_devices_)
revoked_origins.push_back(map_entry.first);
ephemeral_devices_.clear();
// Notify all device observers.
for (auto& observer : device_observer_list_)
observer.OnHidManagerConnectionError();
// Notify permission observers that all ephemeral permissions have been
// revoked.
for (auto& observer : permission_observer_list_) {
observer.OnObjectPermissionChanged(guard_content_settings_type_,
data_content_settings_type_);
for (const auto& origin : revoked_origins)
observer.OnPermissionRevoked(origin);
}
}
bool HidChooserContext::CanApplyPolicy() {
#if BUILDFLAG(IS_CHROMEOS)
auto* profile_helper = ash::ProfileHelper::Get();
DCHECK(profile_helper);
user_manager::User* user = profile_helper->GetUserByProfile(profile_);
return !user || user->IsAffiliated();
#else
return true;
#endif
}
|