Chromium Code Reviews| 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 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 new_ids.push_back(id); |
| 205 recommended_printers_[id] = std::move(printer_dictionary); | 206 // Move existing printers, create othewise. |
| 207 auto old = recommended_printers_.find(id); | |
| 208 if (old != recommended_printers_.end()) { | |
|
Carlson
2017/05/08 21:01:17
Generally looks fine, but back to my original comm
skau
2017/05/08 21:19:25
We expect all printer entries to be unique within
| |
| 209 new_printers[id] = std::move(old->second); | |
| 210 } else { | |
| 211 auto printer = | |
| 212 printing::RecommendedPrinterToPrinter(*printer_dictionary, timestamp); | |
| 213 printer->set_source(Printer::SRC_POLICY); | |
| 214 | |
| 215 new_printers[id] = std::move(printer); | |
| 216 } | |
| 206 } | 217 } |
| 218 | |
| 219 // Objects not in the most recent update get deallocated after method exit. | |
| 220 recommended_printer_ids_.swap(new_ids); | |
| 221 recommended_printers_.swap(new_printers); | |
| 222 } | |
| 223 | |
| 224 void PrintersManager::PrinterInstalled(const Printer& printer) { | |
| 225 DCHECK(!printer.last_updated().is_null()); | |
| 226 installed_printer_timestamps_[printer.id()] = printer.last_updated(); | |
| 227 } | |
| 228 | |
| 229 bool PrintersManager::IsConfigurationCurrent(const Printer& printer) { | |
| 230 auto found = installed_printer_timestamps_.find(printer.id()); | |
| 231 if (found == installed_printer_timestamps_.end()) | |
| 232 return false; | |
| 233 | |
| 234 return found->second == printer.last_updated(); | |
| 207 } | 235 } |
| 208 | 236 |
| 209 } // namespace chromeos | 237 } // namespace chromeos |
| OLD | NEW |