OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/printing/printer_translator.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/chromeos/printing/printer_configuration.h" | |
15 | |
16 using base::DictionaryValue; | |
17 | |
18 namespace chromeos { | |
19 | |
20 const char kPrinterId[] = "id"; | |
21 | |
22 namespace { | |
23 | |
24 // ppd fields | |
25 const char kPPDid[] = "id"; | |
26 const char kFileName[] = "file_name"; | |
27 const char kVersionNumber[] = "version_number"; | |
28 const char kFromQuirks[] = "from_quirks_server"; | |
29 | |
30 // printer fields | |
31 const char kDisplayName[] = "display_name"; | |
32 const char kDescription[] = "description"; | |
33 const char kManufacturer[] = "manufacturer"; | |
34 const char kModel[] = "model"; | |
35 const char kUri[] = "uri"; | |
36 const char kPPD[] = "ppd"; | |
37 const char kUUID[] = "uuid"; | |
38 | |
39 void MergePPDFile(const DictionaryValue* value, PPDFile* ppd) { | |
40 int id = -1; | |
41 value->GetInteger(kPPDid, &id); | |
42 ppd->id = id; | |
43 | |
44 std::string file_name; | |
45 if (value->GetString(kFileName, &file_name)) | |
46 ppd->file_name = file_name; | |
47 | |
48 int version_number = 0; | |
49 value->GetInteger(kVersionNumber, &version_number); | |
50 ppd->version_number = version_number; | |
51 | |
52 bool from_quirks; | |
53 if (value->GetBoolean(kFromQuirks, &from_quirks)) | |
54 ppd->from_quirks_server = from_quirks; | |
55 } | |
56 | |
57 void PPDFileToDictionary(const PPDFile& ppd, DictionaryValue* dictionary) { | |
58 dictionary->SetInteger(kPPDid, ppd.id); | |
59 dictionary->SetString(kFileName, ppd.file_name); | |
60 dictionary->SetInteger(kVersionNumber, ppd.version_number); | |
61 dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server); | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 bool MergePrinterPreference(const DictionaryValue& value, Printer* printer) { | |
67 std::string id; | |
68 if (value.GetString(kPrinterId, &id)) { | |
69 printer->set_id(id); | |
70 } else { | |
71 DVLOG(1) << "Record id required"; | |
72 return false; | |
73 } | |
stevenjb
2016/07/29 16:56:51
invert logic and elim. else
Also, should this be a
skau
2016/08/01 19:37:21
It's an unexpected condition that we don't want to
stevenjb
2016/08/01 20:48:16
Logging guide line is here: https://chromium.googl
| |
74 | |
75 std::string display_name; | |
76 if (value.GetString(kDisplayName, &display_name)) | |
77 printer->set_display_name(display_name); | |
78 | |
79 std::string description; | |
80 if (value.GetString(kDescription, &description)) | |
81 printer->set_description(description); | |
82 | |
83 std::string manufacturer; | |
84 if (value.GetString(kManufacturer, &manufacturer)) | |
85 printer->set_manufacturer(manufacturer); | |
86 | |
87 std::string model; | |
88 if (value.GetString(kModel, &model)) | |
89 printer->set_model(model); | |
90 | |
91 std::string uri; | |
92 if (value.GetString(kUri, &uri)) | |
93 printer->set_uri(uri); | |
94 | |
95 std::string uuid; | |
96 if (value.GetString(kUUID, &uuid)) | |
97 printer->set_uuid(uuid); | |
98 | |
99 const DictionaryValue* ppd; | |
100 if (value.GetDictionary(kPPD, &ppd)) { | |
101 // TODO(skau): merge? | |
102 PPDFile ppdFile; | |
103 MergePPDFile(ppd, &ppdFile); | |
104 printer->set_ppd(ppdFile); | |
105 } | |
106 | |
107 return true; | |
108 } | |
109 | |
110 void PrinterToPref(const Printer& printer, DictionaryValue* dictionary) { | |
111 dictionary->SetString(kPrinterId, printer.id()); | |
112 dictionary->SetString(kDisplayName, printer.display_name()); | |
113 dictionary->SetString(kDescription, printer.description()); | |
114 dictionary->SetString(kManufacturer, printer.manufacturer()); | |
115 dictionary->SetString(kModel, printer.model()); | |
116 dictionary->SetString(kUri, printer.uri()); | |
117 dictionary->SetString(kUUID, printer.uuid()); | |
118 | |
119 std::unique_ptr<DictionaryValue> ppd = base::MakeUnique<DictionaryValue>(); | |
120 PPDFileToDictionary(printer.ppd(), ppd.get()); | |
121 // create a std::unique_ptr<Value> from ppd since | |
122 // std::unique_ptr<DictionaryValue> and std::unique_ptr<Value> are not | |
123 // compatible types. | |
124 dictionary->Set(kPPD, std::unique_ptr<base::Value>(std::move(ppd))); | |
125 } | |
126 | |
127 } // namespace chromeos | |
OLD | NEW |