forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_backend_cups_ipp.cc
132 lines (104 loc) · 3.87 KB
/
print_backend_cups_ipp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/backend/print_backend_cups_ipp.h"
#include <cups/cups.h>
#include <memory>
#include <utility>
#include <vector>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "printing/backend/cups_connection.h"
#include "printing/backend/cups_ipp_util.h"
#include "printing/backend/print_backend_consts.h"
#include "printing/units.h"
namespace printing {
PrintBackendCupsIpp::PrintBackendCupsIpp(
std::unique_ptr<CupsConnection> cups_connection)
: cups_connection_(std::move(cups_connection)) {}
PrintBackendCupsIpp::~PrintBackendCupsIpp() {}
bool PrintBackendCupsIpp::EnumeratePrinters(PrinterList* printer_list) {
DCHECK(printer_list);
printer_list->clear();
std::vector<CupsPrinter> printers = cups_connection_->GetDests();
if (printers.empty()) {
LOG(WARNING) << "CUPS: Error getting printers from CUPS server"
<< ", server: " << cups_connection_->server_name()
<< ", error: "
<< static_cast<int>(cups_connection_->last_error());
return false;
}
for (const auto& printer : printers) {
PrinterBasicInfo basic_info;
if (printer.ToPrinterInfo(&basic_info)) {
printer_list->push_back(basic_info);
}
}
return true;
}
std::string PrintBackendCupsIpp::GetDefaultPrinterName() {
std::vector<CupsPrinter> printers = cups_connection_->GetDests();
for (const auto& printer : printers) {
if (printer.is_default()) {
return printer.GetName();
}
}
return std::string();
}
bool PrintBackendCupsIpp::GetPrinterBasicInfo(const std::string& printer_name,
PrinterBasicInfo* printer_info) {
std::unique_ptr<CupsPrinter> printer(
cups_connection_->GetPrinter(printer_name));
if (!printer || !printer->IsAvailable())
return false;
DCHECK_EQ(printer_name, printer->GetName());
return printer->ToPrinterInfo(printer_info);
}
bool PrintBackendCupsIpp::GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
PrinterSemanticCapsAndDefaults* printer_info) {
std::unique_ptr<CupsPrinter> printer(
cups_connection_->GetPrinter(printer_name));
if (!printer)
return false;
CapsAndDefaultsFromPrinter(*printer, printer_info);
return true;
}
bool PrintBackendCupsIpp::GetPrinterCapsAndDefaults(
const std::string& printer_name,
PrinterCapsAndDefaults* printer_info) {
DCHECK(printer_info);
// Read the PPD file for Cloud Print. We don't use PPD anymore otherwise.
std::unique_ptr<CupsPrinter> printer(
cups_connection_->GetPrinter(printer_name));
if (!printer)
return false;
std::string ppd_contents = printer->GetPPD();
if (ppd_contents.empty()) {
LOG(ERROR) << "CUPS: Failed to get PPD, printer name: " << printer_name;
return false;
}
printer_info->printer_capabilities.swap(ppd_contents);
printer_info->caps_mime_type = "application/pagemaker";
// In CUPS, printer defaults is a part of PPD file. Nothing to upload here.
printer_info->printer_defaults.clear();
printer_info->defaults_mime_type.clear();
return true;
}
std::string PrintBackendCupsIpp::GetPrinterDriverInfo(
const std::string& printer_name) {
std::unique_ptr<CupsPrinter> printer(
cups_connection_->GetPrinter(printer_name));
if (!printer || !printer->IsAvailable())
return std::string();
DCHECK_EQ(printer_name, printer->GetName());
return printer->GetMakeAndModel();
}
bool PrintBackendCupsIpp::IsValidPrinter(const std::string& printer_name) {
std::unique_ptr<CupsPrinter> printer(
cups_connection_->GetPrinter(printer_name));
return printer ? printer->IsAvailable() : false;
}
} // namespace printing