OLD | NEW |
---|---|
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 const int kTimeoutMs = 3000; |
19 | 22 |
23 // The number of jobs we'll retrieve for a queue. | |
24 const int kQueueLimit = 10; | |
Carlson
2017/02/24 00:02:08
Probably a little more appropriate to use constexp
skau
2017/02/28 00:59:58
Done.
| |
25 | |
26 // The number of completed jobs that are retrieved. | |
27 const int kCompletedLimit = 3; | |
28 | |
20 class DestinationEnumerator { | 29 class DestinationEnumerator { |
21 public: | 30 public: |
22 DestinationEnumerator() {} | 31 DestinationEnumerator() {} |
23 | 32 |
24 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) { | 33 static int cups_callback(void* user_data, unsigned flags, cups_dest_t* dest) { |
25 cups_dest_t* copied_dest; | 34 cups_dest_t* copied_dest; |
26 cupsCopyDest(dest, 0, &copied_dest); | 35 cupsCopyDest(dest, 0, &copied_dest); |
27 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest( | 36 reinterpret_cast<DestinationEnumerator*>(user_data)->store_dest( |
28 copied_dest); | 37 copied_dest); |
29 | 38 |
30 // keep going | 39 // keep going |
31 return 1; | 40 return 1; |
32 } | 41 } |
33 | 42 |
34 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); } | 43 void store_dest(cups_dest_t* dest) { dests_.emplace_back(dest); } |
35 | 44 |
36 // Returns the collected destinations. | 45 // Returns the collected destinations. |
37 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() { | 46 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>>& get_dests() { |
38 return dests_; | 47 return dests_; |
39 } | 48 } |
40 | 49 |
41 private: | 50 private: |
42 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_; | 51 std::vector<std::unique_ptr<cups_dest_t, DestinationDeleter>> dests_; |
43 | 52 |
44 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator); | 53 DISALLOW_COPY_AND_ASSIGN(DestinationEnumerator); |
45 }; | 54 }; |
46 | 55 |
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 | 56 } // namespace |
84 | 57 |
85 CupsConnection::CupsConnection(const GURL& print_server_url, | 58 CupsConnection::CupsConnection(const GURL& print_server_url, |
86 http_encryption_t encryption, | 59 http_encryption_t encryption, |
87 bool blocking) | 60 bool blocking) |
88 : print_server_url_(print_server_url), | 61 : print_server_url_(print_server_url), |
89 cups_encryption_(encryption), | 62 cups_encryption_(encryption), |
90 blocking_(blocking), | 63 blocking_(blocking), |
91 cups_http_(nullptr) {} | 64 cups_http_(nullptr) {} |
92 | 65 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr); | 128 cups_dest_t* dest = cupsGetNamedDest(cups_http_.get(), name.c_str(), nullptr); |
156 if (!dest) | 129 if (!dest) |
157 return nullptr; | 130 return nullptr; |
158 | 131 |
159 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest); | 132 cups_dinfo_t* info = cupsCopyDestInfo(cups_http_.get(), dest); |
160 return base::MakeUnique<CupsPrinter>( | 133 return base::MakeUnique<CupsPrinter>( |
161 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest), | 134 cups_http_.get(), std::unique_ptr<cups_dest_t, DestinationDeleter>(dest), |
162 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info)); | 135 std::unique_ptr<cups_dinfo_t, DestInfoDeleter>(info)); |
163 } | 136 } |
164 | 137 |
165 std::vector<CupsJob> CupsConnection::GetJobs() { | 138 bool CupsConnection::GetJobs(const std::vector<std::string>& printer_ids, |
166 cups_job_t* jobs; | 139 std::vector<QueueStatus>* queues) { |
167 int num_jobs = cupsGetJobs2(cups_http_.get(), // http connection | 140 if (!Connect()) { |
168 &jobs, // out param | 141 LOG(ERROR) << "Could not establish connection to CUPS"; |
169 nullptr, // all printers | 142 return false; |
170 0, // all users | |
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 } | 143 } |
181 | 144 |
182 return job_copies; | 145 bool success = true; |
146 for (const std::string& id : printer_ids) { | |
147 PrinterStatus printer_status; | |
148 if (!GetPrinterStatus(cups_http_.get(), id, &printer_status)) { | |
149 LOG(WARNING) << "Could not retrieve printer status for " << id; | |
150 success = false; | |
Carlson
2017/02/24 00:02:08
Should these failure cases also continue the loop?
skau
2017/02/28 00:59:58
Originally, the intent was to collect all data. Ho
| |
151 } | |
152 | |
153 std::vector<CupsJob> printer_jobs; | |
154 if (!GetCupsJobs(cups_http_.get(), id, kCompletedLimit, true, | |
155 &printer_jobs)) { | |
156 LOG(WARNING) << "Could not get completed jobs for " << id; | |
157 success = false; | |
158 } | |
159 | |
160 if (!GetCupsJobs(cups_http_.get(), id, kQueueLimit, false, &printer_jobs)) { | |
161 LOG(WARNING) << "Could not get in progress jobs for " << id; | |
162 success = false; | |
163 } | |
164 | |
165 queues->emplace_back(std::move(printer_status), std::move(printer_jobs)); | |
166 } | |
167 | |
168 return success; | |
183 } | 169 } |
184 | 170 |
185 std::string CupsConnection::server_name() const { | 171 std::string CupsConnection::server_name() const { |
186 return print_server_url_.host(); | 172 return print_server_url_.host(); |
187 } | 173 } |
188 | 174 |
189 int CupsConnection::last_error() const { | 175 int CupsConnection::last_error() const { |
190 return cupsLastError(); | 176 return cupsLastError(); |
191 } | 177 } |
192 | 178 |
193 } // namespace printing | 179 } // namespace printing |
OLD | NEW |