ppd_provider.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 CHROMEOS_PRINTING_PPD_PROVIDER_H_
  5. #define CHROMEOS_PRINTING_PPD_PROVIDER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/component_export.h"
  13. #include "base/strings/string_piece.h"
  14. #include "base/version.h"
  15. #include "chromeos/printing/printer_configuration.h"
  16. #include "chromeos/printing/usb_printer_id.h"
  17. namespace network {
  18. namespace mojom {
  19. class URLLoaderFactory;
  20. }
  21. }
  22. namespace chromeos {
  23. class PpdCache;
  24. class PrinterConfigCache;
  25. class PpdMetadataManager;
  26. // Everything we might know about a printer when looking for a
  27. // driver for it. All of the default values for fields in this struct
  28. // mean we *don't* have that piece of information.
  29. //
  30. // Fields are listed in search order preference -- we use earlier
  31. // fields first to attempt to find a match.
  32. struct COMPONENT_EXPORT(CHROMEOS_PRINTING) PrinterSearchData {
  33. PrinterSearchData();
  34. PrinterSearchData(const PrinterSearchData& other);
  35. ~PrinterSearchData();
  36. // Make-and-model string guesses.
  37. std::vector<std::string> make_and_model;
  38. // 16-bit usb identifiers.
  39. int usb_vendor_id = 0;
  40. int usb_product_id = 0;
  41. // Original make and model for USB printer. Note, it is used only in metrics
  42. // for USB printers (in printer_event_tracker.cc).
  43. std::string usb_manufacturer;
  44. std::string usb_model;
  45. // Method of printer discovery.
  46. enum PrinterDiscoveryType {
  47. kUnknown = 0,
  48. kManual = 1,
  49. kUsb = 2,
  50. kZeroconf = 3,
  51. kDiscoveryTypeMax
  52. };
  53. PrinterDiscoveryType discovery_type;
  54. // Set of MIME types supported by this printer.
  55. std::vector<std::string> supported_document_formats;
  56. // Representation of IEEE1284 standard printing device ID.
  57. // Contains a set of languages this printer understands.
  58. UsbPrinterId printer_id;
  59. };
  60. // PpdProvider is responsible for mapping printer descriptions to
  61. // CUPS-PostScript Printer Description (PPD) files. It provides PPDs that a
  62. // user previously identified for use, and falls back to querying quirksserver
  63. // based on manufacturer/model of the printer.
  64. //
  65. // All functions in this class must be called from a sequenced context.
  66. class COMPONENT_EXPORT(CHROMEOS_PRINTING) PpdProvider
  67. : public base::RefCounted<PpdProvider> {
  68. public:
  69. // Possible result codes of a Resolve*() call.
  70. enum CallbackResultCode {
  71. SUCCESS,
  72. // Looked for a PPD for this configuration, but couldn't find a match.
  73. // Never returned for QueryAvailable().
  74. NOT_FOUND,
  75. // Failed to contact an external server needed to finish resolution.
  76. SERVER_ERROR,
  77. // Other error that is not expected to be transient.
  78. INTERNAL_ERROR,
  79. // The provided PPD was too large to be processed.
  80. PPD_TOO_LARGE,
  81. };
  82. // Construction-time options. Everything in this structure should have
  83. // a sane default.
  84. struct Options {
  85. Options() {}
  86. // Any results from PpdCache older than this are treated as
  87. // non-authoritative -- PpdProvider will attempt to re-resolve from the
  88. // network anyways and only use the cache results if the network is
  89. // unavailable.
  90. base::TimeDelta cache_staleness_age = base::Days(14);
  91. // Root of the ppd serving hierarchy.
  92. std::string ppd_server_root = "https://www.gstatic.com/chromeos_printing";
  93. };
  94. // Defines the limitations on when we show a particular PPD
  95. // Not to be confused with the new Restrictions struct used in the
  96. // v3 PpdProvider, defined in ppd_metadata_parser.h
  97. struct LegacyRestrictions {
  98. // Minimum milestone for ChromeOS build
  99. base::Version min_milestone = base::Version("0.0");
  100. // Maximum milestone for ChomeOS build
  101. base::Version max_milestone = base::Version("0.0");
  102. };
  103. struct ResolvedPpdReference {
  104. // The name of the model of printer or printer line
  105. std::string name;
  106. // Correct PpdReferece to be used with this printer
  107. Printer::PpdReference ppd_ref;
  108. };
  109. // Result of a ResolvePpd() call.
  110. // If the result code is SUCCESS, then:
  111. // string holds the contents of a PPD (that may or may not be gzipped).
  112. // Otherwise, these fields will be empty.
  113. using ResolvePpdCallback =
  114. base::OnceCallback<void(CallbackResultCode, const std::string&)>;
  115. // Result of a ResolveManufacturers() call. If the result code is SUCCESS,
  116. // then the vector contains a sorted list of manufacturers for which we have
  117. // at least one printer driver.
  118. using ResolveManufacturersCallback =
  119. base::OnceCallback<void(CallbackResultCode,
  120. const std::vector<std::string>&)>;
  121. // A list of printer names paired with the PpdReference that should be used
  122. // for that printer.
  123. using ResolvedPrintersList = std::vector<ResolvedPpdReference>;
  124. // Result of a ResolvePrinters() call. If the result code is SUCCESS, then
  125. // the vector contains a sorted list <model_name, PpdReference> tuples of all
  126. // printer models from the given manufacturer for which we have a driver,
  127. // sorted by model_name.
  128. using ResolvePrintersCallback =
  129. base::OnceCallback<void(CallbackResultCode, const ResolvedPrintersList&)>;
  130. // Result of a ResolvePpdReference call. If the result code is SUCCESS, then
  131. // the second argument contains the a PpdReference that we have high
  132. // confidence can be used to obtain a driver for the printer. NOT_FOUND means
  133. // we couldn't confidently figure out a driver for the printer. If we got
  134. // NOT_FOUND from a USB printer, we may have been able to determine the
  135. // manufacturer name which is the third argument.
  136. using ResolvePpdReferenceCallback =
  137. base::OnceCallback<void(CallbackResultCode,
  138. const Printer::PpdReference& ref,
  139. const std::string& manufacturer)>;
  140. // Result of a ResolvePpdLicense call. If |result| is SUCCESS, then
  141. // |license_name| will be used to indicate the license associated with the
  142. // requested PPD. If |license_name| is empty, then the requested PPD does not
  143. // require a license.
  144. using ResolvePpdLicenseCallback =
  145. base::OnceCallback<void(CallbackResultCode result,
  146. const std::string& license_name)>;
  147. // Result of a ReverseLookup call. If the result code is SUCCESS, then
  148. // |manufactuer| and |model| contain the strings that could have generated
  149. // the reference being looked up.
  150. using ReverseLookupCallback =
  151. base::OnceCallback<void(CallbackResultCode,
  152. const std::string& manufacturer,
  153. const std::string& model)>;
  154. // Called to get the current URLLoaderFactory on demand. Needs to be
  155. // Repeating since it gets called once per fetch.
  156. using LoaderFactoryGetter =
  157. base::RepeatingCallback<network::mojom::URLLoaderFactory*()>;
  158. // Create and return a new PpdProvider with the given cache and options.
  159. // A references to |url_context_getter| is taken.
  160. static scoped_refptr<PpdProvider> Create(
  161. const base::Version& current_version,
  162. scoped_refptr<PpdCache> cache,
  163. std::unique_ptr<PpdMetadataManager> metadata_manager,
  164. std::unique_ptr<PrinterConfigCache> config_cache);
  165. // Get all manufacturers for which we have drivers. Keys of the map will be
  166. // localized in the default browser locale or the closest available fallback.
  167. //
  168. // |cb| will be called on the invoking thread, and will be sequenced.
  169. //
  170. // PpdProvider will enqueue calls to this method and answer them in
  171. // the order received; it will opt to invoke |cb| with failure if the
  172. // queue grows overlong, failing the oldest calls first. The exact
  173. // queue length at which this occurs is unspecified.
  174. virtual void ResolveManufacturers(ResolveManufacturersCallback cb) = 0;
  175. // Get all models from a given manufacturer, localized in the
  176. // default browser locale or the closest available fallback.
  177. // |manufacturer| must be a value returned from a successful
  178. // ResolveManufacturers() call performed from this PpdProvider
  179. // instance.
  180. //
  181. // |cb| will be called on the invoking thread, and will be sequenced.
  182. virtual void ResolvePrinters(const std::string& manufacturer,
  183. ResolvePrintersCallback cb) = 0;
  184. // Attempt to find a PpdReference for the given printer. You should supply
  185. // as much information in search_data as you can.
  186. virtual void ResolvePpdReference(const PrinterSearchData& search_data,
  187. ResolvePpdReferenceCallback cb) = 0;
  188. // Given a PpdReference, attempt to get the PPD for printing.
  189. //
  190. // |cb| will be called on the invoking thread, and will be sequenced.
  191. virtual void ResolvePpd(const Printer::PpdReference& reference,
  192. ResolvePpdCallback cb) = 0;
  193. // Retrieves the name of the PPD license associated with the given printer
  194. // |effective_make_and_model|. If the name of the retrieved license is empty,
  195. // then the PPD does not require a license. If |effective_make_and_model| is
  196. // already present in the cache, then |cb| will fire immediately. Otherwise,
  197. // the PpdIndex will be fetched in order to retrieve the associated license.
  198. //
  199. // |cb| will be called on the invoking thread, and will be sequenced.
  200. virtual void ResolvePpdLicense(base::StringPiece effective_make_and_model,
  201. ResolvePpdLicenseCallback cb) = 0;
  202. // For a given PpdReference, retrieve the make and model strings used to
  203. // construct that reference.
  204. //
  205. // PpdProvider will enqueue calls to this method and answer them in
  206. // the order received; it will opt to invoke |cb| with failure if the
  207. // queue grows overlong, failing the oldest calls first. The exact
  208. // queue length at which this occurs is unspecified.
  209. virtual void ReverseLookup(const std::string& effective_make_and_model,
  210. ReverseLookupCallback cb) = 0;
  211. // Transform from ppd reference to ppd cache key. This is exposed for
  212. // testing, and should not be used by other code.
  213. static std::string PpdReferenceToCacheKey(
  214. const Printer::PpdReference& reference);
  215. // Used to "dereference" the PPD previously named by the cache key from
  216. // Printer::PpdReference::effective_make_and_model.
  217. static std::string PpdBasenameToCacheKey(base::StringPiece ppd_basename);
  218. protected:
  219. friend class base::RefCounted<PpdProvider>;
  220. virtual ~PpdProvider() {}
  221. };
  222. } // namespace chromeos
  223. #endif // CHROMEOS_PRINTING_PPD_PROVIDER_H_