| OLD | NEW |
| (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 const char kPdfMimeType[] = "application/pdf"; |
| 21 const char kPwgRasterMimeType[] = "image/pwg-raster"; |
| 22 |
| 23 // Returns true if any of the |ipp_versions| are greater than or equal to 2.0 |
| 24 // represented as <2,0>. |
| 25 bool AllowedIpp(const std::vector<std::pair<int, int>>& ipp_versions) { |
| 26 auto found = std::find_if( |
| 27 ipp_versions.begin(), ipp_versions.end(), |
| 28 [](const std::pair<int, int>& version) { return version.first >= 2; }); |
| 29 |
| 30 return found != ipp_versions.end(); |
| 31 } |
| 32 |
| 33 // Returns true if |mime_type| is one of the supported types. |
| 34 bool SupportedMime(const std::string& mime_type) { |
| 35 return mime_type == kPwgRasterMimeType || mime_type == kPdfMimeType; |
| 36 } |
| 37 |
| 38 // Returns true if |formats| contains one of the supported printer description |
| 39 // languages for an autoconf printer identified by MIME type. |
| 40 bool SupportsRequiredPDLS(const std::vector<std::string>& formats) { |
| 41 auto found = std::find_if(formats.begin(), formats.end(), &SupportedMime); |
| 42 return found != formats.end(); |
| 43 } |
| 44 |
| 45 // Returns true if |info| describes a printer for which we want to attempt |
| 46 // automatic configuration. |
| 47 bool IsAutoconf(const ::printing::PrinterInfo& info) { |
| 48 return info.ipp_everywhere || (AllowedIpp(info.ipp_versions) && |
| 49 SupportsRequiredPDLS(info.document_formats)); |
| 50 } |
| 51 |
| 52 // Dispatches an IPP request to |host| to retrieve printer information. Returns |
| 53 // a nullptr if the request fails. |
| 54 std::unique_ptr<::printing::PrinterInfo> QueryPrinterImpl( |
| 55 const std::string& host, |
| 56 const int port, |
| 57 const std::string& path) { |
| 58 auto info = base::MakeUnique<::printing::PrinterInfo>(); |
| 59 if (!::printing::GetPrinterInfo(host, port, path, info.get())) { |
| 60 LOG(ERROR) << "Could not retrieve printer info"; |
| 61 return nullptr; |
| 62 } |
| 63 |
| 64 return info; |
| 65 } |
| 66 |
| 67 // Handles the request for |info|. Parses make and model information before |
| 68 // calling |callback|. |
| 69 void OnPrinterQueried(const chromeos::PrinterInfoCallback& callback, |
| 70 std::unique_ptr<::printing::PrinterInfo> info) { |
| 71 if (!info) { |
| 72 VLOG(1) << "Could not reach printer"; |
| 73 callback.Run(false, std::string(), std::string(), false); |
| 74 return; |
| 75 } |
| 76 |
| 77 // TODO(skau): Handle manufacturers with two word names. |
| 78 base::StringPiece make_and_model(info->make_and_model); |
| 79 base::StringPiece make; |
| 80 base::StringPiece model; |
| 81 |
| 82 size_t first_space = make_and_model.find(" "); |
| 83 if (first_space != base::StringPiece::npos) { |
| 84 make = make_and_model.substr(0, first_space); |
| 85 model = make_and_model.substr(first_space + 1); |
| 86 } else { |
| 87 // If there's only one word or an empty string, use it. |
| 88 model = make_and_model; |
| 89 } |
| 90 |
| 91 callback.Run(true, make.as_string(), model.as_string(), IsAutoconf(*info)); |
| 92 } |
| 93 |
| 94 } // namespace |
| 95 |
| 96 namespace chromeos { |
| 97 |
| 98 void QueryIppPrinter(const std::string& host, |
| 99 const int port, |
| 100 const std::string& path, |
| 101 const PrinterInfoCallback& callback) { |
| 102 DCHECK(!host.empty()); |
| 103 |
| 104 // QueryPrinterImpl could block on a network call for a noticable amount of |
| 105 // time (100s of ms). Also the user is waiting on this result. Thus, run at |
| 106 // USER_VISIBLE with MayBlock. |
| 107 base::PostTaskWithTraitsAndReplyWithResult( |
| 108 FROM_HERE, |
| 109 base::TaskTraits(base::TaskPriority::USER_VISIBLE, base::MayBlock()), |
| 110 base::Bind(&QueryPrinterImpl, host, port, path), |
| 111 base::Bind(&OnPrinterQueried, callback)); |
| 112 } |
| 113 |
| 114 } // namespace chromeos |
| OLD | NEW |