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

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: ready for review 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..08669d105bc299c079b10f0e6578c96b4a4b8e17
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_pref_manager.cc
@@ -0,0 +1,113 @@
+// 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/chromeos/printing/printer_configuration.h"
+#include "chrome/browser/chromeos/printing/printer_translator.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/pref_names.h"
stevenjb 2016/07/29 16:59:26 Note: This file will need to remain in chrome/brow
skau 2016/08/01 19:37:20 Acknowledged.
+#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 {
+
+using base::DictionaryValue;
stevenjb 2016/07/29 16:56:50 nit: This is fine, but generally not necessary. ba
skau 2016/08/01 19:37:19 Done.
+
+namespace {
+
+const base::ListValue* GetPrinterList(Profile* profile) {
+ PrefService* user_state = profile->GetPrefs();
stevenjb 2016/07/29 16:56:51 nit: local not necessary: 'return profile->GetPre
skau 2016/08/01 19:37:20 Done.
+ return user_state->GetList(prefs::kPrintingDevices);
+}
+
+DictionaryValue* FindMatchingPrinter(base::ListValue* values,
stevenjb 2016/07/29 16:56:51 const base::ListValue&
skau 2016/08/01 19:37:19 Thanks... Why can I get a mutable DictionaryValue
stevenjb 2016/08/01 20:48:16 Good question, I don't know. Anyway, my apologies,
skau 2016/08/01 21:10:10 It looks like values behaves like a const collecti
+ const std::string& id) {
+ DictionaryValue* match = nullptr;
stevenjb 2016/07/29 16:56:51 Not needed, see below
skau 2016/08/01 19:37:20 Done.
+ size_t size = values->GetSize();
+ for (size_t i = 0; i < size; ++i) {
stevenjb 2016/07/29 16:56:50 for (const auto& value : values) { DictionaryVal
skau 2016/08/01 19:37:20 Thanks. I didn't notice that ListValue implemente
+ DictionaryValue* printer_dictionary;
+ if (!values->GetDictionary(i, &printer_dictionary)) {
+ continue;
+ }
+
+ std::string printer_id;
+ if (printer_dictionary->GetString(kPrinterId, &printer_id) &&
+ id == printer_id) {
+ match = printer_dictionary;
stevenjb 2016/07/29 16:56:51 return printer_dictionary;
skau 2016/08/01 19:37:20 Done.
+ break;
+ }
+ }
+
+ return match;
stevenjb 2016/07/29 16:56:50 return nullptr;
skau 2016/08/01 19:37:19 Done.
+}
+
+void UpdatePrinterPref(Profile* profile,
+ const std::string& id,
+ std::unique_ptr<DictionaryValue> printer_dictionary) {
+ ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices);
+ base::ListValue* printer_list = update.Get();
stevenjb 2016/07/29 16:56:50 DCHECK(printer_list) This documents that update.G
skau 2016/08/01 19:37:20 Done.
+ DictionaryValue* printer = FindMatchingPrinter(printer_list, id);
+ if (!printer) {
+ printer_list->Append(
+ std::unique_ptr<base::Value>(std::move(printer_dictionary)));
stevenjb 2016/07/29 16:56:51 return; no else
skau 2016/08/01 19:37:20 Done.
+ } else {
+ 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_);
+ size_t size = values->GetSize();
+ for (size_t i = 0; i < size; ++i) {
stevenjb 2016/07/29 16:56:51 for (const auto& value : *values), etc.
skau 2016/08/01 19:37:20 Done.
+ const DictionaryValue* printer_dictionary;
stevenjb 2016/07/29 16:56:50 = nullptr;
skau 2016/08/01 19:37:20 Done.
+ if (values->GetDictionary(i, &printer_dictionary)) {
stevenjb 2016/07/29 16:56:51 value->GetAsDictionary(&printer_dictionary); DCHEC
skau 2016/08/01 19:37:20 Done.
+ std::unique_ptr<Printer> printer = base::MakeUnique<Printer>();
+ if (MergePrinterPreference(*printer_dictionary, printer.get()))
+ printers.push_back(std::move(printer));
+ } else {
+ NOTREACHED();
stevenjb 2016/07/29 16:56:51 elim (use DCHECK mentioned above instead)
skau 2016/08/01 19:37:20 Done.
+ }
+ }
+
+ return printers;
+}
+
+void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) {
+ if (printer->id().empty()) {
+ printer->set_id(base::GenerateGUID());
+ }
stevenjb 2016/07/29 16:56:50 nit: no {} around single line if statement (option
skau 2016/08/01 19:37:20 Righto. I've been writing too much Java.
+
+ std::unique_ptr<base::DictionaryValue> updated_printer =
+ base::MakeUnique<DictionaryValue>();
+ 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