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

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: Fix 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
« no previous file with comments | « printing/backend/cups_connection.h ('k') | printing/backend/cups_deleters.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/stringprintf.h"
13
14 namespace printing {
15
16 namespace {
17
18 const int kTimeoutMs = 3000;
19
20 class DestinationEnumerator {
21 public:
22 DestinationEnumerator() {}
23
24 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) {
25 cups_dest_t* copied_dest;
26 cupsCopyDest(dest, 0, &copied_dest);
27 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest(
28 copied_dest);
29
30 // keep going
31 return 1;
32 }
33
34 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); }
35
36 // Returns the collected destinations.
37 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() {
38 return dests_;
39 }
40
41 private:
42 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_;
43
44 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator);
45 };
46
47 } // namespace
48
49 CupsConnection::CupsConnection(const GURL& print_server_url,
50 http_encryption_t encryption,
51 bool blocking)
52 : print_server_url_(print_server_url),
53 cups_encryption_(encryption),
54 blocking_(blocking),
55 cups_http_(nullptr) {}
56
57 CupsConnection::CupsConnection(CupsConnection&& connection)
58 : print_server_url_(connection.print_server_url_),
59 cups_encryption_(connection.cups_encryption_),
60 blocking_(connection.blocking_),
61 cups_http_(std::move(connection.cups_http_)) {}
62
63 CupsConnection::~CupsConnection() {}
64
65 bool CupsConnection::Connect() {
66 if (cups_http_)
67 return true; // we're already connected
68
69 std::string host;
70 int port;
71
72 if (!print_server_url_.is_empty()) {
73 host = print_server_url_.host();
74 port = print_server_url_.IntPort();
75 } else {
76 host = cupsServer();
77 port = ippPort();
78 }
79
80 cups_http_.reset(httpConnect2(host.c_str(), port, nullptr, AF_UNSPEC,
81 cups_encryption_, blocking_ ? 1 : 0, kTimeoutMs,
82 nullptr));
83 return !!cups_http_;
84 }
85
86 std::vector<CupsPrinter> CupsConnection::GetDests() {
87 if (!Connect()) {
88 LOG(WARNING) << "CUPS connection failed";
89 return std::vector<CupsPrinter>();
90 }
91
92 DestinationEnumerator enumerator;
93 int success =
94 cupsEnumDests(CUPS_DEST_FLAGS_NONE, kTimeoutMs,
95 nullptr, // no cancel signal
96 0, // all the printers
97 CUPS_PRINTER_SCANNER, // except the scanners
98 &DestinationEnumerator::cups_callback, &enumerator);
99
100 if (!success) {
101 LOG(WARNING) << "Enumerating printers failed";
102 return std::vector<CupsPrinter>();
103 }
104
105 auto dests = std::move(enumerator.get_dests());
106 std::vector<CupsPrinter> printers;
107 for (auto& dest : dests) {
108 printers.emplace_back(cups_http_.get(), std::move(dest), nullptr);
109 }
110
111 return printers;
112 }
113
114 std::unique_ptr<CupsPrinter> CupsConnection::GetPrinter(
115 const std::string& name) {
116 if (!Connect())
117 return nullptr;
118
119 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr);
120 if (!dest)
121 return nullptr;
122
123 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest);
124 return base::MakeUnique<CupsPrinter>(
125 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest),
126 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info));
127 }
128
129 std::string CupsConnection::server_name() const {
130 return print_server_url_.host();
131 }
132
133 int CupsConnection::last_error() const {
134 return cupsLastError();
135 }
136
137 } // namespace printing
OLDNEW
« no previous file with comments | « printing/backend/cups_connection.h ('k') | printing/backend/cups_deleters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698