| 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 "chrome/browser/chromeos/printing/printer_pref_manager.h" | 5 #include "chrome/browser/chromeos/printing/printer_pref_manager.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 13 matching lines...) Expand all Loading... |
| 24 namespace chromeos { | 24 namespace chromeos { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const base::ListValue* GetPrinterList(Profile* profile) { | 28 const base::ListValue* GetPrinterList(Profile* profile) { |
| 29 return profile->GetPrefs()->GetList(prefs::kPrintingDevices); | 29 return profile->GetPrefs()->GetList(prefs::kPrintingDevices); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Returns the printer with the matching |id| from the list |values|. The | 32 // Returns the printer with the matching |id| from the list |values|. The |
| 33 // returned value is mutable and |values| could be modified. | 33 // returned value is mutable and |values| could be modified. |
| 34 base::DictionaryValue* FindPrinterPref(base::ListValue* values, | 34 base::DictionaryValue* FindPrinterPref(const base::ListValue* values, |
| 35 const std::string& id) { | 35 const std::string& id) { |
| 36 for (const auto& value : *values) { | 36 for (const auto& value : *values) { |
| 37 base::DictionaryValue* printer_dictionary; | 37 base::DictionaryValue* printer_dictionary; |
| 38 if (!value->GetAsDictionary(&printer_dictionary)) | 38 if (!value->GetAsDictionary(&printer_dictionary)) |
| 39 continue; | 39 continue; |
| 40 | 40 |
| 41 std::string printer_id; | 41 std::string printer_id; |
| 42 if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) && | 42 if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) && |
| 43 id == printer_id) | 43 id == printer_id) |
| 44 return printer_dictionary; | 44 return printer_dictionary; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 89 |
| 90 std::unique_ptr<Printer> printer = | 90 std::unique_ptr<Printer> printer = |
| 91 printing::PrefToPrinter(*printer_dictionary); | 91 printing::PrefToPrinter(*printer_dictionary); |
| 92 if (printer) | 92 if (printer) |
| 93 printers.push_back(std::move(printer)); | 93 printers.push_back(std::move(printer)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 return printers; | 96 return printers; |
| 97 } | 97 } |
| 98 | 98 |
| 99 std::unique_ptr<Printer> PrinterPrefManager::GetPrinter( |
| 100 const std::string& printer_id) const { |
| 101 const base::ListValue* values = GetPrinterList(profile_); |
| 102 const base::DictionaryValue* printer = FindPrinterPref(values, printer_id); |
| 103 |
| 104 return printer ? printing::PrefToPrinter(*printer) : nullptr; |
| 105 } |
| 106 |
| 99 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { | 107 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { |
| 100 if (printer->id().empty()) | 108 if (printer->id().empty()) |
| 101 printer->set_id(base::GenerateGUID()); | 109 printer->set_id(base::GenerateGUID()); |
| 102 | 110 |
| 103 std::unique_ptr<base::DictionaryValue> updated_printer = | 111 std::unique_ptr<base::DictionaryValue> updated_printer = |
| 104 printing::PrinterToPref(*(printer.get())); | 112 printing::PrinterToPref(*printer); |
| 105 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); | 113 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); |
| 106 } | 114 } |
| 107 | 115 |
| 108 bool PrinterPrefManager::RemovePrinter(const std::string& printer_id) { | 116 bool PrinterPrefManager::RemovePrinter(const std::string& printer_id) { |
| 109 DCHECK(!printer_id.empty()); | 117 DCHECK(!printer_id.empty()); |
| 110 ListPrefUpdate update(profile_->GetPrefs(), prefs::kPrintingDevices); | 118 ListPrefUpdate update(profile_->GetPrefs(), prefs::kPrintingDevices); |
| 111 base::ListValue* printer_list = update.Get(); | 119 base::ListValue* printer_list = update.Get(); |
| 112 DCHECK(printer_list) << "Printer preference not registered"; | 120 DCHECK(printer_list) << "Printer preference not registered"; |
| 113 base::DictionaryValue* printer = FindPrinterPref(printer_list, printer_id); | 121 base::DictionaryValue* printer = FindPrinterPref(printer_list, printer_id); |
| 114 | 122 |
| 115 return printer && printer_list->Remove(*printer, nullptr); | 123 return printer && printer_list->Remove(*printer, nullptr); |
| 116 } | 124 } |
| 117 | 125 |
| 118 } // namespace chromeos | 126 } // namespace chromeos |
| OLD | NEW |