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

Side by Side Diff: printing/backend/cups_connection.cc

Issue 2691093006: Implement IPP Get-Jobs and Get-Printer-Attributes requests. (Closed)
Patch Set: use a const array Created 3 years, 9 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_ipp_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "printing/backend/cups_connection.h" 5 #include "printing/backend/cups_connection.h"
6 6
7 #include <map>
8 #include <set>
7 #include <string> 9 #include <string>
8 #include <utility> 10 #include <utility>
9 11
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
12 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "printing/backend/cups_jobs.h"
13 16
14 namespace printing { 17 namespace printing {
15 18
16 namespace { 19 namespace {
17 20
18 const int kTimeoutMs = 3000; 21 constexpr int kTimeoutMs = 3000;
22
23 // The number of jobs we'll retrieve for a queue. We expect a user to queue at
24 // most 10 jobs per printer. If they queue more, they won't receive updates for
25 // the 11th job until one finishes.
26 constexpr int kProcessingJobsLimit = 10;
27
28 // The number of completed jobs that are retrieved. We only need one update for
29 // a completed job to confirm its final status. We could retrieve one but we
30 // retrieve the last 3 in case that many finished between queries.
31 constexpr int kCompletedJobsLimit = 3;
19 32
20 class DestinationEnumerator { 33 class DestinationEnumerator {
21 public: 34 public:
22 DestinationEnumerator() {} 35 DestinationEnumerator() {}
23 36
24 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) { 37 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) {
25 cups_dest_t* copied_dest; 38 cups_dest_t* copied_dest;
26 cupsCopyDest(dest, 0, &copied_dest); 39 cupsCopyDest(dest, 0, &copied_dest);
27 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest( 40 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest(
28 copied_dest); 41 copied_dest);
29 42
30 // keep going 43 // keep going
31 return 1; 44 return 1;
32 } 45 }
33 46
34 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); } 47 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); }
35 48
36 // Returns the collected destinations. 49 // Returns the collected destinations.
37 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() { 50 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() {
38 return dests_; 51 return dests_;
39 } 52 }
40 53
41 private: 54 private:
42 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_; 55 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_;
43 56
44 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator); 57 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator);
45 }; 58 };
46 59
47 CupsJob createCupsJob(int job_id,
48 base::StringPiece job_title,
49 base::StringPiece printer_id,
50 ipp_jstate_t state) {
51 CupsJob::JobState converted_state = CupsJob::UNKNOWN;
52 switch (state) {
53 case IPP_JOB_ABORTED:
54 converted_state = CupsJob::ABORTED;
55 break;
56 case IPP_JOB_CANCELLED:
57 converted_state = CupsJob::CANCELED;
58 break;
59 case IPP_JOB_COMPLETED:
60 converted_state = CupsJob::COMPLETED;
61 break;
62 case IPP_JOB_HELD:
63 converted_state = CupsJob::HELD;
64 break;
65 case IPP_JOB_PENDING:
66 converted_state = CupsJob::PENDING;
67 break;
68 case IPP_JOB_PROCESSING:
69 converted_state = CupsJob::PROCESSING;
70 break;
71 case IPP_JOB_STOPPED:
72 converted_state = CupsJob::STOPPED;
73 break;
74 default:
75 NOTREACHED();
76 break;
77 }
78
79 return {job_id, job_title.as_string(), printer_id.as_string(),
80 converted_state};
81 }
82
83 } // namespace 60 } // namespace
84 61
85 CupsConnection::CupsConnection(const GURL& print_server_url, 62 CupsConnection::CupsConnection(const GURL& print_server_url,
86 http_encryption_t encryption, 63 http_encryption_t encryption,
87 bool blocking) 64 bool blocking)
88 : print_server_url_(print_server_url), 65 : print_server_url_(print_server_url),
89 cups_encryption_(encryption), 66 cups_encryption_(encryption),
90 blocking_(blocking), 67 blocking_(blocking),
91 cups_http_(nullptr) {} 68 cups_http_(nullptr) {}
92 69
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr); 132 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr);
156 if (!dest) 133 if (!dest)
157 return nullptr; 134 return nullptr;
158 135
159 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest); 136 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest);
160 return base::MakeUnique<CupsPrinter>( 137 return base::MakeUnique<CupsPrinter>(
161 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest), 138 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest),
162 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info)); 139 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info));
163 } 140 }
164 141
165 std::vector<CupsJob> CupsConnection::GetJobs() { 142 bool CupsConnection::GetJobs(const std::vector<std::string>& printer_ids,
166 cups_job_t* jobs; 143 std::vector<QueueStatus>* queues) {
167 int num_jobs = cupsGetJobs2(cups_http_.get(), // http connection 144 DCHECK(queues);
168 &jobs, // out param 145 if (!Connect()) {
169 nullptr, // all printers 146 LOG(ERROR) << "Could not establish connection to CUPS";
170 0, // all users 147 return false;
171 CUPS_WHICHJOBS_ALL);
172
173 const JobsDeleter deleter(num_jobs);
174 std::unique_ptr<cups_job_t, const JobsDeleter&> scoped_jobs(jobs, deleter);
175
176 std::vector<CupsJob> job_copies;
177 for (int i = 0; i < num_jobs; i++) {
178 job_copies.push_back(
179 createCupsJob(jobs[i].id, jobs[i].title, jobs[i].dest, jobs[i].state));
180 } 148 }
181 149
182 return job_copies; 150 std::vector<QueueStatus> temp_queues;
151
152 for (const std::string& id : printer_ids) {
153 temp_queues.emplace_back();
154 QueueStatus* queue_status = &temp_queues.back();
155
156 if (!GetPrinterStatus(cups_http_.get(), id,
157 &queue_status->printer_status)) {
158 LOG(WARNING) << "Could not retrieve printer status for " << id;
159 return false;
160 }
161
162 if (!GetCupsJobs(cups_http_.get(), id, kCompletedJobsLimit, COMPLETED,
163 &queue_status->jobs)) {
164 LOG(WARNING) << "Could not get completed jobs for " << id;
165 return false;
166 }
167
168 if (!GetCupsJobs(cups_http_.get(), id, kProcessingJobsLimit, PROCESSING,
169 &queue_status->jobs)) {
170 LOG(WARNING) << "Could not get in progress jobs for " << id;
171 return false;
172 }
173 }
174 queues->insert(queues->end(), temp_queues.begin(), temp_queues.end());
175
176 return true;
183 } 177 }
184 178
185 std::string CupsConnection::server_name() const { 179 std::string CupsConnection::server_name() const {
186 return print_server_url_.host(); 180 return print_server_url_.host();
187 } 181 }
188 182
189 int CupsConnection::last_error() const { 183 int CupsConnection::last_error() const {
190 return cupsLastError(); 184 return cupsLastError();
191 } 185 }
192 186
193 } // namespace printing 187 } // namespace printing
OLDNEW
« no previous file with comments | « printing/backend/cups_connection.h ('k') | printing/backend/cups_ipp_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698