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

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

Issue 2891643002: Add a method to query IPP printers for attributes. (Closed)
Patch Set: remove statics 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
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 ScopedIppPtr GetPrinterAttributes(http_t* http,
371 const std::string& printer_uri,
372 int num_attributes,
373 const char* const* attributes,
374 ipp_status_t* status) {
375 base::ThreadRestrictions::AssertIOAllowed();
376 DCHECK(http);
377
378 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES));
379
380 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri,
381 nullptr, printer_uri.data());
382
383 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
384 kRequestedAttributes, num_attributes, nullptr, attributes);
385
386 auto response =
387 WrapIpp(cupsDoRequest(http, request.release(), printer_uri.c_str()));
388 *status = ippGetStatusCode(response.get());
389
390 return response;
391 }
392
312 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) { 393 void ParsePrinterStatus(ipp_t* response, PrinterStatus* printer_status) {
313 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr; 394 for (ipp_attribute_t* attr = ippFirstAttribute(response); attr != nullptr;
314 attr = ippNextAttribute(response)) { 395 attr = ippNextAttribute(response)) {
315 base::StringPiece name = ippGetName(attr); 396 base::StringPiece name = ippGetName(attr);
316 if (name.empty()) { 397 if (name.empty()) {
317 continue; 398 continue;
318 } 399 }
319 400
320 if (name == kPrinterState) { 401 if (name == kPrinterState) {
321 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr)); 402 DCHECK_EQ(IPP_TAG_ENUM, ippGetValueTag(attr));
322 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0)); 403 printer_status->state = static_cast<ipp_pstate_t>(ippGetInteger(attr, 0));
323 } else if (name == kPrinterStateReasons) { 404 } else if (name == kPrinterStateReasons) {
324 std::vector<std::string> reason_strings; 405 std::vector<std::string> reason_strings;
325 ParseCollection(attr, &reason_strings); 406 ParseCollection(attr, &reason_strings);
326 for (const std::string& reason : reason_strings) { 407 for (const std::string& reason : reason_strings) {
327 printer_status->reasons.push_back(ToPrinterReason(reason)); 408 printer_status->reasons.push_back(ToPrinterReason(reason));
328 } 409 }
329 } else if (name == kPrinterStateMessage) { 410 } else if (name == kPrinterStateMessage) {
330 printer_status->message = ippGetString(attr, 0, nullptr); 411 printer_status->message = ippGetString(attr, 0, nullptr);
331 } 412 }
332 } 413 }
333 } 414 }
334 415
416 bool GetPrinterInfo(const std::string& address,
417 const int port,
418 const std::string& resource,
419 PrinterInfo* printer_info) {
420 base::ThreadRestrictions::AssertIOAllowed();
421
422 ScopedHttpPtr http = ScopedHttpPtr(
423 httpConnect2(address.data(), port, nullptr, AF_INET,
424 HTTP_ENCRYPTION_IF_REQUESTED, 0, 200, nullptr));
425 if (!http)
426 return false;
427
428 ipp_status_t status;
429 ScopedIppPtr response = GetPrinterAttributes(
430 http.get(), resource, kPrinterInfo.size(), kPrinterInfo.data(), &status);
431 return status == IPP_STATUS_OK &&
432 ParsePrinterInfo(response.get(), printer_info);
433 }
434
335 bool GetPrinterStatus(http_t* http, 435 bool GetPrinterStatus(http_t* http,
336 const std::string& printer_id, 436 const std::string& printer_id,
337 PrinterStatus* printer_status) { 437 PrinterStatus* printer_status) {
338 DCHECK(http); 438 base::ThreadRestrictions::AssertIOAllowed();
339 439
340 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES)); 440 ipp_status_t status;
441 const std::string printer_uri = PrinterUriFromName(printer_id);
341 442
342 const std::string printer_uri = PrinterUriFromName(printer_id); 443 ScopedIppPtr response =
343 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, 444 GetPrinterAttributes(http, printer_uri, kPrinterAttributes.size(),
344 nullptr, printer_uri.data()); 445 kPrinterAttributes.data(), &status);
345 446
346 ippAddStrings(request.get(), IPP_TAG_OPERATION, IPP_TAG_KEYWORD, 447 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; 448 return false;
355 449
356 ParsePrinterStatus(response.get(), printer_status); 450 ParsePrinterStatus(response.get(), printer_status);
357 451
358 return true; 452 return true;
359 } 453 }
360 454
361 bool GetCupsJobs(http_t* http, 455 bool GetCupsJobs(http_t* http,
362 const std::string& printer_id, 456 const std::string& printer_id,
363 int limit, 457 int limit,
364 JobCompletionState which, 458 JobCompletionState which,
365 std::vector<CupsJob>* jobs) { 459 std::vector<CupsJob>* jobs) {
460 base::ThreadRestrictions::AssertIOAllowed();
366 DCHECK(http); 461 DCHECK(http);
367 462
368 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_JOBS)); 463 auto request = WrapIpp(ippNewRequest(IPP_OP_GET_JOBS));
369 const std::string printer_uri = PrinterUriFromName(printer_id); 464 const std::string printer_uri = PrinterUriFromName(printer_id);
370 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri, 465 ippAddString(request.get(), IPP_TAG_OPERATION, IPP_TAG_URI, kPrinterUri,
371 nullptr, printer_uri.data()); 466 nullptr, printer_uri.data());
372 ippAddInteger(request.get(), IPP_TAG_OPERATION, IPP_TAG_INTEGER, kLimit, 467 ippAddInteger(request.get(), IPP_TAG_OPERATION, IPP_TAG_INTEGER, kLimit,
373 limit); 468 limit);
374 469
375 std::vector<const char*> job_attributes = { 470 std::vector<const char*> job_attributes = {
(...skipping 23 matching lines...) Expand all
399 LOG(WARNING) << "IPP Error: " << cupsLastErrorString(); 494 LOG(WARNING) << "IPP Error: " << cupsLastErrorString();
400 return false; 495 return false;
401 } 496 }
402 497
403 ParseJobsResponse(response.get(), printer_id, jobs); 498 ParseJobsResponse(response.get(), printer_id, jobs);
404 499
405 return true; 500 return true;
406 } 501 }
407 502
408 } // namespace printing 503 } // namespace printing
OLDNEW
« chrome/browser/chromeos/printing/printer_info_cups.cc ('K') | « printing/backend/cups_jobs.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698