Chromium Code Reviews| Index: chrome/browser/chromeos/printing/printer_pref_manager.cc |
| diff --git a/chrome/browser/chromeos/printing/printer_pref_manager.cc b/chrome/browser/chromeos/printing/printer_pref_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83d100e6024985d3984c0aa8404e4cc31b15750e |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/printing/printer_pref_manager.cc |
| @@ -0,0 +1,106 @@ |
| +// 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_pref_manager.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/guid.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/printing/printer_configuration.h" |
| +#include "chromeos/printing/printer_translator.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/prefs/scoped_user_pref_update.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const base::ListValue* GetPrinterList(Profile* profile) { |
| + return profile->GetPrefs()->GetList(prefs::kPrintingDevices); |
| +} |
| + |
| +base::DictionaryValue* FindMatchingPrinter(const base::ListValue& values, |
| + const std::string& id) { |
| + for (const auto& value : values) { |
|
stevenjb
2016/08/01 20:48:16
This should still work with *values and without th
skau
2016/08/01 21:10:10
See comment on earlier revision. This works as we
|
| + base::DictionaryValue* printer_dictionary; |
| + if (!value->GetAsDictionary(&printer_dictionary)) |
| + continue; |
| + |
| + std::string printer_id; |
| + if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) && |
| + id == printer_id) |
| + return printer_dictionary; |
| + } |
| + |
| + return nullptr; |
| +} |
| + |
| +void UpdatePrinterPref( |
| + Profile* profile, |
| + const std::string& id, |
| + std::unique_ptr<base::DictionaryValue> printer_dictionary) { |
| + ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices); |
| + base::ListValue* printer_list = update.Get(); |
| + DCHECK(printer_list) << "Register the printer preference"; |
| + base::DictionaryValue* printer = FindMatchingPrinter(*printer_list, id); |
| + if (!printer) { |
| + printer_list->Append( |
| + std::unique_ptr<base::Value>(std::move(printer_dictionary))); |
| + return; |
| + } |
| + |
| + printer->MergeDictionary(printer_dictionary.get()); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +// static |
| +void PrinterPrefManager::RegisterProfilePrefs( |
| + user_prefs::PrefRegistrySyncable* registry) { |
| + // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when |
| + // sync is implemented. |
| + registry->RegisterListPref(prefs::kPrintingDevices, |
| + PrefRegistry::NO_REGISTRATION_FLAGS); |
| +} |
| + |
| +PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {} |
| + |
| +std::vector<std::unique_ptr<Printer>> PrinterPrefManager::GetPrinters() const { |
| + std::vector<std::unique_ptr<Printer>> printers; |
| + |
| + const base::ListValue* values = GetPrinterList(profile_); |
| + for (const auto& value : *values) { |
| + const base::DictionaryValue* printer_dictionary = nullptr; |
| + value->GetAsDictionary(&printer_dictionary); |
| + |
| + DCHECK(printer_dictionary); |
| + |
| + std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); |
| + if (printing::MergePrinterPreference(*printer_dictionary, printer.get())) |
| + printers.push_back(std::move(printer)); |
| + } |
| + |
| + return printers; |
| +} |
| + |
| +void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { |
| + if (printer->id().empty()) |
| + printer->set_id(base::GenerateGUID()); |
| + |
| + std::unique_ptr<base::DictionaryValue> updated_printer = |
| + base::MakeUnique<base::DictionaryValue>(); |
| + printing::PrinterToPref(*(printer.get()), updated_printer.get()); |
| + UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); |
| +} |
| + |
| +} // namespace chromeos |