Chromium Code Reviews| Index: chromeos/printing/printer_translator.cc |
| diff --git a/chromeos/printing/printer_translator.cc b/chromeos/printing/printer_translator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c5a183e99ee82a0b60db3862f41ea199d4a4ec9 |
| --- /dev/null |
| +++ b/chromeos/printing/printer_translator.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/printing/printer_translator.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "chromeos/printing/printer_configuration.h" |
| + |
| +using base::DictionaryValue; |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +// ppd fields |
| +const char kPPDid[] = "id"; |
| +const char kFileName[] = "file_name"; |
| +const char kVersionNumber[] = "version_number"; |
| +const char kFromQuirks[] = "from_quirks_server"; |
| + |
| +// printer fields |
| +const char kDisplayName[] = "display_name"; |
| +const char kDescription[] = "description"; |
| +const char kManufacturer[] = "manufacturer"; |
| +const char kModel[] = "model"; |
| +const char kUri[] = "uri"; |
| +const char kPPD[] = "ppd"; |
| +const char kUUID[] = "uuid"; |
| + |
| +void MergePPDFile(const DictionaryValue* value, Printer::PPDFile* ppd) { |
| + int id = -1; |
| + value->GetInteger(kPPDid, &id); |
| + ppd->id = id; |
| + |
| + std::string file_name; |
| + if (value->GetString(kFileName, &file_name)) |
| + ppd->file_name = file_name; |
| + |
| + int version_number = 0; |
| + value->GetInteger(kVersionNumber, &version_number); |
|
Lei Zhang
2016/08/02 02:35:52
Check return value like you do above and below?
skau
2016/08/02 20:17:22
Done.
|
| + ppd->version_number = version_number; |
| + |
| + bool from_quirks; |
| + if (value->GetBoolean(kFromQuirks, &from_quirks)) |
| + ppd->from_quirks_server = from_quirks; |
| +} |
| + |
| +void PPDFileToDictionary(const Printer::PPDFile& ppd, |
|
Lei Zhang
2016/08/02 02:35:52
Same comment about returning a std::unique_ptr<bas
skau
2016/08/02 20:17:22
Done.
|
| + DictionaryValue* dictionary) { |
| + dictionary->SetInteger(kPPDid, ppd.id); |
| + dictionary->SetString(kFileName, ppd.file_name); |
| + dictionary->SetInteger(kVersionNumber, ppd.version_number); |
| + dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace printing { |
| + |
| +const char kPrinterId[] = "id"; |
| + |
| +bool MergePrinterPreference(const DictionaryValue& value, Printer* printer) { |
| + std::string id; |
| + if (!value.GetString(kPrinterId, &id)) { |
| + LOG(WARNING) << "Record id required"; |
| + return false; |
| + } |
| + |
| + printer->set_id(id); |
| + |
| + std::string display_name; |
| + if (value.GetString(kDisplayName, &display_name)) |
| + printer->set_display_name(display_name); |
| + |
| + std::string description; |
| + if (value.GetString(kDescription, &description)) |
| + printer->set_description(description); |
| + |
| + std::string manufacturer; |
| + if (value.GetString(kManufacturer, &manufacturer)) |
| + printer->set_manufacturer(manufacturer); |
| + |
| + std::string model; |
| + if (value.GetString(kModel, &model)) |
| + printer->set_model(model); |
| + |
| + std::string uri; |
| + if (value.GetString(kUri, &uri)) |
| + printer->set_uri(uri); |
| + |
| + std::string uuid; |
| + if (value.GetString(kUUID, &uuid)) |
| + printer->set_uuid(uuid); |
| + |
| + const DictionaryValue* ppd; |
| + if (value.GetDictionary(kPPD, &ppd)) { |
| + // TODO(skau): merge? |
| + Printer::PPDFile ppdFile; |
| + MergePPDFile(ppd, &ppdFile); |
| + printer->set_ppd(ppdFile); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void PrinterToPref(const Printer& printer, DictionaryValue* dictionary) { |
| + dictionary->SetString(kPrinterId, printer.id()); |
| + dictionary->SetString(kDisplayName, printer.display_name()); |
| + dictionary->SetString(kDescription, printer.description()); |
| + dictionary->SetString(kManufacturer, printer.manufacturer()); |
| + dictionary->SetString(kModel, printer.model()); |
| + dictionary->SetString(kUri, printer.uri()); |
| + dictionary->SetString(kUUID, printer.uuid()); |
| + |
| + std::unique_ptr<DictionaryValue> ppd = base::MakeUnique<DictionaryValue>(); |
| + PPDFileToDictionary(printer.ppd(), ppd.get()); |
| + // create a std::unique_ptr<Value> from ppd since |
| + // std::unique_ptr<DictionaryValue> and std::unique_ptr<Value> are not |
| + // compatible types. |
| + dictionary->Set(kPPD, std::unique_ptr<base::Value>(std::move(ppd))); |
|
Lei Zhang
2016/08/02 02:35:52
Does this work?
dictionary->Set(kPPD, std::move(p
skau
2016/08/02 20:17:22
That does work. Neat.
|
| +} |
| + |
| +} // namespace printing |
| + |
| +} // namespace chromeos |