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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/printing/printer_translator.h" 5 #include "chromeos/printing/printer_translator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chromeos/printing/printer_configuration.h" 14 #include "chromeos/printing/printer_configuration.h"
15 15
16 using base::DictionaryValue; 16 using base::DictionaryValue;
17 17
18 namespace chromeos { 18 namespace chromeos {
19 19
20 namespace { 20 namespace {
21 21
22 // PPD reference fields 22 // For historical reasons, the effective_make_and_model field is just
23 const char kUserSuppliedPpdUrl[] = "user_supplied_ppd_url"; 23 // effective_model for policy printers.
24 // TODO(justincarlson) -- This should be changed to effective_make_and_model to
25 // match the implementation.
26 const char kEffectiveModel[] = "effective_model"; 24 const char kEffectiveModel[] = "effective_model";
27 25
28 // printer fields 26 // printer fields
29 const char kDisplayName[] = "display_name"; 27 const char kDisplayName[] = "display_name";
30 const char kDescription[] = "description"; 28 const char kDescription[] = "description";
31 const char kManufacturer[] = "manufacturer"; 29 const char kManufacturer[] = "manufacturer";
32 const char kModel[] = "model"; 30 const char kModel[] = "model";
33 const char kUri[] = "uri"; 31 const char kUri[] = "uri";
34 const char kPpdReference[] = "ppd_reference";
35 const char kUUID[] = "uuid"; 32 const char kUUID[] = "uuid";
36
37 // The name of the PpdResource for policy printers.
38 const char kPpdResource[] = "ppd_resource"; 33 const char kPpdResource[] = "ppd_resource";
39 34
40 Printer::PpdReference DictionaryToPpdReference(
41 const base::DictionaryValue* value) {
42 Printer::PpdReference ppd;
43 value->GetString(kUserSuppliedPpdUrl, &ppd.user_supplied_ppd_url);
44 value->GetString(kEffectiveModel, &ppd.effective_make_and_model);
45 return ppd;
46 }
47
48 // Convert a PpdReference struct to a DictionaryValue.
49 std::unique_ptr<base::DictionaryValue> PpdReferenceToDictionary(
50 const Printer::PpdReference& ppd) {
51 auto dictionary = base::MakeUnique<DictionaryValue>();
52 if (!ppd.user_supplied_ppd_url.empty()) {
53 dictionary->SetString(kUserSuppliedPpdUrl, ppd.user_supplied_ppd_url);
54 }
55 if (!ppd.effective_make_and_model.empty()) {
56 dictionary->SetString(kEffectiveModel, ppd.effective_make_and_model);
57 }
58 return dictionary;
59 }
60
61 // Converts |value| into a Printer object for the fields that are shared 35 // Converts |value| into a Printer object for the fields that are shared
62 // between pref printers and policy printers. 36 // between pref printers and policy printers.
63 std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) { 37 std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) {
64 std::string id; 38 std::string id;
65 if (!value.GetString(printing::kPrinterId, &id)) { 39 if (!value.GetString(printing::kPrinterId, &id)) {
66 LOG(WARNING) << "Record id required"; 40 LOG(WARNING) << "Record id required";
67 return nullptr; 41 return nullptr;
68 } 42 }
69 43
70 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id); 44 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id);
71 45
46 // Mandatory fields
72 std::string display_name; 47 std::string display_name;
73 if (value.GetString(kDisplayName, &display_name)) 48 if (value.GetString(kDisplayName, &display_name)) {
74 printer->set_display_name(display_name); 49 printer->set_display_name(display_name);
50 } else {
51 LOG(WARNING) << "Display name required";
52 return nullptr;
53 }
75 54
55 std::string uri;
56 if (value.GetString(kUri, &uri)) {
57 printer->set_uri(uri);
58 } else {
59 LOG(WARNING) << "Uri required";
60 return nullptr;
61 }
62
63 // Optional fields
76 std::string description; 64 std::string description;
77 if (value.GetString(kDescription, &description)) 65 if (value.GetString(kDescription, &description))
78 printer->set_description(description); 66 printer->set_description(description);
79 67
80 std::string manufacturer; 68 std::string manufacturer;
81 if (value.GetString(kManufacturer, &manufacturer)) 69 if (value.GetString(kManufacturer, &manufacturer))
82 printer->set_manufacturer(manufacturer); 70 printer->set_manufacturer(manufacturer);
83 71
84 std::string model; 72 std::string model;
85 if (value.GetString(kModel, &model)) 73 if (value.GetString(kModel, &model))
86 printer->set_model(model); 74 printer->set_model(model);
87 75
88 std::string uri;
89 if (value.GetString(kUri, &uri))
90 printer->set_uri(uri);
91
92 std::string uuid; 76 std::string uuid;
93 if (value.GetString(kUUID, &uuid)) 77 if (value.GetString(kUUID, &uuid))
94 printer->set_uuid(uuid); 78 printer->set_uuid(uuid);
95 79
96 return printer; 80 return printer;
97 } 81 }
98 82
99 } // namespace 83 } // namespace
100 84
101 namespace printing { 85 namespace printing {
102 86
103 const char kPrinterId[] = "id"; 87 const char kPrinterId[] = "id";
104 88
105 std::unique_ptr<Printer> PrefToPrinter(const DictionaryValue& value) {
106 if (!value.HasKey(kPrinterId)) {
107 LOG(WARNING) << "Record id required";
108 return nullptr;
109 }
110
111 std::unique_ptr<Printer> printer = DictionaryToPrinter(value);
112 printer->set_source(Printer::SRC_USER_PREFS);
113
114 const DictionaryValue* ppd;
115 if (value.GetDictionary(kPpdReference, &ppd)) {
116 *printer->mutable_ppd_reference() = DictionaryToPpdReference(ppd);
117 }
118
119 return printer;
120 }
121
122 std::unique_ptr<base::DictionaryValue> PrinterToPref(const Printer& printer) {
123 std::unique_ptr<DictionaryValue> dictionary =
124 base::MakeUnique<base::DictionaryValue>();
125 dictionary->SetString(kPrinterId, printer.id());
126 dictionary->SetString(kDisplayName, printer.display_name());
127 dictionary->SetString(kDescription, printer.description());
128 dictionary->SetString(kManufacturer, printer.manufacturer());
129 dictionary->SetString(kModel, printer.model());
130 dictionary->SetString(kUri, printer.uri());
131 dictionary->SetString(kUUID, printer.uuid());
132
133 dictionary->Set(kPpdReference,
134 PpdReferenceToDictionary(printer.ppd_reference()));
135
136 return dictionary;
137 }
138
139 std::unique_ptr<Printer> RecommendedPrinterToPrinter( 89 std::unique_ptr<Printer> RecommendedPrinterToPrinter(
140 const base::DictionaryValue& pref) { 90 const base::DictionaryValue& pref) {
141 std::unique_ptr<Printer> printer = DictionaryToPrinter(pref); 91 std::unique_ptr<Printer> printer = DictionaryToPrinter(pref);
92 if (!printer) {
93 LOG(WARNING) << "Failed to parse policy printer.";
94 return nullptr;
95 }
96
142 printer->set_source(Printer::SRC_POLICY); 97 printer->set_source(Printer::SRC_POLICY);
143 98
144 const DictionaryValue* ppd; 99 const DictionaryValue* ppd;
145 if (pref.GetDictionary(kPpdResource, &ppd)) { 100 std::string make_and_model;
146 *printer->mutable_ppd_reference() = DictionaryToPpdReference(ppd); 101 if (pref.GetDictionary(kPpdResource, &ppd) &&
102 ppd->GetString(kEffectiveModel, &make_and_model)) {
103 printer->mutable_ppd_reference()->effective_make_and_model = make_and_model;
104 } else {
105 // Make and model is mandatory
106 LOG(WARNING) << "Missing model information for policy printer.";
107 return nullptr;
147 } 108 }
148 109
149 return printer; 110 return printer;
150 } 111 }
151 112
152 } // namespace printing 113 } // namespace printing
153 } // namespace chromeos 114 } // namespace chromeos
OLDNEW
« 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