OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef PRINTING_BACKEND_CUPS_PRINTER_H_ |
| 6 #define PRINTING_BACKEND_CUPS_PRINTER_H_ |
| 7 |
| 8 #include <cups/cups.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "printing/backend/cups_deleters.h" |
| 15 #include "printing/printing_export.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace base { |
| 19 class FilePath; |
| 20 } |
| 21 |
| 22 namespace printing { |
| 23 |
| 24 struct PrinterBasicInfo; |
| 25 |
| 26 // Provides information regarding cups options. |
| 27 class PRINTING_EXPORT CupsOptionProvider { |
| 28 public: |
| 29 virtual ~CupsOptionProvider() = default; |
| 30 |
| 31 // Returns the supported ipp attributes for the given |option_name|. |
| 32 // ipp_attribute_t* is owned by CupsOptionProvider. |
| 33 virtual ipp_attribute_t* GetSupportedOptionValues( |
| 34 base::StringPiece option_name) const = 0; |
| 35 |
| 36 // Returns supported attribute values for |option_name| where the value can be |
| 37 // convered to a string. |
| 38 virtual std::vector<base::StringPiece> GetSupportedOptionValueStrings( |
| 39 base::StringPiece option_name) const = 0; |
| 40 |
| 41 // Returns the default ipp attributes for the given |option_name|. |
| 42 // ipp_attribute_t* is owned by CupsOptionProvider. |
| 43 virtual ipp_attribute_t* GetDefaultOptionValue( |
| 44 base::StringPiece option_name) const = 0; |
| 45 |
| 46 // Returns true if the |value| is supported by option |name|. |
| 47 virtual bool CheckOptionSupported(base::StringPiece name, |
| 48 base::StringPiece value) const = 0; |
| 49 }; |
| 50 |
| 51 // Represents a CUPS printer. |
| 52 // Retrieves information from CUPS printer objects as requested. This class |
| 53 // is only valid as long as the CupsConnection which created it exists as they |
| 54 // share an http connection which the CupsConnection closes on destruction. |
| 55 class PRINTING_EXPORT CupsPrinter : public CupsOptionProvider { |
| 56 public: |
| 57 // Create a printer with a connection defined by |http| and |dest|. |info| |
| 58 // can be null and will be lazily initialized when needed. |
| 59 CupsPrinter(http_t* http, |
| 60 std::unique_ptr<cups_dest_t, DestinationDeleter> dest, |
| 61 std::unique_ptr<cups_dinfo_t, DestInfoDeleter> info); |
| 62 |
| 63 CupsPrinter(CupsPrinter&& printer); |
| 64 |
| 65 ~CupsPrinter() override; |
| 66 |
| 67 // Returns true if this is the default printer |
| 68 bool is_default() const; |
| 69 |
| 70 // CupsOptionProvider |
| 71 ipp_attribute_t* GetSupportedOptionValues( |
| 72 base::StringPiece option_name) const override; |
| 73 std::vector<base::StringPiece> GetSupportedOptionValueStrings( |
| 74 base::StringPiece option_name) const override; |
| 75 ipp_attribute_t* GetDefaultOptionValue( |
| 76 base::StringPiece option_name) const override; |
| 77 bool CheckOptionSupported(base::StringPiece name, |
| 78 base::StringPiece value) const override; |
| 79 |
| 80 // Returns the file name for the PPD retrieved from the print server. |
| 81 base::FilePath GetPPD() const; |
| 82 |
| 83 // Returns the name of the printer as configured in CUPS |
| 84 std::string GetName() const; |
| 85 |
| 86 std::string GetMakeAndModel() const; |
| 87 |
| 88 // Returns true if the printer is currently reachable and working. |
| 89 bool IsAvailable() const; |
| 90 |
| 91 // Populates |basic_info| with the relevant information about the printer |
| 92 bool ToPrinterInfo(PrinterBasicInfo* basic_info) const; |
| 93 |
| 94 // Start a print job. Writes the id of the started job to |job_id|. |job_id| |
| 95 // is 0 if there is an error. Check availability before using this operation. |
| 96 // Usage on an unavailable printer is undefined. |
| 97 ipp_status_t CreateJob(int* job_id, |
| 98 base::StringPiece job_title, |
| 99 const std::vector<cups_option_t>& options); |
| 100 |
| 101 // Add a document to a print job. |job_id| must be non-zero and refer to a |
| 102 // job started with CreateJob. |document_name| will be displayed in print |
| 103 // status. |last_doc| should be true if this is the last document for this |
| 104 // print job. |options| should be IPP key value pairs for the Send-Document |
| 105 // operation. |
| 106 bool StartDocument(int job_id, |
| 107 base::StringPiece document_name, |
| 108 bool last_doc, |
| 109 const std::vector<cups_option_t>& options); |
| 110 |
| 111 // Add data to the current document started by StartDocument. Calling this |
| 112 // without a started document will fail. |
| 113 bool StreamData(const std::vector<char>& buffer); |
| 114 |
| 115 // Finish the current document. Another document can be added or the job can |
| 116 // be closed to complete printing. |
| 117 bool FinishDocument(); |
| 118 |
| 119 // Close the job. If the job is not closed, the documents will not be |
| 120 // printed. |job_id| should match the id from CreateJob. |
| 121 ipp_status_t CloseJob(int job_id); |
| 122 |
| 123 private: |
| 124 // Lazily initialize dest info as it can require a network call |
| 125 bool InitializeDestInfo() const; |
| 126 |
| 127 // http connection owned by the CupsConnection which created this object |
| 128 http_t* const cups_http_; |
| 129 |
| 130 // information to identify a printer |
| 131 std::unique_ptr<cups_dest_t, DestinationDeleter> destination_; |
| 132 |
| 133 // opaque object containing printer attributes and options |
| 134 mutable std::unique_ptr<cups_dinfo_t, DestInfoDeleter> dest_info_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(CupsPrinter); |
| 137 }; |
| 138 |
| 139 } // namespace printing |
| 140 |
| 141 #endif // PRINTING_BACKEND_CUPS_PRINTER_H_ |
OLD | NEW |