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/ui/webui/settings/chromeos/cups_printers_handler.h" | 5 #include "chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 CHECK(printer_dict->GetString("printerAddress", &printer_address)); | 201 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| 202 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); | 202 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| 203 // printerQueue might be null for a printer whose protocol is not 'LPD'. | 203 // printerQueue might be null for a printer whose protocol is not 'LPD'. |
| 204 printer_dict->GetString("printerQueue", &printer_queue); | 204 printer_dict->GetString("printerQueue", &printer_queue); |
| 205 // printerPPDPath might be null for an auto-discovered printer. | 205 // printerPPDPath might be null for an auto-discovered printer. |
| 206 printer_dict->GetString("printerPPDPath", &printer_ppd_path); | 206 printer_dict->GetString("printerPPDPath", &printer_ppd_path); |
| 207 std::string printer_uri = | 207 std::string printer_uri = |
| 208 printer_protocol + "://" + printer_address + "/" + printer_queue; | 208 printer_protocol + "://" + printer_address + "/" + printer_queue; |
| 209 | 209 |
| 210 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 210 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
| 211 printer_id = printer->id(); | |
| 212 printer->set_display_name(printer_name); | 211 printer->set_display_name(printer_name); |
| 213 printer->set_description(printer_description); | 212 printer->set_description(printer_description); |
| 214 printer->set_manufacturer(printer_manufacturer); | 213 printer->set_manufacturer(printer_manufacturer); |
| 215 printer->set_model(printer_model); | 214 printer->set_model(printer_model); |
| 216 printer->set_uri(printer_uri); | 215 printer->set_uri(printer_uri); |
| 217 if (!printer_ppd_path.empty()) { | 216 if (!printer_ppd_path.empty()) { |
| 218 printer->mutable_ppd_reference()->user_supplied_ppd_url = printer_ppd_path; | 217 printer->mutable_ppd_reference()->user_supplied_ppd_url = printer_ppd_path; |
| 218 bool cached = ppd_provider_->CachePpd(printer->ppd_reference(), | |
| 219 base::FilePath(printer_ppd_path)); | |
| 220 VLOG(1) << "PPD Cached: " << cached; | |
|
dpapad
2016/12/05 23:09:12
Is this VLOG useful enough to be checked in?
skau
2016/12/06 20:55:46
This actually needs to be handled as an error. I'
| |
| 219 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { | 221 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { |
| 220 Printer::PpdReference* ppd = printer->mutable_ppd_reference(); | 222 Printer::PpdReference* ppd = printer->mutable_ppd_reference(); |
| 221 ppd->effective_manufacturer = printer_manufacturer; | 223 ppd->effective_manufacturer = printer_manufacturer; |
| 222 ppd->effective_model = printer_model; | 224 ppd->effective_model = printer_model; |
| 223 } | 225 } |
| 224 | 226 |
| 225 chromeos::DebugDaemonClient* client = | 227 if (printer->IsIppEverywhere()) { |
| 226 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 228 AddPrinterToCups(std::move(printer), "", true); |
| 227 client->CupsAddPrinter( | 229 return; |
| 228 printer_id, // record id | 230 } |
| 229 printer_uri, // uri | 231 |
| 230 printer_ppd_path, // ppd location | 232 // We need to save a reference to members of printer since we transfer |
| 231 printer_ppd_path.empty() ? true : false, // ipp everywhere | 233 // ownership in the bind call. |
| 232 base::Bind(&CupsPrintersHandler::OnAddedPrinter, | 234 const Printer::PpdReference& ppd_reference = printer->ppd_reference(); |
| 233 weak_factory_.GetWeakPtr(), base::Passed(std::move(printer))), | 235 ppd_provider_->Resolve( |
| 234 base::Bind(&CupsPrintersHandler::OnAddPrinterError, | 236 ppd_reference, |
| 235 weak_factory_.GetWeakPtr())); | 237 base::Bind(&CupsPrintersHandler::OnPPDResolved, |
| 238 weak_factory_.GetWeakPtr(), base::Passed(&printer))); | |
|
xdai1
2016/12/01 23:18:17
Is base::Passed(&printer)) preferred over base::Pa
skau
2016/12/05 20:40:51
They are equivalent. The post in chromium-dev tha
| |
| 236 } | 239 } |
| 237 | 240 |
| 238 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, | 241 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, |
| 239 bool success) { | 242 bool success) { |
| 240 std::string printer_name = printer->display_name(); | 243 std::string printer_name = printer->display_name(); |
| 241 if (success) { | 244 if (success) { |
| 242 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 245 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
| 243 std::move(printer)); | 246 std::move(printer)); |
| 244 } | 247 } |
| 245 CallJavascriptFunction( | 248 CallJavascriptFunction( |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 CallJavascriptFunction("cr.webUIListenerCallback", | 387 CallJavascriptFunction("cr.webUIListenerCallback", |
| 385 base::StringValue("on-printer-discovered"), | 388 base::StringValue("on-printer-discovered"), |
| 386 *printers_list); | 389 *printers_list); |
| 387 } | 390 } |
| 388 | 391 |
| 389 void CupsPrintersHandler::OnDiscoveryDone() { | 392 void CupsPrintersHandler::OnDiscoveryDone() { |
| 390 CallJavascriptFunction("cr.webUIListenerCallback", | 393 CallJavascriptFunction("cr.webUIListenerCallback", |
| 391 base::StringValue("on-printer-discovery-done")); | 394 base::StringValue("on-printer-discovery-done")); |
| 392 } | 395 } |
| 393 | 396 |
| 397 void CupsPrintersHandler::AddPrinterToCups(std::unique_ptr<Printer> printer, | |
| 398 const std::string& ppd_path, | |
| 399 bool ipp_everywhere) { | |
| 400 std::string printer_id = printer->id(); | |
| 401 std::string printer_uri = printer->uri(); | |
| 402 | |
| 403 chromeos::DebugDaemonClient* client = | |
| 404 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | |
| 405 client->CupsAddPrinter( | |
| 406 printer_id, printer_uri, ppd_path, ipp_everywhere, | |
| 407 base::Bind(&CupsPrintersHandler::OnAddedPrinter, | |
| 408 weak_factory_.GetWeakPtr(), base::Passed(&printer)), | |
| 409 base::Bind(&CupsPrintersHandler::OnAddPrinterError, | |
| 410 weak_factory_.GetWeakPtr())); | |
| 411 } | |
| 412 | |
| 413 void CupsPrintersHandler::OnPPDResolved( | |
| 414 std::unique_ptr<Printer> printer, | |
| 415 printing::PpdProvider::CallbackResultCode result, | |
| 416 base::FilePath path) { | |
| 417 if (result != printing::PpdProvider::SUCCESS) { | |
| 418 // TODO(skau): Add appropriate failure modes crbug.com/670068. | |
| 419 OnAddPrinterError(); | |
| 420 return; | |
| 421 } | |
| 422 | |
| 423 AddPrinterToCups(std::move(printer), path.value(), | |
| 424 false /* never ipp everywhere */); | |
| 425 } | |
| 426 | |
| 394 } // namespace settings | 427 } // namespace settings |
| 395 } // namespace chromeos | 428 } // namespace chromeos |
| OLD | NEW |