Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Unified Diff: chromeos/printing/printer_translator.cc

Issue 2795043003: Clean up printer preference handling. (Closed)
Patch Set: log missing fields Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/printing/printer_translator.h ('k') | chromeos/printing/printer_translator_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/printing/printer_translator.cc
diff --git a/chromeos/printing/printer_translator.cc b/chromeos/printing/printer_translator.cc
index b559ec4dff965bdc8c3ae9aa9f78f4cc8edb6ddc..017d5e435404430c43d20c9c26d8d870aec0f8e9 100644
--- a/chromeos/printing/printer_translator.cc
+++ b/chromeos/printing/printer_translator.cc
@@ -19,10 +19,8 @@ namespace chromeos {
namespace {
-// PPD reference fields
-const char kUserSuppliedPpdUrl[] = "user_supplied_ppd_url";
-// TODO(justincarlson) -- This should be changed to effective_make_and_model to
-// match the implementation.
+// For historical reasons, the effective_make_and_model field is just
+// effective_model for policy printers.
const char kEffectiveModel[] = "effective_model";
// printer fields
@@ -31,33 +29,9 @@ const char kDescription[] = "description";
const char kManufacturer[] = "manufacturer";
const char kModel[] = "model";
const char kUri[] = "uri";
-const char kPpdReference[] = "ppd_reference";
const char kUUID[] = "uuid";
-
-// The name of the PpdResource for policy printers.
const char kPpdResource[] = "ppd_resource";
-Printer::PpdReference DictionaryToPpdReference(
- const base::DictionaryValue* value) {
- Printer::PpdReference ppd;
- value->GetString(kUserSuppliedPpdUrl, &ppd.user_supplied_ppd_url);
- value->GetString(kEffectiveModel, &ppd.effective_make_and_model);
- return ppd;
-}
-
-// Convert a PpdReference struct to a DictionaryValue.
-std::unique_ptr<base::DictionaryValue> PpdReferenceToDictionary(
- const Printer::PpdReference& ppd) {
- auto dictionary = base::MakeUnique<DictionaryValue>();
- if (!ppd.user_supplied_ppd_url.empty()) {
- dictionary->SetString(kUserSuppliedPpdUrl, ppd.user_supplied_ppd_url);
- }
- if (!ppd.effective_make_and_model.empty()) {
- dictionary->SetString(kEffectiveModel, ppd.effective_make_and_model);
- }
- return dictionary;
-}
-
// Converts |value| into a Printer object for the fields that are shared
// between pref printers and policy printers.
std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) {
@@ -69,10 +43,24 @@ std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) {
std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id);
+ // Mandatory fields
std::string display_name;
- if (value.GetString(kDisplayName, &display_name))
+ if (value.GetString(kDisplayName, &display_name)) {
printer->set_display_name(display_name);
+ } else {
+ LOG(WARNING) << "Display name required";
+ return nullptr;
+ }
+ std::string uri;
+ if (value.GetString(kUri, &uri)) {
+ printer->set_uri(uri);
+ } else {
+ LOG(WARNING) << "Uri required";
+ return nullptr;
+ }
+
+ // Optional fields
std::string description;
if (value.GetString(kDescription, &description))
printer->set_description(description);
@@ -85,10 +73,6 @@ std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) {
if (value.GetString(kModel, &model))
printer->set_model(model);
- std::string uri;
- if (value.GetString(kUri, &uri))
- printer->set_uri(uri);
-
std::string uuid;
if (value.GetString(kUUID, &uuid))
printer->set_uuid(uuid);
@@ -102,48 +86,25 @@ namespace printing {
const char kPrinterId[] = "id";
-std::unique_ptr<Printer> PrefToPrinter(const DictionaryValue& value) {
- if (!value.HasKey(kPrinterId)) {
- LOG(WARNING) << "Record id required";
- return nullptr;
- }
-
- std::unique_ptr<Printer> printer = DictionaryToPrinter(value);
- printer->set_source(Printer::SRC_USER_PREFS);
-
- const DictionaryValue* ppd;
- if (value.GetDictionary(kPpdReference, &ppd)) {
- *printer->mutable_ppd_reference() = DictionaryToPpdReference(ppd);
- }
-
- return printer;
-}
-
-std::unique_ptr<base::DictionaryValue> PrinterToPref(const Printer& printer) {
- std::unique_ptr<DictionaryValue> dictionary =
- base::MakeUnique<base::DictionaryValue>();
- dictionary->SetString(kPrinterId, printer.id());
- dictionary->SetString(kDisplayName, printer.display_name());
- dictionary->SetString(kDescription, printer.description());
- dictionary->SetString(kManufacturer, printer.manufacturer());
- dictionary->SetString(kModel, printer.model());
- dictionary->SetString(kUri, printer.uri());
- dictionary->SetString(kUUID, printer.uuid());
-
- dictionary->Set(kPpdReference,
- PpdReferenceToDictionary(printer.ppd_reference()));
-
- return dictionary;
-}
-
std::unique_ptr<Printer> RecommendedPrinterToPrinter(
const base::DictionaryValue& pref) {
std::unique_ptr<Printer> printer = DictionaryToPrinter(pref);
+ if (!printer) {
+ LOG(WARNING) << "Failed to parse policy printer.";
+ return nullptr;
+ }
+
printer->set_source(Printer::SRC_POLICY);
const DictionaryValue* ppd;
- if (pref.GetDictionary(kPpdResource, &ppd)) {
- *printer->mutable_ppd_reference() = DictionaryToPpdReference(ppd);
+ std::string make_and_model;
+ if (pref.GetDictionary(kPpdResource, &ppd) &&
+ ppd->GetString(kEffectiveModel, &make_and_model)) {
+ printer->mutable_ppd_reference()->effective_make_and_model = make_and_model;
+ } else {
+ // Make and model is mandatory
+ LOG(WARNING) << "Missing model information for policy printer.";
+ return nullptr;
}
return printer;
« no previous file with comments | « chromeos/printing/printer_translator.h ('k') | chromeos/printing/printer_translator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698