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

Side by Side 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, 4 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 unified diff | Download patch
OLDNEW
(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/chromeos/printing/printer_configuration.h"
16 #include "chrome/browser/chromeos/printing/printer_translator.h"
17 #include "chrome/browser/profiles/profile.h"
18 #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.
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 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.
27
28 namespace {
29
30 const base::ListValue* GetPrinterList(Profile* profile) {
31 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.
32 return user_state->GetList(prefs::kPrintingDevices);
33 }
34
35 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
36 const std::string& id) {
37 DictionaryValue* match = nullptr;
stevenjb 2016/07/29 16:56:51 Not needed, see below
skau 2016/08/01 19:37:20 Done.
38 size_t size = values->GetSize();
39 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
40 DictionaryValue* printer_dictionary;
41 if (!values->GetDictionary(i, &printer_dictionary)) {
42 continue;
43 }
44
45 std::string printer_id;
46 if (printer_dictionary->GetString(kPrinterId, &printer_id) &&
47 id == printer_id) {
48 match = printer_dictionary;
stevenjb 2016/07/29 16:56:51 return printer_dictionary;
skau 2016/08/01 19:37:20 Done.
49 break;
50 }
51 }
52
53 return match;
stevenjb 2016/07/29 16:56:50 return nullptr;
skau 2016/08/01 19:37:19 Done.
54 }
55
56 void UpdatePrinterPref(Profile* profile,
57 const std::string& id,
58 std::unique_ptr<DictionaryValue> printer_dictionary) {
59 ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices);
60 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.
61 DictionaryValue* printer = FindMatchingPrinter(printer_list, id);
62 if (!printer) {
63 printer_list->Append(
64 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.
65 } else {
66 printer->MergeDictionary(printer_dictionary.get());
67 }
68 }
69
70 } // anonymous namespace
71
72 // static
73 void PrinterPrefManager::RegisterProfilePrefs(
74 user_prefs::PrefRegistrySyncable* registry) {
75 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when
76 // sync is implemented.
77 registry->RegisterListPref(prefs::kPrintingDevices,
78 PrefRegistry::NO_REGISTRATION_FLAGS);
79 }
80
81 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {}
82
83 std::vector<std::unique_ptr<Printer>> PrinterPrefManager::GetPrinters() const {
84 std::vector<std::unique_ptr<Printer>> printers;
85
86 const base::ListValue* values = GetPrinterList(profile_);
87 size_t size = values->GetSize();
88 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.
89 const DictionaryValue* printer_dictionary;
stevenjb 2016/07/29 16:56:50 = nullptr;
skau 2016/08/01 19:37:20 Done.
90 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.
91 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>();
92 if (MergePrinterPreference(*printer_dictionary, printer.get()))
93 printers.push_back(std::move(printer));
94 } else {
95 NOTREACHED();
stevenjb 2016/07/29 16:56:51 elim (use DCHECK mentioned above instead)
skau 2016/08/01 19:37:20 Done.
96 }
97 }
98
99 return printers;
100 }
101
102 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) {
103 if (printer->id().empty()) {
104 printer->set_id(base::GenerateGUID());
105 }
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.
106
107 std::unique_ptr<base::DictionaryValue> updated_printer =
108 base::MakeUnique<DictionaryValue>();
109 PrinterToPref(*(printer.get()), updated_printer.get());
110 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer));
111 }
112
113 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698