Chromium Code Reviews| Index: printing/backend/cups_connection.cc |
| diff --git a/printing/backend/cups_connection.cc b/printing/backend/cups_connection.cc |
| index 2b0f8c8355788479bcb0a67c0b83bffee2ef183a..2f315ad9ba58f5a07e5f524d43b45d5280cf8ff7 100644 |
| --- a/printing/backend/cups_connection.cc |
| +++ b/printing/backend/cups_connection.cc |
| @@ -4,12 +4,15 @@ |
| #include "printing/backend/cups_connection.h" |
| +#include <map> |
| +#include <set> |
| #include <string> |
| #include <utility> |
| #include "base/logging.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/strings/stringprintf.h" |
| +#include "printing/backend/cups_jobs.h" |
| namespace printing { |
| @@ -17,6 +20,12 @@ namespace { |
| const int kTimeoutMs = 3000; |
| +// The number of jobs we'll retrieve for a queue. |
| +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.
|
| + |
| +// The number of completed jobs that are retrieved. |
| +const int kCompletedLimit = 3; |
| + |
| class DestinationEnumerator { |
| public: |
| DestinationEnumerator() {} |
| @@ -44,42 +53,6 @@ class DestinationEnumerator { |
| 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, |
| @@ -162,24 +135,37 @@ std::unique_ptr<CupsPrinter> CupsConnection::GetPrinter( |
| 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)); |
| +bool CupsConnection::GetJobs(const std::vector<std::string>& printer_ids, |
| + std::vector<QueueStatus>* queues) { |
| + if (!Connect()) { |
| + LOG(ERROR) << "Could not establish connection to CUPS"; |
| + return false; |
| + } |
| + |
| + bool success = true; |
| + for (const std::string& id : printer_ids) { |
| + PrinterStatus printer_status; |
| + if (!GetPrinterStatus(cups_http_.get(), id, &printer_status)) { |
| + LOG(WARNING) << "Could not retrieve printer status for " << id; |
| + 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
|
| + } |
| + |
| + std::vector<CupsJob> printer_jobs; |
| + if (!GetCupsJobs(cups_http_.get(), id, kCompletedLimit, true, |
| + &printer_jobs)) { |
| + LOG(WARNING) << "Could not get completed jobs for " << id; |
| + success = false; |
| + } |
| + |
| + if (!GetCupsJobs(cups_http_.get(), id, kQueueLimit, false, &printer_jobs)) { |
| + LOG(WARNING) << "Could not get in progress jobs for " << id; |
| + success = false; |
| + } |
| + |
| + queues->emplace_back(std::move(printer_status), std::move(printer_jobs)); |
| } |
| - return job_copies; |
| + return success; |
| } |
| std::string CupsConnection::server_name() const { |