Chromium Code Reviews| Index: chrome/browser/chromeos/printing/printer_translator.cc |
| diff --git a/chrome/browser/chromeos/printing/printer_translator.cc b/chrome/browser/chromeos/printing/printer_translator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fbae47cfa07ddc954fc3668c61037b101f41ec4b |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/printing/printer_translator.cc |
| @@ -0,0 +1,127 @@ |
| +// 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 "chrome/browser/chromeos/printing/printer_translator.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/printing/printer_configuration.h" |
| + |
| +using base::DictionaryValue; |
| + |
| +namespace chromeos { |
| + |
| +const char kPrinterId[] = "id"; |
| + |
| +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, 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); |
| + ppd->version_number = version_number; |
| + |
| + bool from_quirks; |
| + if (value->GetBoolean(kFromQuirks, &from_quirks)) |
| + ppd->from_quirks_server = from_quirks; |
| +} |
| + |
| +void PPDFileToDictionary(const PPDFile& ppd, 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 |
| + |
| +bool MergePrinterPreference(const DictionaryValue& value, Printer* printer) { |
| + std::string id; |
| + if (value.GetString(kPrinterId, &id)) { |
| + printer->set_id(id); |
| + } else { |
| + DVLOG(1) << "Record id required"; |
| + return false; |
| + } |
|
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
|
| + |
| + 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? |
| + 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))); |
| +} |
| + |
| +} // namespace chromeos |