summaryrefslogtreecommitdiff
path: root/chromium/printing/backend/cups_connection.cc
blob: 2b0f8c8355788479bcb0a67c0b83bffee2ef183a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 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 printing {

namespace {

const int kTimeoutMs = 3000;

class DestinationEnumerator {
 public:
  DestinationEnumerator() {}

  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_.emplace_back(dest); }

  // Returns the collected destinations.
  std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() {
    return dests_;
  }

 private:
  std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_;

  DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator);
};

CupsJob createCupsJob(int job_id,
                      base::StringPiece job_title,
                      base::StringPiece printer_id,
                      ipp_jstate_t state) {
  CupsJob::JobState converted_state = CupsJob::UNKNOWN;
  switch (state) {
    case IPP_JOB_ABORTED:
      converted_state = CupsJob::ABORTED;
      break;
    case IPP_JOB_CANCELLED:
      converted_state = CupsJob::CANCELED;
      break;
    case IPP_JOB_COMPLETED:
      converted_state = CupsJob::COMPLETED;
      break;
    case IPP_JOB_HELD:
      converted_state = CupsJob::HELD;
      break;
    case IPP_JOB_PENDING:
      converted_state = CupsJob::PENDING;
      break;
    case IPP_JOB_PROCESSING:
      converted_state = CupsJob::PROCESSING;
      break;
    case IPP_JOB_STOPPED:
      converted_state = CupsJob::STOPPED;
      break;
    default:
      NOTREACHED();
      break;
  }

  return {job_id, job_title.as_string(), printer_id.as_string(),
          converted_state};
}

}  // namespace

CupsConnection::CupsConnection(const GURL& print_server_url,
                               http_encryption_t encryption,
                               bool blocking)
    : print_server_url_(print_server_url),
      cups_encryption_(encryption),
      blocking_(blocking),
      cups_http_(nullptr) {}

CupsConnection::CupsConnection(CupsConnection&& connection)
    : print_server_url_(connection.print_server_url_),
      cups_encryption_(connection.cups_encryption_),
      blocking_(connection.blocking_),
      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();
  }

  cups_http_.reset(httpConnect2(host.c_str(), port, nullptr, AF_UNSPEC,
                                cups_encryption_, blocking_ ? 1 : 0, kTimeoutMs,
                                nullptr));
  return !!cups_http_;
}

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 = std::move(enumerator.get_dests());
  std::vector<CupsPrinter> printers;
  for (auto& dest : dests) {
    printers.emplace_back(cups_http_.get(), std::move(dest), nullptr);
  }

  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>(
      cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest),
      std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info));
}

std::vector<CupsJob> CupsConnection::GetJobs() {
  cups_job_t* jobs;
  int num_jobs = cupsGetJobs2(cups_http_.get(),  // http connection
                              &jobs,             // out param
                              nullptr,           // all printers
                              0,                 // all users
                              CUPS_WHICHJOBS_ALL);

  const JobsDeleter deleter(num_jobs);
  std::unique_ptr<cups_job_t, const JobsDeleter&> scoped_jobs(jobs, deleter);

  std::vector<CupsJob> job_copies;
  for (int i = 0; i < num_jobs; i++) {
    job_copies.push_back(
        createCupsJob(jobs[i].id, jobs[i].title, jobs[i].dest, jobs[i].state));
  }

  return job_copies;
}

std::string CupsConnection::server_name() const {
  return print_server_url_.host();
}

int CupsConnection::last_error() const {
  return cupsLastError();
}

}  // namespace printing