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