OLD | NEW |
---|---|
(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 of |values|. The | |
33 // returned value is mutable and |values| could be modified. | |
34 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.
| |
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))); | |
61 return; | |
62 } | |
63 | |
64 printer->MergeDictionary(printer_dictionary.get()); | |
65 } | |
66 | |
67 } // anonymous namespace | |
68 | |
69 // static | |
70 void PrinterPrefManager::RegisterProfilePrefs( | |
71 user_prefs::PrefRegistrySyncable* registry) { | |
72 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when | |
73 // sync is implemented. | |
74 registry->RegisterListPref(prefs::kPrintingDevices, | |
75 PrefRegistry::NO_REGISTRATION_FLAGS); | |
76 } | |
77 | |
78 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {} | |
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 = base::MakeUnique<Printer>(); | |
91 if (printing::MergePrinterPreference(*printer_dictionary, printer.get())) | |
92 printers.push_back(std::move(printer)); | |
93 } | |
94 | |
95 return printers; | |
96 } | |
97 | |
98 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { | |
99 if (printer->id().empty()) | |
100 printer->set_id(base::GenerateGUID()); | |
101 | |
102 std::unique_ptr<base::DictionaryValue> updated_printer = | |
103 base::MakeUnique<base::DictionaryValue>(); | |
104 printing::PrinterToPref(*(printer.get()), updated_printer.get()); | |
105 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); | |
106 } | |
107 | |
108 } // namespace chromeos | |
OLD | NEW |