#include "chrome/browser/usb/chrome_usb_delegate.h"
#include <string_view>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/usb/usb_blocklist.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/usb/usb_chooser_controller.h"
#include "chrome/browser/usb/usb_connection_tracker.h"
#include "chrome/browser/usb/usb_connection_tracker_factory.h"
#include "chrome/browser/usb/web_usb_chooser.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/url_constants.h"
#include "components/permissions/object_permission_context_base.h"
#include "content/public/browser/isolated_context_util.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
#include "services/device/public/mojom/usb_enumeration_options.mojom.h"
#include "third_party/blink/public/common/features_generated.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/containers/fixed_flat_set.h"
#include "chrome/common/chrome_features.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#endif
namespace {
UsbChooser;
UsbChooserContext* GetChooserContext(content::BrowserContext* browser_context) { … }
#if !BUILDFLAG(IS_ANDROID)
UsbConnectionTracker* GetConnectionTracker(
content::BrowserContext* browser_context,
bool create) { … }
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
constexpr auto kSmartCardPrivilegedExtensionIds = …;
bool DeviceHasInterfaceWithClass(
const device::mojom::UsbDeviceInfo& device_info,
uint8_t interface_class) { … }
#endif
bool IsDevicePermissionAutoGranted(
const url::Origin& origin,
const device::mojom::UsbDeviceInfo& device_info) { … }
}
class ChromeUsbDelegate::ContextObservation
: public permissions::ObjectPermissionContextBase::PermissionObserver,
public UsbChooserContext::DeviceObserver { … };
ChromeUsbDelegate::ChromeUsbDelegate() = default;
ChromeUsbDelegate::~ChromeUsbDelegate() = default;
void ChromeUsbDelegate::AdjustProtectedInterfaceClasses(
content::BrowserContext* browser_context,
const url::Origin& origin,
content::RenderFrameHost* frame,
std::vector<uint8_t>& classes) { … }
std::unique_ptr<UsbChooser> ChromeUsbDelegate::RunChooser(
content::RenderFrameHost& frame,
blink::mojom::WebUsbRequestDeviceOptionsPtr options,
blink::mojom::WebUsbService::GetPermissionCallback callback) { … }
bool ChromeUsbDelegate::PageMayUseUsb(content::Page& page) { … }
bool ChromeUsbDelegate::CanRequestDevicePermission(
content::BrowserContext* browser_context,
const url::Origin& origin) { … }
void ChromeUsbDelegate::RevokeDevicePermissionWebInitiated(
content::BrowserContext* browser_context,
const url::Origin& origin,
const device::mojom::UsbDeviceInfo& device) { … }
const device::mojom::UsbDeviceInfo* ChromeUsbDelegate::GetDeviceInfo(
content::BrowserContext* browser_context,
const std::string& guid) { … }
bool ChromeUsbDelegate::HasDevicePermission(
content::BrowserContext* browser_context,
content::RenderFrameHost* frame,
const url::Origin& origin,
const device::mojom::UsbDeviceInfo& device_info) { … }
void ChromeUsbDelegate::GetDevices(
content::BrowserContext* browser_context,
blink::mojom::WebUsbService::GetDevicesCallback callback) { … }
void ChromeUsbDelegate::GetDevice(
content::BrowserContext* browser_context,
const std::string& guid,
base::span<const uint8_t> blocked_interface_classes,
mojo::PendingReceiver<device::mojom::UsbDevice> device_receiver,
mojo::PendingRemote<device::mojom::UsbDeviceClient> device_client) { … }
void ChromeUsbDelegate::AddObserver(content::BrowserContext* browser_context,
Observer* observer) { … }
void ChromeUsbDelegate::RemoveObserver(content::BrowserContext* browser_context,
Observer* observer) { … }
ChromeUsbDelegate::ContextObservation* ChromeUsbDelegate::GetContextObserver(
content::BrowserContext* browser_context) { … }
bool ChromeUsbDelegate::IsServiceWorkerAllowedForOrigin(
const url::Origin& origin) { … }
void ChromeUsbDelegate::IncrementConnectionCount(
content::BrowserContext* browser_context,
const url::Origin& origin) { … }
void ChromeUsbDelegate::DecrementConnectionCount(
content::BrowserContext* browser_context,
const url::Origin& origin) { … }