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

Unified 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: convert deleters to structs 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « printing/backend/cups_connection.h ('k') | printing/backend/cups_deleters.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: printing/backend/cups_connection.cc
diff --git a/printing/backend/cups_connection.cc b/printing/backend/cups_connection.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d558cb42346369794026b23f0f045dc13b1514cf
--- /dev/null
+++ b/printing/backend/cups_connection.cc
@@ -0,0 +1,157 @@
+// 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/cups_connection.h"
+
+#include <string>
+#include <utility>
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/stringprintf.h"
+
+namespace {
+
+static const int kTimeoutMs = 3000;
Lei Zhang 2016/07/19 22:49:17 Noo static inside anonymous naemspaces
skau 2016/07/20 23:58:23 Done.
+
+class DestinationEnumerator {
+ public:
+ static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) {
Lei Zhang 2016/07/19 22:49:17 Can this be a private method?
skau 2016/07/20 23:58:23 Unfortunately not. I need a function pointer matc
+ cups_dest_t* copied_dest;
+ cupsCopyDest(dest, 0, &copied_dest);
+ reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest(
+ copied_dest);
+
+ // keep going
+ return 1;
+ }
+
+ void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); }
+
+ // Returns the collected destinations. Remove desired destinations from the
Lei Zhang 2016/07/19 22:49:17 I think the Remove... comment can be inferred from
skau 2016/07/20 23:58:23 Done.
+ // vector or they will be cleaned up when this object is destroyed.
+ std::vector<std::unique_ptr<cups_dest_t, printing::DestinationDeleter>>&
Lei Zhang 2016/07/19 22:49:17 If you extend the printing namespace to color the
skau 2016/07/20 23:58:23 Done.
+ get_dests() {
+ return dests_;
+ }
+
+ private:
+ std::vector<std::unique_ptr<cups_dest_t, printing::DestinationDeleter>>
+ dests_;
+};
Lei Zhang 2016/07/19 22:49:17 DISALLOW_COPY_AND_ASSIGN() is always good to have.
skau 2016/07/20 23:58:23 Done.
+
+} // namespace
+
+namespace printing {
+
+CupsConnection::Impl::Impl(http_t* http)
+ : cups_http_(http), http_factory_(http) {}
+
+CupsConnection::Impl::~Impl() {
+ http_factory_.InvalidateWeakPtrs();
+}
+
+base::WeakPtr<http_t> CupsConnection::Impl::GetHttp() {
+ return http_factory_.GetWeakPtr();
+}
+
+CupsConnection::CupsConnection(const GURL& print_server_url,
+ http_encryption_t encryption,
+ bool blocking)
+ : print_server_url_(print_server_url),
+ cups_encryption_(encryption),
+ blocking_(blocking),
+ impl_(nullptr),
+ cups_http_(nullptr) {}
+
+CupsConnection::CupsConnection(CupsConnection&& connection)
+ : print_server_url_(connection.print_server_url_),
+ cups_encryption_(connection.cups_encryption_),
+ blocking_(connection.blocking_),
+ impl_(std::move(connection.impl_)),
+ cups_http_(std::move(connection.cups_http_)) {}
+
+CupsConnection::~CupsConnection() {}
+
+bool CupsConnection::Connect() {
+ if (cups_http_)
+ return true; // we're already connected
+
+ std::string host;
+ int port;
+
+ if (!print_server_url_.is_empty()) {
+ host = print_server_url_.host();
+ port = print_server_url_.IntPort();
+ } else {
+ host = cupsServer();
+ port = ippPort();
+ }
+
+ http_t* connection =
+ httpConnect2(host.c_str(), port, nullptr, AF_UNSPEC, cups_encryption_,
+ blocking_ ? 1 : 0, kTimeoutMs, nullptr);
+
+ if (!connection)
+ return false;
+
+ impl_ = base::MakeUnique<Impl>(connection);
+ cups_http_ = impl_->GetHttp();
+ return true;
+}
+
+std::vector<CupsPrinter> CupsConnection::GetDests() {
+ if (!Connect()) {
+ LOG(WARNING) << "CUPS connection failed";
+ return std::vector<CupsPrinter>();
+ }
+
+ DestinationEnumerator enumerator;
+ int success =
+ cupsEnumDests(CUPS_DEST_FLAGS_NONE, kTimeoutMs,
+ nullptr, // no cancel signal
+ 0, // all the printers
+ CUPS_PRINTER_SCANNER, // except the scanners
+ &DestinationEnumerator::cups_callback, &enumerator);
+
+ if (!success) {
+ LOG(WARNING) << "Enumerating printers failed";
+ return std::vector<CupsPrinter>();
+ }
+
+ auto& dests = enumerator.get_dests();
+ std::vector<CupsPrinter> printers;
+ for (auto& dest : dests) {
+ printers.emplace_back(impl_->GetHttp(), std::move(dest), nullptr);
+ }
+
+ dests.clear(); // CupsPrinter takes ownership of all the cups_dest_t objects
+
+ return printers;
+}
+
+std::unique_ptr<CupsPrinter> CupsConnection::GetPrinter(
+ const std::string& name) {
+ if (!Connect())
+ return nullptr;
+
+ cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr);
+ if (!dest)
+ return nullptr;
+
+ cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest);
+ return base::MakeUnique<CupsPrinter>(
+ impl_->GetHttp(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest),
+ std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info));
+}
+
+std::string CupsConnection::server_name() const {
+ return print_server_url_.host();
+}
+
+int CupsConnection::last_error() const {
+ return cupsLastError();
+}
+
+} // namespace printing
« 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