| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/printing/printer_translator.h" | 5 #include "chromeos/printing/printer_translator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 // printer fields | 26 // printer fields |
| 27 const char kDisplayName[] = "display_name"; | 27 const char kDisplayName[] = "display_name"; |
| 28 const char kDescription[] = "description"; | 28 const char kDescription[] = "description"; |
| 29 const char kManufacturer[] = "manufacturer"; | 29 const char kManufacturer[] = "manufacturer"; |
| 30 const char kModel[] = "model"; | 30 const char kModel[] = "model"; |
| 31 const char kUri[] = "uri"; | 31 const char kUri[] = "uri"; |
| 32 const char kUUID[] = "uuid"; | 32 const char kUUID[] = "uuid"; |
| 33 const char kPpdResource[] = "ppd_resource"; | 33 const char kPpdResource[] = "ppd_resource"; |
| 34 | 34 |
| 35 // Converts |value| into a Printer object for the fields that are shared | 35 // Populates the |printer| object with corresponding fields from |value|. |
| 36 // between pref printers and policy printers. | 36 // Returns false if |value| is missing a required field. |
| 37 std::unique_ptr<Printer> DictionaryToPrinter(const DictionaryValue& value) { | 37 bool DictionaryToPrinter(const DictionaryValue& value, Printer* printer) { |
| 38 std::string id; | |
| 39 if (!value.GetString(printing::kPrinterId, &id)) { | |
| 40 LOG(WARNING) << "Record id required"; | |
| 41 return nullptr; | |
| 42 } | |
| 43 | |
| 44 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id); | |
| 45 | |
| 46 // Mandatory fields | 38 // Mandatory fields |
| 47 std::string display_name; | 39 std::string display_name; |
| 48 if (value.GetString(kDisplayName, &display_name)) { | 40 if (value.GetString(kDisplayName, &display_name)) { |
| 49 printer->set_display_name(display_name); | 41 printer->set_display_name(display_name); |
| 50 } else { | 42 } else { |
| 51 LOG(WARNING) << "Display name required"; | 43 LOG(WARNING) << "Display name required"; |
| 52 return nullptr; | 44 return false; |
| 53 } | 45 } |
| 54 | 46 |
| 55 std::string uri; | 47 std::string uri; |
| 56 if (value.GetString(kUri, &uri)) { | 48 if (value.GetString(kUri, &uri)) { |
| 57 printer->set_uri(uri); | 49 printer->set_uri(uri); |
| 58 } else { | 50 } else { |
| 59 LOG(WARNING) << "Uri required"; | 51 LOG(WARNING) << "Uri required"; |
| 60 return nullptr; | 52 return false; |
| 61 } | 53 } |
| 62 | 54 |
| 63 // Optional fields | 55 // Optional fields |
| 64 std::string description; | 56 std::string description; |
| 65 if (value.GetString(kDescription, &description)) | 57 if (value.GetString(kDescription, &description)) |
| 66 printer->set_description(description); | 58 printer->set_description(description); |
| 67 | 59 |
| 68 std::string manufacturer; | 60 std::string manufacturer; |
| 69 if (value.GetString(kManufacturer, &manufacturer)) | 61 if (value.GetString(kManufacturer, &manufacturer)) |
| 70 printer->set_manufacturer(manufacturer); | 62 printer->set_manufacturer(manufacturer); |
| 71 | 63 |
| 72 std::string model; | 64 std::string model; |
| 73 if (value.GetString(kModel, &model)) | 65 if (value.GetString(kModel, &model)) |
| 74 printer->set_model(model); | 66 printer->set_model(model); |
| 75 | 67 |
| 76 std::string uuid; | 68 std::string uuid; |
| 77 if (value.GetString(kUUID, &uuid)) | 69 if (value.GetString(kUUID, &uuid)) |
| 78 printer->set_uuid(uuid); | 70 printer->set_uuid(uuid); |
| 79 | 71 |
| 80 return printer; | 72 return true; |
| 81 } | 73 } |
| 82 | 74 |
| 83 } // namespace | 75 } // namespace |
| 84 | 76 |
| 85 namespace printing { | 77 namespace printing { |
| 86 | 78 |
| 87 const char kPrinterId[] = "id"; | 79 const char kPrinterId[] = "id"; |
| 88 | 80 |
| 89 std::unique_ptr<Printer> RecommendedPrinterToPrinter( | 81 std::unique_ptr<Printer> RecommendedPrinterToPrinter( |
| 90 const base::DictionaryValue& pref) { | 82 const base::DictionaryValue& pref, |
| 91 std::unique_ptr<Printer> printer = DictionaryToPrinter(pref); | 83 const base::Time& timestamp) { |
| 92 if (!printer) { | 84 DCHECK(!timestamp.is_null()); |
| 85 |
| 86 std::string id; |
| 87 if (!pref.GetString(printing::kPrinterId, &id)) { |
| 88 LOG(WARNING) << "Record id required"; |
| 89 return nullptr; |
| 90 } |
| 91 |
| 92 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(id, timestamp); |
| 93 if (!DictionaryToPrinter(pref, printer.get())) { |
| 93 LOG(WARNING) << "Failed to parse policy printer."; | 94 LOG(WARNING) << "Failed to parse policy printer."; |
| 94 return nullptr; | 95 return nullptr; |
| 95 } | 96 } |
| 96 | 97 |
| 97 printer->set_source(Printer::SRC_POLICY); | 98 printer->set_source(Printer::SRC_POLICY); |
| 98 | 99 |
| 99 const DictionaryValue* ppd; | 100 const DictionaryValue* ppd; |
| 100 std::string make_and_model; | 101 std::string make_and_model; |
| 101 if (pref.GetDictionary(kPpdResource, &ppd) && | 102 if (pref.GetDictionary(kPpdResource, &ppd) && |
| 102 ppd->GetString(kEffectiveModel, &make_and_model)) { | 103 ppd->GetString(kEffectiveModel, &make_and_model)) { |
| 103 printer->mutable_ppd_reference()->effective_make_and_model = make_and_model; | 104 printer->mutable_ppd_reference()->effective_make_and_model = make_and_model; |
| 104 } else { | 105 } else { |
| 105 // Make and model is mandatory | 106 // Make and model is mandatory |
| 106 LOG(WARNING) << "Missing model information for policy printer."; | 107 LOG(WARNING) << "Missing model information for policy printer."; |
| 107 return nullptr; | 108 return nullptr; |
| 108 } | 109 } |
| 109 | 110 |
| 110 return printer; | 111 return printer; |
| 111 } | 112 } |
| 112 | 113 |
| 113 } // namespace printing | 114 } // namespace printing |
| 114 } // namespace chromeos | 115 } // namespace chromeos |
| OLD | NEW |