1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/printing/cups_print_job_manager_utils.h"
#include <algorithm>
#include <string_view>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "chrome/browser/ash/printing/cups_print_job.h"
#include "chrome/browser/chromeos/printing/printer_error_codes.h"
#include "components/device_event_log/device_event_log.h"
#include "printing/backend/cups_jobs.h"
#include "printing/printed_document.h"
#include "printing/printer_status.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
namespace ash {
namespace {
using ::chromeos::PrinterErrorCode;
using ::chromeos::PrinterErrorCodeFromPrinterStatusReasons;
// The amount of time elapsed from print job creation before a timeout is
// acknowledged. CUPS has a timeout of ~25s.
constexpr base::TimeDelta kMinElaspedPrintJobTimeout = base::Seconds(30);
// Returns the equivalent CupsPrintJob#State from a CupsJob#JobState.
CupsPrintJob::State ConvertState(::printing::CupsJob::JobState state) {
switch (state) {
case ::printing::CupsJob::PENDING:
return CupsPrintJob::State::STATE_WAITING;
case ::printing::CupsJob::HELD:
return CupsPrintJob::State::STATE_SUSPENDED;
case ::printing::CupsJob::PROCESSING:
return CupsPrintJob::State::STATE_STARTED;
case ::printing::CupsJob::CANCELED:
return CupsPrintJob::State::STATE_CANCELLED;
case ::printing::CupsJob::COMPLETED:
return CupsPrintJob::State::STATE_DOCUMENT_DONE;
case ::printing::CupsJob::STOPPED:
return CupsPrintJob::State::STATE_SUSPENDED;
case ::printing::CupsJob::ABORTED:
return CupsPrintJob::State::STATE_FAILED;
case ::printing::CupsJob::UNKNOWN:
break;
}
NOTREACHED();
}
// Returns a string description of CUPS printer-state names
std::string_view PrinterStateName(ipp_pstate_t state) {
switch (state) {
case IPP_PSTATE_IDLE:
return "idle";
case IPP_PSTATE_PROCESSING:
return "processing";
case IPP_PSTATE_STOPPED:
return "stopped";
default:
return "unknown";
}
}
// Returns a string description of CUPS job-state names
std::string_view JobStateName(::printing::CupsJob::JobState state) {
switch (state) {
case ::printing::CupsJob::PENDING:
return "pending";
case ::printing::CupsJob::HELD:
return "held";
case ::printing::CupsJob::PROCESSING:
return "processing";
case ::printing::CupsJob::CANCELED:
return "canceled";
case ::printing::CupsJob::COMPLETED:
return "completed";
case ::printing::CupsJob::STOPPED:
return "stopped";
case ::printing::CupsJob::ABORTED:
return "aborted";
case ::printing::CupsJob::UNKNOWN:
return "unknown";
}
NOTREACHED();
}
std::string GetStateDescription(const ::printing::PrinterStatus& printer_status,
const ::printing::CupsJob& job) {
return base::StringPrintf("job=%s[%s] printer=%s[%s]",
JobStateName(job.state),
base::JoinString(job.state_reasons, ";"),
PrinterStateName(printer_status.state),
printer_status.AllReasonsAsString());
}
// Update the current printed page. Returns true of the page has been updated.
bool UpdateCurrentPage(const ::printing::CupsJob& job,
CupsPrintJob* print_job) {
bool pages_updated = false;
if (job.current_pages <= 0 ||
print_job->state() == CupsPrintJob::State::STATE_WAITING) {
print_job->set_printed_page_number(std::max(job.current_pages, 0));
print_job->set_state(CupsPrintJob::State::STATE_STARTED);
} else {
pages_updated = job.current_pages != print_job->printed_page_number();
print_job->set_printed_page_number(job.current_pages);
print_job->set_state(CupsPrintJob::State::STATE_PAGE_DONE);
}
return pages_updated;
}
void UpdateProcessingJob(const ::printing::PrinterStatus& printer_status,
const ::printing::CupsJob& job,
CupsPrintJob* print_job,
bool* pages_updated) {
*pages_updated = UpdateCurrentPage(job, print_job);
const PrinterErrorCode printer_error_code =
PrinterErrorCodeFromPrinterStatusReasons(printer_status);
const bool delay_print_job_timeout =
printer_error_code == PrinterErrorCode::PRINTER_UNREACHABLE &&
(base::Time::Now() - print_job->creation_time() <
kMinElaspedPrintJobTimeout);
if (printer_error_code != PrinterErrorCode::NO_ERROR &&
!delay_print_job_timeout) {
print_job->set_error_code(printer_error_code);
print_job->set_state(printer_error_code ==
PrinterErrorCode::PRINTER_UNREACHABLE
? CupsPrintJob::State::STATE_FAILED
: CupsPrintJob::State::STATE_ERROR);
} else {
print_job->set_error_code(PrinterErrorCode::NO_ERROR);
}
}
void UpdateCompletedJob(const ::printing::PrinterStatus& printer_status,
const ::printing::CupsJob& job,
CupsPrintJob* print_job) {
if (job.current_pages >= print_job->total_page_number()) {
print_job->set_error_code(PrinterErrorCode::NO_ERROR);
print_job->set_state(CupsPrintJob::State::STATE_DOCUMENT_DONE);
} else {
print_job->set_error_code(
PrinterErrorCodeFromPrinterStatusReasons(printer_status));
print_job->set_state(CupsPrintJob::State::STATE_CANCELLED);
}
}
void UpdateStoppedJob(const ::printing::CupsJob& job, CupsPrintJob* print_job) {
// If cups job STOPPED but with filter failure, treat as ERROR
if (job.ContainsStateReason(
::printing::CupsJob::JobStateReason::kJobCompletedWithErrors)) {
print_job->set_error_code(PrinterErrorCode::FILTER_FAILED);
print_job->set_state(CupsPrintJob::State::STATE_FAILED);
} else {
print_job->set_error_code(PrinterErrorCode::NO_ERROR);
print_job->set_state(ConvertState(job.state));
}
}
void UpdateHeldJob(const ::printing::CupsJob& job, CupsPrintJob* print_job) {
// If cups job STOPPED but with cups held for authentication, treat as ERROR
if (job.ContainsStateReason(
::printing::CupsJob::JobStateReason::kCupsHeldForAuthentication)) {
print_job->set_error_code(PrinterErrorCode::CLIENT_UNAUTHORIZED);
print_job->set_state(CupsPrintJob::State::STATE_FAILED);
} else {
print_job->set_error_code(PrinterErrorCode::NO_ERROR);
print_job->set_state(ConvertState(job.state));
}
}
} // namespace
bool UpdatePrintJob(const ::printing::PrinterStatus& printer_status,
const ::printing::CupsJob& job,
CupsPrintJob* print_job) {
static absl::flat_hash_map<int, std::string> old_status;
DCHECK_EQ(job.id, print_job->job_id());
CupsPrintJob::State old_state = print_job->state();
bool pages_updated = false;
switch (job.state) {
case ::printing::CupsJob::PROCESSING:
UpdateProcessingJob(printer_status, job, print_job, &pages_updated);
break;
case ::printing::CupsJob::COMPLETED:
UpdateCompletedJob(printer_status, job, print_job);
break;
case ::printing::CupsJob::STOPPED:
UpdateStoppedJob(job, print_job);
break;
case ::printing::CupsJob::HELD:
UpdateHeldJob(job, print_job);
break;
case ::printing::CupsJob::ABORTED:
case ::printing::CupsJob::CANCELED:
print_job->set_error_code(
PrinterErrorCodeFromPrinterStatusReasons(printer_status));
[[fallthrough]];
default:
print_job->set_state(ConvertState(job.state));
break;
}
// If the status has changed since the last time the job was polled, log the
// new status. The printer-state-reasons and job-state-reasons can change
// even if the job-state doesn't change, so this is based on matching the
// previous status message instead of looking at just the state.
bool updated = print_job->state() != old_state || pages_updated;
std::string status = base::StringPrintf(
"%s: job %d changed to page %d/%d with state: %s", job.printer_id, job.id,
print_job->printed_page_number(), print_job->total_page_number(),
GetStateDescription(printer_status, job));
if (status != old_status[job.id]) {
if (updated) {
PRINTER_LOG(EVENT) << status;
} else {
PRINTER_LOG(DEBUG) << status;
}
old_status[job.id] = status;
}
if (job.state == ::printing::CupsJob::COMPLETED ||
job.state == ::printing::CupsJob::CANCELED ||
job.state == ::printing::CupsJob::ABORTED) {
// No need to save statuses for terminal states, since no more updates are
// expected.
old_status.erase(job.id);
}
return updated;
}
int CalculatePrintJobTotalPages(const ::printing::PrintedDocument* document) {
if (document->settings().copies() == 0) {
return document->page_count();
}
return document->page_count() * document->settings().copies();
}
} // namespace ash
|