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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/printing/printer_info_cups.cc
diff --git a/chrome/browser/chromeos/printing/printer_info_cups.cc b/chrome/browser/chromeos/printing/printer_info_cups.cc
new file mode 100644
index 0000000000000000000000000000000000000000..20e9f71b2d37f3535e037592e0fc2c7ab7b15abf
--- /dev/null
+++ b/chrome/browser/chromeos/printing/printer_info_cups.cc
@@ -0,0 +1,91 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/printing/printer_info.h"
+
+#include <algorithm>
+#include <string>
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/string_piece.h"
+#include "base/task_runner_util.h"
+#include "base/task_scheduler/post_task.h"
+#include "base/task_scheduler/task_traits.h"
+#include "printing/backend/cups_jobs.h"
+
+namespace {
+
+// 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.
+bool AllowedIpp(const std::vector<std::pair<int, int>>& ipp_versions) {
+ auto found = std::find_if(
+ ipp_versions.begin(), ipp_versions.end(),
+ [](const std::pair<int, int>& version) { return version.first >= 2; });
+
+ return found != ipp_versions.end();
+}
+
+// Returns true if |info| describes a printer for which we want to attempt
+// automatic configuration.
+bool IsAutoconf(const ::printing::PrinterInfo& info) {
+ return info.ipp_everywhere || AllowedIpp(info.ipp_versions);
+}
+
+std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl(
Carlson 2017/05/25 19:04:35 Function comment?
skau 2017/05/27 02:01:19 Done.
+ const std::string& host,
+ const int port,
+ const std::string& path) {
+ auto info = base::MakeUnique<::printing::PrinterInfo>();
+ if (!::printing::GetPrinterInfo(host, port, path, info.get())) {
+ LOG(ERROR) << "Could not retrieve printer info";
+ return nullptr;
+ }
+
+ return info;
+}
+
+void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback,
+ std::unique_ptr<::printing::PrinterInfo> info) {
+ if (!info) {
+ VLOG(1) << "Could not reach printer";
+ callback.Run(false, std::string(), std::string(), false);
+ return;
+ }
+
+ // TODO(skau): Handle manufacturers with two word names.
+ base::StringPiece make_and_model(info->make_and_model);
+ base::StringPiece make;
+ base::StringPiece model;
+
+ size_t first_space = make_and_model.find(" ");
+ if (first_space != base::BaseStringPiece::npos &&
+ (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
+ make = make_and_model.substr(0, first_space);
+ model = make_and_model.substr(first_space + 1);
+ } else {
+ // If there's only one word or an empty string, use it.
+ model = make_and_model;
+ }
+
+ callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info));
+}
+
+} // namespace
+
+namespace chromeos {
+
+void QueryPrinter(const std::string& host,
+ const int port,
+ const std::string& path,
+ const PrinterInfoCallback& callback) {
+ DCHECK(!host.empty());
+
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.
+ base::PostTaskWithTraitsAndReplyWithResult(
+ FROM_HERE,
+ base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
+ base::Bind(&QueryPrinterImpl, host, port, path),
+ base::Bind(&OnPrinterQueried, callback));
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698