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 void MergePPDFile(const DictionaryValue* value, Printer::PPDFile* ppd) { |
| 38 int id = -1; |
| 39 value->GetInteger(kPPDid, &id); |
| 40 ppd->id = id; |
| 41 |
| 42 std::string file_name; |
| 43 if (value->GetString(kFileName, &file_name)) |
| 44 ppd->file_name = file_name; |
| 45 |
| 46 int version_number = 0; |
| 47 value->GetInteger(kVersionNumber, &version_number); |
| 48 ppd->version_number = version_number; |
| 49 |
| 50 bool from_quirks; |
| 51 if (value->GetBoolean(kFromQuirks, &from_quirks)) |
| 52 ppd->from_quirks_server = from_quirks; |
| 53 } |
| 54 |
| 55 void PPDFileToDictionary(const Printer::PPDFile& ppd, |
| 56 DictionaryValue* dictionary) { |
| 57 dictionary->SetInteger(kPPDid, ppd.id); |
| 58 dictionary->SetString(kFileName, ppd.file_name); |
| 59 dictionary->SetInteger(kVersionNumber, ppd.version_number); |
| 60 dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server); |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 namespace printing { |
| 66 |
| 67 const char kPrinterId[] = "id"; |
| 68 |
| 69 bool MergePrinterPreference(const DictionaryValue& value, Printer* printer) { |
| 70 std::string id; |
| 71 if (!value.GetString(kPrinterId, &id)) { |
| 72 LOG(WARNING) << "Record id required"; |
| 73 return false; |
| 74 } |
| 75 |
| 76 printer->set_id(id); |
| 77 |
| 78 std::string display_name; |
| 79 if (value.GetString(kDisplayName, &display_name)) |
| 80 printer->set_display_name(display_name); |
| 81 |
| 82 std::string description; |
| 83 if (value.GetString(kDescription, &description)) |
| 84 printer->set_description(description); |
| 85 |
| 86 std::string manufacturer; |
| 87 if (value.GetString(kManufacturer, &manufacturer)) |
| 88 printer->set_manufacturer(manufacturer); |
| 89 |
| 90 std::string model; |
| 91 if (value.GetString(kModel, &model)) |
| 92 printer->set_model(model); |
| 93 |
| 94 std::string uri; |
| 95 if (value.GetString(kUri, &uri)) |
| 96 printer->set_uri(uri); |
| 97 |
| 98 std::string uuid; |
| 99 if (value.GetString(kUUID, &uuid)) |
| 100 printer->set_uuid(uuid); |
| 101 |
| 102 const DictionaryValue* ppd; |
| 103 if (value.GetDictionary(kPPD, &ppd)) { |
| 104 // TODO(skau): merge? |
| 105 Printer::PPDFile ppdFile; |
| 106 MergePPDFile(ppd, &ppdFile); |
| 107 printer->set_ppd(ppdFile); |
| 108 } |
| 109 |
| 110 return true; |
| 111 } |
| 112 |
| 113 void PrinterToPref(const Printer& printer, DictionaryValue* dictionary) { |
| 114 dictionary->SetString(kPrinterId, printer.id()); |
| 115 dictionary->SetString(kDisplayName, printer.display_name()); |
| 116 dictionary->SetString(kDescription, printer.description()); |
| 117 dictionary->SetString(kManufacturer, printer.manufacturer()); |
| 118 dictionary->SetString(kModel, printer.model()); |
| 119 dictionary->SetString(kUri, printer.uri()); |
| 120 dictionary->SetString(kUUID, printer.uuid()); |
| 121 |
| 122 std::unique_ptr<DictionaryValue> ppd = base::MakeUnique<DictionaryValue>(); |
| 123 PPDFileToDictionary(printer.ppd(), ppd.get()); |
| 124 // create a std::unique_ptr<Value> from ppd since |
| 125 // std::unique_ptr<DictionaryValue> and std::unique_ptr<Value> are not |
| 126 // compatible types. |
| 127 dictionary->Set(kPPD, std::unique_ptr<base::Value>(std::move(ppd))); |
| 128 } |
| 129 |
| 130 } // namespace printing |
| 131 |
| 132 } // namespace chromeos |
OLD | NEW |