Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4961)

Unified Diff: chrome/browser/chromeos/printing/printer_pref_manager.cc

Issue 2161933003: Define printer configuration objects for Chrome OS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..c59f00a8f7e03ff9c98f83c1c7cae519018e42b7
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_pref_manager.cc
@@ -0,0 +1,108 @@
+// 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);
+}
+
+// Returns the printer with the matching |id| from the list of |values|. The
+// returned value is mutable and |values| could be modified.
+base::DictionaryValue* FindPrinterPref(const base::ListValue& values,
stevenjb 2016/08/01 21:19:14 Since we are returning a modifiable pointer to a m
skau 2016/08/01 22:17:00 Done.
+ const std::string& id) {
+ for (const auto& value : values) {
+ 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 = FindPrinterPref(*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

Powered by Google App Engine
This is Rietveld 408576698