OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/printing/synced_printers_manager.h" | 5 #include "chrome/browser/chromeos/printing/synced_printers_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Adds |printer| with |id| to prefs. Returns true if the printer is new, | 31 // Adds |printer| with |id| to prefs. Returns true if the printer is new, |
32 // false for an update. | 32 // false for an update. |
33 bool UpdatePrinterPref(PrintersSyncBridge* sync_bridge, | 33 bool UpdatePrinterPref(PrintersSyncBridge* sync_bridge, |
34 const std::string& id, | 34 const std::string& id, |
35 const Printer& printer) { | 35 const Printer& printer) { |
36 base::Optional<sync_pb::PrinterSpecifics> specifics = | 36 base::Optional<sync_pb::PrinterSpecifics> specifics = |
37 sync_bridge->GetPrinter(id); | 37 sync_bridge->GetPrinter(id); |
38 if (!specifics.has_value()) { | 38 if (!specifics.has_value()) { |
39 sync_bridge->AddPrinter(printing::PrinterToSpecifics(printer)); | 39 sync_bridge->AddPrinter(PrinterToSpecifics(printer)); |
40 return true; | 40 return true; |
41 } | 41 } |
42 | 42 |
43 // Preserve fields in the proto which we don't understand. | 43 // Preserve fields in the proto which we don't understand. |
44 std::unique_ptr<sync_pb::PrinterSpecifics> updated_printer = | 44 std::unique_ptr<sync_pb::PrinterSpecifics> updated_printer = |
45 base::MakeUnique<sync_pb::PrinterSpecifics>(*specifics); | 45 base::MakeUnique<sync_pb::PrinterSpecifics>(*specifics); |
46 printing::MergePrinterToSpecifics(printer, updated_printer.get()); | 46 MergePrinterToSpecifics(printer, updated_printer.get()); |
47 sync_bridge->AddPrinter(std::move(updated_printer)); | 47 sync_bridge->AddPrinter(std::move(updated_printer)); |
48 | 48 |
49 return false; | 49 return false; |
50 } | 50 } |
51 | 51 |
52 } // anonymous namespace | 52 } // anonymous namespace |
53 | 53 |
54 SyncedPrintersManager::SyncedPrintersManager( | 54 SyncedPrintersManager::SyncedPrintersManager( |
55 Profile* profile, | 55 Profile* profile, |
56 std::unique_ptr<PrintersSyncBridge> sync_bridge) | 56 std::unique_ptr<PrintersSyncBridge> sync_bridge) |
(...skipping 16 matching lines...) Expand all Loading... |
73 registry->RegisterListPref(prefs::kRecommendedNativePrinters); | 73 registry->RegisterListPref(prefs::kRecommendedNativePrinters); |
74 } | 74 } |
75 | 75 |
76 std::vector<std::unique_ptr<Printer>> SyncedPrintersManager::GetPrinters() | 76 std::vector<std::unique_ptr<Printer>> SyncedPrintersManager::GetPrinters() |
77 const { | 77 const { |
78 std::vector<std::unique_ptr<Printer>> printers; | 78 std::vector<std::unique_ptr<Printer>> printers; |
79 | 79 |
80 std::vector<sync_pb::PrinterSpecifics> values = | 80 std::vector<sync_pb::PrinterSpecifics> values = |
81 sync_bridge_->GetAllPrinters(); | 81 sync_bridge_->GetAllPrinters(); |
82 for (const auto& value : values) { | 82 for (const auto& value : values) { |
83 printers.push_back(printing::SpecificsToPrinter(value)); | 83 printers.push_back(SpecificsToPrinter(value)); |
84 } | 84 } |
85 | 85 |
86 return printers; | 86 return printers; |
87 } | 87 } |
88 | 88 |
89 std::vector<std::unique_ptr<Printer>> | 89 std::vector<std::unique_ptr<Printer>> |
90 SyncedPrintersManager::GetRecommendedPrinters() const { | 90 SyncedPrintersManager::GetRecommendedPrinters() const { |
91 std::vector<std::unique_ptr<Printer>> printers; | 91 std::vector<std::unique_ptr<Printer>> printers; |
92 | 92 |
93 for (const std::string& key : recommended_printer_ids_) { | 93 for (const std::string& key : recommended_printer_ids_) { |
(...skipping 11 matching lines...) Expand all Loading... |
105 // check for a policy printer first | 105 // check for a policy printer first |
106 const auto& policy_printers = recommended_printers_; | 106 const auto& policy_printers = recommended_printers_; |
107 auto found = policy_printers.find(printer_id); | 107 auto found = policy_printers.find(printer_id); |
108 if (found != policy_printers.end()) { | 108 if (found != policy_printers.end()) { |
109 // Copy a printer. | 109 // Copy a printer. |
110 return base::MakeUnique<Printer>(*(found->second)); | 110 return base::MakeUnique<Printer>(*(found->second)); |
111 } | 111 } |
112 | 112 |
113 base::Optional<sync_pb::PrinterSpecifics> printer = | 113 base::Optional<sync_pb::PrinterSpecifics> printer = |
114 sync_bridge_->GetPrinter(printer_id); | 114 sync_bridge_->GetPrinter(printer_id); |
115 return printer.has_value() ? printing::SpecificsToPrinter(*printer) : nullptr; | 115 return printer.has_value() ? SpecificsToPrinter(*printer) : nullptr; |
116 } | 116 } |
117 | 117 |
118 void SyncedPrintersManager::RegisterPrinter(std::unique_ptr<Printer> printer) { | 118 void SyncedPrintersManager::RegisterPrinter(std::unique_ptr<Printer> printer) { |
119 if (printer->id().empty()) { | 119 if (printer->id().empty()) { |
120 printer->set_id(base::GenerateGUID()); | 120 printer->set_id(base::GenerateGUID()); |
121 } | 121 } |
122 | 122 |
123 DCHECK_EQ(Printer::SRC_USER_PREFS, printer->source()); | 123 DCHECK_EQ(Printer::SRC_USER_PREFS, printer->source()); |
124 bool new_printer = | 124 bool new_printer = |
125 UpdatePrinterPref(sync_bridge_.get(), printer->id(), *printer); | 125 UpdatePrinterPref(sync_bridge_.get(), printer->id(), *printer); |
126 | 126 |
127 if (new_printer) { | 127 if (new_printer) { |
128 for (Observer& obs : observers_) { | 128 for (Observer& obs : observers_) { |
129 obs.OnPrinterAdded(*printer); | 129 obs.OnPrinterAdded(*printer); |
130 } | 130 } |
131 } else { | 131 } else { |
132 for (Observer& obs : observers_) { | 132 for (Observer& obs : observers_) { |
133 obs.OnPrinterUpdated(*printer); | 133 obs.OnPrinterUpdated(*printer); |
134 } | 134 } |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 bool SyncedPrintersManager::RemovePrinter(const std::string& printer_id) { | 138 bool SyncedPrintersManager::RemovePrinter(const std::string& printer_id) { |
139 DCHECK(!printer_id.empty()); | 139 DCHECK(!printer_id.empty()); |
140 | 140 |
141 base::Optional<sync_pb::PrinterSpecifics> printer = | 141 base::Optional<sync_pb::PrinterSpecifics> printer = |
142 sync_bridge_->GetPrinter(printer_id); | 142 sync_bridge_->GetPrinter(printer_id); |
143 bool success = false; | 143 bool success = false; |
144 if (printer.has_value()) { | 144 if (printer.has_value()) { |
145 std::unique_ptr<Printer> p = printing::SpecificsToPrinter(*printer); | 145 std::unique_ptr<Printer> p = SpecificsToPrinter(*printer); |
146 success = sync_bridge_->RemovePrinter(p->id()); | 146 success = sync_bridge_->RemovePrinter(p->id()); |
147 if (success) { | 147 if (success) { |
148 for (Observer& obs : observers_) { | 148 for (Observer& obs : observers_) { |
149 obs.OnPrinterRemoved(*p); | 149 obs.OnPrinterRemoved(*p); |
150 } | 150 } |
151 } | 151 } |
152 } else { | 152 } else { |
153 LOG(WARNING) << "Could not find printer" << printer_id; | 153 LOG(WARNING) << "Could not find printer" << printer_id; |
154 } | 154 } |
155 | 155 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 if (!printer_dictionary) { | 194 if (!printer_dictionary) { |
195 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: " | 195 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: " |
196 << printer_json; | 196 << printer_json; |
197 continue; | 197 continue; |
198 } | 198 } |
199 | 199 |
200 // Policy printers don't have id's but the ids only need to be locally | 200 // Policy printers don't have id's but the ids only need to be locally |
201 // unique so we'll hash the record. This will not collide with the UUIDs | 201 // unique so we'll hash the record. This will not collide with the UUIDs |
202 // generated for user entries. | 202 // generated for user entries. |
203 std::string id = base::MD5String(printer_json); | 203 std::string id = base::MD5String(printer_json); |
204 printer_dictionary->SetString(printing::kPrinterId, id); | 204 printer_dictionary->SetString(kPrinterId, id); |
205 | 205 |
206 if (base::ContainsKey(new_printers, id)) { | 206 if (base::ContainsKey(new_printers, id)) { |
207 // Skip duplicated entries. | 207 // Skip duplicated entries. |
208 LOG(WARNING) << "Duplicate printer ignored."; | 208 LOG(WARNING) << "Duplicate printer ignored."; |
209 continue; | 209 continue; |
210 } | 210 } |
211 | 211 |
212 new_ids.push_back(id); | 212 new_ids.push_back(id); |
213 // Move existing printers, create othewise. | 213 // Move existing printers, create othewise. |
214 auto old = recommended_printers_.find(id); | 214 auto old = recommended_printers_.find(id); |
215 if (old != recommended_printers_.end()) { | 215 if (old != recommended_printers_.end()) { |
216 new_printers[id] = std::move(old->second); | 216 new_printers[id] = std::move(old->second); |
217 } else { | 217 } else { |
218 auto printer = | 218 auto printer = |
219 printing::RecommendedPrinterToPrinter(*printer_dictionary, timestamp); | 219 RecommendedPrinterToPrinter(*printer_dictionary, timestamp); |
220 printer->set_source(Printer::SRC_POLICY); | 220 printer->set_source(Printer::SRC_POLICY); |
221 | 221 |
222 new_printers[id] = std::move(printer); | 222 new_printers[id] = std::move(printer); |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 // Objects not in the most recent update get deallocated after method exit. | 226 // Objects not in the most recent update get deallocated after method exit. |
227 recommended_printer_ids_.swap(new_ids); | 227 recommended_printer_ids_.swap(new_ids); |
228 recommended_printers_.swap(new_printers); | 228 recommended_printers_.swap(new_printers); |
229 } | 229 } |
230 | 230 |
231 void SyncedPrintersManager::PrinterInstalled(const Printer& printer) { | 231 void SyncedPrintersManager::PrinterInstalled(const Printer& printer) { |
232 DCHECK(!printer.last_updated().is_null()); | 232 DCHECK(!printer.last_updated().is_null()); |
233 installed_printer_timestamps_[printer.id()] = printer.last_updated(); | 233 installed_printer_timestamps_[printer.id()] = printer.last_updated(); |
234 } | 234 } |
235 | 235 |
236 bool SyncedPrintersManager::IsConfigurationCurrent( | 236 bool SyncedPrintersManager::IsConfigurationCurrent( |
237 const Printer& printer) const { | 237 const Printer& printer) const { |
238 auto found = installed_printer_timestamps_.find(printer.id()); | 238 auto found = installed_printer_timestamps_.find(printer.id()); |
239 if (found == installed_printer_timestamps_.end()) | 239 if (found == installed_printer_timestamps_.end()) |
240 return false; | 240 return false; |
241 | 241 |
242 return found->second == printer.last_updated(); | 242 return found->second == printer.last_updated(); |
243 } | 243 } |
244 | 244 |
245 } // namespace chromeos | 245 } // namespace chromeos |
OLD | NEW |