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/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 Loading... | |
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 Loading... | |
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 int64_t timestamp = base::Time::Now().ToJavaTime(); | |
179 | 178 |
180 recommended_printer_ids_.clear(); | 179 recommended_printer_ids_.clear(); |
181 for (const auto& value : *values) { | 180 for (const auto& value : *values) { |
182 std::string printer_json; | 181 std::string printer_json; |
183 if (!value.GetAsString(&printer_json)) { | 182 if (!value.GetAsString(&printer_json)) { |
184 NOTREACHED(); | 183 NOTREACHED(); |
185 continue; | 184 continue; |
186 } | 185 } |
187 | 186 |
188 std::unique_ptr<base::DictionaryValue> printer_dictionary = | 187 std::unique_ptr<base::DictionaryValue> printer_dictionary = |
189 base::DictionaryValue::From(base::JSONReader::Read( | 188 base::DictionaryValue::From(base::JSONReader::Read( |
190 printer_json, base::JSON_ALLOW_TRAILING_COMMAS)); | 189 printer_json, base::JSON_ALLOW_TRAILING_COMMAS)); |
191 | 190 |
192 if (!printer_dictionary) { | 191 if (!printer_dictionary) { |
193 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: " | 192 LOG(WARNING) << "Ignoring invalid printer. Invalid JSON object: " |
194 << printer_json; | 193 << printer_json; |
195 continue; | 194 continue; |
196 } | 195 } |
197 | 196 |
198 // Policy printers don't have id's but the ids only need to be locally | 197 // 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 | 198 // unique so we'll hash the record. This will not collide with the UUIDs |
200 // generated for user entries. | 199 // generated for user entries. |
201 std::string id = base::MD5String(printer_json); | 200 std::string id = base::MD5String(printer_json); |
202 printer_dictionary->SetString(printing::kPrinterId, id); | 201 printer_dictionary->SetString(printing::kPrinterId, id); |
203 | 202 |
204 recommended_printer_ids_.push_back(id); | 203 recommended_printer_ids_.push_back(id); |
Carlson
2017/05/05 23:59:16
Does this mean we're happy to push the same printe
skau
2017/05/08 18:38:02
It's cleared on line 179. We save the ids to keep
Carlson
2017/05/08 18:49:25
I'm still a little confused by this, sorry.
In th
| |
205 recommended_printers_[id] = std::move(printer_dictionary); | 204 if (!base::ContainsKey(recommended_printers_, id)) { |
205 auto printer = | |
206 printing::RecommendedPrinterToPrinter(*printer_dictionary, timestamp); | |
207 printer->set_source(Printer::SRC_POLICY); | |
208 | |
209 recommended_printers_[id] = std::move(printer); | |
210 } | |
206 } | 211 } |
207 } | 212 } |
208 | 213 |
214 void PrintersManager::PrinterInstalled(const Printer& printer) { | |
215 DCHECK_GT(printer.last_updated(), 0); | |
216 installed_printer_timestamps_[printer.id()] = printer.last_updated(); | |
217 } | |
218 | |
219 bool PrintersManager::IsConfigurationCurrent(const Printer& printer) { | |
220 auto found = installed_printer_timestamps_.find(printer.id()); | |
221 if (found == installed_printer_timestamps_.end()) | |
222 return false; | |
223 | |
224 return found->second == printer.last_updated(); | |
225 } | |
226 | |
209 } // namespace chromeos | 227 } // namespace chromeos |
OLD | NEW |