ppd_metadata_parser.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // This file declares parsing functions for use with PPD metadata.
  5. // The PpdMetadataManager class is the primary consumer.
  6. //
  7. // Each Parse*() function promises these invariants:
  8. // 1. they attempt to parse as much JSON as possible (returning
  9. // all relevant data that can be reasonably extracted),
  10. // 2. they return absl::nullopt on irrecoverable parse error, and
  11. // 3. they never return a non-nullopt value that unwraps into an empty
  12. // container.
  13. //
  14. // Googlers: you may consult the primary documentation for PPD metadata
  15. // at go/cros-printing:ppd-metadata
  16. #ifndef CHROMEOS_PRINTING_PPD_METADATA_PARSER_H_
  17. #define CHROMEOS_PRINTING_PPD_METADATA_PARSER_H_
  18. #include <string>
  19. #include <vector>
  20. #include "base/component_export.h"
  21. #include "base/containers/flat_map.h"
  22. #include "base/strings/string_piece_forward.h"
  23. #include "base/version.h"
  24. #include "third_party/abseil-cpp/absl/types/optional.h"
  25. namespace chromeos {
  26. // Defines the limitations on when we show a particular PPD.
  27. struct Restrictions {
  28. Restrictions();
  29. ~Restrictions();
  30. Restrictions(const Restrictions&);
  31. Restrictions& operator=(const Restrictions&);
  32. absl::optional<base::Version> min_milestone;
  33. absl::optional<base::Version> max_milestone;
  34. };
  35. struct COMPONENT_EXPORT(CHROMEOS_PRINTING) ReverseIndexLeaf {
  36. std::string manufacturer;
  37. std::string model;
  38. };
  39. // A ParsedPrinter is a value parsed from printers metadata.
  40. struct COMPONENT_EXPORT(CHROMEOS_PRINTING) ParsedPrinter {
  41. ParsedPrinter();
  42. ~ParsedPrinter();
  43. ParsedPrinter(const ParsedPrinter&);
  44. ParsedPrinter& operator=(const ParsedPrinter&);
  45. std::string user_visible_printer_name;
  46. std::string effective_make_and_model;
  47. Restrictions restrictions;
  48. };
  49. // A single leaf value parsed from a forward index.
  50. struct COMPONENT_EXPORT(CHROMEOS_PRINTING) ParsedIndexLeaf {
  51. ParsedIndexLeaf();
  52. ~ParsedIndexLeaf();
  53. ParsedIndexLeaf(const ParsedIndexLeaf&);
  54. ParsedIndexLeaf& operator=(const ParsedIndexLeaf&);
  55. std::string ppd_basename;
  56. Restrictions restrictions;
  57. std::string license;
  58. };
  59. // A collection of values parsed from a forward index.
  60. // Corresponds to one effective-make-and-model string.
  61. struct COMPONENT_EXPORT(CHROMEOS_PRINTING) ParsedIndexValues {
  62. ParsedIndexValues();
  63. ~ParsedIndexValues();
  64. ParsedIndexValues(const ParsedIndexValues&);
  65. ParsedIndexValues& operator=(const ParsedIndexValues&);
  66. std::vector<ParsedIndexLeaf> values;
  67. };
  68. // Maps manufacturer names to basenames of printers metadata.
  69. using ParsedManufacturers = base::flat_map<std::string, std::string>;
  70. using ParsedPrinters = std::vector<ParsedPrinter>;
  71. // * Keys are effective-make-and-model strings.
  72. // * Values collect information corresponding to each
  73. // effective-make-and-model string - chiefly information about
  74. // individual PPDs.
  75. // * Googlers, see also: go/cros-printing:ppd-metadata#index
  76. using ParsedIndex = base::flat_map<std::string, ParsedIndexValues>;
  77. // Maps USB product IDs to effective-make-and-model strings.
  78. using ParsedUsbIndex = base::flat_map<int, std::string>;
  79. // Maps USB vendor IDs to manufacturer names.
  80. using ParsedUsbVendorIdMap = base::flat_map<int, std::string>;
  81. // Keyed on effective-make-and-model strings.
  82. using ParsedReverseIndex = base::flat_map<std::string, ReverseIndexLeaf>;
  83. // Parses |locales_json| and returns a list of locales.
  84. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  85. absl::optional<std::vector<std::string>> ParseLocales(
  86. base::StringPiece locales_json);
  87. // Parses |manufacturers_json| and returns the parsed map type.
  88. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  89. absl::optional<ParsedManufacturers> ParseManufacturers(
  90. base::StringPiece manufacturers_json);
  91. // Parses |printers_json| and returns the parsed map type.
  92. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  93. absl::optional<ParsedPrinters> ParsePrinters(base::StringPiece printers_json);
  94. // Parses |forward_index_json| and returns the parsed map type.
  95. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  96. absl::optional<ParsedIndex> ParseForwardIndex(
  97. base::StringPiece forward_index_json);
  98. // Parses |usb_index_json| and returns a map of USB product IDs to
  99. // effective-make-and-model strings.
  100. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  101. absl::optional<ParsedUsbIndex> ParseUsbIndex(base::StringPiece usb_index_json);
  102. // Parses |usb_vendor_id_map_json| and returns a map of USB vendor IDs
  103. // to manufacturer names.
  104. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  105. absl::optional<ParsedUsbVendorIdMap> ParseUsbVendorIdMap(
  106. base::StringPiece usb_vendor_id_map_json);
  107. // Parses |reverse_index_json| and returns the parsed map type.
  108. COMPONENT_EXPORT(CHROMEOS_PRINTING)
  109. absl::optional<ParsedReverseIndex> ParseReverseIndex(
  110. base::StringPiece reverse_index_json);
  111. } // namespace chromeos
  112. #endif // CHROMEOS_PRINTING_PPD_METADATA_PARSER_H_