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

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: 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..cc8cb191c89a6f5fdd976a2f87e447e661f2eff0
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_pref_manager.cc
@@ -0,0 +1,171 @@
+#include "chromeos/printing/printer_pref_handler.h"
+
+#include <memory>
+#include <vector>
+
+#include "base/values.h"
+#include "chrome/browser/printing/printer_config.h"
+#include "components/prefs/pref_registry_simple.h"
+
+namespace chromeos {
+
+namespace {
+
+// ppd fields
+const char kPPDid[] = "id";
+const char kFileName[] = "file_name";
+const char kVersionNumber[] = "version_number";
+const char kFromQuirks[] = "from_quirks_server";
+
+// printer fields
+const char kPrinterId[] = "id";
+const char kManufacturer[] = "manufacturer";
+const char kModel[] = "model";
+const char kUri[] = "uri";
+const char kPPD[] = "ppd";
+
+PPDFile ParsePPDFile(const DictionaryValue* value) {
+ PPDFile ppd;
+
+ int id = -1;
+ value->GetInteger(kPPDid, &id);
+ ppd.id = id;
+
+ std::string file_name;
+ if (value->GetString(kFileName, &file_name)) {
+ ppd.file_name = file_name;
+ }
+
+ int version_number = 0;
+ value->GetInteger(kVersionNumber, &version_number)
+ ppd.version_number = version_number;
+
+ bool from_quirks;
+ if (value->GetBoolean(kFromQuirks, &from_quirks)) {
+ ppd.from_quirks_server = from_quirks;
+ }
+
+ return ppd;
+}
+
+Printer ParsePrinter(const DictionaryValue* value) {
+ Printer printer;
+
+ std::string id;
+ if (value->GetString(kPrinterId, &id)) {
+ printer.id = id;
+ }
+
+ std::string manufacturer;
+ if (value->GetString(kManufacturer, &manufacturer)) {
+ printer.manufacturer = manufacturer;
+ }
+
+ std::string model;
+ if (value->GetString(kModel, &model)) {
+ printer.model = model;
+ }
+
+ std::string uri;
+ if (value->GetString(kUri, &uri)) {
+ printer.uri = uri;
+ }
+
+ const DictionaryValue* ppd;
+ if (value->GetDictionary(kPPD, &ppd)) {
+ printer.ppd = ParsePPDFile(ppd);
+ }
+
+ return printer;
+}
+
+void PPDFileToDictionary(const PPDFile& ppd, DictionaryValue* dictionary) {
+ dictionary->SetInteger(kPPDid, ppd.id);
+ dictionary->SetString(kFileName, ppd.file_name);
+ dictionary->SetInteger(kVersionNumber, ppd.version_number);
+ dictionary->SetBoolean(kFromQuirks, ppd.from_quirks_server);
+}
+
+void PrinterToDictionary(const Printer& printer, DictionaryValue* dictionary) {
+ dictionary->SetString(kPrinterId, printer.id);
+ dictionary->SetString(kManufacturer, printer.manufacturer);
+ dictionary->SetString(kModel, printer.model);
+ dictionary->SetString(kUri, printer.uri);
+
+ std::unique_ptr<DictionaryValue> ppd = base::MakeUnique<DictionaryValue>();
+ PPDFileToDictionary(printer.ppd, ppd.get());
+ dictionary->Set(kPPD, ppd);
+}
+
+const base::ListValue* GetPrinterList(Profile* profile) {
+ PrefService* user_state = profile->GetPrefs();
+ return user_state->GetList(prefs::kPrintingDevices);
+}
+
+base::DictionaryValue* FindMatchingPrinter(const base::ListValue* values, const std::string& id) {
+ DictionaryValue* match = nullptr;
+ size_t size = values->GetSize();
+ for (size_t i=0; i<size; ++i) {
+ 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;
+ break;
+ }
+ }
+
+ return match;
+}
+
+} // anonymous namespace
+
+// static
+void PrinterPrefManager::RegisterProfilePrefs(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>> GetPrinters() const {
+ std::vector<std::unique_ptr<Printer>> printers;
+
+ const base::ListValue* values = GetPrinteList(profile_);
+ size_t size = values->GetSize();
+ for (size_t i=0; i<size; ++i) {
+ const DictionaryValue* printer_dictionary;
+ if(values->GetDictionary(i, &printer_dictionary)) {
+ printers.push_back(ParsePrinter(printer_dictionary));
+ } else {
+ NOTREACHED();
+ }
+ }
+
+ return printers;
+}
+
+void RegisterPrinter(const Printer& printer) {
+ base::DictionaryValue* found_printer = nullptr;
+ std::unique_ptr<base::DictionaryValue> updated_printer = std::MakeUnique<DictionaryValue>();
+ const base::ListValue* printer_list = GetPrinterList(profile_);
+
+ if (printer.id.empty()) {
+ printer.id = base::GenerateGUID();
+ } else {
+ found_printer = FindMatchingPrinter(printer_list, printer.id);
+ }
+
+ PrinterToDictionary(printer, updated_printer.get());
+ if (found_printer) {
+ found_printer->MergeDictionary(updated_printer.get());
+ } else {
+ printer_list->Append(updated_printer);
+ }
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698