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

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: comments addressed 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 <string>
9
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_piece.h"
13 #include "base/task_runner_util.h"
14 #include "base/task_scheduler/post_task.h"
15 #include "base/task_scheduler/task_traits.h"
16 #include "base/version.h"
17 #include "printing/backend/cups_jobs.h"
18
19 namespace {
20
21 const char kPdfMimeType[] = "application/pdf";
22 const char kPwgRasterMimeType[] = "image/pwg-raster";
23
24 // Returns true if any of the |ipp_versions| are greater than or equal to 2.0.
25 bool AllowedIpp(const std::vector<base::Version>& ipp_versions) {
26 static const std::vector<uint32_t>* kComponents =
Lei Zhang 2017/06/01 23:25:11 Was it not possible to make these non-pointers?
skau 2017/06/02 23:58:36 No, it's non POD so there's a clang warning that a
Carlson 2017/06/03 00:07:25 Can't you just do return version.components()[0]
Lei Zhang 2017/06/03 00:23:32 I wanted to suggest CR_DEFINE_STATIC_LOCAL, but no
skau 2017/06/03 00:35:01 The current parsing code checks validity before ad
27 new std::vector<uint32_t>{2, 0};
28 static const base::Version* kTwoDotZero = new base::Version(*kComponents);
29 auto found = std::find_if(
30 ipp_versions.begin(), ipp_versions.end(),
31 [](const base::Version& version) { return version >= *kTwoDotZero; });
32
33 return found != ipp_versions.end();
34 }
35
36 // Returns true if |mime_type| is one of the supported types.
37 bool SupportedMime(const std::string& mime_type) {
38 return mime_type == kPwgRasterMimeType || mime_type == kPdfMimeType;
39 }
40
41 // Returns true if |formats| contains one of the supported printer description
42 // languages for an autoconf printer identified by MIME type.
43 bool SupportsRequiredPDLS(const std::vector<std::string>& formats) {
44 auto found = std::find_if(formats.begin(), formats.end(), &SupportedMime);
45 return found != formats.end();
46 }
47
48 // Returns true if |info| describes a printer for which we want to attempt
49 // automatic configuration.
50 bool IsAutoconf(const ::printing::PrinterInfo& info) {
51 return info.ipp_everywhere || (AllowedIpp(info.ipp_versions) &&
52 SupportsRequiredPDLS(info.document_formats));
53 }
54
55 // Dispatches an IPP request to |host| to retrieve printer information. Returns
56 // a nullptr if the request fails.
57 std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl(
58 const std::string& host,
59 const int port,
60 const std::string& path) {
61 auto info = base::MakeUnique<::printing::PrinterInfo>();
62 if (!::printing::GetPrinterInfo(host, port, path, info.get())) {
63 LOG(ERROR) << "Could not retrieve printer info";
64 return nullptr;
65 }
66
67 return info;
68 }
69
70 // Handles the request for |info|. Parses make and model information before
71 // calling |callback|.
72 void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback,
73 std::unique_ptr<::printing::PrinterInfo> info) {
74 if (!info) {
75 VLOG(1) << "Could not reach printer";
76 callback.Run(false, std::string(), std::string(), false);
77 return;
78 }
79
80 // TODO(skau): Handle manufacturers with two word names.
81 base::StringPiece make_and_model(info->make_and_model);
82 base::StringPiece make;
83 base::StringPiece model;
84
85 size_t first_space = make_and_model.find(" ");
86 if (first_space != base::StringPiece::npos) {
87 make = make_and_model.substr(0, first_space);
88 model = make_and_model.substr(first_space + 1);
89 } else {
90 // If there's only one word or an empty string, use it.
91 model = make_and_model;
92 }
93
94 callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info));
95 }
96
97 } // namespace
98
99 namespace chromeos {
100
101 void QueryIppPrinter(const std::string& host,
102 const int port,
103 const std::string& path,
104 const PrinterInfoCallback& callback) {
105 DCHECK(!host.empty());
106
107 // QueryPrinterImpl could block on a network call for a noticable amount of
108 // time (100s of ms). Also the user is waiting on this result. Thus, run at
109 // USER_VISIBLE with MayBlock.
110 base::PostTaskWithTraitsAndReplyWithResult(
111 FROM_HERE,
112 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
113 base::Bind(&QueryPrinterImpl, host, port, path),
114 base::Bind(&OnPrinterQueried, callback));
115 }
116
117 } // 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