#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/serial/serial_chooser_context.h"
#include <string_view>
#include <utility>
#include "base/base64.h"
#include "base/containers/contains.h"
#include "base/metrics/histogram_macros.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/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/serial/serial_blocklist.h"
#include "chrome/browser/serial/serial_chooser_histograms.h"
#include "chrome/browser/serial/serial_policy_allowed_ports.h"
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/common/content_settings.h"
#include "content/public/browser/device_service.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/cpp/usb/usb_ids.h"
#include "ui/base/l10n/l10n_util.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "components/user_manager/user.h"
#endif
namespace {
constexpr char kPortNameKey[] = …;
constexpr char kTokenKey[] = …;
constexpr char kBluetoothDevicePathKey[] = …;
#if BUILDFLAG(IS_WIN)
constexpr char kDeviceInstanceIdKey[] = "device_instance_id";
#else
constexpr char kVendorIdKey[] = …;
constexpr char kProductIdKey[] = …;
constexpr char kSerialNumberKey[] = …;
#if BUILDFLAG(IS_MAC)
constexpr char kUsbDriverKey[] = "usb_driver";
#endif
#endif
SettingSource;
std::string EncodeToken(const base::UnguessableToken& token) { … }
base::UnguessableToken DecodeToken(std::string_view input) { … }
bool IsPolicyGrantedObject(const base::Value::Dict& object) { … }
base::Value VendorAndProductIdsToValue(uint16_t vendor_id,
uint16_t product_id) { … }
base::Value VendorIdToValue(uint16_t vendor_id) { … }
void RecordPermissionRevocation(SerialPermissionRevoked type) { … }
}
SerialChooserContext::SerialChooserContext(Profile* profile)
: … { … }
SerialChooserContext::~SerialChooserContext() = default;
base::Value::Dict SerialChooserContext::PortInfoToValue(
const device::mojom::SerialPortInfo& port) { … }
std::string SerialChooserContext::GetKeyForObject(
const base::Value::Dict& object) { … }
bool SerialChooserContext::IsValidObject(const base::Value::Dict& object) { … }
std::u16string SerialChooserContext::GetObjectDisplayName(
const base::Value::Dict& object) { … }
void SerialChooserContext::OnPermissionRevoked(const url::Origin& origin) { … }
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
SerialChooserContext::GetGrantedObjects(const url::Origin& origin) { … }
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
SerialChooserContext::GetAllGrantedObjects() { … }
void SerialChooserContext::RevokeObjectPermission(
const url::Origin& origin,
const base::Value::Dict& object) { … }
void SerialChooserContext::RevokePortPermissionWebInitiated(
const url::Origin& origin,
const base::UnguessableToken& token) { … }
void SerialChooserContext::RevokeObjectPermissionInternal(
const url::Origin& origin,
const base::Value::Dict& object,
bool revoked_by_website = false) { … }
void SerialChooserContext::GrantPortPermission(
const url::Origin& origin,
const device::mojom::SerialPortInfo& port) { … }
bool SerialChooserContext::HasPortPermission(
const url::Origin& origin,
const device::mojom::SerialPortInfo& port) { … }
bool SerialChooserContext::CanStorePersistentEntry(
const device::mojom::SerialPortInfo& port) { … }
const device::mojom::SerialPortInfo* SerialChooserContext::GetPortInfo(
const base::UnguessableToken& token) { … }
device::mojom::SerialPortManager* SerialChooserContext::GetPortManager() { … }
void SerialChooserContext::AddPortObserver(PortObserver* observer) { … }
void SerialChooserContext::RemovePortObserver(PortObserver* observer) { … }
void SerialChooserContext::SetPortManagerForTesting(
mojo::PendingRemote<device::mojom::SerialPortManager> manager) { … }
void SerialChooserContext::FlushPortManagerConnectionForTesting() { … }
base::WeakPtr<SerialChooserContext> SerialChooserContext::AsWeakPtr() { … }
void SerialChooserContext::OnPortAdded(device::mojom::SerialPortInfoPtr port) { … }
void SerialChooserContext::OnPortRemoved(
device::mojom::SerialPortInfoPtr port) { … }
void SerialChooserContext::OnPortConnectedStateChanged(
device::mojom::SerialPortInfoPtr port) { … }
void SerialChooserContext::Shutdown() { … }
void SerialChooserContext::EnsurePortManagerConnection() { … }
void SerialChooserContext::SetUpPortManagerConnection(
mojo::PendingRemote<device::mojom::SerialPortManager> manager) { … }
void SerialChooserContext::OnGetDevices(
std::vector<device::mojom::SerialPortInfoPtr> ports) { … }
void SerialChooserContext::OnPortManagerConnectionError() { … }
bool SerialChooserContext::CanApplyPortSpecificPolicy() { … }