#include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h"
#include <algorithm>
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/pdf/pdf_pref_names.h"
#include "chrome/browser/printing/pwg_raster_converter.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_utils.h"
#include "components/cloud_devices/common/cloud_device_description.h"
#include "components/cloud_devices/common/printer_description.h"
#include "components/device_event_log/device_event_log.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/api/device_permissions_manager.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
#include "extensions/browser/api/usb/usb_device_manager.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/api/printer_provider/usb_printer_manifest_data.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/permissions/usb_device_permission.h"
#include "extensions/common/permissions/usb_device_permission_data.h"
#include "printing/print_job_constants.h"
#include "printing/pwg_raster_settings.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "ui/gfx/geometry/size.h"
DevicePermissionsManager;
Extension;
ExtensionRegistry;
UsbDeviceManager;
UsbPrinterManifestData;
namespace printing {
namespace {
const char kContentTypePdf[] = …;
const char kContentTypePWGRaster[] = …;
const char kContentTypeAll[] = …;
const char kInvalidDataPrintError[] = …;
const char kInvalidTicketPrintError[] = …;
const char kProvisionalUsbLabel[] = …;
void UpdateJobFileInfo(std::unique_ptr<extensions::PrinterProviderPrintJob> job,
ExtensionPrinterHandler::PrintJobCallback callback,
base::ReadOnlySharedMemoryRegion pwg_region) { … }
bool HasUsbPrinterProviderPermissions(const Extension* extension) { … }
std::string GenerateProvisionalUsbPrinterId(
const Extension* extension,
const device::mojom::UsbDeviceInfo& device) { … }
struct ProvisionalUsbPrinter { … };
std::optional<ProvisionalUsbPrinter> ParseProvisionalUsbPrinterId(
const std::string& printer_id) { … }
extensions::PrinterProviderAPI* GetPrinterProviderAPI(Profile* profile) { … }
struct ExtensionPrinterSettings { … };
ExtensionPrinterSettings ParseExtensionPrinterSettings(
const base::Value::Dict& settings) { … }
}
ExtensionPrinterHandler::ExtensionPrinterHandler(Profile* profile)
: … { … }
ExtensionPrinterHandler::~ExtensionPrinterHandler() { … }
void ExtensionPrinterHandler::Reset() { … }
void ExtensionPrinterHandler::StartGetPrinters(
AddedPrintersCallback callback,
GetPrintersDoneCallback done_callback) { … }
void ExtensionPrinterHandler::StartGetCapability(
const std::string& destination_id,
GetCapabilityCallback callback) { … }
void ExtensionPrinterHandler::StartPrint(
const std::u16string& job_title,
base::Value::Dict settings,
scoped_refptr<base::RefCountedMemory> print_data,
PrintCallback callback) { … }
void ExtensionPrinterHandler::StartGrantPrinterAccess(
const std::string& printer_id,
GetPrinterInfoCallback callback) { … }
void ExtensionPrinterHandler::SetPwgRasterConverterForTesting(
std::unique_ptr<PwgRasterConverter> pwg_raster_converter) { … }
void ExtensionPrinterHandler::ConvertToPWGRaster(
scoped_refptr<base::RefCountedMemory> data,
const cloud_devices::CloudDeviceDescription& printer_description,
const cloud_devices::CloudDeviceDescription& print_ticket,
const gfx::Size& page_size,
std::unique_ptr<extensions::PrinterProviderPrintJob> job,
PrintJobCallback callback) { … }
void ExtensionPrinterHandler::DispatchPrintJob(
PrintCallback callback,
std::unique_ptr<extensions::PrinterProviderPrintJob> print_job) { … }
void ExtensionPrinterHandler::WrapGetPrintersCallback(
AddedPrintersCallback callback,
base::Value::List printers,
bool done) { … }
void ExtensionPrinterHandler::WrapGetCapabilityCallback(
GetCapabilityCallback callback,
base::Value::Dict capability) { … }
void ExtensionPrinterHandler::WrapPrintCallback(PrintCallback callback,
const base::Value& status) { … }
void ExtensionPrinterHandler::WrapGetPrinterInfoCallback(
GetPrinterInfoCallback callback,
base::Value::Dict printer_info) { … }
void ExtensionPrinterHandler::OnUsbDevicesEnumerated(
AddedPrintersCallback callback,
std::vector<device::mojom::UsbDeviceInfoPtr> devices) { … }
}