Chromium Code Reviews| 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..0a271c2e21e610272ce76130fde256224100b5c2 |
| --- /dev/null |
| +++ b/printing/backend/cups_connection.cc |
| @@ -0,0 +1,149 @@ |
| +// 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 "base/logging.h" |
| +#include "base/strings/stringprintf.h" |
| + |
| +namespace { |
| + |
| +static const int kTimeoutMs = 3000; |
| + |
| +class HttpDeleter { |
| + public: |
| + void operator()(http_t* http) const { httpClose(http); } |
| +}; |
| + |
| +class DestinationEnumerator { |
| + public: |
| + ~DestinationEnumerator() { |
| + for (cups_dest_t* dest : dests_) { |
| + cupsFreeDests(1, dest); |
| + } |
| + |
| + dests_.clear(); |
| + } |
| + |
| + static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) { |
| + 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_.push_back(dest); } |
| + |
| + // Returns the collected destinations. Remove desired destinations from the |
| + // vector or they will be cleaned up when this object is destroyed. |
| + std::vector<cups_dest_t*>& get_dests() { return dests_; } |
| + |
| + private: |
| + std::vector<cups_dest_t*> dests_; |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace printing { |
| + |
| +CupsConnection::CupsConnection(const GURL& print_server_url, |
| + http_encryption_t encryption, |
| + bool blocking) |
| + : print_server_url_(print_server_url), |
| + cups_encryption_(encryption), |
| + blocking_(blocking) {} |
| + |
| +CupsConnection::CupsConnection(const CupsConnection& connection) |
| + : print_server_url_(connection.print_server_url_), |
| + cups_encryption_(connection.cups_encryption_), |
| + blocking_(connection.blocking_), |
| + cups_http_(connection.cups_http_) {} |
| + |
| +CupsConnection::~CupsConnection() {} |
| + |
| +bool CupsConnection::Connect() { |
| + if (cups_http_) |
| + return true; // we're already connected |
| + |
| + std::string host_string; |
| + const char* host; |
| + int port; |
| + |
| + if (!print_server_url_.is_empty()) { |
| + host_string = print_server_url_.host(); |
| + host = host_string.c_str(); |
| + port = print_server_url_.IntPort(); |
| + } else { |
| + host = cupsServer(); |
| + port = ippPort(); |
| + } |
| + |
| + http_t* connection = |
| + httpConnect2(host, port, NULL, AF_UNSPEC, cups_encryption_, |
| + blocking_ ? 1 : 0, kTimeoutMs, NULL); |
| + |
| + if (connection == NULL) |
|
Lei Zhang
2016/07/08 01:19:50
NULL -> nullptr
foo == NULL -> !foo
skau
2016/07/08 21:24:08
Thanks!
|
| + return false; |
| + |
| + cups_http_ = {connection, HttpDeleter()}; |
| + 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, |
| + NULL, // 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>(); |
| + } |
| + |
| + std::vector<CupsPrinter> printers; |
| + std::vector<cups_dest_t*>& dests = enumerator.get_dests(); |
| + for (cups_dest_t* dest : dests) { |
| + CupsPrinter printer(cups_http_, dest, nullptr); |
| + printers.push_back(std::move(printer)); |
| + } |
| + |
| + dests.clear(); // CupsPrinter takes ownership of all the cups_dest_t objects |
| + |
| + return printers; |
| +} |
| + |
| +CupsPrinter* CupsConnection::GetPrinter(const std::string& name) { |
| + if (!Connect()) |
| + return nullptr; |
| + |
| + cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), NULL); |
| + if (dest == NULL) |
| + return nullptr; |
| + |
| + cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest); |
| + return new CupsPrinter(cups_http_, dest, info); |
| +} |
| + |
| +std::string CupsConnection::server_name() const { |
| + return print_server_url_.host(); |
| +} |
| + |
| +int CupsConnection::last_error() const { |
| + return cupsLastError(); |
| +} |
| + |
| +} // namespace printing |