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

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 printing gyp file. 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 DestinationEnumerator {
17 public:
18 ~DestinationEnumerator() {
19 for (cups_dest_t* dest : dests_) {
20 cupsFreeDests(1, dest);
Lei Zhang 2016/07/13 01:08:42 Can |dests_| be: std::vector<std::unique_ptr<cups_
skau 2016/07/14 20:43:05 I didn't think of that. Thanks!
21 }
22
23 dests_.clear();
24 }
25
26 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) {
27 cups_dest_t* copied_dest;
28 cupsCopyDest(dest, 0, &copied_dest);
29 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest(
30 copied_dest);
31
32 // keep going
33 return 1;
34 }
35
36 void store_dest(cups_dest_t* dest) { dests_.push_back(dest); }
37
38 // Returns the collected destinations. Remove desired destinations from the
39 // vector or they will be cleaned up when this object is destroyed.
40 std::vector<cups_dest_t*>& get_dests() { return dests_; }
41
42 private:
43 std::vector<cups_dest_t*> dests_;
44 };
45
46 } // namespace
47
48 namespace printing {
49
50 void HttpDeleter::operator()(http_t* http) const {
51 httpClose(http);
52 }
53
54 CupsConnection::Impl::Impl(http_t* http)
55 : cups_http_(http), http_factory_(http) {}
56
57 CupsConnection::Impl::~Impl() {
58 http_factory_.InvalidateWeakPtrs();
59 }
60
61 base::WeakPtr<http_t> CupsConnection::Impl::GetHttp() {
62 return http_factory_.GetWeakPtr();
63 }
64
65 CupsConnection::CupsConnection(const GURL& print_server_url,
66 http_encryption_t encryption,
67 bool blocking)
68 : print_server_url_(print_server_url),
69 cups_encryption_(encryption),
70 blocking_(blocking),
71 impl_(nullptr),
72 cups_http_(nullptr) {}
73
74 CupsConnection::CupsConnection(CupsConnection&& connection)
75 : print_server_url_(connection.print_server_url_),
76 cups_encryption_(connection.cups_encryption_),
77 blocking_(connection.blocking_),
78 impl_(std::move(connection.impl_)),
79 cups_http_(std::move(connection.cups_http_)) {}
80
81 CupsConnection::~CupsConnection() {}
82
83 bool CupsConnection::Connect() {
84 if (cups_http_)
85 return true; // we're already connected
86
87 std::string host_string;
Lei Zhang 2016/07/13 01:08:42 Only used inside the if block below - move it ther
skau 2016/07/14 20:43:05 I needed the c_str buffer to stay in scope. I swi
88 const char* host;
89 int port;
90
91 if (!print_server_url_.is_empty()) {
92 host_string = print_server_url_.host();
93 host = host_string.c_str();
94 port = print_server_url_.IntPort();
95 } else {
96 host = cupsServer();
97 port = ippPort();
98 }
99
100 http_t* connection =
101 httpConnect2(host, port, NULL, AF_UNSPEC, cups_encryption_,
Lei Zhang 2016/07/13 01:08:42 nullptr all around
skau 2016/07/14 20:43:05 Done.
102 blocking_ ? 1 : 0, kTimeoutMs, NULL);
103
104 if (!connection)
105 return false;
106
107 impl_ = std::unique_ptr<Impl>(new Impl(connection));
Lei Zhang 2016/07/13 01:08:42 Use base::MakeUnique<Impl>(connection) ?
skau 2016/07/14 20:43:05 Done.
108 cups_http_ = impl_->GetHttp();
109 return true;
110 }
111
112 std::vector<CupsPrinter> CupsConnection::GetDests() {
113 if (!Connect()) {
114 LOG(WARNING) << "CUPS connection failed";
115 return std::vector<CupsPrinter>();
116 }
117
118 DestinationEnumerator enumerator;
119 int success =
120 cupsEnumDests(CUPS_DEST_FLAGS_NONE, kTimeoutMs,
121 NULL, // no cancel signal
122 0, // all the printers
123 CUPS_PRINTER_SCANNER, // except the scanners
124 &DestinationEnumerator::cups_callback, &enumerator);
125
126 if (!success) {
127 LOG(WARNING) << "Enumerating printers failed";
128 return std::vector<CupsPrinter>();
129 }
130
131 std::vector<CupsPrinter> printers;
132 std::vector<cups_dest_t*>& dests = enumerator.get_dests();
133 for (cups_dest_t* dest : dests) {
134 CupsPrinter printer(impl_->GetHttp(), dest, nullptr);
135 printers.push_back(std::move(printer));
136 }
137
138 dests.clear(); // CupsPrinter takes ownership of all the cups_dest_t objects
139
140 return printers;
141 }
142
143 CupsPrinter* CupsConnection::GetPrinter(const std::string& name) {
144 if (!Connect())
145 return nullptr;
146
147 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), NULL);
148 if (!dest)
149 return nullptr;
150
151 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest);
152 return new CupsPrinter(impl_->GetHttp(), dest, info);
153 }
154
155 std::string CupsConnection::server_name() const {
156 return print_server_url_.host();
157 }
158
159 int CupsConnection::last_error() const {
160 return cupsLastError();
161 }
162
163 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698