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

Side by Side Diff: chrome/browser/chromeos/printing/printer_pref_manager.cc

Issue 2161933003: Define printer configuration objects for Chrome OS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
(Empty)
1 #include "chromeos/printing/printer_pref_handler.h"
2
3 #include <memory>
4 #include <vector>
5
6 #include "base/values.h"
7 #include "chrome/browser/printing/printer_config.h"
8 #include "components/prefs/pref_registry_simple.h"
9
10 namespace chromeos {
11
12 namespace {
13
14 // ppd fields
15 const char kPPDid[] = "id";
16 const char kFileName[] = "file_name";
17 const char kVersionNumber[] = "version_number";
18 const char kFromQuirks[] = "from_quirks_server";
19
20 // printer fields
21 const char kPrinterId[] = "id";
22 const char kManufacturer[] = "manufacturer";
23 const char kModel[] = "model";
24 const char kUri[] = "uri";
25 const char kPPD[] = "ppd";
26
27 PPDFile ParsePPDFile(const DictionaryValue* value) {
28 PPDFile ppd;
29
30 int id = -1;
31 value->GetInteger(kPPDid, &id);
32 ppd.id = id;
33
34 std::string file_name;
35 if (value->GetString(kFileName, &file_name)) {
36 ppd.file_name = file_name;
37 }
38
39 int version_number = 0;
40 value->GetInteger(kVersionNumber, &version_number)
41 ppd.version_number = version_number;
42
43 bool from_quirks;
44 if (value->GetBoolean(kFromQuirks, &from_quirks)) {
45 ppd.from_quirks_server = from_quirks;
46 }
47
48 return ppd;
49 }
50
51 Printer ParsePrinter(const DictionaryValue* value) {
52 Printer printer;
53
54 std::string id;
55 if (value->GetString(kPrinterId, &id)) {
56 printer.id = id;
57 }
58
59 std::string manufacturer;
60 if (value->GetString(kManufacturer, &manufacturer)) {
61 printer.manufacturer = manufacturer;
62 }
63
64 std::string model;
65 if (value->GetString(kModel, &model)) {
66 printer.model = model;
67 }
68
69 std::string uri;
70 if (value->GetString(kUri, &uri)) {
71 printer.uri = uri;
72 }
73
74 const DictionaryValue* ppd;
75 if (value->GetDictionary(kPPD, &ppd)) {
76 printer.ppd = ParsePPDFile(ppd);
77 }
78
79 return printer;
80 }
81
82 void PPDFileToDictionary(const PPDFile& ppd, DictionaryValue* dictionary) {
83 dictionary->SetInteger(kPPDid, ppd.id);
84 dictionary->SetString(kFileName, ppd.file_name);
85 dictionary->SetInteger(kVersionNumber, ppd.version_number);
86 dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server);
87 }
88
89 void PrinterToDictionary(const Printer& printer, DictionaryValue* dictionary) {
90 dictionary->SetString(kPrinterId, printer.id);
91 dictionary->SetString(kManufacturer, printer.manufacturer);
92 dictionary->SetString(kModel, printer.model);
93 dictionary->SetString(kUri, printer.uri);
94
95 std::unique_ptr<DictionaryValue> ppd = base::MakeUnique<DictionaryValue>();
96 PPDFileToDictionary(printer.ppd, ppd.get());
97 dictionary->Set(kPPD, ppd);
98 }
99
100 const base::ListValue* GetPrinterList(Profile* profile) {
101 PrefService* user_state = profile->GetPrefs();
102 return user_state->GetList(prefs::kPrintingDevices);
103 }
104
105 base::DictionaryValue* FindMatchingPrinter(const base::ListValue* values, const std::string& id) {
106 DictionaryValue* match = nullptr;
107 size_t size = values->GetSize();
108 for (size_t i=0; i<size; ++i) {
109 DictionaryValue* printer_dictionary;
110 if(!values->GetDictionary(i, &printer_dictionary)) {
111 continue;
112 }
113
114 std::string printer_id;
115 if(printer_dictionary.GetString(kPrinterId, &printer_id) && id == printer_id ) {
116 match = printer_dictionary;
117 break;
118 }
119 }
120
121 return match;
122 }
123
124 } // anonymous namespace
125
126 // static
127 void PrinterPrefManager::RegisterProfilePrefs(PrefRegistrySyncable* registry) {
128 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when
129 // sync is implemented.
130 registry->RegisterListPref(prefs::kPrintingDevices, PrefRegistry::NO_REGISTRAT ION_FLAGS);
131 }
132
133 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {}
134
135 std::vector<std::unique_ptr<Printer>> GetPrinters() const {
136 std::vector<std::unique_ptr<Printer>> printers;
137
138 const base::ListValue* values = GetPrinteList(profile_);
139 size_t size = values->GetSize();
140 for (size_t i=0; i<size; ++i) {
141 const DictionaryValue* printer_dictionary;
142 if(values->GetDictionary(i, &printer_dictionary)) {
143 printers.push_back(ParsePrinter(printer_dictionary));
144 } else {
145 NOTREACHED();
146 }
147 }
148
149 return printers;
150 }
151
152 void RegisterPrinter(const Printer& printer) {
153 base::DictionaryValue* found_printer = nullptr;
154 std::unique_ptr<base::DictionaryValue> updated_printer = std::MakeUnique<Dicti onaryValue>();
155 const base::ListValue* printer_list = GetPrinterList(profile_);
156
157 if (printer.id.empty()) {
158 printer.id = base::GenerateGUID();
159 } else {
160 found_printer = FindMatchingPrinter(printer_list, printer.id);
161 }
162
163 PrinterToDictionary(printer, updated_printer.get());
164 if (found_printer) {
165 found_printer->MergeDictionary(updated_printer.get());
166 } else {
167 printer_list->Append(updated_printer);
168 }
169 }
170
171 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698