#ifndef PRINTING_BACKEND_PRINT_BACKEND_H_
#define PRINTING_BACKEND_PRINT_BACKEND_H_
#include <stdint.h>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "base/component_export.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "printing/mojom/print.mojom.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#if BUILDFLAG(IS_WIN)
#include "base/types/expected.h"
#endif
namespace printing {
using PrinterBasicInfoOptions = std::map<std::string, std::string>;
struct COMPONENT_EXPORT(PRINT_BACKEND) PrinterBasicInfo {
PrinterBasicInfo();
PrinterBasicInfo(const std::string& printer_name,
const std::string& display_name,
const std::string& printer_description,
const PrinterBasicInfoOptions& options);
PrinterBasicInfo(const PrinterBasicInfo& other);
~PrinterBasicInfo();
bool operator==(const PrinterBasicInfo& other) const;
std::string printer_name;
std::string display_name;
std::string printer_description;
PrinterBasicInfoOptions options;
};
using PrinterList = std::vector<PrinterBasicInfo>;
#if BUILDFLAG(IS_CHROMEOS)
struct COMPONENT_EXPORT(PRINT_BACKEND) AdvancedCapabilityValue {
AdvancedCapabilityValue();
AdvancedCapabilityValue(const std::string& name,
const std::string& display_name);
AdvancedCapabilityValue(const AdvancedCapabilityValue& other);
~AdvancedCapabilityValue();
bool operator==(const AdvancedCapabilityValue& other) const;
std::string name;
std::string display_name;
};
struct COMPONENT_EXPORT(PRINT_BACKEND) AdvancedCapability {
enum class Type : uint8_t { kBoolean, kFloat, kInteger, kString };
AdvancedCapability();
AdvancedCapability(const std::string& name, AdvancedCapability::Type type);
AdvancedCapability(const std::string& name,
const std::string& display_name,
AdvancedCapability::Type type,
const std::string& default_value,
const std::vector<AdvancedCapabilityValue>& values);
AdvancedCapability(const AdvancedCapability& other);
~AdvancedCapability();
bool operator==(const AdvancedCapability& other) const;
std::string name;
std::string display_name;
AdvancedCapability::Type type;
std::string default_value;
std::vector<AdvancedCapabilityValue> values;
};
using AdvancedCapabilities = std::vector<AdvancedCapability>;
struct COMPONENT_EXPORT(PRINT_BACKEND) PaperMargins {
PaperMargins();
PaperMargins(int32_t top_margin_um,
int32_t right_margin_um,
int32_t bottom_margin_um,
int32_t left_margin_um);
PaperMargins(const PaperMargins& other);
PaperMargins& operator=(const PaperMargins& other);
~PaperMargins();
bool operator==(const PaperMargins& other) const;
int32_t top_margin_um;
int32_t right_margin_um;
int32_t bottom_margin_um;
int32_t left_margin_um;
};
#endif
#if BUILDFLAG(IS_WIN)
struct COMPONENT_EXPORT(PRINT_BACKEND) PageOutputQualityAttribute {
PageOutputQualityAttribute();
PageOutputQualityAttribute(const std::string& display_name,
const std::string& name);
~PageOutputQualityAttribute();
bool operator==(const PageOutputQualityAttribute& other) const;
bool operator<(const PageOutputQualityAttribute& other) const;
std::string display_name;
std::string name;
};
using PageOutputQualityAttributes = std::vector<PageOutputQualityAttribute>;
struct COMPONENT_EXPORT(PRINT_BACKEND) PageOutputQuality {
PageOutputQuality();
PageOutputQuality(PageOutputQualityAttributes qualities,
std::optional<std::string> default_quality);
PageOutputQuality(const PageOutputQuality& other);
~PageOutputQuality();
PageOutputQualityAttributes qualities;
std::optional<std::string> default_quality;
};
#if defined(UNIT_TEST)
COMPONENT_EXPORT(PRINT_BACKEND)
bool operator==(const PageOutputQuality& quality1,
const PageOutputQuality& quality2);
#endif
struct COMPONENT_EXPORT(PRINT_BACKEND) XpsCapabilities {
XpsCapabilities();
XpsCapabilities(const XpsCapabilities&) = delete;
XpsCapabilities& operator=(const XpsCapabilities&) = delete;
XpsCapabilities(XpsCapabilities&& other) noexcept;
XpsCapabilities& operator=(XpsCapabilities&& other) noexcept;
~XpsCapabilities();
std::optional<PageOutputQuality> page_output_quality;
};
#endif
struct COMPONENT_EXPORT(PRINT_BACKEND) PrinterSemanticCapsAndDefaults {
PrinterSemanticCapsAndDefaults();
PrinterSemanticCapsAndDefaults(const PrinterSemanticCapsAndDefaults& other);
~PrinterSemanticCapsAndDefaults();
bool collate_capable = false;
bool collate_default = false;
int32_t copies_max = 1;
std::vector<mojom::DuplexMode> duplex_modes;
mojom::DuplexMode duplex_default = mojom::DuplexMode::kUnknownDuplexMode;
bool color_changeable = false;
bool color_default = false;
mojom::ColorModel color_model = mojom::ColorModel::kUnknownColorModel;
mojom::ColorModel bw_model = mojom::ColorModel::kUnknownColorModel;
class COMPONENT_EXPORT(PRINT_BACKEND) Paper {
public:
Paper();
Paper(const std::string& display_name,
const std::string& vendor_id,
const gfx::Size& size_um);
Paper(const std::string& display_name,
const std::string& vendor_id,
const gfx::Size& size_um,
const gfx::Rect& printable_area_um);
Paper(const std::string& display_name,
const std::string& vendor_id,
const gfx::Size& size_um,
const gfx::Rect& printable_area_um,
int max_height_um);
Paper(const std::string& display_name,
const std::string& vendor_id,
const gfx::Size& size_um,
const gfx::Rect& printable_area_um,
int max_height_um,
bool has_borderless_variant
#if BUILDFLAG(IS_CHROMEOS)
,
std::optional<PaperMargins> supported_margins_um = std::nullopt
#endif
);
~Paper();
Paper(const Paper& other);
Paper& operator=(const Paper& other);
bool operator==(const Paper& other) const;
const std::string& display_name() const { return display_name_; }
const std::string& vendor_id() const { return vendor_id_; }
const gfx::Size& size_um() const { return size_um_; }
const gfx::Rect& printable_area_um() const { return printable_area_um_; }
int max_height_um() const { return max_height_um_; }
bool has_borderless_variant() const { return has_borderless_variant_; }
void set_display_name(const std::string& display_name) {
display_name_ = display_name;
}
void set_vendor_id(const std::string& vendor_id) { vendor_id_ = vendor_id; }
void set_has_borderless_variant(bool has_borderless_variant) {
has_borderless_variant_ = has_borderless_variant;
}
void set_printable_area_to_paper_size() {
printable_area_um_ = gfx::Rect(size_um_);
}
bool SupportsCustomSize() const;
bool IsSizeWithinBounds(const gfx::Size& other_um) const;
#if BUILDFLAG(IS_CHROMEOS)
const std::optional<PaperMargins>& supported_margins_um() const {
return supported_margins_um_;
}
#endif
private:
std::string display_name_;
std::string vendor_id_;
gfx::Size size_um_;
gfx::Rect printable_area_um_;
int max_height_um_ = 0;
bool has_borderless_variant_ = false;
#if BUILDFLAG(IS_CHROMEOS)
std::optional<PaperMargins> supported_margins_um_;
#endif
};
using Papers = std::vector<Paper>;
Papers papers;
Papers user_defined_papers;
Paper default_paper;
struct COMPONENT_EXPORT(PRINT_BACKEND) MediaType {
std::string display_name;
std::string vendor_id;
bool operator==(const MediaType& other) const;
};
using MediaTypes = std::vector<MediaType>;
MediaTypes media_types;
MediaType default_media_type;
std::vector<gfx::Size> dpis;
gfx::Size default_dpi;
#if BUILDFLAG(IS_CHROMEOS)
bool pin_supported = false;
AdvancedCapabilities advanced_capabilities;
std::vector<mojom::PrintScalingType> print_scaling_types;
mojom::PrintScalingType print_scaling_type_default =
mojom::PrintScalingType::kUnknownPrintScalingType;
#endif
#if BUILDFLAG(IS_WIN)
std::optional<PageOutputQuality> page_output_quality;
#endif
};
#if defined(UNIT_TEST)
COMPONENT_EXPORT(PRINT_BACKEND)
bool operator==(const PrinterSemanticCapsAndDefaults& caps1,
const PrinterSemanticCapsAndDefaults& caps2);
#endif
struct COMPONENT_EXPORT(PRINT_BACKEND) PrinterCapsAndDefaults {
PrinterCapsAndDefaults();
PrinterCapsAndDefaults(const PrinterCapsAndDefaults& other);
~PrinterCapsAndDefaults();
std::string printer_capabilities;
std::string caps_mime_type;
std::string printer_defaults;
std::string defaults_mime_type;
};
class COMPONENT_EXPORT(PRINT_BACKEND) PrintBackend
: public base::RefCountedThreadSafe<PrintBackend> {
public:
virtual mojom::ResultCode EnumeratePrinters(PrinterList& printer_list) = 0;
virtual mojom::ResultCode GetDefaultPrinterName(
std::string& default_printer) = 0;
virtual mojom::ResultCode GetPrinterBasicInfo(
const std::string& printer_name,
PrinterBasicInfo* printer_info) = 0;
virtual mojom::ResultCode GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_info) = 0;
#if BUILDFLAG(IS_WIN)
virtual mojom::ResultCode GetPrinterCapsAndDefaults(
const std::string& printer_name,
PrinterCapsAndDefaults* printer_info) = 0;
virtual std::optional<gfx::Rect> GetPaperPrintableArea(
const std::string& printer_name,
const std::string& paper_vendor_id,
const gfx::Size& paper_size_um) = 0;
#endif
virtual std::vector<std::string> GetPrinterDriverInfo(
const std::string& printer_name) = 0;
virtual bool IsValidPrinter(const std::string& printer_name) = 0;
#if BUILDFLAG(IS_WIN)
virtual base::expected<std::string, mojom::ResultCode>
GetXmlPrinterCapabilitiesForXpsDriver(const std::string& printer_name);
#endif
static scoped_refptr<PrintBackend> CreateInstance(const std::string& locale);
static void SetPrintBackendForTesting(PrintBackend* print_backend);
protected:
friend class base::RefCountedThreadSafe<PrintBackend>;
PrintBackend();
virtual ~PrintBackend();
static scoped_refptr<PrintBackend> CreateInstanceImpl(
const std::string& locale);
};
}
#endif