Commit c4758a3c authored by Kalvin Lee's avatar Kalvin Lee Committed by Commit Bot

printing: implement PpdMetadataManager

This change implements the PpdMetadataManager class and provides unit
tests for the same.

Bug: chromium:888189
Change-Id: Ifb9672258a747722c3d48bd1a1f1095365555e49
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2261972
Commit-Queue: Kalvin Lee <kdlee@chromium.org>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784552}
parent c74b282c
...@@ -92,6 +92,8 @@ component("chromeos") { ...@@ -92,6 +92,8 @@ component("chromeos") {
if (is_printing_ppd_provider_v3) { if (is_printing_ppd_provider_v3) {
sources += [ sources += [
"printing/ppd_metadata_manager.cc",
"printing/ppd_metadata_manager.h",
"printing/ppd_metadata_parser.cc", "printing/ppd_metadata_parser.cc",
"printing/ppd_metadata_parser.h", "printing/ppd_metadata_parser.h",
"printing/ppd_provider_v3.cc", "printing/ppd_provider_v3.cc",
...@@ -219,6 +221,8 @@ test("chromeos_unittests") { ...@@ -219,6 +221,8 @@ test("chromeos_unittests") {
sources += [ sources += [
"printing/fake_printer_config_cache.cc", "printing/fake_printer_config_cache.cc",
"printing/fake_printer_config_cache.h", "printing/fake_printer_config_cache.h",
"printing/ppd_metadata_manager_unittest.cc",
"printing/ppd_metadata_matchers.h",
"printing/ppd_metadata_parser_unittest.cc", "printing/ppd_metadata_parser_unittest.cc",
"printing/printer_config_cache_unittest.cc", "printing/printer_config_cache_unittest.cc",
] ]
......
This diff is collapsed.
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_PRINTING_PPD_METADATA_MANAGER_H_
#define CHROMEOS_PRINTING_PPD_METADATA_MANAGER_H_
#include <memory>
#include <string>
#include "base/containers/flat_map.h"
#include "base/optional.h"
#include "base/strings/string_piece.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/printing/ppd_metadata_parser.h"
#include "chromeos/printing/ppd_provider.h"
#include "chromeos/printing/printer_config_cache.h"
namespace chromeos {
// A PpdMetadataManager is the class responsible for fetching and
// parsing PPD metadata to answer high-level queries about metadata.
//
// This class must be called from a sequenced context.
class CHROMEOS_EXPORT PpdMetadataManager {
public:
// Used by GetLocale().
// Argument denotes success of setting metadata locale for |this|.
using GetLocaleCallback = base::OnceCallback<void(bool)>;
// Used by GetPrinters().
// Arguments denote
// * whether the call succeeded and
// * upon success, ParsedPrinters as requested.
using GetPrintersCallback =
base::OnceCallback<void(bool, const ParsedPrinters&)>;
// Assumes ownership of |config_cache|.
static std::unique_ptr<PpdMetadataManager> Create(
base::StringPiece browser_locale,
base::Clock* clock,
std::unique_ptr<PrinterConfigCache> config_cache);
virtual ~PpdMetadataManager() = default;
// Primes |this| with the best-fit locale advertised by the Chrome OS
// Printing serving root. The best-fit locale is the one closest to
// the |browser_locale| passed to Create(). "Closest" is an
// implementation-defined concept.
//
// If a best-fit locale is already set in |this|, |this| invokes |cb|
// immediately and indicates success.
//
// With few exceptions, caller must not call any other method of
// |this| until the |cb| indicates success. Exceptional methods are
// documented in this header file.
//
// See also: SetLocaleForTesting()
virtual void GetLocale(GetLocaleCallback cb) = 0;
// Calls |cb| with a list of manufacturers.
// * On success, the list is created from metadata no older than
// |age|.
// * On failure, the first argument to |cb| is set accordingly.
virtual void GetManufacturers(
base::TimeDelta age,
PpdProvider::ResolveManufacturersCallback cb) = 0;
// Calls |cb| with a map of printers made by |manufacturer|.
// * Caller must have previously successfully called
// GetManufacturers().
// * On success, the map is created from metadata no older than
// |age|.
// * On failure, the first argument to |cb| is set accordingly.
virtual void GetPrinters(base::StringPiece manufacturer,
base::TimeDelta age,
GetPrintersCallback cb) = 0;
// Calls |cb| with the make and model of
// |effective_make_and_model|.
// * On success, the split is performed against metadata no older than
// |age|.
// * On failure, the first argument to |cb| is set accordingly.
//
// The split is defined by the reverse index metadata that this method
// fetches, appropriate to the |effective_make_and_model|.
// Googlers: you may consult
// go/cros-printing:ppd-metadata#reverse-index
virtual void SplitMakeAndModel(base::StringPiece effective_make_and_model,
base::TimeDelta age,
PpdProvider::ReverseLookupCallback cb) = 0;
// Returns a borrowed pointer to the PrinterConfigCache passed to
// Create(); |this| retains ownership.
virtual PrinterConfigCache* GetPrinterConfigCacheForTesting() const = 0;
// Fakes a successful call to GetLocale(), setting the internal
// locale of |this| to |locale|.
//
// This method is useful for bypassing a real call to GetLocale(),
// which consumers of this class ordinarily must complete successfully
// before calling any other method of |this|.
virtual void SetLocaleForTesting(base::StringPiece locale) = 0;
// Fakes a successful call to GetManufacturers(), providing |this|
// with a list of manufacturers.
//
// This method returns true if |this| successfully parses and stores
// off the list of |manufacturers_json|. Caller must verify that this
// method returns true.
virtual bool SetManufacturersForTesting(
base::StringPiece manufacturers_json) = 0;
// Returns the metadata locale currently set in |this|.
virtual base::StringPiece ExposeMetadataLocaleForTesting() const = 0;
};
} // namespace chromeos
#endif // CHROMEOS_PRINTING_PPD_METADATA_MANAGER_H_
This diff is collapsed.
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains matchers useful for testing with parsed PPD
// metadata.
#include <string>
#include "testing/gmock/include/gmock/gmock-matchers.h"
#ifndef CHROMEOS_PRINTING_PPD_METADATA_MATCHERS_H_
#define CHROMEOS_PRINTING_PPD_METADATA_MATCHERS_H_
namespace chromeos {
using ::testing::ExplainMatchResult;
using ::testing::Field;
using ::testing::StrEq;
// Matches a ReverseIndexLeaf struct against its |manufacturer| and
// |model| members.
MATCHER_P2(ReverseIndexLeafLike,
manufacturer,
model,
"is a ReverseIndexLeaf with manufacturer ``" +
std::string(manufacturer) + "'' and model ``" +
std::string(model) + "''") {
return ExplainMatchResult(
Field(&ReverseIndexLeaf::manufacturer, StrEq(manufacturer)), arg,
result_listener) &&
ExplainMatchResult(Field(&ReverseIndexLeaf::model, StrEq(model)), arg,
result_listener);
}
// Matches a ParsedPrinter struct against its
// |user_visible_printer_name| and |effective_make_and_model| members.
MATCHER_P2(ParsedPrinterLike,
name,
emm,
"is a ParsedPrinter with user_visible_printer_name``" +
std::string(name) + "'' and effective_make_and_model ``" +
std::string(emm) + "''") {
return ExplainMatchResult(
Field(&ParsedPrinter::user_visible_printer_name, StrEq(name)), arg,
result_listener) &&
ExplainMatchResult(
Field(&ParsedPrinter::effective_make_and_model, StrEq(emm)), arg,
result_listener);
}
} // namespace chromeos
#endif // CHROMEOS_PRINTING_PPD_METADATA_MATCHERS_H_
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chromeos/printing/ppd_metadata_parser.h" #include "chromeos/printing/ppd_metadata_parser.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "chromeos/printing/ppd_metadata_matchers.h"
#include "testing/gmock/include/gmock/gmock-matchers.h" #include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -15,44 +16,12 @@ namespace { ...@@ -15,44 +16,12 @@ namespace {
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::ExplainMatchResult; using ::testing::ExplainMatchResult;
using ::testing::Field; using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Pair; using ::testing::Pair;
using ::testing::StrEq; using ::testing::StrEq;
using ::testing::UnorderedElementsAre; using ::testing::UnorderedElementsAre;
constexpr base::StringPiece kInvalidJson = "blah blah invalid JSON"; constexpr base::StringPiece kInvalidJson = "blah blah invalid JSON";
// Matches a ReverseIndexLeaf struct against its |manufacturer| and
// |model| members.
MATCHER_P2(ReverseIndexLeafLike,
manufacturer,
model,
"is a ReverseIndexLeaf with manufacturer ``" +
std::string(manufacturer) + "'' and model ``" +
std::string(model) + "''") {
return ExplainMatchResult(
Field(&ReverseIndexLeaf::manufacturer, StrEq(manufacturer)), arg,
result_listener) &&
ExplainMatchResult(Field(&ReverseIndexLeaf::model, StrEq(model)), arg,
result_listener);
}
// Matches a ParsedPrinter struct against its
// |user_visible_printer_name| and |effective_make_and_model| members.
MATCHER_P2(ParsedPrinterLike,
name,
emm,
"is a ParsedPrinter with user_visible_printer_name``" +
std::string(name) + "'' and effective_make_and_model ``" +
std::string(emm) + "''") {
return ExplainMatchResult(
Field(&ParsedPrinter::user_visible_printer_name, StrEq(name)), arg,
result_listener) &&
ExplainMatchResult(
Field(&ParsedPrinter::effective_make_and_model, StrEq(emm)), arg,
result_listener);
}
// Verifies that ParseLocales() can parse locales metadata. // Verifies that ParseLocales() can parse locales metadata.
TEST(PpdMetadataParserTest, CanParseLocales) { TEST(PpdMetadataParserTest, CanParseLocales) {
constexpr base::StringPiece kLocalesJson = R"( constexpr base::StringPiece kLocalesJson = R"(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment