Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: printing/backend/cups_jobs.cc

Issue 2891643002: Add a method to query IPP printers for attributes. (Closed)
Patch Set: check empty Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « printing/backend/cups_jobs.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
14 #include "base/strings/string_piece.h" 15 #include "base/strings/string_piece.h"
15 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/version.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
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 ScopedIppPtr = std::unique_ptr<ipp_t, void (*)(ipp_t*)>;
108
109 ScopedIppPtr WrapIpp(ipp_t* ipp) {
110 return ScopedIppPtr(ipp, &ippDelete);
93 } 111 }
94 112
113 using ScopedHttpPtr = 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
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 // Extracts PrinterInfo fields from |response| and populates |printer_info|.
305 // Returns true if at least printer-make-and-model and ipp-versions-supported
306 // were read.
307 bool ParsePrinterInfo(ipp_t* response, PrinterInfo* printer_info) {
308 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr;
309 attr = ippNextAttribute(response)) {
310 base::StringPiece name = ippGetName(attr);
311
312 if (name == base::StringPiece(kPrinterMakeAndModel)) {
313 DCHECK_EQ(IPP_TAG_TEXT, ippGetValueTag(attr));
314 printer_info->make_and_model = ippGetString(attr, 0, nullptr);
315 } else if (name == base::StringPiece(kIppVersionsSupported)) {
316 std::vector<std::string> ipp_versions;
317 ParseCollection(attr, &ipp_versions);
318 for (const std::string& version : ipp_versions) {
319 base::Version major_minor(version);
320 if (major_minor.IsValid()) {
321 printer_info->ipp_versions.push_back(major_minor);
322 }
323 }
324 } else if (name == base::StringPiece(kIppFeaturesSupported)) {
325 std::vector<std::string> features;
326 ParseCollection(attr, &features);
327 printer_info->ipp_everywhere =
328 base::ContainsValue(features, kIppEverywhere);
329 } else if (name == base::StringPiece(kDocumentFormatSupported)) {
330 ParseCollection(attr, &printer_info->document_formats);
331 }
332 }
333
334 return !printer_info->make_and_model.empty() &&
335 !printer_info->ipp_versions.empty();
336 }
337
284 } // namespace 338 } // namespace
285 339
286 CupsJob::CupsJob() = default; 340 CupsJob::CupsJob() = default;
287 341
288 CupsJob::CupsJob(const CupsJob& other) = default; 342 CupsJob::CupsJob(const CupsJob& other) = default;
289 343
290 CupsJob::~CupsJob() = default; 344 CupsJob::~CupsJob() = default;
291 345
292 PrinterStatus::PrinterStatus() = default; 346 PrinterStatus::PrinterStatus() = default;
293 347
294 PrinterStatus::PrinterStatus(const PrinterStatus& other) = default; 348 PrinterStatus::PrinterStatus(const PrinterStatus& other) = default;
295 349
296 PrinterStatus::~PrinterStatus() = default; 350 PrinterStatus::~PrinterStatus() = default;
297 351
352 PrinterInfo::PrinterInfo() = default;
353
354 PrinterInfo::~PrinterInfo() = default;
355
298 void ParseJobsResponse(ipp_t* response, 356 void ParseJobsResponse(ipp_t* response,
299 const std::string& printer_id, 357 const std::string& printer_id,
300 std::vector<CupsJob>* jobs) { 358 std::vector<CupsJob>* jobs) {
301 // Advance the position in the response to the jobs section. 359 // Advance the position in the response to the jobs section.
302 ipp_attribute_t* attr = ippFirstAttribute(response); 360 ipp_attribute_t* attr = ippFirstAttribute(response);
303 while (attr != nullptr && ippGetGroupTag(attr) != IPP_TAG_JOB) { 361 while (attr != nullptr && ippGetGroupTag(attr) != IPP_TAG_JOB) {
304 attr = ippNextAttribute(response); 362 attr = ippNextAttribute(response);
305 } 363 }
306 364
307 if (attr != nullptr) { 365 if (attr != nullptr) {
308 ParseJobs(response, printer_id, attr, jobs); 366 ParseJobs(response, printer_id, attr, jobs);
309 } 367 }
310 } 368 }
311 369
370 // Returns an IPP response for a Get-Printer-Attributes request to |http|. For
371 // print servers, |printer_uri| is used as the printer-uri value.
372 // |resource_path| specifies the path portion of the server URI.
373 // |num_attributes| is the number of attributes in |attributes| which should be
374 // a list of IPP attributes. |status| is updated with status code for the
375 // request. A successful request will have the |status| IPP_STATUS_OK.
376 ScopedIppPtr GetPrinterAttributes(http_t* http,
377 const std::string& printer_uri,
378 const std::string& resource_path,
379 int num_attributes,
380 const char* const* attributes,
381 ipp_status_t* status) {
382 base::ThreadRestrictions::AssertIOAllowed();
383 DCHECK(http);
384
385 // CUPS expects a leading slash for resource names. Add one if it's missing.
386 std::string rp = !resource_path.empty() && resource_path.front() == '/'
387 ? resource_path
388 : "/" + resource_path;
389
390 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES));
391
392 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri,
393 nullptr, printer_uri.data());
394
395 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
396 kRequestedAttributes, num_attributes, nullptr, attributes);
397
398 auto response = WrapIpp(cupsDoRequest(http, request.release(), rp.c_str()));
399 *status = ippGetStatusCode(response.get());
400
401 return response;
402 }
403
312 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) { 404 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) {
313 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr; 405 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr;
314 attr = ippNextAttribute(response)) { 406 attr = ippNextAttribute(response)) {
315 base::StringPiece name = ippGetName(attr); 407 base::StringPiece name = ippGetName(attr);
316 if (name.empty()) { 408 if (name.empty()) {
317 continue; 409 continue;
318 } 410 }
319 411
320 if (name == kPrinterState) { 412 if (name == kPrinterState) {
321 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); 413 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr));
322 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0)); 414 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0));
323 } else if (name == kPrinterStateReasons) { 415 } else if (name == kPrinterStateReasons) {
324 std::vector<std::string> reason_strings; 416 std::vector<std::string> reason_strings;
325 ParseCollection(attr, &reason_strings); 417 ParseCollection(attr, &reason_strings);
326 for (const std::string& reason : reason_strings) { 418 for (const std::string& reason : reason_strings) {
327 printer_status->reasons.push_back(ToPrinterReason(reason)); 419 printer_status->reasons.push_back(ToPrinterReason(reason));
328 } 420 }
329 } else if (name == kPrinterStateMessage) { 421 } else if (name == kPrinterStateMessage) {
330 printer_status->message = ippGetString(attr, 0, nullptr); 422 printer_status->message = ippGetString(attr, 0, nullptr);
331 } 423 }
332 } 424 }
333 } 425 }
334 426
427 bool GetPrinterInfo(const std::string& address,
428 const int port,
429 const std::string& resource,
430 PrinterInfo* printer_info) {
431 base::ThreadRestrictions::AssertIOAllowed();
432
433 ScopedHttpPtr http = ScopedHttpPtr(
434 httpConnect2(address.data(), port, nullptr, AF_INET,
435 HTTP_ENCRYPTION_IF_REQUESTED, 0, 200, nullptr));
436 if (!http) {
437 LOG(WARNING) << "Could not connect to host";
438 return false;
439 }
440
441 ipp_status_t status;
442 ScopedIppPtr response =
443 GetPrinterAttributes(http.get(), resource, resource, kPrinterInfo.size(),
444 kPrinterInfo.data(), &status);
445 if (status != IPP_STATUS_OK || response.get() == nullptr) {
446 LOG(WARNING) << "Get attributes failure: " << status;
447 return false;
448 }
449
450 return ParsePrinterInfo(response.get(), printer_info);
451 }
452
335 bool GetPrinterStatus(http_t* http, 453 bool GetPrinterStatus(http_t* http,
336 const std::string& printer_id, 454 const std::string& printer_id,
337 PrinterStatus* printer_status) { 455 PrinterStatus* printer_status) {
338 DCHECK(http); 456 base::ThreadRestrictions::AssertIOAllowed();
339 457
340 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES)); 458 ipp_status_t status;
459 const std::string printer_uri = PrinterUriFromName(printer_id);
341 460
342 const std::string printer_uri = PrinterUriFromName(printer_id); 461 ScopedIppPtr response =
343 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, 462 GetPrinterAttributes(http, printer_uri, "/", kPrinterAttributes.size(),
344 nullptr, printer_uri.data()); 463 kPrinterAttributes.data(), &status);
345 464
346 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, 465 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; 466 return false;
355 467
356 ParsePrinterStatus(response.get(), printer_status); 468 ParsePrinterStatus(response.get(), printer_status);
357 469
358 return true; 470 return true;
359 } 471 }
360 472
361 bool GetCupsJobs(http_t* http, 473 bool GetCupsJobs(http_t* http,
362 const std::string& printer_id, 474 const std::string& printer_id,
363 int limit, 475 int limit,
364 JobCompletionState which, 476 JobCompletionState which,
365 std::vector<CupsJob>* jobs) { 477 std::vector<CupsJob>* jobs) {
478 base::ThreadRestrictions::AssertIOAllowed();
366 DCHECK(http); 479 DCHECK(http);
367 480
368 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_JOBS)); 481 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_JOBS));
369 const std::string printer_uri = PrinterUriFromName(printer_id); 482 const std::string printer_uri = PrinterUriFromName(printer_id);
370 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, 483 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri,
371 nullptr, printer_uri.data()); 484 nullptr, printer_uri.data());
372 ippAddInteger(request.get(), IPP_TAG_OPERATION, IPP_TAG_INTEGER, kLimit, 485 ippAddInteger(request.get(), IPP_TAG_OPERATION, IPP_TAG_INTEGER, kLimit,
373 limit); 486 limit);
374 487
375 std::vector<const char*> job_attributes = { 488 std::vector<const char*> job_attributes = {
376 kJobUri, kJobId, kJobState, 489 kJobUri, kJobId, kJobState,
377 kJobStateReasons, kJobStateMessage, kJobImpressionsCompleted, 490 kJobStateReasons, kJobStateMessage, kJobImpressionsCompleted,
378 kTimeAtProcessing}; 491 kTimeAtProcessing};
379 492
380 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, 493 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
381 kRequestedAttributes, job_attributes.size(), nullptr, 494 kRequestedAttributes, job_attributes.size(), nullptr,
382 job_attributes.data()); 495 job_attributes.data());
383 496
384 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, kWhichJobs, 497 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, kWhichJobs,
385 nullptr, which == COMPLETED ? kCompleted : kNotCompleted); 498 nullptr, which == COMPLETED ? kCompleted : kNotCompleted);
386 499
387 if (ippValidateAttributes(request.get()) != 1) { 500 if (ippValidateAttributes(request.get()) != 1) {
388 LOG(WARNING) << "Could not validate ipp request: " << cupsLastErrorString(); 501 LOG(WARNING) << "Could not validate ipp request: " << cupsLastErrorString();
389 return false; 502 return false;
390 } 503 }
391 504
392 // cupsDoRequest will delete the request. 505 // cupsDoRequest will delete the request.
393 auto response = 506 auto response = WrapIpp(cupsDoRequest(http, request.release(), "/"));
394 WrapIpp(cupsDoRequest(http, request.release(), printer_uri.c_str()));
395 507
396 ipp_status_t status = ippGetStatusCode(response.get()); 508 ipp_status_t status = ippGetStatusCode(response.get());
397 509
398 if (status != IPP_OK) { 510 if (status != IPP_STATUS_OK) {
399 LOG(WARNING) << "IPP Error: " << cupsLastErrorString(); 511 LOG(WARNING) << "IPP Error: " << cupsLastErrorString();
400 return false; 512 return false;
401 } 513 }
402 514
403 ParseJobsResponse(response.get(), printer_id, jobs); 515 ParseJobsResponse(response.get(), printer_id, jobs);
404 516
405 return true; 517 return true;
406 } 518 }
407 519
408 } // namespace printing 520 } // namespace printing
OLDNEW
« no previous file with comments | « printing/backend/cups_jobs.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698