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