| 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 "chrome/browser/chromeos/printing/printer_pref_manager.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/guid.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "chromeos/printing/printer_configuration.h" | |
| 18 #include "chromeos/printing/printer_translator.h" | |
| 19 #include "components/pref_registry/pref_registry_syncable.h" | |
| 20 #include "components/prefs/pref_registry_simple.h" | |
| 21 #include "components/prefs/pref_service.h" | |
| 22 #include "components/prefs/scoped_user_pref_update.h" | |
| 23 | |
| 24 namespace chromeos { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const base::ListValue* GetPrinterList(Profile* profile) { | |
| 29 return profile->GetPrefs()->GetList(prefs::kPrintingDevices); | |
| 30 } | |
| 31 | |
| 32 // Returns the printer with the matching |id| from the list |values|. The | |
| 33 // returned value is mutable and |values| could be modified. | |
| 34 base::DictionaryValue* FindPrinterPref(base::ListValue* values, | |
| 35 const std::string& id) { | |
| 36 for (const auto& value : *values) { | |
| 37 base::DictionaryValue* printer_dictionary; | |
| 38 if (!value->GetAsDictionary(&printer_dictionary)) | |
| 39 continue; | |
| 40 | |
| 41 std::string printer_id; | |
| 42 if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) && | |
| 43 id == printer_id) | |
| 44 return printer_dictionary; | |
| 45 } | |
| 46 | |
| 47 return nullptr; | |
| 48 } | |
| 49 | |
| 50 void UpdatePrinterPref( | |
| 51 Profile* profile, | |
| 52 const std::string& id, | |
| 53 std::unique_ptr<base::DictionaryValue> printer_dictionary) { | |
| 54 ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices); | |
| 55 base::ListValue* printer_list = update.Get(); | |
| 56 DCHECK(printer_list) << "Register the printer preference"; | |
| 57 base::DictionaryValue* printer = FindPrinterPref(printer_list, id); | |
| 58 if (!printer) { | |
| 59 printer_list->Append(std::move(printer_dictionary)); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 printer->MergeDictionary(printer_dictionary.get()); | |
| 64 } | |
| 65 | |
| 66 } // anonymous namespace | |
| 67 | |
| 68 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {} | |
| 69 | |
| 70 // static | |
| 71 void PrinterPrefManager::RegisterProfilePrefs( | |
| 72 user_prefs::PrefRegistrySyncable* registry) { | |
| 73 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when | |
| 74 // sync is implemented. | |
| 75 registry->RegisterListPref(prefs::kPrintingDevices, | |
| 76 PrefRegistry::NO_REGISTRATION_FLAGS); | |
| 77 } | |
| 78 | |
| 79 std::vector<std::unique_ptr<Printer>> PrinterPrefManager::GetPrinters() const { | |
| 80 std::vector<std::unique_ptr<Printer>> printers; | |
| 81 | |
| 82 const base::ListValue* values = GetPrinterList(profile_); | |
| 83 for (const auto& value : *values) { | |
| 84 const base::DictionaryValue* printer_dictionary = nullptr; | |
| 85 value->GetAsDictionary(&printer_dictionary); | |
| 86 | |
| 87 DCHECK(printer_dictionary); | |
| 88 | |
| 89 std::unique_ptr<Printer> printer = | |
| 90 printing::PrefToPrinter(*printer_dictionary); | |
| 91 if (printer) | |
| 92 printers.push_back(std::move(printer)); | |
| 93 } | |
| 94 | |
| 95 return printers; | |
| 96 } | |
| 97 | |
| 98 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { | |
| 99 if (printer->id().empty()) | |
| 100 printer->set_id(base::GenerateGUID()); | |
| 101 | |
| 102 std::unique_ptr<base::DictionaryValue> updated_printer = | |
| 103 printing::PrinterToPref(*(printer.get())); | |
| 104 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); | |
| 105 } | |
| 106 | |
| 107 } // namespace chromeos | |
| OLD | NEW |