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

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: Created 3 years, 7 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 "printing/backend/cups_jobs.h"
17
18 namespace {
19
20 // Returns true if any of the |ipp_versions| are greater than or equal to 2.0.
Carlson 2017/05/25 19:04:35 what's the pair? <major, minor> ?
skau 2017/05/27 02:01:19 Done.
21 bool AllowedIpp(const std::vector<std::pair<int, int>>& ipp_versions) {
22 auto found = std::find_if(
23 ipp_versions.begin(), ipp_versions.end(),
24 [](const std::pair<int, int>& version) { return version.first >= 2; });
25
26 return found != ipp_versions.end();
27 }
28
29 // Returns true if |info| describes a printer for which we want to attempt
30 // automatic configuration.
31 bool IsAutoconf(const ::printing::PrinterInfo& info) {
32 return info.ipp_everywhere || AllowedIpp(info.ipp_versions);
33 }
34
35 std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl(
Carlson 2017/05/25 19:04:35 Function comment?
skau 2017/05/27 02:01:19 Done.
36 const std::string& host,
37 const int port,
38 const std::string& path) {
39 auto info = base::MakeUnique<::printing::PrinterInfo>();
40 if (!::printing::GetPrinterInfo(host, port, path, info.get())) {
41 LOG(ERROR) << "Could not retrieve printer info";
42 return nullptr;
43 }
44
45 return info;
46 }
47
48 void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback,
49 std::unique_ptr<::printing::PrinterInfo> info) {
50 if (!info) {
51 VLOG(1) << "Could not reach printer";
52 callback.Run(false, std::string(), std::string(), false);
53 return;
54 }
55
56 // TODO(skau): Handle manufacturers with two word names.
57 base::StringPiece make_and_model(info->make_and_model);
58 base::StringPiece make;
59 base::StringPiece model;
60
61 size_t first_space = make_and_model.find(" ");
62 if (first_space != base::BaseStringPiece::npos &&
63 (first_space + 1) < make_and_model.length()) {
Carlson 2017/05/25 19:04:36 I don't think the second half of this conditional
skau 2017/05/27 02:01:19 Yes. You are correct. The primary concern is a t
64 make = make_and_model.substr(0, first_space);
65 model = make_and_model.substr(first_space + 1);
66 } else {
67 // If there's only one word or an empty string, use it.
68 model = make_and_model;
69 }
70
71 callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info));
72 }
73
74 } // namespace
75
76 namespace chromeos {
77
78 void QueryPrinter(const std::string& host,
79 const int port,
80 const std::string& path,
81 const PrinterInfoCallback& callback) {
82 DCHECK(!host.empty());
83
Carlson 2017/05/25 19:04:35 A comment on why you want these specific traits wo
skau 2017/05/27 02:01:19 Done.
84 base::PostTaskWithTraitsAndReplyWithResult(
85 FROM_HERE,
86 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
87 base::Bind(&QueryPrinterImpl, host, port, path),
88 base::Bind(&OnPrinterQueried, callback));
89 }
90
91 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698