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 base::DictionaryValue* FindMatchingPrinter(const base::ListValue& values, | |
33 const std::string& id) { | |
34 for (const auto& value : values) { | |
stevenjb
2016/08/01 20:48:16
This should still work with *values and without th
skau
2016/08/01 21:10:10
See comment on earlier revision. This works as we
| |
35 base::DictionaryValue* printer_dictionary; | |
36 if (!value->GetAsDictionary(&printer_dictionary)) | |
37 continue; | |
38 | |
39 std::string printer_id; | |
40 if (printer_dictionary->GetString(printing::kPrinterId, &printer_id) && | |
41 id == printer_id) | |
42 return printer_dictionary; | |
43 } | |
44 | |
45 return nullptr; | |
46 } | |
47 | |
48 void UpdatePrinterPref( | |
49 Profile* profile, | |
50 const std::string& id, | |
51 std::unique_ptr<base::DictionaryValue> printer_dictionary) { | |
52 ListPrefUpdate update(profile->GetPrefs(), prefs::kPrintingDevices); | |
53 base::ListValue* printer_list = update.Get(); | |
54 DCHECK(printer_list) << "Register the printer preference"; | |
55 base::DictionaryValue* printer = FindMatchingPrinter(*printer_list, id); | |
56 if (!printer) { | |
57 printer_list->Append( | |
58 std::unique_ptr<base::Value>(std::move(printer_dictionary))); | |
59 return; | |
60 } | |
61 | |
62 printer->MergeDictionary(printer_dictionary.get()); | |
63 } | |
64 | |
65 } // anonymous namespace | |
66 | |
67 // static | |
68 void PrinterPrefManager::RegisterProfilePrefs( | |
69 user_prefs::PrefRegistrySyncable* registry) { | |
70 // TODO(skau): Change to user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) when | |
71 // sync is implemented. | |
72 registry->RegisterListPref(prefs::kPrintingDevices, | |
73 PrefRegistry::NO_REGISTRATION_FLAGS); | |
74 } | |
75 | |
76 PrinterPrefManager::PrinterPrefManager(Profile* profile) : profile_(profile) {} | |
77 | |
78 std::vector<std::unique_ptr<Printer>> PrinterPrefManager::GetPrinters() const { | |
79 std::vector<std::unique_ptr<Printer>> printers; | |
80 | |
81 const base::ListValue* values = GetPrinterList(profile_); | |
82 for (const auto& value : *values) { | |
83 const base::DictionaryValue* printer_dictionary = nullptr; | |
84 value->GetAsDictionary(&printer_dictionary); | |
85 | |
86 DCHECK(printer_dictionary); | |
87 | |
88 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(); | |
89 if (printing::MergePrinterPreference(*printer_dictionary, printer.get())) | |
90 printers.push_back(std::move(printer)); | |
91 } | |
92 | |
93 return printers; | |
94 } | |
95 | |
96 void PrinterPrefManager::RegisterPrinter(std::unique_ptr<Printer> printer) { | |
97 if (printer->id().empty()) | |
98 printer->set_id(base::GenerateGUID()); | |
99 | |
100 std::unique_ptr<base::DictionaryValue> updated_printer = | |
101 base::MakeUnique<base::DictionaryValue>(); | |
102 printing::PrinterToPref(*(printer.get()), updated_printer.get()); | |
103 UpdatePrinterPref(profile_, printer->id(), std::move(updated_printer)); | |
104 } | |
105 | |
106 } // namespace chromeos | |
OLD | NEW |