OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "printing/backend/print_backend_cups_ipp.h" |
| 6 |
| 7 #include <cups/cups.h> |
| 8 #include <dlfcn.h> |
| 9 #include <errno.h> |
| 10 #include <pthread.h> |
| 11 |
| 12 #include <memory> |
| 13 #include <utility> |
| 14 #include <vector> |
| 15 |
| 16 #include "base/debug/leak_annotations.h" |
| 17 #include "base/files/file_util.h" |
| 18 #include "base/lazy_instance.h" |
| 19 #include "base/logging.h" |
| 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_split.h" |
| 22 #include "base/strings/string_util.h" |
| 23 #include "base/synchronization/lock.h" |
| 24 #include "base/values.h" |
| 25 #include "printing/backend/cups_connection.h" |
| 26 #include "printing/backend/cups_ipp_util.h" |
| 27 #include "printing/backend/print_backend_consts.h" |
| 28 #include "printing/units.h" |
| 29 #include "url/gurl.h" |
| 30 |
| 31 namespace printing { |
| 32 |
| 33 PrintBackendCupsIpp::PrintBackendCupsIpp( |
| 34 std::unique_ptr<CupsConnection> cups_connection) |
| 35 : cups_connection_(std::move(cups_connection)) {} |
| 36 |
| 37 PrintBackendCupsIpp::~PrintBackendCupsIpp() {} |
| 38 |
| 39 bool PrintBackendCupsIpp::EnumeratePrinters(PrinterList* printer_list) { |
| 40 DCHECK(printer_list); |
| 41 printer_list->clear(); |
| 42 |
| 43 std::vector<CupsPrinter> printers = cups_connection_->GetDests(); |
| 44 if (printers.empty()) { |
| 45 LOG(WARNING) << "CUPS: Error getting printers from CUPS server" |
| 46 << ", server: " << cups_connection_->server_name() |
| 47 << ", error: " |
| 48 << static_cast<int>(cups_connection_->last_error()); |
| 49 |
| 50 return false; |
| 51 } |
| 52 |
| 53 for (const auto& printer : printers) { |
| 54 PrinterBasicInfo basic_info; |
| 55 if (printer.ToPrinterInfo(&basic_info)) { |
| 56 printer_list->push_back(basic_info); |
| 57 } |
| 58 } |
| 59 |
| 60 return true; |
| 61 } |
| 62 |
| 63 std::string PrintBackendCupsIpp::GetDefaultPrinterName() { |
| 64 std::vector<CupsPrinter> printers = cups_connection_->GetDests(); |
| 65 for (const auto& printer : printers) { |
| 66 if (printer.is_default()) { |
| 67 return printer.GetName(); |
| 68 } |
| 69 } |
| 70 |
| 71 return std::string(); |
| 72 } |
| 73 |
| 74 bool PrintBackendCupsIpp::GetPrinterBasicInfo(const std::string& printer_name, |
| 75 PrinterBasicInfo* printer_info) { |
| 76 std::unique_ptr<CupsPrinter> printer( |
| 77 cups_connection_->GetPrinter(printer_name)); |
| 78 if (!printer || !printer->IsAvailable()) |
| 79 return false; |
| 80 |
| 81 DCHECK_EQ(printer_name, printer->GetName()); |
| 82 |
| 83 return printer->ToPrinterInfo(printer_info); |
| 84 } |
| 85 |
| 86 bool PrintBackendCupsIpp::GetPrinterSemanticCapsAndDefaults( |
| 87 const std::string& printer_name, |
| 88 PrinterSemanticCapsAndDefaults* printer_info) { |
| 89 std::unique_ptr<CupsPrinter> printer( |
| 90 cups_connection_->GetPrinter(printer_name)); |
| 91 if (!printer) |
| 92 return false; |
| 93 |
| 94 CapsAndDefaultsFromPrinter(*printer, printer_info); |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool PrintBackendCupsIpp::GetPrinterCapsAndDefaults( |
| 100 const std::string& printer_name, |
| 101 PrinterCapsAndDefaults* printer_info) { |
| 102 DCHECK(printer_info); |
| 103 |
| 104 // Read the ppd file for Cloud Print. We don't use PPD anymore otherwise. |
| 105 std::unique_ptr<CupsPrinter> printer( |
| 106 cups_connection_->GetPrinter(printer_name)); |
| 107 if (!printer) |
| 108 return false; |
| 109 |
| 110 base::FilePath ppd_path(printer->GetPPD()); |
| 111 // In some cases CUPS failed to get ppd file. |
| 112 if (ppd_path.empty()) { |
| 113 LOG(ERROR) << "CUPS: Failed to get PPD, printer name: " << printer_name; |
| 114 return false; |
| 115 } |
| 116 |
| 117 std::string content; |
| 118 bool res = base::ReadFileToString(ppd_path, &content); |
| 119 |
| 120 base::DeleteFile(ppd_path, false); |
| 121 |
| 122 if (res) { |
| 123 printer_info->printer_capabilities.swap(content); |
| 124 printer_info->caps_mime_type = "application/pagemaker"; |
| 125 // In CUPS, printer defaults is a part of PPD file. Nothing to upload here. |
| 126 printer_info->printer_defaults.clear(); |
| 127 printer_info->defaults_mime_type.clear(); |
| 128 } |
| 129 |
| 130 return res; |
| 131 } |
| 132 |
| 133 std::string PrintBackendCupsIpp::GetPrinterDriverInfo( |
| 134 const std::string& printer_name) { |
| 135 std::unique_ptr<CupsPrinter> printer( |
| 136 cups_connection_->GetPrinter(printer_name)); |
| 137 if (!printer || !printer->IsAvailable()) |
| 138 return std::string(); |
| 139 |
| 140 DCHECK_EQ(printer_name, printer->GetName()); |
| 141 return printer->GetMakeAndModel(); |
| 142 } |
| 143 |
| 144 bool PrintBackendCupsIpp::IsValidPrinter(const std::string& printer_name) { |
| 145 std::unique_ptr<CupsPrinter> printer( |
| 146 cups_connection_->GetPrinter(printer_name)); |
| 147 if (!printer) |
| 148 return false; |
| 149 |
| 150 return printer->IsAvailable(); |
| 151 } |
| 152 |
| 153 } // namespace printing |
OLD | NEW |