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

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

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

Powered by Google App Engine
This is Rietveld 408576698