| 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 "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 "chromeos/printing/printer_configuration.h" | |
| 15 | |
| 16 using base::DictionaryValue; | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // ppd fields | |
| 23 const char kPPDid[] = "id"; | |
| 24 const char kFileName[] = "file_name"; | |
| 25 const char kVersionNumber[] = "version_number"; | |
| 26 const char kFromQuirks[] = "from_quirks_server"; | |
| 27 | |
| 28 // printer fields | |
| 29 const char kDisplayName[] = "display_name"; | |
| 30 const char kDescription[] = "description"; | |
| 31 const char kManufacturer[] = "manufacturer"; | |
| 32 const char kModel[] = "model"; | |
| 33 const char kUri[] = "uri"; | |
| 34 const char kPPD[] = "ppd"; | |
| 35 const char kUUID[] = "uuid"; | |
| 36 | |
| 37 std::unique_ptr<Printer::PPDFile> DictionaryToPPDFile( | |
| 38 const base::DictionaryValue* value) { | |
| 39 std::unique_ptr<Printer::PPDFile> ppd = base::MakeUnique<Printer::PPDFile>(); | |
| 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 if (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 return ppd; | |
| 57 } | |
| 58 | |
| 59 std::unique_ptr<base::DictionaryValue> PPDFileToDictionary( | |
| 60 const Printer::PPDFile& ppd) { | |
| 61 std::unique_ptr<base::DictionaryValue> dictionary = | |
| 62 base::MakeUnique<base::DictionaryValue>(); | |
| 63 dictionary->SetInteger(kPPDid, ppd.id); | |
| 64 dictionary->SetString(kFileName, ppd.file_name); | |
| 65 dictionary->SetInteger(kVersionNumber, ppd.version_number); | |
| 66 dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server); | |
| 67 return dictionary; | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 namespace printing { | |
| 73 | |
| 74 const char kPrinterId[] = "id"; | |
| 75 | |
| 76 std::unique_ptr<Printer> PrefToPrinter(const DictionaryValue& value) { | |
| 77 std::string id; | |
| 78 if (!value.GetString(kPrinterId, &id)) { | |
| 79 LOG(WARNING) << "Record id required"; | |
| 80 return nullptr; | |
| 81 } | |
| 82 | |
| 83 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id); | |
| 84 | |
| 85 std::string display_name; | |
| 86 if (value.GetString(kDisplayName, &display_name)) | |
| 87 printer->set_display_name(display_name); | |
| 88 | |
| 89 std::string description; | |
| 90 if (value.GetString(kDescription, &description)) | |
| 91 printer->set_description(description); | |
| 92 | |
| 93 std::string manufacturer; | |
| 94 if (value.GetString(kManufacturer, &manufacturer)) | |
| 95 printer->set_manufacturer(manufacturer); | |
| 96 | |
| 97 std::string model; | |
| 98 if (value.GetString(kModel, &model)) | |
| 99 printer->set_model(model); | |
| 100 | |
| 101 std::string uri; | |
| 102 if (value.GetString(kUri, &uri)) | |
| 103 printer->set_uri(uri); | |
| 104 | |
| 105 std::string uuid; | |
| 106 if (value.GetString(kUUID, &uuid)) | |
| 107 printer->set_uuid(uuid); | |
| 108 | |
| 109 const DictionaryValue* ppd; | |
| 110 if (value.GetDictionary(kPPD, &ppd)) { | |
| 111 printer->SetPPD(DictionaryToPPDFile(ppd)); | |
| 112 } | |
| 113 | |
| 114 return printer; | |
| 115 } | |
| 116 | |
| 117 std::unique_ptr<base::DictionaryValue> PrinterToPref(const Printer& printer) { | |
| 118 std::unique_ptr<DictionaryValue> dictionary = | |
| 119 base::MakeUnique<base::DictionaryValue>(); | |
| 120 dictionary->SetString(kPrinterId, printer.id()); | |
| 121 dictionary->SetString(kDisplayName, printer.display_name()); | |
| 122 dictionary->SetString(kDescription, printer.description()); | |
| 123 dictionary->SetString(kManufacturer, printer.manufacturer()); | |
| 124 dictionary->SetString(kModel, printer.model()); | |
| 125 dictionary->SetString(kUri, printer.uri()); | |
| 126 dictionary->SetString(kUUID, printer.uuid()); | |
| 127 | |
| 128 dictionary->Set(kPPD, PPDFileToDictionary(printer.ppd())); | |
| 129 | |
| 130 return dictionary; | |
| 131 } | |
| 132 | |
| 133 } // namespace printing | |
| 134 | |
| 135 } // namespace chromeos | |
| OLD | NEW |