Chromium Code Reviews| 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 constexpr int kTimeoutMs = 3000; |
| 22 | |
| 23 // The number of jobs we'll retrieve for a queue. | |
| 24 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.
| |
| 25 | |
| 26 // The number of completed jobs that are retrieved. | |
| 27 constexpr int kCompletedLimit = 3; | |
| 19 | 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 DCHECK(queues); |
| 168 &jobs, // out param | 141 if (!Connect()) { |
| 169 nullptr, // all printers | 142 LOG(ERROR) << "Could not establish connection to CUPS"; |
| 170 0, // all users | 143 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 } | 144 } |
| 181 | 145 |
| 182 return job_copies; | 146 std::vector<QueueStatus> temp_queues; |
| 147 | |
| 148 bool success = true; | |
| 149 for (const std::string& id : printer_ids) { | |
| 150 temp_queues.emplace_back(); | |
| 151 QueueStatus* queue_status = &temp_queues.back(); | |
| 152 | |
| 153 if (!GetPrinterStatus(cups_http_.get(), id, | |
| 154 &queue_status->printer_status)) { | |
| 155 LOG(WARNING) << "Could not retrieve printer status for " << id; | |
| 156 success = false; | |
| 157 break; | |
| 158 } | |
| 159 | |
| 160 if (!GetCupsJobs(cups_http_.get(), id, kCompletedLimit, COMPLETED, | |
| 161 &queue_status->jobs)) { | |
| 162 LOG(WARNING) << "Could not get completed jobs for " << id; | |
| 163 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.
| |
| 164 break; | |
| 165 } | |
| 166 | |
| 167 if (!GetCupsJobs(cups_http_.get(), id, kQueueLimit, PROCESSING, | |
| 168 &queue_status->jobs)) { | |
| 169 LOG(WARNING) << "Could not get in progress jobs for " << id; | |
| 170 success = false; | |
| 171 break; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 if (success) | |
| 176 queues->insert(queues->end(), temp_queues.begin(), temp_queues.end()); | |
| 177 | |
| 178 return success; | |
| 183 } | 179 } |
| 184 | 180 |
| 185 std::string CupsConnection::server_name() const { | 181 std::string CupsConnection::server_name() const { |
| 186 return print_server_url_.host(); | 182 return print_server_url_.host(); |
| 187 } | 183 } |
| 188 | 184 |
| 189 int CupsConnection::last_error() const { | 185 int CupsConnection::last_error() const { |
| 190 return cupsLastError(); | 186 return cupsLastError(); |
| 191 } | 187 } |
| 192 | 188 |
| 193 } // namespace printing | 189 } // namespace printing |
| OLD | NEW |