| 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 if (!ppd_provider_->CachePpd(printer->ppd_reference(), |
| 219 base::FilePath(printer_ppd_path))) { |
| 220 LOG(WARNING) << "PPD could not be stored in the cache"; |
| 221 OnAddPrinterError(); |
| 222 return; |
| 223 } |
| 219 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { | 224 } else if (!printer_manufacturer.empty() && !printer_model.empty()) { |
| 220 Printer::PpdReference* ppd = printer->mutable_ppd_reference(); | 225 Printer::PpdReference* ppd = printer->mutable_ppd_reference(); |
| 221 ppd->effective_manufacturer = printer_manufacturer; | 226 ppd->effective_manufacturer = printer_manufacturer; |
| 222 ppd->effective_model = printer_model; | 227 ppd->effective_model = printer_model; |
| 223 } | 228 } |
| 224 | 229 |
| 225 chromeos::DebugDaemonClient* client = | 230 if (printer->IsIppEverywhere()) { |
| 226 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 231 AddPrinterToCups(std::move(printer), base::FilePath(), true); |
| 227 client->CupsAddPrinter( | 232 return; |
| 228 printer_id, // record id | 233 } |
| 229 printer_uri, // uri | 234 |
| 230 printer_ppd_path, // ppd location | 235 // We need to save a reference to members of printer since we transfer |
| 231 printer_ppd_path.empty() ? true : false, // ipp everywhere | 236 // ownership in the bind call. |
| 232 base::Bind(&CupsPrintersHandler::OnAddedPrinter, | 237 const Printer::PpdReference& ppd_reference = printer->ppd_reference(); |
| 233 weak_factory_.GetWeakPtr(), base::Passed(std::move(printer))), | 238 ppd_provider_->Resolve( |
| 234 base::Bind(&CupsPrintersHandler::OnAddPrinterError, | 239 ppd_reference, |
| 235 weak_factory_.GetWeakPtr())); | 240 base::Bind(&CupsPrintersHandler::OnPPDResolved, |
| 241 weak_factory_.GetWeakPtr(), base::Passed(&printer))); |
| 236 } | 242 } |
| 237 | 243 |
| 238 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, | 244 void CupsPrintersHandler::OnAddedPrinter(std::unique_ptr<Printer> printer, |
| 239 bool success) { | 245 bool success) { |
| 240 std::string printer_name = printer->display_name(); | 246 std::string printer_name = printer->display_name(); |
| 241 if (success) { | 247 if (success) { |
| 242 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | 248 PrinterPrefManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
| 243 std::move(printer)); | 249 std::move(printer)); |
| 244 } | 250 } |
| 245 CallJavascriptFunction( | 251 CallJavascriptFunction( |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 CallJavascriptFunction("cr.webUIListenerCallback", | 390 CallJavascriptFunction("cr.webUIListenerCallback", |
| 385 base::StringValue("on-printer-discovered"), | 391 base::StringValue("on-printer-discovered"), |
| 386 *printers_list); | 392 *printers_list); |
| 387 } | 393 } |
| 388 | 394 |
| 389 void CupsPrintersHandler::OnDiscoveryDone() { | 395 void CupsPrintersHandler::OnDiscoveryDone() { |
| 390 CallJavascriptFunction("cr.webUIListenerCallback", | 396 CallJavascriptFunction("cr.webUIListenerCallback", |
| 391 base::StringValue("on-printer-discovery-done")); | 397 base::StringValue("on-printer-discovery-done")); |
| 392 } | 398 } |
| 393 | 399 |
| 400 void CupsPrintersHandler::AddPrinterToCups(std::unique_ptr<Printer> printer, |
| 401 const base::FilePath& ppd_path, |
| 402 bool ipp_everywhere) { |
| 403 std::string printer_id = printer->id(); |
| 404 std::string printer_uri = printer->uri(); |
| 405 |
| 406 chromeos::DebugDaemonClient* client = |
| 407 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 408 client->CupsAddPrinter( |
| 409 printer_id, printer_uri, ppd_path.value(), ipp_everywhere, |
| 410 base::Bind(&CupsPrintersHandler::OnAddedPrinter, |
| 411 weak_factory_.GetWeakPtr(), base::Passed(&printer)), |
| 412 base::Bind(&CupsPrintersHandler::OnAddPrinterError, |
| 413 weak_factory_.GetWeakPtr())); |
| 414 } |
| 415 |
| 416 void CupsPrintersHandler::OnPPDResolved( |
| 417 std::unique_ptr<Printer> printer, |
| 418 printing::PpdProvider::CallbackResultCode result, |
| 419 base::FilePath path) { |
| 420 if (result != printing::PpdProvider::SUCCESS) { |
| 421 // TODO(skau): Add appropriate failure modes crbug.com/670068. |
| 422 OnAddPrinterError(); |
| 423 return; |
| 424 } |
| 425 |
| 426 AddPrinterToCups(std::move(printer), path, false /* never ipp everywhere */); |
| 427 } |
| 428 |
| 394 } // namespace settings | 429 } // namespace settings |
| 395 } // namespace chromeos | 430 } // namespace chromeos |
| OLD | NEW |