ppd_metadata_manager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2020 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_METADATA_MANAGER_H_
  5. #define CHROMEOS_PRINTING_PPD_METADATA_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "base/containers/flat_map.h"
  10. #include "base/strings/string_piece.h"
  11. #include "base/time/time.h"
  12. #include "chromeos/printing/ppd_metadata_parser.h"
  13. #include "chromeos/printing/ppd_provider.h"
  14. #include "chromeos/printing/printer_config_cache.h"
  15. namespace chromeos {
  16. // A PpdMetadataManager is the class responsible for fetching and
  17. // parsing PPD metadata to answer high-level queries about metadata.
  18. //
  19. // This class must be called from a sequenced context.
  20. class COMPONENT_EXPORT(CHROMEOS_PRINTING) PpdMetadataManager {
  21. public:
  22. // Used by GetLocale().
  23. // Argument denotes success of setting metadata locale for |this|.
  24. using GetLocaleCallback = base::OnceCallback<void(bool)>;
  25. // Used by GetPrinters().
  26. // Arguments denote
  27. // * whether the call succeeded and
  28. // * upon success, ParsedPrinters as requested.
  29. using GetPrintersCallback =
  30. base::OnceCallback<void(bool, const ParsedPrinters&)>;
  31. // Used by FindAllEmmsAvailableInIndex().
  32. // Contains a map
  33. // * whose keys are effective-make-and-model strings (provided by the
  34. // caller) that are available in forward index metadata and
  35. // * whose values contain the corresponding information read from the
  36. // forward index metadata.
  37. using FindAllEmmsAvailableInIndexCallback = base::OnceCallback<void(
  38. const base::flat_map<std::string, ParsedIndexValues>&)>;
  39. // Used by FindDeviceInUsbIndex().
  40. // * Contains the effective-make-and-model string corresponding to
  41. // the vendor id / product id pair originally provided by caller.
  42. // * The argument is empty if no appropriate effective-make-and-model
  43. // string was found.
  44. using FindDeviceInUsbIndexCallback =
  45. base::OnceCallback<void(const std::string&)>;
  46. // Used by GetUsbManufacturerName().
  47. // * Contains the unlocalized manufacturer name corresponding to the
  48. // vendor id originally provided by caller.
  49. // * The argument is empty if the manufacturer name is not found.
  50. using GetUsbManufacturerNameCallback =
  51. base::OnceCallback<void(const std::string&)>;
  52. // Assumes ownership of |config_cache|.
  53. static std::unique_ptr<PpdMetadataManager> Create(
  54. base::StringPiece browser_locale,
  55. base::Clock* clock,
  56. std::unique_ptr<PrinterConfigCache> config_cache);
  57. virtual ~PpdMetadataManager() = default;
  58. // Primes |this| with the best-fit locale advertised by the Chrome OS
  59. // Printing serving root. The best-fit locale is the one closest to
  60. // the |browser_locale| passed to Create(). "Closest" is an
  61. // implementation-defined concept.
  62. //
  63. // If a best-fit locale is already set in |this|, |this| invokes |cb|
  64. // immediately and indicates success.
  65. //
  66. // With few exceptions, caller must not call any other method of
  67. // |this| until the |cb| indicates success. Exceptional methods are
  68. // documented in this header file.
  69. //
  70. // See also: SetLocaleForTesting()
  71. virtual void GetLocale(GetLocaleCallback cb) = 0;
  72. // Calls |cb| with a list of manufacturers.
  73. // * On success, the list is created from metadata no older than
  74. // |age|.
  75. // * On failure, the first argument to |cb| is set accordingly.
  76. virtual void GetManufacturers(
  77. base::TimeDelta age,
  78. PpdProvider::ResolveManufacturersCallback cb) = 0;
  79. // Calls |cb| with a map of printers made by |manufacturer|.
  80. // * Caller must have previously successfully called
  81. // GetManufacturers().
  82. // * On success, the map is created from metadata no older than
  83. // |age|.
  84. // * On failure, the first argument to |cb| is set accordingly.
  85. virtual void GetPrinters(base::StringPiece manufacturer,
  86. base::TimeDelta age,
  87. GetPrintersCallback cb) = 0;
  88. // Calls |cb| with the subset of strings from |emms| that are
  89. // available in forward index metadata mapped to the corresponding
  90. // values read from forward index metadata.
  91. // * Does not rely on prior call to GetLocale().
  92. // * During operation, operates with metadata no older than |age|.
  93. // * On failure, calls |cb| with an empty map.
  94. virtual void FindAllEmmsAvailableInIndex(
  95. const std::vector<std::string>& emms,
  96. base::TimeDelta age,
  97. FindAllEmmsAvailableInIndexCallback cb) = 0;
  98. // Searches USB index metadata for a printer with the given
  99. // |vendor_id| and |product_id|, calling |cb| with the appropriate
  100. // effective-make-and-model string if one is found.
  101. // * Does not rely on prior call to GetLocale().
  102. // * During operation, operates with metadata no older than |age|.
  103. // * On failure, calls |cb| with an empty string.
  104. virtual void FindDeviceInUsbIndex(int vendor_id,
  105. int product_id,
  106. base::TimeDelta age,
  107. FindDeviceInUsbIndexCallback cb) = 0;
  108. // Searches the USB vendor ID map for a manufacturer with the given
  109. // |vendor_id|, calling |cb| with the name found (if any).
  110. // * Does not rely on prior call to GetLocale().
  111. // * During operation, operates with metadata no older than |age|.
  112. // * On failure, calls |cb| with an empty string.
  113. virtual void GetUsbManufacturerName(int vendor_id,
  114. base::TimeDelta age,
  115. GetUsbManufacturerNameCallback cb) = 0;
  116. // Calls |cb| with the make and model of
  117. // |effective_make_and_model|.
  118. // * On success, the split is performed against metadata no older than
  119. // |age|.
  120. // * On failure, the first argument to |cb| is set accordingly.
  121. //
  122. // The split is defined by the reverse index metadata that this method
  123. // fetches, appropriate to the |effective_make_and_model|.
  124. // Googlers: you may consult
  125. // go/cros-printing:ppd-metadata#reverse-index
  126. virtual void SplitMakeAndModel(base::StringPiece effective_make_and_model,
  127. base::TimeDelta age,
  128. PpdProvider::ReverseLookupCallback cb) = 0;
  129. // Returns a borrowed pointer to the PrinterConfigCache passed to
  130. // Create(); |this| retains ownership.
  131. virtual PrinterConfigCache* GetPrinterConfigCacheForTesting() const = 0;
  132. // Fakes a successful call to GetLocale(), setting the internal
  133. // locale of |this| to |locale|.
  134. //
  135. // This method is useful for bypassing a real call to GetLocale(),
  136. // which consumers of this class ordinarily must complete successfully
  137. // before calling any other method of |this|.
  138. virtual void SetLocaleForTesting(base::StringPiece locale) = 0;
  139. // Fakes a successful call to GetManufacturers(), providing |this|
  140. // with a list of manufacturers.
  141. //
  142. // This method returns true if |this| successfully parses and stores
  143. // off the list of |manufacturers_json|. Caller must verify that this
  144. // method returns true.
  145. virtual bool SetManufacturersForTesting(
  146. base::StringPiece manufacturers_json) = 0;
  147. // Returns the metadata locale currently set in |this|.
  148. virtual base::StringPiece ExposeMetadataLocaleForTesting() const = 0;
  149. };
  150. } // namespace chromeos
  151. #endif // CHROMEOS_PRINTING_PPD_METADATA_MANAGER_H_