cups_printer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PRINTING_BACKEND_CUPS_PRINTER_H_
  5. #define PRINTING_BACKEND_CUPS_PRINTER_H_
  6. #include <cups/cups.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "printing/backend/cups_deleters.h"
  12. #include "url/gurl.h"
  13. namespace printing {
  14. struct PrinterBasicInfo;
  15. // Provides information regarding cups options.
  16. class COMPONENT_EXPORT(PRINT_BACKEND) CupsOptionProvider {
  17. public:
  18. virtual ~CupsOptionProvider() = default;
  19. // Returns the supported ipp attributes for the given `option_name`.
  20. // ipp_attribute_t* is owned by CupsOptionProvider.
  21. virtual ipp_attribute_t* GetSupportedOptionValues(
  22. const char* option_name) const = 0;
  23. // Returns supported attribute values for `option_name` where the value can be
  24. // converted to a string.
  25. virtual std::vector<base::StringPiece> GetSupportedOptionValueStrings(
  26. const char* option_name) const = 0;
  27. // Returns the default ipp attributes for the given `option_name`.
  28. // ipp_attribute_t* is owned by CupsOptionProvider.
  29. virtual ipp_attribute_t* GetDefaultOptionValue(
  30. const char* option_name) const = 0;
  31. // Returns true if the `value` is supported by option `name`.
  32. virtual bool CheckOptionSupported(const char* name,
  33. const char* value) const = 0;
  34. };
  35. // Represents a CUPS printer.
  36. // Retrieves information from CUPS printer objects as requested. This class
  37. // is only valid as long as the CupsConnection which created it exists as they
  38. // share an http connection which the CupsConnection closes on destruction.
  39. class COMPONENT_EXPORT(PRINT_BACKEND) CupsPrinter : public CupsOptionProvider {
  40. public:
  41. // Represents the margins that CUPS reports for some given media.
  42. // Its members are valued in PWG units (100ths of mm).
  43. // This struct approximates a cups_size_t, which is BLRT.
  44. struct CupsMediaMargins {
  45. int bottom;
  46. int left;
  47. int right;
  48. int top;
  49. };
  50. ~CupsPrinter() override = default;
  51. // Create a printer with a connection defined by `http` and `dest`.
  52. static std::unique_ptr<CupsPrinter> Create(http_t* http,
  53. ScopedDestination dest);
  54. // Returns true if this is the default printer
  55. virtual bool is_default() const = 0;
  56. // Returns the name of the printer as configured in CUPS
  57. virtual std::string GetName() const = 0;
  58. virtual std::string GetMakeAndModel() const = 0;
  59. // Returns the "printer-info" option of the printer as configured in CUPS.
  60. virtual std::string GetInfo() const = 0;
  61. virtual std::string GetUri() const = 0;
  62. // Lazily initialize dest info as it can require a network call
  63. virtual bool EnsureDestInfo() const = 0;
  64. // Populates `basic_info` with the relevant information about the printer
  65. virtual bool ToPrinterInfo(PrinterBasicInfo* basic_info) const = 0;
  66. // Start a print job. Writes the id of the started job to `job_id`. `job_id`
  67. // is 0 if there is an error. `title` is not sent if empty. `username` is
  68. // not sent if empty. Check availability before using this operation. Usage
  69. // on an unavailable printer is undefined.
  70. virtual ipp_status_t CreateJob(int* job_id,
  71. const std::string& title,
  72. const std::string& username,
  73. const std::vector<cups_option_t>& options) = 0;
  74. // Add a document to a print job. `job_id` must be non-zero and refer to a
  75. // job started with CreateJob. `docname` will be displayed in print status
  76. // if not empty. `last_doc` should be true if this is the last document for
  77. // this print job. `username` is not sent if empty. `options` should be IPP
  78. // key value pairs for the Send-Document operation.
  79. virtual bool StartDocument(int job_id,
  80. const std::string& docname,
  81. bool last_doc,
  82. const std::string& username,
  83. const std::vector<cups_option_t>& options) = 0;
  84. // Add data to the current document started by StartDocument. Calling this
  85. // without a started document will fail.
  86. virtual bool StreamData(const std::vector<char>& buffer) = 0;
  87. // Finish the current document. Another document can be added or the job can
  88. // be closed to complete printing.
  89. virtual bool FinishDocument() = 0;
  90. // Close the job. If the job is not closed, the documents will not be
  91. // printed. `job_id` should match the id from CreateJob. `username` is not
  92. // sent if empty.
  93. virtual ipp_status_t CloseJob(int job_id, const std::string& username) = 0;
  94. // Cancel the print job `job_id`. Returns true if the operation succeeded.
  95. // Returns false if it failed for any reason.
  96. virtual bool CancelJob(int job_id) = 0;
  97. // Queries CUPS for the margins of the media named by `media_id`.
  98. //
  99. // A `media_id` is any vendor ID known to CUPS for a given printer.
  100. // Vendor IDs are exemplified by the keys of the big map in
  101. // print_media_l10n.cc.
  102. //
  103. // Returns all zeroes if the CUPS API call fails.
  104. virtual CupsMediaMargins GetMediaMarginsByName(
  105. const std::string& media_id) = 0;
  106. };
  107. } // namespace printing
  108. #endif // PRINTING_BACKEND_CUPS_PRINTER_H_