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

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: lint 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/profiles/profile.h"
16 #include "chrome/common/pref_names.h"
17 #include "chromeos/printing/printer_configuration.h"
18 #include "chromeos/printing/printer_translator.h"
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 namespace {
27
28 const base::ListValue* GetPrinterList(Profile* profile) {
29 return profile->GetPrefs()->GetList(prefs::kPrintingDevices);
30 }
31
32 // Returns the printer with the matching |id| from the list |values|. The
33 // returned value is mutable and |values| could be modified.
34 base::DictionaryValue* FindPrinterPref(base::ListValue* values,
35 const std::string& id) {
36 for (const auto& value : *values) {
37 base::DictionaryValue* printer_dictionary;
38 if (!value->GetAsDictionary(&printer_dictionary))
39 continue;
40
41 std::string printer_id;
42 if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) &&
43 id == printer_id)
44 return printer_dictionary;
45 }
46
47 return nullptr;
48 }
49
50 void UpdatePrinterPref(
51 Profile* profile,
52 const std::string& id,
53 std::unique_ptr<base::DictionaryValue> printer_dictionary) {
54 ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices);
55 base::ListValue* printer_list = update.Get();
56 DCHECK(printer_list) << "Register the printer preference";
57 base::DictionaryValue* printer = FindPrinterPref(printer_list, id);
58 if (!printer) {
59 printer_list->Append(
60 std::unique_ptr<base::Value>(std::move(printer_dictionary)));
Lei Zhang 2016/08/02 20:52:41 Do you need the "std::unique_ptr<base::Value>(" bi
skau 2016/08/02 21:16:00 Done.
61 return;
62 }
63
64 printer->MergeDictionary(printer_dictionary.get());
65 }
66
67 } // anonymous namespace
68
69 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {}
70
71 // static
72 void PrinterPrefManager::RegisterProfilePrefs(
73 user_prefs::PrefRegistrySyncable* registry) {
74 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when
75 // sync is implemented.
76 registry->RegisterListPref(prefs::kPrintingDevices,
77 PrefRegistry::NO_REGISTRATION_FLAGS);
78 }
79
80 std::vector<std::unique_ptr<Printer>> PrinterPrefManager::GetPrinters() const {
81 std::vector<std::unique_ptr<Printer>> printers;
82
83 const base::ListValue* values = GetPrinterList(profile_);
84 for (const auto& value : *values) {
85 const base::DictionaryValue* printer_dictionary = nullptr;
86 value->GetAsDictionary(&printer_dictionary);
87
88 DCHECK(printer_dictionary);
89
90 std::unique_ptr<Printer> printer =
91 printing::PrefToPrinter(*printer_dictionary);
92 if (printer)
93 printers.push_back(std::move(printer));
94 }
95
96 return printers;
97 }
98
99 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) {
100 if (printer->id().empty())
101 printer->set_id(base::GenerateGUID());
102
103 std::unique_ptr<base::DictionaryValue> updated_printer =
104 printing::PrinterToPref(*(printer.get()));
105 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer));
106 }
107
108 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698