Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_jobs.h" | 5 #include "printing/backend/cups_jobs.h" |
| 6 | 6 |
| 7 #include <cups/ipp.h> | 7 #include <cups/ipp.h> |
| 8 | 8 |
| 9 #include <array> | 9 #include <array> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/stl_util.h" | |
| 15 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_split.h" | |
| 15 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "printing/backend/cups_deleters.h" | |
| 16 | 20 |
| 17 namespace printing { | 21 namespace printing { |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 using PReason = PrinterStatus::PrinterReason::Reason; | 24 using PReason = PrinterStatus::PrinterReason::Reason; |
| 21 using PSeverity = PrinterStatus::PrinterReason::Severity; | 25 using PSeverity = PrinterStatus::PrinterReason::Severity; |
| 22 | 26 |
| 23 // printer attributes | 27 // printer attributes |
| 24 const char kPrinterUri[] = "printer-uri"; | 28 const char kPrinterUri[] = "printer-uri"; |
| 25 const char kPrinterState[] = "printer-state"; | 29 const char kPrinterState[] = "printer-state"; |
| 26 const char kPrinterStateReasons[] = "printer-state-reasons"; | 30 const char kPrinterStateReasons[] = "printer-state-reasons"; |
| 27 const char kPrinterStateMessage[] = "printer-state-message"; | 31 const char kPrinterStateMessage[] = "printer-state-message"; |
| 28 | 32 |
| 33 const char kPrinterMakeAndModel[] = "printer-make-and-model"; | |
| 34 const char kIppVersionsSupported[] = "ipp-versions-supported"; | |
| 35 const char kIppFeaturesSupported[] = "ipp-features-supported"; | |
| 36 const char kDocumentFormatSupported[] = "document-format-supported"; | |
| 37 | |
| 29 // job attributes | 38 // job attributes |
| 30 const char kJobUri[] = "job-uri"; | 39 const char kJobUri[] = "job-uri"; |
| 31 const char kJobId[] = "job-id"; | 40 const char kJobId[] = "job-id"; |
| 32 const char kJobState[] = "job-state"; | 41 const char kJobState[] = "job-state"; |
| 33 const char kJobStateReasons[] = "job-state-reasons"; | 42 const char kJobStateReasons[] = "job-state-reasons"; |
| 34 const char kJobStateMessage[] = "job-state-message"; | 43 const char kJobStateMessage[] = "job-state-message"; |
| 35 const char kJobImpressionsCompleted[] = "job-impressions-completed"; | 44 const char kJobImpressionsCompleted[] = "job-impressions-completed"; |
| 36 const char kTimeAtProcessing[] = "time-at-processing"; | 45 const char kTimeAtProcessing[] = "time-at-processing"; |
| 37 | 46 |
| 38 // request parameters | 47 // request parameters |
| 39 const char kRequestedAttributes[] = "requested-attributes"; | 48 const char kRequestedAttributes[] = "requested-attributes"; |
| 40 const char kWhichJobs[] = "which-jobs"; | 49 const char kWhichJobs[] = "which-jobs"; |
| 41 const char kLimit[] = "limit"; | 50 const char kLimit[] = "limit"; |
| 42 | 51 |
| 43 // request values | 52 // request values |
| 44 const char kCompleted[] = "completed"; | 53 const char kCompleted[] = "completed"; |
| 45 const char kNotCompleted[] = "not-completed"; | 54 const char kNotCompleted[] = "not-completed"; |
| 46 | 55 |
| 56 // ipp features | |
| 57 const char kIppEverywhere[] = "ipp-everywhere"; | |
| 58 | |
| 47 // printer state severities | 59 // printer state severities |
| 48 const char kSeverityReport[] = "report"; | 60 const char kSeverityReport[] = "report"; |
| 49 const char kSeverityWarn[] = "warning"; | 61 const char kSeverityWarn[] = "warning"; |
| 50 const char kSeverityError[] = "error"; | 62 const char kSeverityError[] = "error"; |
| 51 | 63 |
| 52 // printer state reason values | 64 // printer state reason values |
| 53 const char kNone[] = "none"; | 65 const char kNone[] = "none"; |
| 54 const char kMediaNeeded[] = "media-needed"; | 66 const char kMediaNeeded[] = "media-needed"; |
| 55 const char kMediaJam[] = "media-jam"; | 67 const char kMediaJam[] = "media-jam"; |
| 56 const char kMovingToPaused[] = "moving-to-paused"; | 68 const char kMovingToPaused[] = "moving-to-paused"; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 81 const char kOpcNearEol[] = "opc-near-eol"; | 93 const char kOpcNearEol[] = "opc-near-eol"; |
| 82 const char kOpcLifeOver[] = "opc-life-over"; | 94 const char kOpcLifeOver[] = "opc-life-over"; |
| 83 const char kDeveloperLow[] = "developer-low"; | 95 const char kDeveloperLow[] = "developer-low"; |
| 84 const char kDeveloperEmpty[] = "developer-empty"; | 96 const char kDeveloperEmpty[] = "developer-empty"; |
| 85 const char kInterpreterResourceUnavailable[] = | 97 const char kInterpreterResourceUnavailable[] = |
| 86 "interpreter-resource-unavailable"; | 98 "interpreter-resource-unavailable"; |
| 87 | 99 |
| 88 constexpr std::array<const char* const, 3> kPrinterAttributes{ | 100 constexpr std::array<const char* const, 3> kPrinterAttributes{ |
| 89 {kPrinterState, kPrinterStateReasons, kPrinterStateMessage}}; | 101 {kPrinterState, kPrinterStateReasons, kPrinterStateMessage}}; |
| 90 | 102 |
| 91 std::unique_ptr<ipp_t, void (*)(ipp_t*)> WrapIpp(ipp_t* ipp) { | 103 constexpr std::array<const char* const, 4> kPrinterInfo{ |
| 92 return std::unique_ptr<ipp_t, void (*)(ipp_t*)>(ipp, &ippDelete); | 104 {kPrinterMakeAndModel, kIppVersionsSupported, kIppFeaturesSupported, |
| 105 kDocumentFormatSupported}}; | |
| 106 | |
| 107 using IppPtr = std::unique_ptr<ipp_t, void (*)(ipp_t*)>; | |
| 108 | |
| 109 IppPtr WrapIpp(ipp_t* ipp) { | |
| 110 return IppPtr(ipp, &ippDelete); | |
| 93 } | 111 } |
| 94 | 112 |
| 113 using HttpPtr = std::unique_ptr<http_t, HttpDeleter>; | |
| 114 | |
| 95 // Converts an IPP attribute |attr| to the appropriate JobState enum. | 115 // Converts an IPP attribute |attr| to the appropriate JobState enum. |
| 96 CupsJob::JobState ToJobState(ipp_attribute_t* attr) { | 116 CupsJob::JobState ToJobState(ipp_attribute_t* attr) { |
| 97 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); | 117 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); |
| 98 int state = ippGetInteger(attr, 0); | 118 int state = ippGetInteger(attr, 0); |
| 99 switch (state) { | 119 switch (state) { |
| 100 case IPP_JOB_ABORTED: | 120 case IPP_JOB_ABORTED: |
| 101 return CupsJob::ABORTED; | 121 return CupsJob::ABORTED; |
| 102 case IPP_JOB_CANCELLED: | 122 case IPP_JOB_CANCELLED: |
| 103 return CupsJob::CANCELED; | 123 return CupsJob::CANCELED; |
| 104 case IPP_JOB_COMPLETED: | 124 case IPP_JOB_COMPLETED: |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 ParseField(attr, attribute_name, current_job); | 294 ParseField(attr, attribute_name, current_job); |
| 275 } | 295 } |
| 276 } | 296 } |
| 277 | 297 |
| 278 // Returns the uri for printer with |id| as served by CUPS. Assumes that |id| | 298 // Returns the uri for printer with |id| as served by CUPS. Assumes that |id| |
| 279 // is a valid CUPS printer name and performs no error checking or escaping. | 299 // is a valid CUPS printer name and performs no error checking or escaping. |
| 280 std::string PrinterUriFromName(const std::string& id) { | 300 std::string PrinterUriFromName(const std::string& id) { |
| 281 return base::StringPrintf("ipp://localhost/printers/%s", id.c_str()); | 301 return base::StringPrintf("ipp://localhost/printers/%s", id.c_str()); |
| 282 } | 302 } |
| 283 | 303 |
| 304 // Parses the |version| string into a <major, minor> pair. Returns false if | |
| 305 // parsing failed. |major_minor| is only modified if parsing was successful. | |
| 306 bool ToVersionNumber(const std::string& version, | |
| 307 std::pair<int, int>* major_minor) { | |
| 308 std::vector<base::StringPiece> pieces = SplitStringPiece( | |
| 309 version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 310 int major; | |
| 311 int minor; | |
| 312 if (pieces.size() != 2U || !base::StringToInt(pieces[0], &major) || | |
| 313 !base::StringToInt(pieces[1], &minor)) { | |
| 314 return false; | |
| 315 } | |
| 316 | |
| 317 major_minor->first = major; | |
| 318 major_minor->second = minor; | |
| 319 return true; | |
| 320 } | |
| 321 | |
| 322 // Extracts PrinterInfo fields from |response| and populates |printer_info|. | |
| 323 // Returns true if at least printer-make-and-model and ipp-versions-supported | |
| 324 // were read. | |
| 325 bool ParsePrinterInfo(ipp_t* response, PrinterInfo* printer_info) { | |
| 326 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr; | |
| 327 attr = ippNextAttribute(response)) { | |
| 328 base::StringPiece name = ippGetName(attr); | |
| 329 | |
| 330 if (name == base::StringPiece(kPrinterMakeAndModel)) { | |
| 331 DCHECK_EQ(IPP_TAG_TEXT, ippGetValueTag(attr)); | |
|
Carlson
2017/05/30 17:36:41
Is DCHECK the right thing here? If the condition
skau
2017/05/31 00:01:20
I believe that DCHECK is correct. This condition
| |
| 332 printer_info->make_and_model = ippGetString(attr, 0, nullptr); | |
| 333 } else if (name == base::StringPiece(kIppVersionsSupported)) { | |
| 334 std::vector<std::string> ipp_versions; | |
| 335 ParseCollection(attr, &ipp_versions); | |
| 336 for (const std::string& version : ipp_versions) { | |
| 337 std::pair<int, int> major_minor; | |
| 338 if (ToVersionNumber(version, &major_minor)) { | |
| 339 printer_info->ipp_versions.push_back(major_minor); | |
| 340 } | |
| 341 } | |
| 342 } else if (name == base::StringPiece(kIppFeaturesSupported)) { | |
| 343 std::vector<std::string> features; | |
| 344 ParseCollection(attr, &features); | |
| 345 printer_info->ipp_everywhere = | |
| 346 base::ContainsValue(features, kIppEverywhere); | |
| 347 } else if (name == base::StringPiece(kDocumentFormatSupported)) { | |
| 348 ParseCollection(attr, &printer_info->document_formats); | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 return !printer_info->make_and_model.empty() && | |
| 353 !printer_info->ipp_versions.empty(); | |
| 354 } | |
| 355 | |
| 284 } // namespace | 356 } // namespace |
| 285 | 357 |
| 286 CupsJob::CupsJob() = default; | 358 CupsJob::CupsJob() = default; |
| 287 | 359 |
| 288 CupsJob::CupsJob(const CupsJob& other) = default; | 360 CupsJob::CupsJob(const CupsJob& other) = default; |
| 289 | 361 |
| 290 CupsJob::~CupsJob() = default; | 362 CupsJob::~CupsJob() = default; |
| 291 | 363 |
| 292 PrinterStatus::PrinterStatus() = default; | 364 PrinterStatus::PrinterStatus() = default; |
| 293 | 365 |
| 294 PrinterStatus::PrinterStatus(const PrinterStatus& other) = default; | 366 PrinterStatus::PrinterStatus(const PrinterStatus& other) = default; |
| 295 | 367 |
| 296 PrinterStatus::~PrinterStatus() = default; | 368 PrinterStatus::~PrinterStatus() = default; |
| 297 | 369 |
| 370 PrinterInfo::PrinterInfo() = default; | |
| 371 | |
| 372 PrinterInfo::~PrinterInfo() = default; | |
| 373 | |
| 298 void ParseJobsResponse(ipp_t* response, | 374 void ParseJobsResponse(ipp_t* response, |
| 299 const std::string& printer_id, | 375 const std::string& printer_id, |
| 300 std::vector<CupsJob>* jobs) { | 376 std::vector<CupsJob>* jobs) { |
| 301 // Advance the position in the response to the jobs section. | 377 // Advance the position in the response to the jobs section. |
| 302 ipp_attribute_t* attr = ippFirstAttribute(response); | 378 ipp_attribute_t* attr = ippFirstAttribute(response); |
| 303 while (attr != nullptr && ippGetGroupTag(attr) != IPP_TAG_JOB) { | 379 while (attr != nullptr && ippGetGroupTag(attr) != IPP_TAG_JOB) { |
| 304 attr = ippNextAttribute(response); | 380 attr = ippNextAttribute(response); |
| 305 } | 381 } |
| 306 | 382 |
| 307 if (attr != nullptr) { | 383 if (attr != nullptr) { |
| 308 ParseJobs(response, printer_id, attr, jobs); | 384 ParseJobs(response, printer_id, attr, jobs); |
| 309 } | 385 } |
| 310 } | 386 } |
| 311 | 387 |
| 388 IppPtr GetPrinterAttributes(http_t* http, | |
| 389 const std::string& printer_uri, | |
| 390 int num_attributes, | |
| 391 const char* const* attributes, | |
| 392 ipp_status_t* status) { | |
| 393 DCHECK(http); | |
| 394 | |
| 395 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES)); | |
| 396 | |
| 397 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, | |
| 398 nullptr, printer_uri.data()); | |
| 399 | |
| 400 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
| 401 kRequestedAttributes, num_attributes, nullptr, attributes); | |
| 402 | |
| 403 auto response = | |
| 404 WrapIpp(cupsDoRequest(http, request.release(), printer_uri.c_str())); | |
| 405 *status = ippGetStatusCode(response.get()); | |
| 406 | |
| 407 return response; | |
| 408 } | |
| 409 | |
| 312 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) { | 410 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) { |
| 313 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr; | 411 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr; |
| 314 attr = ippNextAttribute(response)) { | 412 attr = ippNextAttribute(response)) { |
| 315 base::StringPiece name = ippGetName(attr); | 413 base::StringPiece name = ippGetName(attr); |
| 316 if (name.empty()) { | 414 if (name.empty()) { |
| 317 continue; | 415 continue; |
| 318 } | 416 } |
| 319 | 417 |
| 320 if (name == kPrinterState) { | 418 if (name == kPrinterState) { |
| 321 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); | 419 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); |
| 322 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0)); | 420 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0)); |
| 323 } else if (name == kPrinterStateReasons) { | 421 } else if (name == kPrinterStateReasons) { |
| 324 std::vector<std::string> reason_strings; | 422 std::vector<std::string> reason_strings; |
| 325 ParseCollection(attr, &reason_strings); | 423 ParseCollection(attr, &reason_strings); |
| 326 for (const std::string& reason : reason_strings) { | 424 for (const std::string& reason : reason_strings) { |
| 327 printer_status->reasons.push_back(ToPrinterReason(reason)); | 425 printer_status->reasons.push_back(ToPrinterReason(reason)); |
| 328 } | 426 } |
| 329 } else if (name == kPrinterStateMessage) { | 427 } else if (name == kPrinterStateMessage) { |
| 330 printer_status->message = ippGetString(attr, 0, nullptr); | 428 printer_status->message = ippGetString(attr, 0, nullptr); |
| 331 } | 429 } |
| 332 } | 430 } |
| 333 } | 431 } |
| 334 | 432 |
| 433 bool GetPrinterInfo(const std::string& address, | |
| 434 const int port, | |
| 435 const std::string& resource, | |
| 436 PrinterInfo* printer_info) { | |
| 437 ipp_status_t status; | |
| 438 HttpPtr http = | |
| 439 HttpPtr(httpConnect2(address.data(), port, nullptr, AF_INET, | |
| 440 HTTP_ENCRYPTION_IF_REQUESTED, 0, 200, nullptr)); | |
| 441 auto response = GetPrinterAttributes( | |
| 442 http.get(), resource, kPrinterInfo.size(), kPrinterInfo.data(), &status); | |
| 443 return status == IPP_STATUS_OK && | |
| 444 ParsePrinterInfo(response.get(), printer_info); | |
| 445 } | |
| 446 | |
| 335 bool GetPrinterStatus(http_t* http, | 447 bool GetPrinterStatus(http_t* http, |
| 336 const std::string& printer_id, | 448 const std::string& printer_id, |
| 337 PrinterStatus* printer_status) { | 449 PrinterStatus* printer_status) { |
| 338 DCHECK(http); | 450 ipp_status_t status; |
| 339 | |
| 340 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES)); | |
| 341 | |
| 342 const std::string printer_uri = PrinterUriFromName(printer_id); | 451 const std::string printer_uri = PrinterUriFromName(printer_id); |
| 343 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, | |
| 344 nullptr, printer_uri.data()); | |
| 345 | |
| 346 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
| 347 kRequestedAttributes, kPrinterAttributes.size(), nullptr, | |
| 348 kPrinterAttributes.data()); | |
| 349 | 452 |
| 350 auto response = | 453 auto response = |
| 351 WrapIpp(cupsDoRequest(http, request.release(), printer_uri.c_str())); | 454 GetPrinterAttributes(http, printer_uri, kPrinterAttributes.size(), |
| 455 kPrinterAttributes.data(), &status); | |
| 352 | 456 |
| 353 if (ippGetStatusCode(response.get()) != IPP_STATUS_OK) | 457 if (status != IPP_STATUS_OK) |
| 354 return false; | 458 return false; |
| 355 | 459 |
| 356 ParsePrinterStatus(response.get(), printer_status); | 460 ParsePrinterStatus(response.get(), printer_status); |
| 357 | 461 |
| 358 return true; | 462 return true; |
| 359 } | 463 } |
| 360 | 464 |
| 361 bool GetCupsJobs(http_t* http, | 465 bool GetCupsJobs(http_t* http, |
| 362 const std::string& printer_id, | 466 const std::string& printer_id, |
| 363 int limit, | 467 int limit, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 399 LOG(WARNING) << "IPP Error: " << cupsLastErrorString(); | 503 LOG(WARNING) << "IPP Error: " << cupsLastErrorString(); |
| 400 return false; | 504 return false; |
| 401 } | 505 } |
| 402 | 506 |
| 403 ParseJobsResponse(response.get(), printer_id, jobs); | 507 ParseJobsResponse(response.get(), printer_id, jobs); |
| 404 | 508 |
| 405 return true; | 509 return true; |
| 406 } | 510 } |
| 407 | 511 |
| 408 } // namespace printing | 512 } // namespace printing |
| OLD | NEW |