Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: printing/backend/cups_connection.cc

Issue 2105463002: Create a new print backend for the updated CUPS APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: typos Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/cups_connection.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11
12 namespace {
13
14 static const int kTimeoutMs = 3000;
15
16 class HttpDeleter {
17 public:
18 void operator()(http_t* http) const { httpClose(http); }
19 };
20
21 class DestinationEnumerator {
22 public:
23 ~DestinationEnumerator() {
24 for (cups_dest_t* dest : dests_) {
25 cupsFreeDests(1, dest);
26 }
27
28 dests_.clear();
29 }
30
31 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) {
32 cups_dest_t* copied_dest;
33 cupsCopyDest(dest, 0, &copied_dest);
34 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest(
35 copied_dest);
36
37 // keep going
38 return 1;
39 }
40
41 void store_dest(cups_dest_t* dest) { dests_.push_back(dest); }
42
43 // Returns the collected destinations. Remove desired destinations from the
44 // vector or they will be cleaned up when this object is destroyed.
45 std::vector<cups_dest_t*>& get_dests() { return dests_; }
46
47 private:
48 std::vector<cups_dest_t*> dests_;
49 };
50
51 } // namespace
52
53 namespace printing {
54
55 CupsConnection::CupsConnection(const GURL& print_server_url,
56 http_encryption_t encryption,
57 bool blocking)
58 : print_server_url_(print_server_url),
59 cups_encryption_(encryption),
60 blocking_(blocking) {}
61
62 CupsConnection::CupsConnection(const CupsConnection& connection)
63 : print_server_url_(connection.print_server_url_),
64 cups_encryption_(connection.cups_encryption_),
65 blocking_(connection.blocking_),
66 cups_http_(connection.cups_http_) {}
67
68 CupsConnection::~CupsConnection() {}
69
70 bool CupsConnection::Connect() {
71 if (cups_http_)
72 return true; // we're already connected
73
74 std::string host_string;
75 const char* host;
76 int port;
77
78 if (!print_server_url_.is_empty()) {
79 host_string = print_server_url_.host();
80 host = host_string.c_str();
81 port = print_server_url_.IntPort();
82 } else {
83 host = cupsServer();
84 port = ippPort();
85 }
86
87 http_t* connection =
88 httpConnect2(host, port, NULL, AF_UNSPEC, cups_encryption_,
89 blocking_ ? 1 : 0, kTimeoutMs, NULL);
90
91 if (connection == NULL)
Lei Zhang 2016/07/08 01:19:50 NULL -> nullptr foo == NULL -> !foo
skau 2016/07/08 21:24:08 Thanks!
92 return false;
93
94 cups_http_ = {connection, HttpDeleter()};
95 return true;
96 }
97
98 std::vector<CupsPrinter> CupsConnection::GetDests() {
99 if (!Connect()) {
100 LOG(WARNING) << "CUPS connection failed";
101 return std::vector<CupsPrinter>();
102 }
103
104 DestinationEnumerator enumerator;
105 int success =
106 cupsEnumDests(CUPS_DEST_FLAGS_NONE, kTimeoutMs,
107 NULL, // no cancel signal
108 0, // all the printers
109 CUPS_PRINTER_SCANNER, // except the scanners
110 &DestinationEnumerator::cups_callback, &enumerator);
111
112 if (!success) {
113 LOG(WARNING) << "Enumerating printers failed";
114 return std::vector<CupsPrinter>();
115 }
116
117 std::vector<CupsPrinter> printers;
118 std::vector<cups_dest_t*>& dests = enumerator.get_dests();
119 for (cups_dest_t* dest : dests) {
120 CupsPrinter printer(cups_http_, dest, nullptr);
121 printers.push_back(std::move(printer));
122 }
123
124 dests.clear(); // CupsPrinter takes ownership of all the cups_dest_t objects
125
126 return printers;
127 }
128
129 CupsPrinter* CupsConnection::GetPrinter(const std::string& name) {
130 if (!Connect())
131 return nullptr;
132
133 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), NULL);
134 if (dest == NULL)
135 return nullptr;
136
137 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest);
138 return new CupsPrinter(cups_http_, dest, info);
139 }
140
141 std::string CupsConnection::server_name() const {
142 return print_server_url_.host();
143 }
144
145 int CupsConnection::last_error() const {
146 return cupsLastError();
147 }
148
149 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698