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..b2af038ebe1861101f88ef4f9e8572bd5b01c58a 100644 |
| --- a/printing/backend/cups_connection.cc |
| +++ b/printing/backend/cups_connection.cc |
| @@ -4,18 +4,27 @@ |
| #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 { |
| namespace { |
| -const int kTimeoutMs = 3000; |
| +constexpr int kTimeoutMs = 3000; |
| + |
| +// The number of jobs we'll retrieve for a queue. |
| +constexpr int kQueueLimit = 10; |
|
Lei Zhang
2017/03/07 23:31:29
Why not |kProcessingJobsLimit| and |kCompletedJobs
skau
2017/03/10 01:07:00
Done.
|
| + |
| +// The number of completed jobs that are retrieved. |
| +constexpr int kCompletedLimit = 3; |
| class DestinationEnumerator { |
| public: |
| @@ -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,47 @@ 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) { |
| + DCHECK(queues); |
| + if (!Connect()) { |
| + LOG(ERROR) << "Could not establish connection to CUPS"; |
| + return false; |
| + } |
| + |
| + std::vector<QueueStatus> temp_queues; |
| + |
| + bool success = true; |
| + for (const std::string& id : printer_ids) { |
| + temp_queues.emplace_back(); |
| + QueueStatus* queue_status = &temp_queues.back(); |
| + |
| + if (!GetPrinterStatus(cups_http_.get(), id, |
| + &queue_status->printer_status)) { |
| + LOG(WARNING) << "Could not retrieve printer status for " << id; |
| + success = false; |
| + break; |
| + } |
| + |
| + if (!GetCupsJobs(cups_http_.get(), id, kCompletedLimit, COMPLETED, |
| + &queue_status->jobs)) { |
| + LOG(WARNING) << "Could not get completed jobs for " << id; |
| + success = false; |
|
Lei Zhang
2017/03/07 23:31:29
Drop the success variable and just return false?
skau
2017/03/10 01:07:00
Done.
|
| + break; |
| + } |
| + |
| + if (!GetCupsJobs(cups_http_.get(), id, kQueueLimit, PROCESSING, |
| + &queue_status->jobs)) { |
| + LOG(WARNING) << "Could not get in progress jobs for " << id; |
| + success = false; |
| + break; |
| + } |
| } |
| - return job_copies; |
| + if (success) |
| + queues->insert(queues->end(), temp_queues.begin(), temp_queues.end()); |
| + |
| + return success; |
| } |
| std::string CupsConnection::server_name() const { |