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

Side by Side Diff: chrome/browser/chromeos/printing/printer_info_cups.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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/printing/printer_info.h"
6
7 #include <algorithm>
8 #include <array>
9 #include <string>
10
11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_util.h"
15 #include "base/task_runner_util.h"
16 #include "base/task_scheduler/post_task.h"
17 #include "base/task_scheduler/task_traits.h"
18 #include "base/version.h"
19 #include "printing/backend/cups_jobs.h"
20
21 namespace {
22
23 const char kPdfMimeType[] = "application/pdf";
24 const char kPwgRasterMimeType[] = "image/pwg-raster";
25
26 // List of known multi-word printer manufacturers to help with make-and-model
27 // string parsing. Keep in UPPER CASE as that's how matches are performed.
28 const std::array<base::StringPiece, 4> kMultiWordManufacturers{
Lei Zhang 2017/06/03 01:17:14 Oh, you can't use StringPiece here. That creates s
skau 2017/06/05 19:51:04 Done.
29 {"FUJI XEROX", "KODAK FUNAI", "KONICA MINOLTA", "TEXAS INSTRUMENTS"}};
30
31 // Returns the length of the portion of |make_and_model| representing the
32 // manufacturer. This is either a value from kMultiWordManufacaturers or the
33 // first token. If there is only one token or less, we assume that it does not
34 // represent the manufacturer and return 0.
35 size_t ManufacturerLength(base::StringPiece make_and_model) {
36 // TODO(crbug.com/729245): Update when better data is available.
37 for (const base::StringPiece& multi_word_manufacturer :
38 kMultiWordManufacturers) {
39 if (base::StartsWith(make_and_model, multi_word_manufacturer,
40 base::CompareCase::INSENSITIVE_ASCII)) {
41 return multi_word_manufacturer.size();
42 }
43 }
44
45 // The position of the first space is equal to the length of the first token.
46 size_t first_space = make_and_model.find(" ");
47 return first_space != base::StringPiece::npos ? first_space : 0;
48 }
49
50 // Returns true if any of the |ipp_versions| are greater than or equal to 2.0.
51 bool AllowedIpp(const std::vector<base::Version>& ipp_versions) {
52 auto found = std::find_if(ipp_versions.begin(), ipp_versions.end(),
53 [](const base::Version& version) {
54 // Check that the major version is at least 2.
55 DCHECK(version.IsValid());
Lei Zhang 2017/06/03 00:53:14 How about making this a real check? return versio
skau 2017/06/05 19:51:03 Done.
56 return version.components()[0] >= 2;
57 });
58
59 return found != ipp_versions.end();
60 }
61
62 // Returns true if |mime_type| is one of the supported types.
63 bool SupportedMime(const std::string& mime_type) {
64 return mime_type == kPwgRasterMimeType || mime_type == kPdfMimeType;
65 }
66
67 // Returns true if |formats| contains one of the supported printer description
68 // languages for an autoconf printer identified by MIME type.
69 bool SupportsRequiredPDLS(const std::vector<std::string>& formats) {
70 auto found = std::find_if(formats.begin(), formats.end(), &SupportedMime);
71 return found != formats.end();
72 }
73
74 // Returns true if |info| describes a printer for which we want to attempt
75 // automatic configuration.
76 bool IsAutoconf(const ::printing::PrinterInfo& info) {
77 return info.ipp_everywhere || (AllowedIpp(info.ipp_versions) &&
78 SupportsRequiredPDLS(info.document_formats));
79 }
80
81 // Dispatches an IPP request to |host| to retrieve printer information. Returns
82 // a nullptr if the request fails.
83 std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl(
84 const std::string& host,
85 const int port,
86 const std::string& path) {
87 auto info = base::MakeUnique<::printing::PrinterInfo>();
88 if (!::printing::GetPrinterInfo(host, port, path, info.get())) {
89 LOG(ERROR) << "Could not retrieve printer info";
90 return nullptr;
91 }
92
93 return info;
94 }
95
96 // Handles the request for |info|. Parses make and model information before
97 // calling |callback|.
98 void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback,
99 std::unique_ptr<::printing::PrinterInfo> info) {
100 if (!info) {
101 VLOG(1) << "Could not reach printer";
102 callback.Run(false, std::string(), std::string(), false);
103 return;
104 }
105
106 // TODO(skau): Handle manufacturers with two word names.
Lei Zhang 2017/06/03 00:53:14 Mostly obsolete now?
skau 2017/06/05 19:51:04 Done.
107 base::StringPiece make_and_model(info->make_and_model);
108 base::StringPiece make;
109 base::StringPiece model;
110
111 size_t split = ManufacturerLength(make_and_model);
112 if (split != 0) {
113 make = make_and_model.substr(0, split);
114 model = make_and_model.substr(split + 1);
115 } else {
116 // If there's only one word or an empty string, use it.
Lei Zhang 2017/06/03 00:53:14 Do you have a collection of PPDs to test with to s
skau 2017/06/05 19:51:03 This isn't an expected condition. Per the IPP spe
Lei Zhang 2017/06/06 22:02:37 If it's against the spec, would it be reasonable t
skau 2017/06/07 22:23:36 I'd rather not rely on the printer implementing th
117 model = make_and_model;
118 }
119
120 callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info));
121 }
122
123 } // namespace
124
125 namespace chromeos {
126
127 void QueryIppPrinter(const std::string& host,
128 const int port,
129 const std::string& path,
130 const PrinterInfoCallback& callback) {
131 DCHECK(!host.empty());
132
133 // QueryPrinterImpl could block on a network call for a noticable amount of
134 // time (100s of ms). Also the user is waiting on this result. Thus, run at
135 // USER_VISIBLE with MayBlock.
136 base::PostTaskWithTraitsAndReplyWithResult(
137 FROM_HERE,
138 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
139 base::Bind(&QueryPrinterImpl, host, port, path),
140 base::Bind(&OnPrinterQueried, callback));
141 }
142
143 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/printing/printer_info.h ('k') | chrome/browser/chromeos/printing/printer_info_stub.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698