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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_PRINTING_PRINTER_CONFIGURATION_H_
#define CHROMEOS_PRINTING_PRINTER_CONFIGURATION_H_
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/component_export.h"
#include "chromeos/printing/cups_printer_status.h"
#include "chromeos/printing/uri.h"
#include "net/base/host_port_pair.h"
namespace net {
class IPEndPoint;
} // namespace net
namespace chromeos {
// Classes of printers tracked. See doc/cups_printer_management.md for
// details on what these mean.
enum class COMPONENT_EXPORT(CHROMEOS_PRINTING) PrinterClass {
kEnterprise,
kAutomatic,
kDiscovered,
kSaved
};
struct COMPONENT_EXPORT(CHROMEOS_PRINTING) IppPrinterInfo {
IppPrinterInfo();
IppPrinterInfo(const std::vector<std::string>& document_formats,
const std::string& document_format_default,
const std::string& document_format_preferred,
const std::vector<std::string>& urf_supported,
const std::vector<std::string>& pdf_versions,
const std::vector<std::string>& ipp_features,
const std::string& mopria_certified,
const std::vector<std::string>& printer_kind);
IppPrinterInfo(const IppPrinterInfo& other);
~IppPrinterInfo();
// document-format-supported
// MIME types for supported formats.
std::vector<std::string> document_formats;
// document-format-default
// MIME type for default format.
std::string document_format_default;
// document-format-preferred
// MIME type for preferred format.
std::string document_format_preferred;
// urf-supported
// A collection of supported URF printing modes.
std::vector<std::string> urf_supported;
// pdf-version-supported
// A collection of supported PDF versions.
std::vector<std::string> pdf_versions;
// ipp-features-supported
// A collection of supported IPP features.
std::vector<std::string> ipp_features;
// mopria-certified
// Mopria certification level of the printer.
std::string mopria_certified;
// printer-kind
// A collection of supported printing categories.
std::vector<std::string> printer_kind;
};
COMPONENT_EXPORT(CHROMEOS_PRINTING)
std::string_view ToString(PrinterClass pclass);
// This function checks if the given URI is a valid printer URI. |uri| is
// considered to be a valid printer URI if it has one of the scheme listed in
// the table below and meets the criteria defined there.
//
// scheme | userinfo | host | port | path | query | fragment
// ---------+----------+----------+----------+----------+----------+----------
// http | NO | required | optional | optional | optional | NO
// https | NO | required | optional | optional | optional | NO
// ipp | NO | required | optional | optional | optional | NO
// ipps | NO | required | optional | optional | optional | NO
// lpd | optional | required | optional | optional | optional | NO
// socket | NO | required | optional | NO | optional | NO
// ippusb | NO | required | NO | required | optional | NO
// usb | NO | required | NO | required | optional | NO
//
// If the given |uri| does not meet the criteria the function returns false and
// set an error message in |error_message| (if it is not nullptr). The message
// has the prefix "Malformed printer URI: ".
bool COMPONENT_EXPORT(CHROMEOS_PRINTING)
IsValidPrinterUri(const Uri& uri, std::string* error_message = nullptr);
class COMPONENT_EXPORT(CHROMEOS_PRINTING) Printer {
public:
// Information needed to find the PPD file for this printer.
//
// If you add fields to this struct, you almost certainly will
// want to update PpdResolver and PpdCache::GetCachePath.
//
// Exactly one of the fields below should be filled in.
//
// At resolution time, we look for a cached PPD that used the same
// PpdReference before.
//
class PpdReference {
public:
// Returns true when this PPD reference is filled in; true whenever any of
// the members is non-empty.
bool IsFilled() const;
// If non-empty, this is the url of a specific PPD the user has specified
// for use with this printer. The ppd can be gzipped or uncompressed.
// Supported schemes for this url are file://, http:// and https://.
std::string user_supplied_ppd_url;
// String that identifies which ppd to use from the ppd server.
// Where possible, this is the same as the ipp/ldap
// printer-make-and-model field.
std::string effective_make_and_model;
// True if the printer should be auto-configured and a PPD is unnecessary.
bool autoconf = false;
};
template <typename T>
struct PrintOption {
std::optional<T> default_value;
std::vector<T> allowed_values;
};
struct Size {
bool operator==(const Size&) const = default;
int width;
int height;
};
// This enum can be modified (adding new values, changing the order of values,
// etc.).
enum DuplexType {
kUnknownDuplexType = 0,
kOneSided = 1,
kShortEdge = 2,
kLongEdge = 3
};
struct Dpi {
bool operator==(const Dpi&) const = default;
int horizontal;
int vertical;
};
// This enum can be modified (adding new values, changing the order of values,
// etc.).
enum QualityType {
kUnknownQualityType = 0,
kDraft = 1,
kNormal = 2,
kHigh = 3
};
struct ManagedPrintOptions {
ManagedPrintOptions();
ManagedPrintOptions(const ManagedPrintOptions& other);
ManagedPrintOptions& operator=(const ManagedPrintOptions& other);
~ManagedPrintOptions();
PrintOption<Size> media_size;
PrintOption<std::string> media_type;
PrintOption<DuplexType> duplex;
PrintOption<bool> color;
PrintOption<Dpi> dpi;
PrintOption<QualityType> quality;
PrintOption<bool> print_as_image;
};
// The location where the printer is stored.
enum Source {
SRC_USER_PREFS,
SRC_POLICY,
};
// An enumeration of printer protocols.
// These values are written to logs. New enum values can be added, but
// existing enums must never be renumbered or deleted and reused.
enum PrinterProtocol {
kUnknown = 0,
kUsb = 1,
kIpp = 2,
kIpps = 3,
kHttp = 4,
kHttps = 5,
kSocket = 6,
kLpd = 7,
kIppUsb = 8,
kProtocolMax
};
// Constructs a printer object that is completely empty.
Printer();
// Constructs a printer object with the given |id|.
explicit Printer(std::string id);
// Copy constructor and assignment.
Printer(const Printer& printer);
Printer& operator=(const Printer& printer);
~Printer();
const std::string& id() const { return id_; }
void set_id(const std::string& id) { id_ = id; }
const std::string& display_name() const { return display_name_; }
void set_display_name(const std::string& display_name) {
display_name_ = display_name;
}
const std::string& description() const { return description_; }
void set_description(const std::string& description) {
description_ = description;
}
const std::string& usb_printer_manufacturer() const {
return usb_printer_manufacturer_;
}
void set_usb_printer_manufacturer(const std::string& manufacturer) {
usb_printer_manufacturer_ = manufacturer;
}
const std::string& make_and_model() const { return make_and_model_; }
void set_make_and_model(const std::string& make_and_model) {
make_and_model_ = make_and_model;
}
ManagedPrintOptions print_job_options() const { return print_job_options_; }
void set_print_job_options(const ManagedPrintOptions& print_job_options) {
print_job_options_ = print_job_options;
}
const Uri& uri() const { return uri_; }
// These methods set |uri| as a new uri. If |uri| is incorrect or does not
// pass the IsValidPrinterUri(...) function defined above, no changes are made
// to the object and false is returned. If |error_message| is not nullptr,
// the error message is written there when the methods return false.
bool SetUri(const Uri& uri, std::string* error_message = nullptr);
bool SetUri(std::string_view uri, std::string* error_message = nullptr);
const PpdReference& ppd_reference() const { return ppd_reference_; }
PpdReference* mutable_ppd_reference() { return &ppd_reference_; }
bool supports_ippusb() const { return supports_ippusb_; }
void set_supports_ippusb(bool supports_ippusb) {
supports_ippusb_ = supports_ippusb;
}
const std::string& print_server_uri() const { return print_server_uri_; }
void set_print_server_uri(const std::string& print_server_uri) {
print_server_uri_ = print_server_uri;
}
const std::string& uuid() const { return uuid_; }
void set_uuid(const std::string& uuid) { uuid_ = uuid; }
const std::optional<IppPrinterInfo>& ipp_printer_info() const {
return ipp_printer_info_;
}
void set_ipp_printer_info(std::optional<IppPrinterInfo> ipp_printer_info) {
ipp_printer_info_ = std::move(ipp_printer_info);
}
// Returns true if the printer should be automatically configured using IPP
// Everywhere. Computed using information from |ppd_reference_| and |uri_|.
bool IsIppEverywhere() const;
// Returns true if the printer should use driverless autoconfiguration through
// IPP-USB instead of the USB printer class.
bool RequiresDriverlessUsb() const;
// Returns the hostname and port for |uri_|. Assumes that the uri is
// well formed. Returns an empty string if |uri_| is not set.
net::HostPortPair GetHostAndPort() const;
// Returns the |uri_| with the host and port replaced with |ip|. Returns an
// empty Uri if |uri_| is empty.
Uri ReplaceHostAndPort(const net::IPEndPoint& ip) const;
// Returns the printer protocol the printer is configured with.
Printer::PrinterProtocol GetProtocol() const;
// Returns true if the current protocol of the printer is one of the following
// "network protocols": [kIpp, kIpps, kHttp, kHttps, kSocket, kLpd]
bool HasNetworkProtocol() const;
// Returns true if the current protocol of the printer is either kUSb or
// kIppUsb.
bool IsUsbProtocol() const;
// Returns true if the current protocol is either local (kUsb or kIppUsb) or
// secure (kIpps or kHttps).
bool HasSecureProtocol() const;
// Returns true if the host component of the printer's URI ends with
// ".local"
//
// This method is meaningless to call without a URI set.
bool IsZeroconf() const;
// Returns true if the printer uri is set and false when the uri is empty.
bool HasUri() const { return !uri_.GetScheme().empty(); }
Source source() const { return source_; }
void set_source(const Source source) { source_ = source; }
const CupsPrinterStatus& printer_status() const { return printer_status_; }
void set_printer_status(const chromeos::CupsPrinterStatus& printer_status) {
printer_status_ = printer_status;
}
// Setter and getter for flag marking that the printer is used in the finch
// experiment created for b:184293121.
bool AffectedByIppUsbMigration() const {
return experimental_setup_of_usb_printer_with_ipp_and_ppd_;
}
void SetAffectedByIppUsbMigration(bool flag) {
experimental_setup_of_usb_printer_with_ipp_and_ppd_ = flag;
}
private:
// Globally unique identifier. Empty indicates a new printer.
std::string id_;
// User defined string for printer identification.
std::string display_name_;
// User defined string for additional printer information.
std::string description_;
// Holds manufacturer name read from USB printer.
std::string usb_printer_manufacturer_;
// The manufactuer and model of the printer in one string. e.g. HP OfficeJet
// 415. This is either read from or derived from printer information and is
// not necessarily suitable for display.
std::string make_and_model_;
// The full path for the printer. Suitable for configuration in CUPS.
// Contains protocol, hostname, port, and queue.
Uri uri_;
// Holds allowed/default values of print job options set by policy.
ManagedPrintOptions print_job_options_;
// How to find the associated postscript printer description.
PpdReference ppd_reference_;
// Represents whether or not the printer supports printing using ipp-over-usb.
bool supports_ippusb_ = false;
// When non-empty, the uri of the print server the printer was added from. We
// use this to determine if a printer is a print server printer.
std::string print_server_uri_;
// The UUID from an autoconf protocol for deduplication. Could be empty.
std::string uuid_;
// The datastore which holds this printer.
Source source_;
// The current status of the printer
chromeos::CupsPrinterStatus printer_status_;
// This flag is set for printers that take part in the finch experiment
// created for b/184293121.
bool experimental_setup_of_usb_printer_with_ipp_and_ppd_ = false;
// IPP attributes, if available.
std::optional<IppPrinterInfo> ipp_printer_info_;
};
} // namespace chromeos
#endif // CHROMEOS_PRINTING_PRINTER_CONFIGURATION_H_
|