| 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..3b90385bd2e82ba7eaec81d5e0a74362164fa50b
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/printing/printer_info_cups.cc
|
| @@ -0,0 +1,114 @@
|
| +// 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 {
|
| +
|
| +const char kPdfMimeType[] = "application/pdf";
|
| +const char kPwgRasterMimeType[] = "image/pwg-raster";
|
| +
|
| +// Returns true if any of the |ipp_versions| are greater than or equal to 2.0
|
| +// represented as <2,0>.
|
| +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 |mime_type| is one of the supported types.
|
| +bool SupportedMime(const std::string& mime_type) {
|
| + return mime_type == kPwgRasterMimeType || mime_type == kPdfMimeType;
|
| +}
|
| +
|
| +// Returns true if |formats| contains one of the supported printer description
|
| +// languages for an autoconf printer identified by MIME type.
|
| +bool SupportsRequiredPDLS(const std::vector<std::string>& formats) {
|
| + auto found = std::find_if(formats.begin(), formats.end(), &SupportedMime);
|
| + return found != formats.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) &&
|
| + SupportsRequiredPDLS(info.document_formats));
|
| +}
|
| +
|
| +// Dispatches an IPP request to |host| to retrieve printer information. Returns
|
| +// a nullptr if the request fails.
|
| +std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl(
|
| + 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;
|
| +}
|
| +
|
| +// Handles the request for |info|. Parses make and model information before
|
| +// calling |callback|.
|
| +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::StringPiece::npos) {
|
| + 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 QueryIppPrinter(const std::string& host,
|
| + const int port,
|
| + const std::string& path,
|
| + const PrinterInfoCallback& callback) {
|
| + DCHECK(!host.empty());
|
| +
|
| + // QueryPrinterImpl could block on a network call for a noticable amount of
|
| + // time (100s of ms). Also the user is waiting on this result. Thus, run at
|
| + // USER_VISIBLE with MayBlock.
|
| + base::PostTaskWithTraitsAndReplyWithResult(
|
| + FROM_HERE,
|
| + base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()),
|
| + base::Bind(&QueryPrinterImpl, host, port, path),
|
| + base::Bind(&OnPrinterQueried, callback));
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|