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

Side by Side Diff: chrome/browser/chromeos/printing/printers_manager.cc

Issue 2858353004: Track printer installations for each configuration. (Closed)
Patch Set: address comments Created 3 years, 7 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
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/printers_manager.h" 5 #include "chrome/browser/chromeos/printing/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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 } 83 }
84 84
85 return printers; 85 return printers;
86 } 86 }
87 87
88 std::vector<std::unique_ptr<Printer>> PrintersManager::GetRecommendedPrinters() 88 std::vector<std::unique_ptr<Printer>> PrintersManager::GetRecommendedPrinters()
89 const { 89 const {
90 std::vector<std::unique_ptr<Printer>> printers; 90 std::vector<std::unique_ptr<Printer>> printers;
91 91
92 for (const std::string& key : recommended_printer_ids_) { 92 for (const std::string& key : recommended_printer_ids_) {
93 const base::DictionaryValue& printer_dictionary = 93 auto printer = recommended_printers_.find(key);
94 *(recommended_printers_.at(key)); 94 if (printer != recommended_printers_.end()) {
95 std::unique_ptr<Printer> printer = 95 printers.push_back(base::MakeUnique<Printer>(*printer->second));
96 printing::RecommendedPrinterToPrinter(printer_dictionary);
97 if (printer) {
98 printer->set_source(Printer::SRC_POLICY);
99 printers.push_back(std::move(printer));
100 } 96 }
101 } 97 }
102 98
103 return printers; 99 return printers;
104 } 100 }
105 101
106 std::unique_ptr<Printer> PrintersManager::GetPrinter( 102 std::unique_ptr<Printer> PrintersManager::GetPrinter(
107 const std::string& printer_id) const { 103 const std::string& printer_id) const {
108 // check for a policy printer first 104 // check for a policy printer first
109 const auto& policy_printers = recommended_printers_; 105 const auto& policy_printers = recommended_printers_;
110 auto found = policy_printers.find(printer_id); 106 auto found = policy_printers.find(printer_id);
111 if (found != policy_printers.end()) 107 if (found != policy_printers.end()) {
112 return printing::RecommendedPrinterToPrinter(*(found->second)); 108 // Copy a printer.
109 return base::MakeUnique<Printer>(*(found->second));
110 }
113 111
114 base::Optional<sync_pb::PrinterSpecifics> printer = 112 base::Optional<sync_pb::PrinterSpecifics> printer =
115 sync_bridge_->GetPrinter(printer_id); 113 sync_bridge_->GetPrinter(printer_id);
116 return printer.has_value() ? printing::SpecificsToPrinter(*printer) : nullptr; 114 return printer.has_value() ? printing::SpecificsToPrinter(*printer) : nullptr;
117 } 115 }
118 116
119 void PrintersManager::RegisterPrinter(std::unique_ptr<Printer> printer) { 117 void PrintersManager::RegisterPrinter(std::unique_ptr<Printer> printer) {
120 if (printer->id().empty()) { 118 if (printer->id().empty()) {
121 printer->set_id(base::GenerateGUID()); 119 printer->set_id(base::GenerateGUID());
122 } 120 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 return sync_bridge_.get(); 167 return sync_bridge_.get();
170 } 168 }
171 169
172 // This method is not thread safe and could interact poorly with readers of 170 // This method is not thread safe and could interact poorly with readers of
173 // |recommended_printers_|. 171 // |recommended_printers_|.
174 void PrintersManager::UpdateRecommendedPrinters() { 172 void PrintersManager::UpdateRecommendedPrinters() {
175 const PrefService* prefs = profile_->GetPrefs(); 173 const PrefService* prefs = profile_->GetPrefs();
176 174
177 const base::ListValue* values = 175 const base::ListValue* values =
178 prefs->GetList(prefs::kRecommendedNativePrinters); 176 prefs->GetList(prefs::kRecommendedNativePrinters);
177 const base::Time timestamp = base::Time::Now();
179 178
180 recommended_printer_ids_.clear(); 179 // Parse the policy JSON into new structures.
180 std::vector<std::string> new_ids;
181 std::map<std::string, std::unique_ptr<Printer>> new_printers;
181 for (const auto& value : *values) { 182 for (const auto& value : *values) {
182 std::string printer_json; 183 std::string printer_json;
183 if (!value.GetAsString(&printer_json)) { 184 if (!value.GetAsString(&printer_json)) {
184 NOTREACHED(); 185 NOTREACHED();
185 continue; 186 continue;
186 } 187 }
187 188
188 std::unique_ptr<base::DictionaryValue> printer_dictionary = 189 std::unique_ptr<base::DictionaryValue> printer_dictionary =
189 base::DictionaryValue::From(base::JSONReader::Read( 190 base::DictionaryValue::From(base::JSONReader::Read(
190 printer_json, base::JSON_ALLOW_TRAILING_COMMAS)); 191 printer_json, base::JSON_ALLOW_TRAILING_COMMAS));
191 192
192 if (!printer_dictionary) { 193 if (!printer_dictionary) {
193 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: " 194 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: "
194 << printer_json; 195 << printer_json;
195 continue; 196 continue;
196 } 197 }
197 198
198 // Policy printers don't have id's but the ids only need to be locally 199 // Policy printers don't have id's but the ids only need to be locally
199 // unique so we'll hash the record. This will not collide with the UUIDs 200 // unique so we'll hash the record. This will not collide with the UUIDs
200 // generated for user entries. 201 // generated for user entries.
201 std::string id = base::MD5String(printer_json); 202 std::string id = base::MD5String(printer_json);
202 printer_dictionary->SetString(printing::kPrinterId, id); 203 printer_dictionary->SetString(printing::kPrinterId, id);
203 204
204 recommended_printer_ids_.push_back(id); 205 if (base::ContainsKey(new_printers, id)) {
205 recommended_printers_[id] = std::move(printer_dictionary); 206 // Skip duplicated entries.
207 LOG(WARNING) << "Duplicate printer ignored.";
208 continue;
209 }
210
211 new_ids.push_back(id);
212 // Move existing printers, create othewise.
213 auto old = recommended_printers_.find(id);
214 if (old != recommended_printers_.end()) {
215 new_printers[id] = std::move(old->second);
216 } else {
217 auto printer =
218 printing::RecommendedPrinterToPrinter(*printer_dictionary, timestamp);
219 printer->set_source(Printer::SRC_POLICY);
220
221 new_printers[id] = std::move(printer);
222 }
206 } 223 }
224
225 // Objects not in the most recent update get deallocated after method exit.
226 recommended_printer_ids_.swap(new_ids);
227 recommended_printers_.swap(new_printers);
228 }
229
230 void PrintersManager::PrinterInstalled(const Printer& printer) {
231 DCHECK(!printer.last_updated().is_null());
232 installed_printer_timestamps_[printer.id()] = printer.last_updated();
233 }
234
235 bool PrintersManager::IsConfigurationCurrent(const Printer& printer) const {
236 auto found = installed_printer_timestamps_.find(printer.id());
237 if (found == installed_printer_timestamps_.end())
238 return false;
239
240 return found->second == printer.last_updated();
207 } 241 }
208 242
209 } // namespace chromeos 243 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698